MyBatisPlus基础入门笔记,源码可见下载链接
大家阅读时可善用目录功能,可以提高大家的阅读效率
下载地址:MyBatisPlus源码+笔记
SpringBoot整合MyBatis(复习)
SpringBoot整合MyBatisPlus(简称mp)
<dependencies>
<dependency>
<groupId>com.baomidougroupId>
<artifactId>mybatis-plus-boot-starterartifactId>
<version>3.5.4version>
dependency>
<dependency>
<groupId>com.alibabagroupId>
<artifactId>druidartifactId>
<version>1.2.20version>
dependency>
<dependency>
<groupId>org.mybatisgroupId>
<artifactId>mybatis-springartifactId>
<version>3.0.3version>
dependency>
dependencies>
@Mapper
public interface UserDao extends BaseMapper<ABC> { }
@TableName("user")
public class ABC {
private Long id;
private String name;
private String password;
private Integer age;
private String tel;
//......
//略去getter setter方法以及toString方法
}
spring:
datasource:
type: com.alibaba.druid.pool.DruidDataSource
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/mybatisplus_db
username: root
password: 123456
@SpringBootTest
class MyBatisPlusApplicationTests {
@Autowired
private UserDao userDao;
@Test
void testGetAll() {
List<ABC> Users = userDao.selectList(null);
System.out.println(Users);
}
}
lombok,一个Java类库,提供了一组注解,简化POJO实体类的开发
常用@Data:注解在类上,提供get、set、equals、hashCode、canEqual、toString、无参构造方法,没有有参构造
pom.xml
<dependency>
<groupId>org.projectlombokgroupId>
<artifactId>lombokartifactId>
<scope>providedscope>
dependency>
@Data
//如果表名和实体类名不一样,可以加上@TableName("对应表名")的注解
@TableName("user")
public class User {
private Long id;
private String name;
private String password;
private Integer age;
private String tel;
}
@Configuration
public class Mpconfig {
@Bean
public MybatisPlusInterceptor mybatisPlusInterceptor(){
//1.定义mp拦截器
MybatisPlusInterceptor mybatisPlusInterceptor = new MybatisPlusInterceptor();
//2.在mp拦截器中添加具体拦截器
mybatisPlusInterceptor.addInnerInterceptor(new PaginationInnerInterceptor());
return mybatisPlusInterceptor;
}
}
@SpringBootTest
class MyBatisPlusApplicationTests {
@Autowired
private UserDao userDao;
@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
@SpringBootTest
class MyBatisPlusApplicationTests {
@Autowired
private UserDao userDao;
@Test
void testGetAll() {
//方式一:按条件查询
// QueryWrapper qw = new QueryWrapper<>();
// qw.lt("age",300);
// List Users = userDao.selectList(qw);
// System.out.println(Users);
//方式二:lambda格式按条件查询
// QueryWrapper qw = new QueryWrapper<>();
// qw.lambda().lt(User::getAge,200);
// List Users = userDao.selectList(qw);
// System.out.println(Users);
//方式三:lambda格式按条件查询(推荐)
// LambdaQueryWrapper qw = new LambdaQueryWrapper<>();
// qw.lt(User::getAge,200);
// List Users = userDao.selectList(qw);
// System.out.println(Users);
//方式四: lambda格式按条件查询(推荐)
LambdaQueryWrapper<User> qw = new LambdaQueryWrapper<>();
// qw.lt(User::getAge,600).gt(User::getAge,300); //可以链式编程
qw.gt(User::getAge, 600).or().lt(User::getAge, 100); //可以链式编程
List<User> Users = userDao.selectList(qw);
System.out.println(Users);
}
}
@SpringBootTest
class MyBatisPlusApplicationTests {
@Autowired
private UserDao userDao;
@Test
void testGetAll() {
UserQuery uq = new UserQuery();
// uq.setAge(100);
// uq.setAge2(300);
//null判定,先判断条件是否为true
LambdaQueryWrapper<User> qw = new LambdaQueryWrapper<>();
qw.gt(null != uq.getAge(), User::getAge, uq.getAge());
qw.lt(null != uq.getAge2(), User::getAge, uq.getAge2());
List<User> Users = userDao.selectList(qw);
System.out.println(Users);
}
}
// 包含模型类中部分属性,不是所有的聚合函数都支持
@SpringBootTest
class MyBatisPlusApplicationTests {
@Autowired
private UserDao userDao;
@Test
void testGetAll() {
QueryWrapper<User> qw = new QueryWrapper<>();
qw.select("count(*) count");
List<Map<String, Object>> Users = userDao.selectMaps(qw);
System.out.println(Users);
}
}
// 包含模型类中中未定义的属性
@SpringBootTest
class MyBatisPlusApplicationTests {
@Autowired
private UserDao userDao;
@Test
void testGetAll() {
QueryWrapper<User> qw = new QueryWrapper<>();
qw.select("count(*) count,age");
qw.groupBy("age");
List<Map<String, Object>> Users = userDao.selectMaps(qw);
System.out.println(Users);
}
}
问题一: 表字段与编码属性设计不同步
问题二: 编码中添加了数据库中未定义的属性
问题三: 采用默认查询开放了更多的字段查看权限
@TableName("user")
public class User {
private Long id;
private String name;
@TableField(value = "password",select = false)
private String pwd;
private Integer age;
private String tel;
@TableField(exist = false)
private Integer online;
}
@SpringBootTest
class MyBatisPlusApplicationTests {
@Autowired
private UserDao userDao;
@Test
void add() {
User user = new User();
user.setName("LonelySnow");
user.setPwd("LonelySnow");
user.setAge(123);
user.setTel("LonelySnow");
userDao.insert(user);
}
@Test
void delete() {
LambdaQueryWrapper<User> qw = new LambdaQueryWrapper<>();
qw.eq(User::getName, "lonelysnow");
userDao.delete(qw);
}
}
雪花算法 —— 生成一个64位的二进制数(必须用long型装)
配置文件统一添加
mybatis-plus:
global-config:
db-config:
#id策略
id-type: assign_id
#所有实体类名前拼接前缀,相当于@TableName
table-prefix: tbl_
逻辑删除步骤
//单独设置
@TableName("user")
public class User {
// @TableId(type = IdType.AUTO)
private Long id;
private String name;
@TableField(value = "password",select = false)
private String pwd;
private Integer age;
private String tel;
@TableLogic(value = "0",delval = "1")
private Integer deleted;
}
//配置通用配置
mybatis-plus:
global-config:
db-config:
logic-delete-field: deleted
logic-not-delete-value: 0
logic-delete-value: 1
乐观锁添加步骤
@Configuration
public class Mpconfig {
@Bean
public MybatisPlusInterceptor mybatisPlusInterceptor(){
//1.定义mp拦截器
MybatisPlusInterceptor mybatisPlusInterceptor = new MybatisPlusInterceptor();
//2.在mp拦截器中添加具体拦截器
mybatisPlusInterceptor.addInnerInterceptor(new PaginationInnerInterceptor());
//添加乐观锁拦截器
mybatisPlusInterceptor.addInnerInterceptor(new OptimisticLockerInnerInterceptor());
return mybatisPlusInterceptor;
}
}
@SpringBootTest
class MyBatisPlusApplicationTests {
@Autowired
private UserDao userDao;
@Test
void update(){
//1. 修改前先查询要修改的数据
User user = userDao.selectById(2L);
//2.将想要修改的属性值传进去
user.setName("snow");
userDao.updateById(user);
}
}