准备一个表,表字段自定义
org.springframework.boot
spring-boot-starter-web
mysql
mysql-connector-java
runtime
org.projectlombok
lombok
true
org.springframework.boot
spring-boot-starter-test
test
com.baomidou
mybatis-plus-boot-starter
3.5.1
com.baomidou
mybatis-plus-generator
3.5.2
org.apache.velocity
velocity-engine-core
2.3
io.springfox
springfox-swagger2
2.7.0
com.github.xiaoymin
swagger-bootstrap-ui
1.9.6
server:
port: 8088
spring:
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/test?
useUnicode=true&characterEncoding=UTF-8&
serverTimezone=Asia/Shanghai
username: root
password: 123
hikari:
pool-name: DateHikariCP
minimum-idle: 5
idle-timeout: 180000
maximum-pool-size: 10
auto-commit: true
max-lifetime: 1800000
connection-timeout: 30000
connection-init-sql: SELECT 1
#Mybatis-plus 配置
mybatis-plus:
mapper-locations: classpath*:/xml/*Mapper.xml
type-aliases-package: springx.pojo
configuration:
map-underscore-to-camel-case: false
##打印 sql
logging:
level:
yeb.service.mapper: debug
public static void main(String[] args) {
//配置数据库路径
String projectPath = System.getProperty("user.dir");
DataSourceConfig.Builder dataSourceConfigBuilder = new DataSourceConfig.Builder(
"jdbc:mysql://localhost:3306/springsucurity?useUnicode=true&
characterEncoding=UTF-8&serverTimezone=Asia/Shanghai",
"root",
"123")
.dbQuery(new MySqlQuery())
.typeConvert(new MySqlTypeConvert())
.keyWordsHandler(new MySqlKeyWordsHandler());
FastAutoGenerator fastAutoGenerator = FastAutoGenerator.create(dataSourceConfigBuilder);
//全局配置
fastAutoGenerator.globalConfig(
globalConfigBuilder -> globalConfigBuilder
//覆盖生成文件
.fileOverride()
.disableOpenDir()
//指定输出目录
.outputDir(projectPath + "/src/main/java")
//设置作者
.author("夏JCC")
//设置日期
.commentDate("yyyy-MM-dd HH:mm:ss")
//使用新的日期格式.
.dateType(DateType.TIME_PACK)
//开启swagger注解
.enableSwagger()
);
//生成表名配置
fastAutoGenerator.strategyConfig(
strategyConfigBuilder -> strategyConfigBuilder
//开启大写命名
.enableCapitalMode()
//开启跳过视图
.enableSkipView()
//禁用sql过滤
.disableSqlFilter()
//添加需要生成的表名
.addInclude("t_admin","t_role")
//忽略表前缀
.addTablePrefix("t_")
);
//包的配置
fastAutoGenerator.packageConfig(
packageConfigBuilder -> packageConfigBuilder
//设置父包名 下面的parent内容要改成自己的父包名,
.parent("mybatisplus")
//以下是配置各个包名
.entity("pojo")
.mapper("mapper")
//根据需要来构筑xml
.xml("mapper.xml")
.service("service")
.serviceImpl("service.impl")
.controller("controller")
);
//实体类的策略配置
fastAutoGenerator.strategyConfig(
strategyConfigBuilder ->
strategyConfigBuilder.entityBuilder()
//开启实体类字段注解
.enableTableFieldAnnotation()
//数据库表和字段映射的时候,下划线转驼峰
.naming(NamingStrategy.underline_to_camel)
.columnNaming(NamingStrategy.underline_to_camel)
//全局的主键类型是ASSIGN_ID
.idType(IdType.AUTO)
//开启lombok
.enableLombok()
//逻辑删除的字段名
.logicDeleteColumnName("deleted")
.logicDeletePropertyName("deleted")
//自动填充配置,创建时间,更新时间
.addTableFills(new Column("create_time",FieldFill.INSERT))
.addTableFills(new Property("updateTime",FieldFill.INSERT_UPDATE))
//开启乐观锁
.versionColumnName("version")
.disableSerialVersionUID()
);
//开启Controller策略配置
fastAutoGenerator
.strategyConfig(strategyConfigBuilder ->
strategyConfigBuilder
//开启@RestController控制器
.controllerBuilder()
//开启请求rest风格
.enableRestStyle()
.enableHyphenStyle()
);
//service策略配置
fastAutoGenerator.strategyConfig(
strategyConfigBuilder -> strategyConfigBuilder
.serviceBuilder()
//格式化文件名称,没什么可注意的,开启即可
.formatServiceFileName("%sService")
.formatServiceImplFileName("%sServiceImpl"));
//注意Mapper策略配置
//.enableBaseColumnList()
//.superClass(BaseMapper.class)
//.enableMapperAnnotation()
fastAutoGenerator.strategyConfig(
strategyConfigBuilder ->
strategyConfigBuilder.mapperBuilder()
//开启resultMap,很重要
.enableBaseResultMap()
.enableBaseColumnList()
.superClass(BaseMapper.class)
.enableMapperAnnotation()
//生成Mapper包名
.formatMapperFileName("%sMapper")
.formatXmlFileName("%sMapper"));
//生成代码
fastAutoGenerator.execute();
点击小鸟即可实现xml和对应mapper的互相跳转
前提,idea如何配置database数据库我就不在这里说明了,可自行百度。
以上就是springboot整合mybatisPlus的内容。