MyBatisPlus(简称MP)是基于MyBatis框架基础上开发的增强型工具,旨在简化开发、提高效率
<dependencies>
<dependency>
<groupId>com.baomidougroupId>
<artifactId>mybatis-plus-boot-starterartifactId>
<version>3.4.1version>
dependency>
<dependency>
<groupId>com.alibabagroupId>
<artifactId>druidartifactId>
<version>1.2.16version>
dependency>
由于mp并未被收录到idea的系统内置配置,无法直接选择加入
spring:
datasource:
type: com.alibaba.druid.pool.DruidDataSource
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/db1
username: root
password: "031006"
package com.example.domain;
public class User {
private Long id;
private String name;
private String password;
private Integer age;
private String tel;
@Override
public String toString() {
return "User{" +
"id=" + id +
", name='" + name + '\'' +
", password='" + password + '\'' +
", age=" + age +
", tel='" + tel + '\'' +
'}';
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public String getTel() {
return tel;
}
public void setTel(String tel) {
this.tel = tel;
}
}
package com.example.dao;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.example.domain.User;
import org.apache.ibatis.annotations.Mapper;
@Mapper
public interface UserDao extends BaseMapper<User> {
}
package com.example;
import com.example.dao.UserDao;
import com.example.domain.User;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import java.util.List;
@SpringBootTest
class MybatisplusApplicationTests {
@Autowired
private UserDao userDao;
@Test
void testGetAll() {
List<User> users = userDao.selectList(null);
System.out.println(users);
}
}
运行结果为
[User{id=1, name='Tom', password='tom123', age=18, tel='181633335556'}, User{id=2, name='Jerry', password='jerry666', age=17, tel='19533336666'}, User{id=3, name='Ben', password='ben555', age=20, tel='13788889999'}, User{id=4, name='王小明', password='wxm123456', age=19, tel='19999997787'}]
MyBatisPlus(简称MP)是基于MyBatis框架基础上开发的增强型工具,旨在简化开发、提高效率
官网: MyBatis-Plus MyBatis-Plus (baomidou.com)
特性
功能 | MP接口 |
---|---|
新增 | int insert(T t) |
删除 | int deleteById(Serializable id) |
修改 | int UpdateById(T t) |
根据id查询 | T selectById(Serializable id) |
查询全部 | List |
分页查询 | IPage |
按条件查询 | IPage |
package com.example;
import com.example.dao.UserDao;
import com.example.domain.User;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import java.util.List;
@SpringBootTest
class MybatisplusApplicationTests {
@Autowired
private UserDao userDao;
@Test
void testInsert() {
User user = new User();
user.setAge(18);
user.setName("Faiz");
user.setTel("18155555555");
user.setPassword("faiz555");
userDao.insert(user);
}
@Test
void testDelete() {
userDao.deleteById(1685579916861468673L);
}
@Test
void testUpdate() {
User user = new User();
// 只修改set好的字段
user.setAge(18);
user.setName("Faiz");
user.setId(4L);
userDao.updateById(user);
}
@Test
void testGetById() {
User user = userDao.selectById(2L);
System.out.println(user);
}
@Test
void testGetAll() {
List<User> users = userDao.selectList(null);
System.out.println(users);
}
}
lombok
Lombok,一个Java类库,提供了一组注解,简化PO30实体类开发
<dependency>
<groupId>org.projectlombokgroupId>
<artifactId>lombokartifactId>
<scope>providedscope>
dependency>
dependencies>
package com.example.domain;
import lombok.*;
// lombok
//@Setter
//@Getter
//@ToString
//@NoArgsConstructor
//@AllArgsConstructor
@Data
public class User {
private Long id;
private String name;
private String password;
private Integer age;
private String tel;
}
常用注解:@Data
package com.example.config;
import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class MpConfig {
@Bean
public MybatisPlusInterceptor mpIntercepter() {
// 1.定义Mp拦截器
MybatisPlusInterceptor mpIntercepter = new MybatisPlusInterceptor();
// 2.添加具体的拦截器
mpIntercepter.addInnerInterceptor(new PaginationInnerInterceptor());
return mpIntercepter;
}
}
@Test
void testGetByPage() {
IPage page = new Page(1,2);
userDao.selectPage(page,null);
System.out.println("当前页码值:"+page.getCurrent());
System.out.println("每页显示数:"+page.getSize());
System.out.println("一共多少页:"+page.getPages());
System.out.println("一共多少条数据:"+page.getTotal());
System.out.println("数据:"+page.getRecords());
}
运行结果为:
当前页码值:1
每页显示数:2
一共多少页:2
一共多少条数据:4
数据:[User(id=1, name=Tom, password=tom123, age=18, tel=181633335556), User(id=2, name=Jerry, password=jerry666, age=17, tel=19533336666)]
开启MP的日志(输出到控制台)
spring:
datasource:
type: com.alibaba.druid.pool.DruidDataSource
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/db1
username: root
password: "031006"
# 开启MP的日志(输出到控制台)
mybatis-plus:
configuration:
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
MyBatisPlus将书写复杂的SQL查询条件进行了封装,使用编程的形式完成查询条件的组合
@Test
void testGetAll() {
// 方式一,按条件查询
QueryWrapper wrapper = new QueryWrapper();
// age < 18
wrapper.lt("age",18);
List<User> users1 = userDao.selectList(wrapper);
System.out.println(users1);
// 方式二,lambda格式按条件查询
QueryWrapper<User> wrapper2 = new QueryWrapper<>();
// age < 18
wrapper2.lambda().lt(User::getAge,18);
List<User> users2 = userDao.selectList(wrapper);
System.out.println(users2);
// 方式三,lambda格式按条件查询
LambdaQueryWrapper<User> lambdaQueryWrapper1 = new LambdaQueryWrapper<>();
// age < 18
lambdaQueryWrapper1.lt(User::getAge,18);
List<User> users = userDao.selectList(lambdaQueryWrapper1);
System.out.println(users);
LambdaQueryWrapper<User> lambdaQueryWrapper = new LambdaQueryWrapper<>();
// // age < 20
// lambdaQueryWrapper.lt(User::getAge,20);
// // age > 17
// lambdaQueryWrapper.gt(User::getAge,17);
// age < 20 && age > 17
// lambdaQueryWrapper.lt(User::getAge,20).gt(User::getAge,17);
// age < 18 || age > 20
lambdaQueryWrapper.lt(User::getAge,18).or().gt(User::getAge,20);
List<User> users3 = userDao.selectList(lambdaQueryWrapper);
System.out.println(users3);
}
null值处理
UserQuery userQuery = new UserQuery();
// userQuery.setAge(20);
userQuery.setAge2(17);
LambdaQueryWrapper<User> lambdaQueryWrapper = new LambdaQueryWrapper<>();
// 先判断第一个条件是否为true,为true则连接当前条件
// age < 20
lambdaQueryWrapper.lt(null != userQuery.getAge(),User::getAge,userQuery.getAge());
// age > 17
lambdaQueryWrapper.gt(null != userQuery.getAge2(),User::getAge,userQuery.getAge2());
List<User> users = userDao.selectList(lambdaQueryWrapper);
System.out.println(users);
// 查询投影
// 方式一
LambdaQueryWrapper<User> lambdaQueryWrapper = new LambdaQueryWrapper<>();
lambdaQueryWrapper.select(User::getId,User::getName,User::getAge);
List<User> users1 = userDao.selectList(lambdaQueryWrapper);
// 方式二
QueryWrapper<User> queryWrapper = new QueryWrapper<>();
queryWrapper.select("id","name","age");
List<User> users2 = userDao.selectList(queryWrapper);
System.out.println(users1);
System.out.println(users2);
// 查询投影
QueryWrapper<User> queryWrapper = new QueryWrapper<>();
queryWrapper.select("count(*) as count,age");
queryWrapper.groupBy("age");
List<Map<String, Object>> users = userDao.selectMaps(queryWrapper);
System.out.println(users);
eq匹配
LambdaQueryWrapper<User> lqw = new LambdaQueryWrapper<>();
lqw.eq(User::getName,"Faiz");
lqw.eq(User::getPassword,"wxm123456");
// List users = userDao.selectList(lqw);
// System.out.println(users);
User user = userDao.selectOne(lqw);
System.out.println(user);
范围查询
LambdaQueryWrapper<User> lqw = new LambdaQueryWrapper<>();
// 范围查询 lt,le,gt,ge,eq,between
// 18 <= age <20
lqw.between(User::getAge,18,20);
List<User> users = userDao.selectList(lqw);
System.out.println(users);
模糊匹配
// 模糊匹配 like
// %e%(String)
// lqw.like(User::getName,"e");
// // [User(id=2, name=Jerry, password=jerry666, age=17, tel=19533336666), User(id=3, name=Ben, password=ben555, age=20, tel=13788889999), User(id=6, name=Decade, password=wxmmmmm, age=19, tel=16666666666)]
// %e(String)
lqw.likeLeft(User::getName,"e");
// [User(id=6, name=Decade, password=wxmmmmm, age=19, tel=16666666666)]
// e%(String)
// lqw.likeRight(User::getName,"e");
// []
List<User> users = userDao.selectList(lqw);
System.out.println(users);
问题一:表字段与编码属性设计不同步
名称:@TableField
类型:属性注解
位置:模型类属性定义上方
作用:设置当前属性对应的数据库表中的字段关系
范例:
public class User {
@TableField(value="pwd")
private String password;
}
相关属性
问题二:编码中添加了数据库中未定义的属性
名称:@TableField
类型:属性注解
位置:模型类属性定义上方
作用:设置当前属性对应的数据库表中的字段关系
范例:
public class User {
@TableField(exist = false)
private Integer online;
}
相关属性
问题三:采用默认查询开放了更多的字段查看权限
名称:@TableField
类型:属性注解
位置:模型类属性定义上方
作用:设置当前属性对应的数据库表中的字段关系
范例:
public class User {
@TableField(value="pwd",select = false)
private String password;
}
相关属性
问题四:表名与编码开发设计不同步
名称:@TableName
类型:类注解
位置:模型类定义上方
作用:设置当前类对应与数据库表关系
范例:
@TableName("tbl_user")
public class User {
private Long id;
}
相关属性
package com.example.domain;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableName;
import lombok.*;
// lombok
//@Setter
//@Getter
//@ToString
//@NoArgsConstructor
//@AllArgsConstructor
@Data
@TableName("tbl_user")
public class User {
private Long id;
private String name;
@TableField(value = "pwd",select = false)
private String password;
private Integer age;
private String tel;
@TableField(exist = false)
private Integer online;
}
id生成策略
不同的表应用不同的id生成策略
id生成策略控制
public class User {
@TableId(type = IdType.AUTO)
private Long id;
}
package com.example.domain;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import lombok.*;
// lombok
//@Setter
//@Getter
//@ToString
//@NoArgsConstructor
//@AllArgsConstructor
@Data
@TableName("tbl_user")
public class User {
// 使用数据库定义的
// @TableId(type = IdType.AUTO)
// 自己定义 user.setId(555L);
// @TableId(type = IdType.INPUT)
// 雪花算法
@TableId(type = IdType.ASSIGN_ID)
private Long id;
private String name;
@TableField(value = "pwd",select = false)
private String password;
private Integer age;
private String tel;
@TableField(exist = false)
private Integer online;
}
id生成策略全局配置
spring:
datasource:
type: com.alibaba.druid.pool.DruidDataSource
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/db1
username: root
password: "031006"
main:
banner-mode: off
# 开启MP的日志(输出到控制台)
mybatis-plus:
configuration:
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
global-config:
banner: false
db-config:
id-type: assign_id
table-prefix: tbl_
多记录操作
@Test
void testDelete() {
// userDao.deleteById(1685579916861468673L);
List<Long> list = new ArrayList<>();
list.add(1685940449313812482L);
list.add(1685941696037003266L);
list.add(1685942172673523714L);
userDao.deleteBatchIds(list);
}
逻辑删除
@Data
//@TableName("tbl_user")
public class User {
// 使用数据库定义的
// @TableId(type = IdType.AUTO)
// 自己定义 user.setId(555L);
// @TableId(type = IdType.INPUT)
// 雪花算法
// @TableId(type = IdType.ASSIGN_ID)
private Long id;
private String name;
@TableField(value = "pwd",select = false)
private String password;
private Integer age;
private String tel;
@TableField(exist = false)
private Integer online;
// 逻辑删除字段,标记当前记录是否被删除
@TableLogic(value = "0",delval = "1")
private Integer deleted;
}
@Test
void testDelete() {
userDao.deleteById(1);
// List list = new ArrayList<>();
// list.add(1685940449313812482L);
// list.add(1685941696037003266L);
// list.add(1685942172673523714L);
// userDao.deleteBatchIds(list);
}
运行结果为:
Logging initialized using 'class org.apache.ibatis.logging.stdout.StdOutImpl' adapter.
Registered plugin: 'com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor@4277127c'
Property 'mapperLocations' was not specified.
Creating a new SqlSession
SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@6c075e9d] was not registered for synchronization because synchronization is not active
JDBC Connection [com.mysql.cj.jdbc.ConnectionImpl@2c06b113] will not be managed by Spring
==> Preparing: UPDATE tbl_user SET deleted=1 WHERE id=? AND deleted=0
==> Parameters: 1(Integer)
<== Updates: 1
Closing non transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@6c075e9d]
查询操作
@Test
void testDelete() {
// userDao.deleteById(1);
userDao.selectList(null);
// List list = new ArrayList<>();
// list.add(1685940449313812482L);
// list.add(1685941696037003266L);
// list.add(1685942172673523714L);
// userDao.deleteBatchIds(list);
}
结果为
Logging initialized using 'class org.apache.ibatis.logging.stdout.StdOutImpl' adapter.
Registered plugin: 'com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor@20f6f88c'
Property 'mapperLocations' was not specified.
Creating a new SqlSession
SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@8fcc534] was not registered for synchronization because synchronization is not active
JDBC Connection [com.mysql.cj.jdbc.ConnectionImpl@16681017] will not be managed by Spring
==> Preparing: SELECT id,name,age,tel,deleted FROM tbl_user WHERE deleted=0
==> Parameters:
<== Columns: id, name, age, tel, deleted
<== Row: 2, Jerry, 17, 19533336666, 0
<== Row: 3, Ben, 20, 13788889999, 0
<== Row: 4, Faiz, 18, 19999997787, 0
<== Row: 5, Build, 21, 17569932244, 0
<== Row: 6, Decade, 19, 16666666666, 0
<== Row: 7, Exaid, 18, 19155555555, 0
<== Row: 111, Exaid, 18, 19155555555, 0
<== Row: 555, Exaid, 18, 19155555555, 0
<== Total: 8
Closing non transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@8fcc534]
注意:
==> Preparing: SELECT id,name,age,tel,deleted FROM tbl_user WHERE deleted=0
配置的方式
spring:
datasource:
type: com.alibaba.druid.pool.DruidDataSource
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/db1
username: root
password: "031006"
main:
banner-mode: off
# 开启MP的日志(输出到控制台)
mybatis-plus:
configuration:
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
global-config:
banner: false
db-config:
id-type: assign_id
table-prefix: tbl_
logic-delete-field: deleted
logic-not-delete-value: 0
logic-delete-value: 1
乐观锁
@Data
//@TableName("tbl_user")
public class User {
// 使用数据库定义的
// @TableId(type = IdType.AUTO)
// 自己定义 user.setId(555L);
// @TableId(type = IdType.INPUT)
// 雪花算法
// @TableId(type = IdType.ASSIGN_ID)
private Long id;
private String name;
@TableField(value = "pwd",select = false)
private String password;
private Integer age;
private String tel;
@TableField(exist = false)
private Integer online;
// 逻辑删除字段,标记当前记录是否被删除
// @TableLogic(value = "0",delval = "1")
private Integer deleted;
@Version
private Integer version;
}
@Configuration
public class MpConfig {
@Bean
public MybatisPlusInterceptor mpIntercepter() {
// 1.定义Mp拦截器
MybatisPlusInterceptor mpIntercepter = new MybatisPlusInterceptor();
// 2.添加具体的拦截器
mpIntercepter.addInnerInterceptor(new PaginationInnerInterceptor());
// 3.添加乐观锁的拦截器
mpIntercepter.addInnerInterceptor(new OptimisticLockerInnerInterceptor());
return mpIntercepter;
}
}
@Test
void testUpdate() {
// User user = new User();
// // 只修改set好的字段
// user.setAge(18);
// user.setName("Faiz");
// user.setId(4L);
// user.setPassword("fa5555");
// user.setVersion(1);
// userDao.updateById(user);
// //1.先通过要修改的数id将当前数据查询出来
// User user = userDao.selectById(4L);
// //2.将要修改的属性逐一设置进去
// user.setName("Faiz555");
// userDao.updateById(user);
User user = userDao.selectById(4L); // version=3,name=Faiz555
User user2 = userDao.selectById(4L); // version=3,name=Faiz555
user2.setName("Faiz666");
userDao.updateById(user2); // version=4,name=Faiz666
user.setName("Faiz777");
userDao.updateById(user);
// 最终:version=4,name=Faiz666
}
运行结果:
Logging initialized using 'class org.apache.ibatis.logging.stdout.StdOutImpl' adapter.
Registered plugin: 'com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor@5b7c8930'
Property 'mapperLocations' was not specified.
Creating a new SqlSession
SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@4a642e4b] was not registered for synchronization because synchronization is not active
JDBC Connection [com.mysql.cj.jdbc.ConnectionImpl@32ab408e] will not be managed by Spring
==> Preparing: SELECT id,name,age,tel,deleted,version FROM tbl_user WHERE id=? AND deleted=0
==> Parameters: 4(Long)
<== Columns: id, name, age, tel, deleted, version
<== Row: 4, Faiz555, 18, 19999997787, 0, 3
<== Total: 1
Closing non transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@4a642e4b]
Creating a new SqlSession
SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@7b297740] was not registered for synchronization because synchronization is not active
JDBC Connection [com.mysql.cj.jdbc.ConnectionImpl@32ab408e] will not be managed by Spring
==> Preparing: SELECT id,name,age,tel,deleted,version FROM tbl_user WHERE id=? AND deleted=0
==> Parameters: 4(Long)
<== Columns: id, name, age, tel, deleted, version
<== Row: 4, Faiz555, 18, 19999997787, 0, 3
<== Total: 1
Closing non transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@7b297740]
Creating a new SqlSession
SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@6e3dd5ce] was not registered for synchronization because synchronization is not active
JDBC Connection [com.mysql.cj.jdbc.ConnectionImpl@32ab408e] will not be managed by Spring
==> Preparing: UPDATE tbl_user SET name=?, age=?, tel=?, version=? WHERE id=? AND version=? AND deleted=0
==> Parameters: Faiz666(String), 18(Integer), 19999997787(String), 4(Integer), 4(Long), 3(Integer)
<== Updates: 1
Closing non transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@6e3dd5ce]
Creating a new SqlSession
SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@7d7ceca8] was not registered for synchronization because synchronization is not active
JDBC Connection [com.mysql.cj.jdbc.ConnectionImpl@32ab408e] will not be managed by Spring
==> Preparing: UPDATE tbl_user SET name=?, age=?, tel=?, version=? WHERE id=? AND version=? AND deleted=0
==> Parameters: Faiz777(String), 18(Integer), 19999997787(String), 4(Integer), 4(Long), 3(Integer)
<== Updates: 0
Closing non transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@7d7ceca8]
Process finished with exit code 0
代码生成器
模板:MyBatisPlus提供
数据库相关配置:读取数据库获取信息
开发者自定义配置:手工配置
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0modelVersion>
<parent>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-parentartifactId>
<version>2.5.1version>
parent>
<groupId>com.itheimagroupId>
<artifactId>mybatisplus_04_generatorartifactId>
<version>0.0.1-SNAPSHOTversion>
<properties>
<java.version>1.8java.version>
properties>
<dependencies>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-webartifactId>
dependency>
<dependency>
<groupId>com.baomidougroupId>
<artifactId>mybatis-plus-boot-starterartifactId>
<version>3.4.1version>
dependency>
<dependency>
<groupId>com.alibabagroupId>
<artifactId>druidartifactId>
<version>1.1.16version>
dependency>
<dependency>
<groupId>mysqlgroupId>
<artifactId>mysql-connector-javaartifactId>
<scope>runtimescope>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-testartifactId>
<scope>testscope>
dependency>
<dependency>
<groupId>org.projectlombokgroupId>
<artifactId>lombokartifactId>
<version>1.18.12version>
dependency>
<dependency>
<groupId>com.baomidougroupId>
<artifactId>mybatis-plus-generatorartifactId>
<version>3.4.1version>
dependency>
<dependency>
<groupId>org.apache.velocitygroupId>
<artifactId>velocity-engine-coreartifactId>
<version>2.3version>
dependency>
dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-maven-pluginartifactId>
plugin>
plugins>
build>
project>
public class CodeGenerator {
public static void main(String[] args) {
//1.获取代码生成器的对象
AutoGenerator autoGenerator = new AutoGenerator();
//设置数据库相关配置
DataSourceConfig dataSource = new DataSourceConfig();
dataSource.setDriverName("com.mysql.cj.jdbc.Driver");
dataSource.setUrl("jdbc:mysql://localhost:3306/db1?serverTimezone=UTC");
dataSource.setUsername("root");
dataSource.setPassword("031006");
autoGenerator.setDataSource(dataSource);
//设置全局配置
GlobalConfig globalConfig = new GlobalConfig();
globalConfig.setOutputDir(System.getProperty("user.dir")+"/src/main/java"); //设置代码生成位置
globalConfig.setOpen(false); //设置生成完毕后是否打开生成代码所在的目录
globalConfig.setAuthor("xlr"); //设置作者
globalConfig.setFileOverride(true); //设置是否覆盖原始生成的文件
globalConfig.setMapperName("%sDao"); //设置数据层接口名,%s为占位符,指代模块名称
globalConfig.setIdType(IdType.ASSIGN_ID); //设置Id生成策略
autoGenerator.setGlobalConfig(globalConfig);
//设置包名相关配置
PackageConfig packageInfo = new PackageConfig();
packageInfo.setParent("com.example"); //设置生成的包名,与代码所在位置不冲突,二者叠加组成完整路径
packageInfo.setEntity("domain"); //设置实体类包名
packageInfo.setMapper("dao"); //设置数据层包名
autoGenerator.setPackageInfo(packageInfo);
//策略设置
StrategyConfig strategyConfig = new StrategyConfig();
strategyConfig.setInclude("tbl_user"); //设置当前参与生成的表名,参数为可变参数
strategyConfig.setTablePrefix("tbl_"); //设置数据库表的前缀名称,模块名 = 数据库表名 - 前缀名 例如: User = tbl_user - tbl_
strategyConfig.setRestControllerStyle(true); //设置是否启用Rest风格
strategyConfig.setVersionFieldName("version"); //设置乐观锁字段名
strategyConfig.setLogicDeleteFieldName("deleted"); //设置逻辑删除字段名
strategyConfig.setEntityLombokModel(true); //设置是否启用lombok
autoGenerator.setStrategy(strategyConfig);
//2.执行生成操作
autoGenerator.execute();
}
}