First introduced maven dependence: It is dependent upon the integration of mybatisplus, reverse engineering time needed to introduce the
<!--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-->
Step 2: Perform configuration file application.yml
the Spring: the DataSource: of the type: com.alibaba.druid.pool.DruidDataSource Driver - class - name: com.mysql.jdbc.Driver url: jdbc: MySQL: // ? localhost: 3306 / = dbname useUnicode to true & characterEncoding = = utf8 & allowMultiQueries to true password : password Druid: # initial size, minimum, maximum Initial -size:. 5 min -idle:. 5 for maxActive: 20 is # configuration obtaining connection waiting timeout time maxWait: 60000 # intervals the frequency of such detection, the detection needs to close idle connections milliseconds timeBetweenEvictionRunsMillis: 60000 # configure a minimum survival time connection pool, in milliseconds minEvictableIdleTimeMillis: 300000 validationQuery: the SELECT 1 testWhileIdle: to true testOnBorrow: false testOnReturn: false # open PSCache, and specify the size of each connection PSCache poolPreparedStatements: to true maxPoolPreparedStatementPerConnectionSize: 20 # configure the monitoring statistics intercepted filters, after removing the monitoring interface sql not statistics , 'Wall' firewalls Filters: STAT, Wall # mergeSql opened by connectProperties function attributes; slow SQL record ConnectionProperties: druid.stat.mergeSql \ = to true ; druid.stat.slowSqlMillis \ = 5000 # configure DruidStatFilter Web -stat- Enabled: to true # password url -pattern: "/ *" Exclusions: . "..... * .js, * GIF, JPG *, * BMP, PNG *, * CSS, ico *, / Druid / *" # Configure DruidStatViewServlet STAT the -view - url -pattern: "/ Druid / *" # IP whitelist (not configured or is empty, then allow all access) the allow: 127.0.0.1,192.168.163.1 # IP blacklist (when there is a common, deny priority to allow) deny: 192.168.1.73 # disable HTML page on the "Reset All" function of the RESET -enable: false # login name the Login - username: ADMIN the Login -password: 123456 the 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
The third step: configuration classes and class tab plug-in configuration of the generated code:
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" public static final String PACKAGE = "com.qingmu.springboot.common"; //TODO 更多配置请参考http://mp.baomidou.com/#/generate-code public void generateByTables(boolean GlobalConfig config = new DataSourceConfig dataSourceConfig = new dataSourceConfig.setDbType(DbType.MYSQL) .setUrl(DB_URL) .setUsername(USER_NAME) .setPassword(PASSWORD) .setDriverName(DRIVER); StrategyConfig strategyConfig = new strategyConfig .setCapitalMode ( to true ) .setEntityLombokModel ( false ) .setDbColumnUnderline ( to true .setNaming (NamingStrategy.underline_to_camel) .setInclude (TableNames); // modify replace the table name you need more than one table name to an array config.setActiveRecord ( false .setAuthor (the AUTHOR) .setOutputDir (OUTPUT_FILE) .setFileOverride ( to true ); IF (! config.setServiceName ( "% Sservice" ); } new 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 @Configuration @MapperScan("com.example.demo.mapper") public class MybatisPlusConfig { @Bean public PaginationInterceptor paginationInterceptor() { return new PaginationInterceptor(); } }
The fourth step: a write Junit test class used to generate the code
@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"); } }
These are used to generate code