springboot+mybatisplus进行整合并且使用逆向工程
首先引入maven依赖:这是整合mybatisplus时,进行逆向工程时候需要引入的依赖
<!--mybaitsplus start--> <dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-boot-starter</artifactId> <version>2.3</version> </dependency> <dependency> <groupId>org.apache.velocity</groupId> <artifactId>velocity-engine-core</artifactId> <version>2.0</version> <scope>test</scope> </dependency> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid-spring-boot-starter</artifactId> <version>1.1.10</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.16</version> </dependency> <!--mybaitsplus end-->
<!-- lombok start-->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.8</version>
</dependency>
<!-- lombok end-->
第二步:进行配置application.yml文件
spring: datasource: type: com.alibaba.druid.pool.DruidDataSource driver-class-name: com.mysql.jdbc.Driver url: jdbc:mysql://localhost:3306/dbname?useUnicode=true&characterEncoding=utf8&allowMultiQueries=true username: username password: password druid: # 初始化大小,最小,最大 initial-size: 5 min-idle: 5 maxActive: 20 # 配置获取连接等待超时的时间 maxWait: 60000 # 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒 timeBetweenEvictionRunsMillis: 60000 # 配置一个连接在池中最小生存的时间,单位是毫秒 minEvictableIdleTimeMillis: 300000 validationQuery: SELECT 1 FROM DUAL testWhileIdle: true testOnBorrow: false testOnReturn: false # 打开PSCache,并且指定每个连接上PSCache的大小 poolPreparedStatements: true maxPoolPreparedStatementPerConnectionSize: 20 # 配置监控统计拦截的filters,去掉后监控界面sql无法统计,'wall'用于防火墙 filters: stat,wall # 通过connectProperties属性来打开mergeSql功能;慢SQL记录 connectionProperties: druid.stat.mergeSql\=true;druid.stat.slowSqlMillis\=5000 # 配置DruidStatFilter web-stat-filter: enabled: true url-pattern: "/*" exclusions: "*.js,*.gif,*.jpg,*.bmp,*.png,*.css,*.ico,/druid/*" # 配置DruidStatViewServlet stat-view-servlet: url-pattern: "/druid/*" # IP白名单(没有配置或者为空,则允许所有访问) allow: 127.0.0.1,192.168.163.1 # IP黑名单 (存在共同时,deny优先于allow) deny: 192.168.1.73 # 禁用HTML页面上的“Reset All”功能 reset-enable: false # 登录名 login-username: admin # 登录密码 login-password: 123456 mybatis-plus: mapper-locations: classpath:/com/example/demo/mapper/*/*.xml typeAliasesPackage: com.example.demo.entity global-config: id-type: 2 field-strategy: 2 db-column-underline: true refresh-mapper: true configuration: map-underscore-to-camel-case: true cache-enabled: false server: port: 8082
第三步:配置生成代码的类和分页插件配置类:
package com.qingmu.springboot.common.Generator; import com.baomidou.mybatisplus.generator.AutoGenerator; import com.baomidou.mybatisplus.generator.config.DataSourceConfig; import com.baomidou.mybatisplus.generator.config.GlobalConfig; import com.baomidou.mybatisplus.generator.config.PackageConfig; import com.baomidou.mybatisplus.generator.config.StrategyConfig; import com.baomidou.mybatisplus.generator.config.rules.DbType; import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy; public class CodeGenerator { public static final String DB_URL = "jdbc:mysql://192.168.2.48:3306/order_system?useUnicode=true&characterEncoding=utf8&allowMultiQueries=true"; public static final String USER_NAME = "root"; public static final String PASSWORD = "root"; public static final String DRIVER = "com.mysql.jdbc.Driver"; public static final String AUTHOR = "qingmu"; //生成的文件输出到哪个目录 public static final String OUTPUT_FILE = "D:\\nums-project\\springboot\\src\\main\\java"; //包名,会按照com/example/demo这种形式生成类 public static final String PACKAGE = "com.qingmu.springboot.common"; //TODO 更多配置请参考http://mp.baomidou.com/#/generate-code public void generateByTables(boolean serviceNameStartWithI, String... tableNames) { GlobalConfig config = new GlobalConfig(); DataSourceConfig dataSourceConfig = new DataSourceConfig(); dataSourceConfig.setDbType(DbType.MYSQL) .setUrl(DB_URL) .setUsername(USER_NAME) .setPassword(PASSWORD) .setDriverName(DRIVER); StrategyConfig strategyConfig = new StrategyConfig(); strategyConfig .setCapitalMode(true) .setEntityLombokModel(false) .setDbColumnUnderline(true) .setNaming(NamingStrategy.underline_to_camel) .setInclude(tableNames);//修改替换成你需要的表名,多个表名传数组 config.setActiveRecord(false) .setAuthor(AUTHOR) .setOutputDir(OUTPUT_FILE) .setFileOverride(true); if (!serviceNameStartWithI) { config.setServiceName("%sService"); } new AutoGenerator().setGlobalConfig(config) .setDataSource(dataSourceConfig) .setStrategy(strategyConfig) .setPackageInfo( new PackageConfig() .setParent(PACKAGE) .setController("controller") .setEntity("entity") ).execute(); } }
package com.example.demo.config; import com.baomidou.mybatisplus.plugins.PaginationInterceptor; import org.mybatis.spring.annotation.MapperScan; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; @Configuration @MapperScan("com.example.demo.mapper") public class MybatisPlusConfig { @Bean public PaginationInterceptor paginationInterceptor() { return new PaginationInterceptor(); } }
第四步:写一个Junit test类,用来生成代码
@RunWith(SpringRunner.class) @SpringBootTest public class DemoApplicationTests { @Test public void contextLoads() { CodeGenerator gse = new CodeGenerator(); //要给那些表生成 gse.generateByTables(false,"tb_user", "tb_role","tb_permission","tb_user_role","tb_role_permission"); } }
以上就是用来生成的代码
本文来自博客园,作者:King-DA,转载请注明原文链接:https://www.cnblogs.com/qingmuchuanqi48/p/11612714.html