MyBatisPlus
-
MyBatisPlus的使用简化了mybatis,而且并不影响其他功能的使用,配合好提供的方法有奇效
-
完整项目可见mybatisPlusDemo
1、MyBatisPlus入门案例
创建数据库及表
create database if not exists mybatisplus_db character set utf8;
use mybatisplus_db;
CREATE TABLE user (
id bigint(20) primary key auto_increment,
name varchar(32) not null,
password varchar(32) not null,
age int(3) not null ,
tel varchar(32) not null
);
insert into user values(1,'Tom','tom',3,'18866668888');
insert into user values(2,'Jerry','jerry',4,'16688886666');
insert into user values(3,'Jock','123456',41,'18812345678');
insert into user values(4,'传智播客','itcast',15,'4006184000');
勾选配置使用技术
pom.xml补全依赖
- 由于MP并未被收录到idea的系统内置配置,无法直接选择加入,需要手动在pom.xml中配置添加
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.4.1</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.16</version>
</dependency>
如果有mp相关报错,可能是冲突了
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.4.1</version>
<exclusions>
<exclusion>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.7</version>
</dependency>
说明:
-
druid数据源可以加也可以不加,SpringBoot有内置的数据源,可以配置成使用Druid数据源
-
从MP的依赖关系可以看出,通过依赖传递已经将MyBatis与MyBatis整合Spring的jar包导入,我们不需要额外在添加MyBatis的相关jar包
添加MP的相关配置信息
#spring基本配置
spring:
datasource:
type: com.alibaba.druid.pool.DruidDataSource
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/mybatisplus_db?serverTimezone=Asia/Shanghai&characterEncoding=utf8&useUnicode=true&useSSL=false
username: root
password: 232104
# mp日志
mybatis-plus:
configuration:
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
根据数据库表创建实体类
特别注意:使用mybatisPlus的时候要根据数据库表创建实体类,它们的名字要相同
@Data
@AllArgsConstructor
@NoArgsConstructor
public class User {
private Long id;
private String name;
private String password;
private Integer age;
private String tel;
}
创建Dao接口,继承BaseMapper
public interface UserDao extends BaseMapper<User>{
}
public interface UserService extends IService<User> {
}
@Service
public class UserServiceImpl extends ServiceImpl<UserDao, User> implements UserService {
}
编写引导类
@SpringBootApplication
@MapperScan("com.haoyu.dao")
public class Mybatisplus01QuickstartApplication {
public static void main(String[] args) {
SpringApplication.run(Mybatisplus01QuickstartApplication.class, args);
}
}
说明:Dao接口要想被容器扫描到,有两种解决方案:
- 方案一:在Dao接口上添加
@Mapper
注解,并且确保Dao处在引导类所在包或其子包中- 该方案的缺点是需要在每一Dao接口中添加注解
- 方案二:在引导类(启动类)或MybatisPlusConfig配置文件上添加
@MapperScan
注解,其属性为所要扫描的Dao所在包- 该方案的好处是只需要写一次,则指定包下的所有Dao接口都能被扫描到,
@Mapper
就可以不写。
- 该方案的好处是只需要写一次,则指定包下的所有Dao接口都能被扫描到,
2、MybatisPlus简介
-
MybatisPlus(简称MP)是基于MyBatis框架基础上开发的增强型工具,旨在简化开发、提供效率。
-
主要开发方式:基于SpringBoot使用MyBatisPlus
-
MyBatisPlus的官网为:
https://mp.baomidou.com/
MP的特性:
-
无侵入:只做增强不做改变,不会对现有工程产生影响
-
强大的 CRUD 操作:内置通用 Mapper,少量配置即可实现单表CRUD 操作
-
支持 Lambda:编写查询条件无需担心字段写错
-
支持主键自动生成
-
内置分页插件
-
……
3、基本使用
——特别注意,以下都是BaseMapper接口的方法(dao实现),以后我们都是用service层方法(实现的是IService接口),里面的方法是有区别的,需要注意。
——就像分页,BaseMapper接口方法为selectPage,而IService接口方法为page(其底层实现实际上也是调用selectPage)
扩展:以上方法Id为Serializable类型。
-
思考:参数类型为什么是一个序列化类?
从这张图可以看出 ,
- String和Number是Serializable的子类,
- Number又是Float,Double,Integer等类的父类,
- 能作为主键的数据类型都已经是Serializable的子类,MP使用Serializable作为参数类型,就好比我们可以用Object接收任何数据类型一样。
3.1、分页功能
IPage<T> selectPage(IPage<T> page, Wrapper<T> queryWrapper)
- IPage:用来构建分页查询条件
- Wrapper:用来构建条件查询的条件,目前我们没有可直接传为Null
- IPage:返回值,你会发现构建分页条件和方法的返回值都是IPage
步骤1:调用方法传入参数获取返回值
@SpringBootTest
class Test{
@Autowired
private UserDao userDao;
//分页查询
@Test
void testSelectPage(){
//1 创建IPage分页对象,设置分页参数,1为当前页码,3为每页显示的记录数
IPage<User> page=new Page<>(1,3);
//2 执行分页查询
userDao.selectPage(page,null);
//3 获取分页结果
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());
}
}
步骤2:设置分页拦截器
这个拦截器MP已经为我们提供好了,我们只需要将其配置成Spring管理的bean对象即可。
@Configuration
public class MybatisPlusConfig {
@Bean
public MybatisPlusInterceptor mybatisPlusInterceptor(){
//1 创建MybatisPlusInterceptor拦截器对象
MybatisPlusInterceptor mpInterceptor=new MybatisPlusInterceptor();
//2 添加分页拦截器
mpInterceptor.addInnerInterceptor(new PaginationInnerInterceptor());
return mpInterceptor;
}
}
**说明:**上面的代码记不住咋办呢?
这些内容在MP的官方文档中有详细的说明,我们可以查看官方文档类配置
步骤3:运行测试程序
3.2、Yaml文件扩展配置
- 查看MP执行的SQL语句
mybatis-plus:
configuration:
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl #打印SQL日志到控制台
打开日志后,就可以在控制台打印出对应的SQL语句,开启日志功能性能就会受到影响,调试完后记得关闭。
- Mq日志
# mp日志
mybatis-plus:
configuration:
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
global-config:
banner: off # 关闭mybatisplus启动图标
4、DQL编程控制(重点)
4.1、条件查询
- MyBatisPlus将书写复杂的SQL查询条件进行了封装,使用编程的形式完成查询条件的组合。
- apper类是用来构建查询条件的,它是一个抽象类,我们一般用他的子类。
方式一:QueryWrapper
QueryWrapper qw = new QueryWrapper();
qw.lt("age",18);
List<User> userList = userDao.selectList(qw);
-
lt: 小于(<) ,最终的sql语句为
SELECT id,name,password,age,tel FROM user WHERE (age < ?)
方式二:LambdaQueryWrapper(也就是在QueryWrapper基础上使用Lambda,推荐使用)
QueryWrapper<User> qw = new QueryWrapper<User>();
qw.lt(User::getAge, 10);//添加条件
List<User> userList = userDao.selectList(qw);
注意:构建LambdaQueryWrapper的时候泛型不能省。 User::getAget,为lambda表达式中的,类名::方法名
重点:构造条件简化书写
userDao.selectList(Wappers.<User>lambdaQuery().lt(User::getAge,10)
4.2、多条件构建
需求:查询数据库表中,年龄小于10或年龄大于30的数据
注意:构建多条件的时候,可以支持链式编程
lqw.lt(User::getAge, 10).or().gt(User::getAge, 30);
List<User> userList = userDao.selectList(lqw);
-
lt: 小于(<),gt:大于(>)
-
or()就相当于我们sql语句中的
or
关键字,不加默认是and
SELECT id,name,password,age,tel FROM user WHERE (age < ? OR age > ?)
4.3、动态SQL
@RequestMapping("/list")
List<Dept> getDepts(@RequestParam("depno") Integer depno,
@RequestParam("dname")String dname,@RequestParam("loc")String loc){
LambdaQueryWrapper<Dept> queryWrapper=new LambdaQueryWrapper<>();
//方式1:
if(null!= depno && depno>0){
queryWrapper.eq(Dept::getDeptno,depno);
}
//方式2:
queryWrapper.eq(null != depno && depno > 0, Dept::getDeptno, depno);
queryWrapper.eq(null != dname && dname != "", Dept::getDname, dname);
queryWrapper.eq(null != loc && loc != "", Dept::getLoc, loc);
return deptService.list(queryWrapper);
}
4.4、查询投影
——不查询所有字段,只查询出指定内容的数据。
LambdaQueryWrapper<User> lqw = new LambdaQueryWrapper<User>();
lqw.select(User::getId,User::getName,User::getAge);
或则
QueryWrapper<User> lqw = new QueryWrapper<User>();
lqw.select("id","name","age","tel");
- 最终的sql语句为:SELECT id,name,age,tel FROM user
4.5、聚合查询
@Test
void testGetAll(){
QueryWrapper<User> lqw = new QueryWrapper<User>();
1. count:总记录数 lqw.select("count(*) as count");
对应:SELECT count(*) as count FROM user
2. max:最大值 lqw.select("max(age) as maxAge");
对应:SELECT max(age) as maxAge FROM user
3. min:最小值 lqw.select("min(age) as minAge");
对应:SELECT min(age) as minAge FROM user
4. sum:求和 lqw.select("sum(age) as sumAge");
对应:SELECT sum(age) as sumAge FROM user
5. avg:平均值 lqw.select("avg(age) as avgAge");
对应:SELECT avg(age) as avgAge FROM user
List<Map<String, Object>> userList = userDao.selectMaps(lqw);
System.out.println(userList);
}
注意:为了在做结果封装的时候能够更简单,我们将上面的聚合函数都起了个名称,方面后期来获取这些数据。
4.6、分组查询
@Test
void testGetAll(){
QueryWrapper<User> lqw = new QueryWrapper<User>();
lqw.select("count(*) as count,tel");
lqw.groupBy("tel");
List<Map<String, Object>> list = userDao.selectMaps(lqw);
System.out.println(list);
}
SELECT count(*) as count,tel FROM user GROUP BY tel
注意:
- 聚合与分组查询,无法使用lambda表达式来完成
- MP只是对MyBatis的增强,如果MP实现不了,我们可以直接在DAO接口中使用MyBatis的方式实现
4.7、查询条件
-
范围匹配
-
gt():大于(>)
-
ge():大于等于(>=)
-
lt():小于(<)
-
lte():小于等于(<=)
-
between():between ? and ?
-
lqw.between(User::getAge, 10, 30);
-
-
-
模糊匹配(like)
-
like():前后加百分号,如 %J%
-
likeLeft():前面加百分号,如 %J
-
lqw.likeLeft(User::getName, "J"); //SELECT * FROM user WHERE (name LIKE %J)
-
-
likeRight():后面加百分号,如 J%
-
-
空判定(null)
-
包含性匹配(in)
-
分组(group)
-
排序(order)
-
LambdaQueryWrapper<User> lwq = new LambdaQueryWrapper<>(); /** * condition :条件,返回boolean, 当condition为true,进行排序,如果为false,则不排序 * isAsc:是否为升序,true为升序,false为降序 * columns:需要操作的列 */ lwq.orderBy(true,false, User::getId);
-
5、映射匹配兼容性
-
表的列名和实体类的属性名发生不一致,会导致数据封装不到模型对象
-
解决方法:使用@TableField注解的value属性
@TableField(value="pwd",select = false) private String password;
-
-
实体类添加了数据库中未定义的属性
-
解决方法:使用@TableField注解 的exist属性
@TableField(exist=false) private Dept dept;
-
-
表的名称和实体类的名称不一致
-
解决方法:使用@TableName 注解
@TableName("tb_user") public class User
-
知识点1:@TableField
名称 | @TableField |
---|---|
类型 | 属性注解 |
位置 | 模型类属性定义上方 |
作用 | 设置当前属性对应的数据库表中的字段关系 |
相关属性 | value(默认):设置数据库表字段名称 exist:设置属性在数据库表字段中是否存在,默认为true,此属性不能与value合并使用 select:设置属性是否参与查询,此属性与select()映射配置不冲突 |
知识点2:@TableName
名称 | @TableName |
---|---|
类型 | 类注解 |
位置 | 模型类定义上方 |
作用 | 设置当前类对应于数据库表关系 |
相关属性 | value(默认):设置数据库表名称 |
知识点3:@TableId
名称 | @TableId |
---|---|
类型 | 属性注解 |
位置 | 模型类中用于表示主键的属性定义上方 |
作用 | 设置当前类中主键属性的生成策略 |
相关属性 | ==value(默认):设置数据库表主键名称 ==type:设置主键属性的生成策略,值查照IdType的枚举值 |
6、表id生成策略(重点)
策略一:主键自增
-
一般使用在单机项目或者小型项目里(数据库只有一台的情况下)。
-
优点:
- 实现简单,依靠数据库即可,成本小。
- 按顺序生成,保证了唯一性,利于主键索引(聚簇索引)的数据插入(每次数据插入的时候,会在 B+Tree 中寻找到适合它自己的位置插入,因为是顺序生成,不会挪动其他节点,插入效率高)。
缺点:
- 不能使用在分布式系统,因为每个节点都需要维护一个独立的计数器,可能会导致冲突和性能问题。
-
实现
//数据库实现 CREATE TABLE test ( id int primary key auto_increment ); //mp实现,特别注意,表结构也要实现主键自增,否者无效 @TableId(type = IdType.AUTO) private Long id;
-
可以点击IdType查看所有策略
- NONE:不设置id生成策略,自己手动添加,容易造成主键冲突
- INPUT:用户手工输入id,相当于None,容易造成主键冲突
- ASSIGN_ID:雪花算法生成id(可兼容Long型与字符串型,mq默认执行策略)
- ASSIGN_UUID:以UUID生成算法作为id生成策略
- 其他的几个策略均已过时
扩展:分布式ID
当数据量足够大的时候,一台数据库服务器存储不下,这个时候就需要多台数据库服务器进行存储。
- 比如订单表就有可能被存储在不同的服务器上
- 如果用数据库表的自增主键,因为在两台服务器上,所以会出现冲突
- 这个时候就需要一个全局唯一ID,这个ID就是分布式ID。
策略二:雪花算法
-
是Twitter官方给出的算法实现,是用Scala写的。其生成的结果是一个64bit大小整数。
-
一般使用在大规模的分布式系统、微服务架构、分布式数据库等。
-
优点:
- 支持分布式环境,生成的ID有序且唯一,包含时间戳信息。
- 保证在分布式环境中生成的 ID 具有唯一性和有序性。
-
缺点:
- 生成的策略和服务器时间有关,如果修改了系统时间就有可能导致出现重复主键
-
实现
——注意生成的ID是一个Long类型的数据。
//数据库实现 CREATE TABLE test ( id BIGINT UNSIGNED NOT NULL PRIMARY KEY DEFAULT (SELECT next_id() FROM tbl_sequencer WHERE name='test'); ); //mq实现(mq默认使用) @TableId(type = IdType.ASSIGN_ID) private Long id;
策略三:UUID
-
是根据算法生成的无序字符串(通常为36个字符)。一般适用于分布式系统或生成唯一ID。.
-
优点:能够完全保证生成id的唯一性。
-
缺点:
- 如果使用UUID当做主键,那么主键索引就会失效。
- 占用较多的存储空间,无序性,可读性较低。
-
实现:
//mysql实现 CREATE TABLE test ( id CHAR(36) NOT NULL PRIMARY KEY DEFAULT (UUID()); ); //mq实现 @TableId(type = IdType.ASSIGN_UUID) private String id;
7、MP实现乐观锁(重点)
实现方式:
- 数据库表中添加version列,比如默认值给1
- 第一个线程要修改数据之前,取出记录时,获取当前数据库中的version=1
- 第二个线程要修改数据之前,取出记录时,获取当前数据库中的version=1
- 假如这两个线程都来更新数据,第一个和第二个线程都可能先执行
- 假如第一个线程先执行更新,会把version改为2,
- 第二个线程再更新的时候,set version = 2 where version = 1,此时数据库表的数据version已经为2,所以第二个线程会修改失败
实现步骤:
步骤1:数据库表添加列
列名可以任意,比如使用version,给列设置默认值为1

步骤2:在模型类中添加对应的属性
根据添加的字段列名,在模型类中添加对应的属性值,并在属性上加上@Version注解。
@Data
@TableName("tbl_user")
public class User {
private String id;
private String name;
@Version
private Integer version;
}
步骤3:添加乐观锁的拦截器
@Configuration
public class MpConfig {
@Bean
public MybatisPlusInterceptor mpInterceptor() {
//1.定义Mp拦截器
MybatisPlusInterceptor mpInterceptor = new MybatisPlusInterceptor();
//2.添加乐观锁拦截器
mpInterceptor.addInnerInterceptor(new OptimisticLockerInnerInterceptor());
return mpInterceptor;
}
}
步骤4:执行更新操作
要想实现乐观锁,首先第一步应该是拿到表中的version,然后拿version当条件在将version加1更新回到数据库表中,所以我们在更新的时候,需要对其进行查询
@SpringBootTest
class Tests {
@Autowired
private UserDao userDao;
@Test
void testUpdate(){
//步骤1:先通过要修改的数据id将当前数据查询出来
User user = userDao.selectById(3L); //version=3
User user2 = userDao.selectById(3L); //version=3
user.setName("haoyu");
user.setVersion=4;
userDao.updateById(user); //此时verion=4
user2.setName("sijia");
user2setVersion=4;
userDao.updateById(user2); //修改失败
}
}
运行程序,分析结果:
8、Mp代码生成器
添加这个技术所需依赖:
<!--代码生成器-->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-generator</artifactId>
<version>3.4.1</version>
</dependency>
<!--velocity模板引擎-->
<dependency>
<groupId>org.apache.velocity</groupId>
<artifactId>velocity-engine-core</artifactId>
<version>2.3</version>
</dependency>
代码生成器类:
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/mybatisplus_db?serverTimezone=UTC&useUnicode=true&useSSL=false&characterEncoding=utf8&allowPublicKeyRetrieval=true");
dataSource.setUsername("root");
dataSource.setPassword("232104");
autoGenerator.setDataSource(dataSource);
//设置全局配置
GlobalConfig globalConfig = new GlobalConfig();
globalConfig.setOutputDir(System.getProperty("user.dir")+"/mybatisplus_generator/src/main/java"); //设置代码生成位置
globalConfig.setOpen(false); //设置生成完毕后是否打开生成代码所在的目录
globalConfig.setAuthor("黑马程序员"); //设置作者
globalConfig.setFileOverride(true); //设置是否覆盖原始生成的文件
globalConfig.setMapperName("%sDao"); //设置数据层接口名,%s为占位符,指代模块名称
globalConfig.setIdType(IdType.ASSIGN_ID); //设置Id生成策略
autoGenerator.setGlobalConfig(globalConfig);
//设置包名相关配置
PackageConfig packageInfo = new PackageConfig();
packageInfo.setParent("com.haoyu.api"); //生成文件放置的目录
packageInfo.setEntity("pojo"); //设置实体类包名
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();
}
}
或
/**
* mybatis-plus 代码生成器
*
*/
public class CodeGenerator {
public static void main(String[] args) {
// 代码生成器
AutoGenerator mpg = new AutoGenerator();
// 全局配置
GlobalConfig gc = new GlobalConfig();
String projectPath = System.getProperty("user.dir");
gc.setOutputDir(projectPath + "/src/main/java");
gc.setAuthor("haoyu"); //改作者
gc.setMapperName("I%sDao");
gc.setOpen(false);
// gc.setSwagger2(true); 实体属性 Swagger2 注解
mpg.setGlobalConfig(gc);
// 数据源配置
DataSourceConfig dsc = new DataSourceConfig();
dsc.setUrl("jdbc:mysql://localhost:3306/db2306?serverTimezone=UTC&useUnicode=true&useSSL=false&characterEncoding=utf8&allowPublicKeyRetrieval=true");
// dsc.setSchemaName("public");
dsc.setDriverName("com.mysql.cj.jdbc.Driver");
dsc.setUsername("root");
dsc.setPassword("232104");
mpg.setDataSource(dsc);
// 包配置
PackageConfig pc = new PackageConfig();
pc.setModuleName(""); //设置模块名
pc.setEntity("pojo");//设置实体类包名
pc.setMapper("dao");
pc.setParent("com.haoyu.dao"); //生成文件放置的目录
mpg.setPackageInfo(pc);
// 自定义配置
InjectionConfig cfg = new InjectionConfig() {
@Override
public void initMap() {
// to do nothing
}
};
// 如果模板引擎是 freemarker
// String templatePath = "/templates/mapper.xml.ftl";
// 如果模板引擎是 velocity
String templatePath = "/templates/mapper.xml.vm";
// 自定义输出配置
List<FileOutConfig> focList = new ArrayList<>();
// 自定义配置会被优先输出
focList.add(new FileOutConfig(templatePath) {
@Override
public String outputFile(TableInfo tableInfo) {
// 自定义输出文件名 , 如果你 Entity 设置了前后缀、此处注意 xml 的名称会跟着发生变化!!
return projectPath + "/src/main/resources/mappers/"
+ "/" + tableInfo.getEntityName() + "Mapper" + StringPool.DOT_XML;
}
});
/*
cfg.setFileCreate(new IFileCreate() {
@Override
public boolean isCreate(ConfigBuilder configBuilder, FileType fileType, String filePath) {
// 判断自定义文件夹是否需要创建
checkDir("调用默认方法创建的目录,自定义目录用");
if (fileType == FileType.MAPPER) {
// 已经生成 mapper 文件判断存在,不想重新生成返回 false
return !new File(filePath).exists();
}
// 允许生成模板文件
return true;
}
});
*/
cfg.setFileOutConfigList(focList);
mpg.setCfg(cfg);
// 配置模板
TemplateConfig templateConfig = new TemplateConfig();
// 配置自定义输出模板
//指定自定义模板路径,注意不要带上.ftl/.vm, 会根据使用的模板引擎自动识别
// templateConfig.setEntity("templates/entity2.java");
// templateConfig.setService();
// templateConfig.setController();
templateConfig.setXml(null);
mpg.setTemplate(templateConfig);
// 策略配置
StrategyConfig strategy = new StrategyConfig();
strategy.setNaming(NamingStrategy.no_change); //驼峰命名
strategy.setColumnNaming(NamingStrategy.no_change);
// strategy.setSuperEntityClass("你自己的父类实体,没有就不用设置!");
strategy.setEntityLombokModel(true);
strategy.setRestControllerStyle(true);
strategy.setEntityTableFieldAnnotationEnable(true);//生成tableid注解
// 公共父类
// strategy.setSuperControllerClass("你自己的父类控制器,没有就不用设置!");
// 写于父类中的公共字段
// strategy.setSuperEntityColumns("id");
// strategy.setInclude(scanner("表名,多个英文逗号分割").split(","));
// strategy.setControllerMappingHyphenStyle(true);
System.out.println(pc.getModuleName());
//指定某张表生成代码,需要哪些表就选哪些表
strategy.setInclude("emp","dept","news", "newstype","user");
strategy.setTablePrefix(pc.getModuleName());
mpg.setStrategy(strategy);
mpg.setTemplateEngine(new VelocityTemplateEngine());
mpg.execute();
}
}