- MyBatisPlus(简称MP)是基于MyBatis框架基础上开发的增强型工具,旨在简化开发,提高效率
- 开发方式
- 基于MyBatis使用MyBatisPlus
- 基于Spring使用MyBatisPlus
- 基于SpringBoot使用MyBatisPlus
- 官网 :https://baomidou.com/
- 无侵入:只做增强不做改变,引入它不会对现有工程产生影响,如丝般顺滑
- 强大的 CRUD 操作:内置通用 Mapper、通用 Service,仅仅通过少量配置即可实现单表大部分 CRUD 操作,更有强大的条件构造器,满足各类使用需求
- 支持 Lambda 形式调用:通过 Lambda 表达式,方便的编写各类查询条件,无需再担心字段写错
- 支持主键自动生成:支持多达 4 种主键策略(内含分布式唯一 ID 生成器 - Sequence),可自由配置,完美解决主键问题
- 内置分页插件:基于 MyBatis 物理分页,开发者无需关心具体操作,配置好插件之后,写分页等同于普通 List 查询
- …
- SpringBoot整合MyBatis开发过程(复习)
- 创建SpringBoot工程
- 勾选配置使用的技术
- 设置DataSource的相关属性(JDBC参数)
- 定义数据层接口映射配置
com.baomidou mybatis-plus-boot-starter 3.5.2 com.alibaba druid 1.2.11
- 由于mp并未被收录到idea的系统内置配置,无法直接选择加入
spring:
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/test
username: root
password: 12345678
type: com.alibaba.druid.pool.DruidDataSource
- 注意:如果使用Druid数据源,则需要导入对应坐标
- 注意这里的主键必须为bigint,因为springboot的底层会做转换
SET NAMES utf8mb4; SET FOREIGN_KEY_CHECKS = 0; -- ---------------------------- -- Table structure for user -- ---------------------------- DROP TABLE IF EXISTS `user`; CREATE TABLE `user` ( `id` bigint NOT NULL AUTO_INCREMENT, `name` char(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL, `password` char(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL, `age` int NULL DEFAULT NULL, `tel` char(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL, PRIMARY KEY (`id`) USING BTREE ) ENGINE = InnoDB AUTO_INCREMENT = 1 CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Dynamic; -- ---------------------------- -- Records of user -- ---------------------------- INSERT INTO `user` VALUES (1, 'null', 'admin', 1, '123456'); INSERT INTO `user` VALUES (2, 'heimachengxuyuan', '12345678', 18, '123456789'); INSERT INTO `user` VALUES (1552709826475171842, NULL, NULL, NULL, NULL); SET FOREIGN_KEY_CHECKS = 1;
public class User {
private Long id;
private String name;
private String password;
private Integer age;
private String tel;
}
⑥定义数据接口,继承BaseMapper@Mapper public interface UserDao extends BaseMapper⑦在测试类中注入dao接口,测试功能{ }
@SpringBootTest
class Mybatis01QuickstartApplicationTests {
@Autowired
private UserDao userDao;
@Test
void testGetAll() {
List list = userDao.selectList(null);
System.out.println(list);
}
}
标准CRUD功能
lombok
- Lombok,一个Java类库,提供了一组注解,简化POJO实体类开发
org.projectlombok lombok 1.18.24 provided
-
常用注解:@Data
-
//lomok //@Setter //@Getter //@ToString //@NoArgsConstructor //@AllArgsConstructor //@EqualsAndHashCode @Data public class User { private Long id; private String name; private String password; private Integer age; private String tel; } -
为当前实体类在编译期设置对应的get/set方法,无参/有参构造方法,toString()方法,hashCode方法,equals方法等
@Configuration
public class MpConfig {
@Bean
public MybatisPlusInterceptor mybatisPlusInterceptor() {
//1.定义Mp拦截器
MybatisPlusInterceptor mybatisPlusInterceptor = new MybatisPlusInterceptor();
//2.添加具体的拦截器
mybatisPlusInterceptor.addInnerInterceptor(new PaginationInnerInterceptor());
return mybatisPlusInterceptor;
}
}
②执行分页查询
@Test
void testGetByPage() {
IPage page = new Page(1, 5);
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());
}
- 开启日志(可以不开启)
mybatis-plus:
configuration:
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
DQL编程控制
条件查询
- MyBatisPlus将书写复杂的SQL查询条件进行了封装,使用编程的形式完成查询条件的组合
@Test
void testGetAll() {
// 方式一:按条件查询
// QueryWrapper qw = new QueryWrapper();
// qw.lt("age", 18);
// List userList = userDao.selectList(qw);
// System.out.println(userList);
// //方式二:Lambda格式按条件查询
// QueryWrapper qw = new QueryWrapper();
// qw.lambda().lt(User::getAge,20);
// List userList = userDao.selectList(qw);
// System.out.println(userList);
//方式三:Lambda格式按条件查询
// LambdaQueryWrapper lqw = new LambdaQueryWrapper();
// lqw.lt(User::getAge,20);
// List userList = userDao.selectList(lqw);
// System.out.println(userList);
LambdaQueryWrapper lqw = new LambdaQueryWrapper();
// lqw.lt(User::getAge,20);
// lqw.gt(User::getAge,10);
//链式编程--and
// lqw.lt(User::getAge, 20).gt(User::getAge, 10);
//或者or
lqw.lt(User::getAge, 10).or().gt(User::getAge, 15);
List userList = userDao.selectList(lqw);
System.out.println(userList);
}
条件查询–null值处理
//模拟页面传递过来的查询数据 UserQuery uq = new UserQuery(); uq.setAge(10); uq.setAge2(30); //null判定 //1.使用if判断 //2.参数:添加一个条件 LambdaQueryWrapper查询投影lqw = new LambdaQueryWrapper (); //先判定第一个参数是否为true,如果为true连接当前条件 lqw.lt(null != uq.getAge2(), User::getAge, uq.getAge2()) .gt(null != uq.getAge(), User::getAge, uq.getAge()); List userList = userDao.selectList(lqw); System.out.println(userList);
//查询投影 // LambdaQueryWrapperlqw = new LambdaQueryWrapper (); // //设置只查哪个字段,并且只适用于匿名表达式 // lqw.select(User::getId, User::getName, User::getAge); //非匿名表达式写法 // QueryWrapper qw = new QueryWrapper (); // qw.select("id","name","age","tel"); // List userList = userDao.selectList(qw); // System.out.println(userList); QueryWrapper qw = new QueryWrapper (); qw.select("count(*) as count,tel"); qw.groupBy("tel"); List
- 查询结果包含模型类中部分属性
LambdaQueryWrapperlqw = new LambdaQueryWrapper (); lqw.select(User::getId,User::getName,User::getAge); List userList = userDao.selectList(lqw); System.out.println(userList);
- 查询结果包含模型类中未定义的属性
QueryWrapper查询条件qm = new QueryWrapper (); qm.select("count(*) as nums,gender"); qm.groupBy("gender"); List
//条件查询 // LambdaQueryWrapperlqw = new LambdaQueryWrapper (); // //等同于= // lqw.eq(User::getName,"jerry").eq(User::getPassword,"jerry"); // User user = userDao.selectOne(lqw); // System.out.println(user); // //范围查询lt le gt ge eq between // LambdaQueryWrapper lqw = new LambdaQueryWrapper (); // //前面必须是小的 // lqw.between(User::getAge,10,20); // List userList = userDao.selectList(lqw); // System.out.println(userList); // 模糊匹配like likeRight--J% likeLeft--%J LambdaQueryWrapper lqw = new LambdaQueryWrapper (); lqw.likeLeft(User::getName,"J"); List userList = userDao.selectList(lqw); System.out.println(userList);
- 用户登录(eq匹配)
LambdaQueryWrapperlqw = new LambdaQueryWrapper (); lqw.eq(User::getName,userQuery.getAge()).eq(User::getPassword,userQuery.getPassword()); User loginUser = userDao.selectOne(lqw); System.out.println(loginUser);
- 购物设定价格区间、户籍设定年龄区间(le ge 匹配或between匹配)
LambdaQueryWrapperlqw = new LambdaQueryWrapper (); //方案一:设定上限下限 lwq.le(User::getAge,userQueery.getAge()).ge(User::getAge,userQuery.getAge2()); //方案二:设定范围 lqw.between(User::getAge,userQuery.getAge(),userQuery.getAge2()); List userList = userDao.selectList(lqw); System.out.println(userList);
- 查信息,搜索新闻(非全文检索版:like匹配)
LambdaQueryWrapperlqw = new LambdaQueryWrapper (); lqw.likeLeft(User::geteTel,userQuery.getTel()); List userList = userDao.selectList(lqw); System.out.println(userList);
- 统计报表(分组查询聚合函数)
QueryWrapper官网(条件构造器)API:qm = new QueryWrapper (); qm.select("count(*) as nums,gender"); qm.groupBy("gender"); List
https://baomidou.com/pages/10c804/#abstractwrapper
字段映射与表名映射- 名称:@TableFiled
- 类型:属性注解
- 位置:模型类属性定义上方
- 作用:设置当前属性对应的数据库表中的字段关系
- 范例:
public class User{
@TableField(value="pwd")
private Stirng password;
}
- 相关属性
- value(默认):设置数据表字段名称
- exist:设置属性在数据库表字段中是否存在,默认为true。此属性无法与value合并使用
- select:设置属性是否参与查询,此属性与select()映射配置不冲突(select=false)
- 名称:@TableName
- 类型:类注解
- 位置:模型类定义上方
- 作用:设置当前类对应的数据库表的关系
- 范例:
@tableName("tbl_user")
public class User{
private Long id;
}
- 相关属性
- value(默认):设置数据库表名称
- 不同的表应用不同的id生成策略
- 日志:自增(1,2,3,4,…)
- 购物订单:特殊规则(FQ23948AK3843)
- 外卖单:关联地区日志日期等信息( 10 04 20200314 34 91)
- 关系表:可省略id
- …
- 名称:@TableId
- 类型:属性注解
- 位置:模型类中用于表示主键的属性定义上方
- 作用:设置当前类中主键属性的生成策略
- 范例:
public class User{
@TableId(type=IdType.AUTO)
private Long id;
}
- 相关属性:
- value:设置数据库主键名称
- type:设置主键属性的生成策略,值参照IdType枚举类型
-
AUTO(0):使用数据库id自增策略控制id生成
-
NONE(1):不设置id自动生成策略
-
INPUT(2):用户手工输入id
-
ASSIGN_ID(3):雪花算法生成id(可兼容数据型与字符串型)(IDEA默认)
-
ASSIGN_UUID(4):以UUID生成算法作为id生成策略
-
雪花算法:(64位整数)
- 第一位占位符必定是0(如果是1表示为负数)
- 时间戳(41位)精确到毫秒,但是算法与我们的时间戳不一样
- 机器码(5+5):群组加机器
- 序列号(12位)
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() {
List list = new ArrayList<>();
list.add(1552924879866527746L);
list.add(1552927500232777730L);
userDao.deleteBatchIds(list);
//同理
// userDao.selectBatchIds();
}
-
根据主键删除多条记录
-
根据主键查询多条记录
- 删除操作业务问题业务数据从数据库中丢弃
- 逻辑删除:为数据设置是否为可用状态字段,删除时设置状态字段为不可用状态,数据保留在数据库中
//逻辑删除字段,标记当前记录是否被删除
// @TableLogic(value = "0", delval = "1")
private Integer deleted;
③配置逻辑删除字面值
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-delete-value: 1
logic-not-delete-value: 0
乐观锁
步骤:
①数据库表中添加锁标记字段
②实体类中添加对应字段,并设定当前字段为逻辑删除标记字段
@Version private Integer version;③配置乐观锁拦截器实现锁机制对应的动态SQL语句拼装
@Bean
public MybatisPlusInterceptor mybatisPlusInterceptor() {
//1.定义Mp拦截器
MybatisPlusInterceptor mybatisPlusInterceptor = new MybatisPlusInterceptor();
//2.添加具体的拦截器
mybatisPlusInterceptor.addInnerInterceptor(new PaginationInnerInterceptor());
//3.添加乐观锁的拦截器
mybatisPlusInterceptor.addInnerInterceptor(new OptimisticLockerInnerInterceptor());
return mybatisPlusInterceptor;
}
④使用乐观锁机制在修改前必须先获取到对应数据的version方可以正常进行
//1.先通过要修改的数据id将当前数据查询出来
User user = userDao.selectById(2L);//version=3
User user2 = userDao.selectById(2L);//version = 3
//2.将要修改的属性逐一设置进去
user.setName("Jackaaa");
userDao.updateById(user);
user.setName("Jack999");
userDao.updateById(user2);
代码生成器(快速开发)
- 模板:MyBatisPlus提供
- 数据库相关配置:读取数据库获取信息
- 开发者自定义配置:手工配置
package com.itheima;
import com.baomidou.mybatisplus.annotation.IdType;
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;
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/test");
dataSource.setUsername("root");
dataSource.setPassword("12345678");
autoGenerator.setDataSource(dataSource);
//设置全局配置
GlobalConfig globalConfig = new GlobalConfig();
globalConfig.setOutputDir(System.getProperty("user.dir")+"/mybatisplus_04_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.aaa"); //设置生成的包名,与代码所在位置不冲突,二者叠加组成完整路径
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();
}
}
package com.southwind;
import com.baomidou.mybatisplus.annotation.DbType;
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.NamingStrategy;
public class GenerateTest {
public static void main(String[] args) {
//创建generator对象
AutoGenerator autoGenerator = new AutoGenerator();
//数据源
DataSourceConfig dataSourceConfig = new DataSourceConfig();
dataSourceConfig.setDbType(DbType.MYSQL);
dataSourceConfig.setDriverName("com.mysql.cj.jdbc.Driver");
dataSourceConfig.setUsername("root");
dataSourceConfig.setPassword("123456");
dataSourceConfig.setUrl("jdbc:mysql://localhost:3306/test11");
autoGenerator.setDataSource(dataSourceConfig);
//全局配置
GlobalConfig globalConfig = new GlobalConfig();
globalConfig.setOutputDir(System.getProperty("user.dir")+"/src/main/java");
globalConfig.setAuthor("admin");
globalConfig.setOpen(false);
globalConfig.setServiceName("%sService");
autoGenerator.setGlobalConfig(globalConfig);
//包信息
PackageConfig packageConfig = new PackageConfig();
packageConfig.setParent("com.southwind");
packageConfig.setEntity("entity");
packageConfig.setMapper("mapper");
packageConfig.setService("service");
packageConfig.setServiceImpl("service.impl");
packageConfig.setController("controller");
autoGenerator.setPackageInfo(packageConfig);
//策略配置
StrategyConfig strategyConfig = new StrategyConfig();
strategyConfig.setInclude("fruit");
strategyConfig.setNaming(NamingStrategy.underline_to_camel);
strategyConfig.setColumnNaming(NamingStrategy.underline_to_camel);
strategyConfig.setEntityLombokModel(true);
autoGenerator.setStrategy(strategyConfig);
//运行
autoGenerator.execute();
}
}
com.baomidou mybatis-plus-generator 3.4.1 org.apache.velocity velocity-engine-core 2.3



