First introduce the maven dependency: this is the dependency that needs to be introduced when reverse engineering when integrating mybatisplus
com.baomidou
mybatis-plus-boot-starter
2.3
org.apache.velocity
velocity-engine-core
2.0
test
com.alibaba
druid-spring-boot-starter
1.1.10
mysql
mysql-connector-java
5.1.16
org.projectlombok
lombok
1.18.8
< p>Step 2: Configure the application.yml file
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:
# Initialization size, minimum, maximum
initial-size: 5
min-idle: 5
maxActive: 20
# Configure the timeout period for obtaining a connection waiting timeout
maxWait: 60000
# Configure how long the interval is to perform a check, check idle connections that need to be closed, the unit is milliseconds
timeBetweenEvictionRunsMillis: 60000
# Configure the minimum survival time of a connection in the pool, in milliseconds
minEvictableIdleTimeMillis: 300000
validationQuery: SELECT 1 FROM DUAL
testWhileIdle: true
testOnBorrow: false
testOnReturn: false
# Open PSCache and specify the size of PSCache on each connection
poolPreparedStatements: true
maxPoolPreparedStatementPerConnectionSize: 20
# Configure the filters for monitoring statistics interception, after removing the monitoring interface sql can’t make statistics,‘wall‘used for firewall
filters: stat,wall
# Open the mergeSql function through the connectProperties property; slow SQL records
connectionProperties: druid.stat.mergeSql\=true;druid.stat.slowSqlMillis\=5000
# Configure DruidStatFilter
web-stat-filter:
enabled: true
url-pattern: "/*"
exclusions: "*.js,*.gif,*.jpg,*.bmp,*.png,*.css,*.ico,/druid/*"
# Configure DruidStatViewServlet
stat-view-servlet:
url-pattern: "/druid/*"
# IP whitelist (no configuration or empty, all access is allowed)
allow: 127.0.0.1,192.168.163.1
# IP blacklist (when there are common, deny takes precedence over allow)
deny: 192.168.1.73
# Disable the "Reset All" function on the HTML page
reset-enable: false
# Login name
login-username: admin
# Login password
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
Step 3: Configure the code generation class and the paging plug-in configuration class:
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”;
//Which directory should the generated files be output to
public static final String OUTPUT_FILE = “D:\\nums-project\\springboot\\src\\main\\java”;
//The package name will follow com/example/demo Form generation class
public static final String PACKAGE = “com.qingmu.springboot.common”;
//TODO For more configuration, please refer to 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);//Modify and replace with what you need Table name, multiple table names are passed as an array
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();
}
}
Step 4: Write a Junit test class to generate code
@ RunWith(SpringRunner.class)
@SpringBootTest
public class DemoApplicationTests {
@Test
public void contextLoads() {
CodeGenerator gse = new CodeGenerator();
//To generate for those tables
gse.generateByTables(false,"tb_user", "tb_role","tb_permission","tb_user_role","tb_role_permission");
}
}
The above is the code used to generate
com.baomidou
mybatis-plus-boot-starter
2.3
org.apache.velocity
velocity-engine-core
2.0
test
com.alibaba
druid-spring-boot-starter
1.1.10
mysql
mysql-connector-java
5.1.16
org.projectlombok
lombok
1.18.8
< p>
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:
# Initialization size, minimum, maximum
initial-size: 5
min-idle: 5
maxActive: 20
# Configure the timeout period for obtaining a connection waiting timeout
maxWait: 60000
# Configure how long the interval is to perform a check, check idle connections that need to be closed, the unit is milliseconds
timeBetweenEvictionRunsMillis: 60000
# Configure the minimum survival time of a connection in the pool, in milliseconds
minEvictableIdleTimeMillis: 300000
validationQuery: SELECT 1 FROM DUAL
testWhileIdle: true
testOnBorrow: false
testOnReturn: false
# Open PSCache and specify the size of PSCache on each connection
poolPreparedStatements: true
maxPoolPreparedStatementPerConnectionSize: 20
# Configure the filters for monitoring statistics interception, after removing the monitoring interface sql can’t make statistics,‘wall‘used for firewall
filters: stat,wall
# Open the mergeSql function through the connectProperties property; slow SQL records
connectionProperties: druid.stat.mergeSql\=true;druid.stat.slowSqlMillis\=5000
# Configure DruidStatFilter
web-stat-filter:
enabled: true
url-pattern: "/*"
exclusions: "*.js,*.gif,*.jpg,*.bmp,*.png,*.css,*.ico,/druid/*"
# Configure DruidStatViewServlet
stat-view-servlet:
url-pattern: "/druid/*"
# IP whitelist (no configuration or empty, all access is allowed)
allow: 127.0.0.1,192.168.163.1
# IP blacklist (when there are common, deny takes precedence over allow)
deny: 192.168.1.73
# Disable the "Reset All" function on the HTML page
reset-enable: false
# Login name
login-username: admin
# Login password
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";
//Which directory should the generated files be output to
public static final String OUTPUT_FILE = "D:\\nums-project\\springboot\\src\\main\\java";
//The package name will follow com/example/demo Form generation class
public static final String PACKAGE = "com.qingmu.springboot.common";
//TODO For more configuration, please refer to 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);//Modify and replace with what you need Table name, multiple table names are passed as an array
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();
}
}
@RunWith(SpringRunner.class)
@SpringBootTest
public class DemoApplicationTests {
@Test
public void contextLoads() {
CodeGenerator gse = new CodeGenerator();
//To generate for those tables
gse.generateByTables(false,"tb_user", "tb_role","tb_permission","tb_user_role","tb_role_permission");
}
}