博客主页:不会压弯的小飞侠
✨欢迎关注:点赞收藏⭐留言✒
✨系列专栏:MyBatis-Plus专栏
✨如果觉得博主的文章还不错的话,请三连支持一下博主。
欢迎大佬指正,一起 学习!一起加油!
MyBatisPlus(简称MP)是基于MyBatis框架基础上开发的增强型工具,旨在简化开发、提高效率。
开发方式
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.4.1</version>
</dependency>
⭐⭐⭐注意:阿里云版本可以直接勾选MyBatisPlus
spring:
datasource:
driver-class-name=com: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/mybatisplus?serverTimezone=UTC
username: root
password: root
type: com.alibaba.druid.pool.DruidDataSource
mybatis-plus:
configuration:
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
使用Druid数据源,需要导入坐标:
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.2.11</version>
</dependency>
package com.jkj.domain;
import lombok.Data;
@Data
public class User {
private Long id;
private String name;
private String password;
private Integer age;
private String tel;
}
package com.jkj.dao;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.jkj.domain.User;
import org.apache.ibatis.annotations.Mapper;
@Mapper
public interface UserDao extends BaseMapper<User> {
}
@SpringBootTest
class Mybatisplus01ApplicationTests {
@Autowired
private UserDao userDao;
@Test
void findAll(){
List<User> users = userDao.selectList(null);
System.out.println(users);
}
}
int insert(T t)
int deleteById(Serializable id)
int updateById(T t)
T selectById(Serializable id)
List selectList()
IPage selectPage( IPage page)
Page selectPage(Wrapper querywrapper)
@Test
void findById(){
User user = userDao.selectById(3L);
System.out.println(user);
}
@Test
void updateById(){
User user=new User();
user.setId(2L);
user.setName("老二");
user.setTel("212121");
userDao.updateById(user);
}
@Test
void deleteById(){
userDao.deleteById(4L);
}
@Test
void save(){
User user=new User();
user.setId(4L);
user.setName("老四");
user.setPassword("444");
user.setAge(67);
user.setTel("555222");
userDao.insert(user);
}
package com.jkj.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 pageInterceptor(){
//定义MP拦截器
MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
//添加具体的拦截器
interceptor.addInnerInterceptor(new PaginationInnerInterceptor());
return interceptor;
}
}
@Test
void selectPage(){
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());
}
mybatis-plus:
configuration:
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
QueryWrapper qw = new QueryWrapper();
qw.lt("age",20);
List<User> users = userDao.selectList(qw);
System.out.println(users);
QueryWrapper<User> qw = new QueryWrapper<User>();
qw.lambda().lt(User::getAge,20);
List<User> users = userDao.selectList(qw);
System.out.println(users);
LambdaQueryWrapper<User> lqw = new LambdaQueryWrapper<>();
lqw.lt(User::getAge,20);
List<User> users = userDao.selectList(lqw);
System.out.println(users);
LambdaQueryWrapper<User> lqw = new LambdaQueryWrapper<>();
lqw.lt(User::getAge,35).gt(User::getAge,20);
List<User> users = userDao.selectList(lqw);
System.out.println(users);
LambdaQueryWrapper<User> lqw = new LambdaQueryWrapper<>();
lqw.lt(User::getAge,20).or().gt(User::getAge,35);
List<User> users = userDao.selectList(lqw);
System.out.println(users);
LambdaQuerywrapper<User> lqw = new LambdaQuerywrapper<User>();
if(null != userQuery.getAge()(
lqwn .ge(User::getAge,userQuery.getAge());
}
if(null != userQuery.getAge2()){
lqw .lt(User::getAge,userQuery.getAge2());
}
List<User> userList = userDao.selectList(lqw);
System.out.print1n(userList);
LambdaQuerywrapper<User> lqw = new LambdaQuerywrapper<User>();
lqw.ge(null != userQuery.getAge(),User::getAge,userQuery.getAge());
lqw.1t(null != userQuery.getAge2( ),User::getAge,userQuery.getAge2());
List<User> userList = userDao.selectList( 1qw ) ;
System.out.println(userList);
LambdaQuerywrapper<User> lqw = new LambdaQuerywrapper<User>();
lqw.ge(null != userQuery.getAge(),User::getAge,userQuery.getAge())
.1t(null != userQuery.getAge2( ),user::getAge,userQuery.getAge2());
List<User> userList = userDao.selectList( 1qw ) ;
System.out.println(userList);
//查询投影(方式一)
LambdaQueryWrapper<User> lqw = new LambdaQueryWrapper<>();
lqw.select(User::getId,User::getName,User::getAge);
List<User> users = userDao.selectList(lqw);
System.out.println(users);
//查询投影(方式二)
QueryWrapper qw = new QueryWrapper();
qw.select("id","name","age");
List<User> users = userDao.selectList(qw);
System.out.println(users);
QueryWrapper qw = new QueryWrapper();
qw.select("count(*) as count","tel");
qw.groupBy("tel");
List<Map<String,Object>> list = userDao.selectMaps(qw);
System.out.println(list);
- 查询条件
- 范围匹配(> = between )
- 模糊匹配( like)
- 空判定(null)
- 包含性匹配 ( in )
- 分组( group)
- 排序( order)
…
LambdaQueryWrapper<User> lqw = new LambdaQueryWrapper<>();
lqw.eq(User::getName,"老二").eq(User::getPassword,"222");
User user = userDao.selectOne(lqw);
System.out.println(user);
LambdaQueryWrapper<User> lqw = new LambdaQueryWrapper<>();
lqw.between(User::getAge,10,35);
List<User> users = userDao.selectList(lqw);
System.out.println(users);
@TableField(value = "psw")
private String password;
@TableField(exist = false)
private String address;
@TableField(value = "psw",select = false)
private String password;
@TableName("xfx_user")
public class User {
}
mybatis-plus:
global-config:
db-config:
id-type: auto
table-prefix: xfx
@TableId(type = IdType.AUTO)
private Integer id;
测试:
@Test
void save(){
User user=new User();
user.setName("老八");
user.setAge(88);
user.setPassword("888");
user.setTel("6888867");
userDao.insert(user);
}
@Test
void all(){
List<Integer> list = new ArrayList<>();
list.add(7);
list.add(8);
list.add(9);
list.add(10);
userDao.deleteBatchIds(list);
}
测试:
@Test
void all(){
List<Integer> list = new ArrayList<>();
list.add(1);
list.add(3);
list.add(5);
list.add(6);
userDao.selectBatchIds(list);
}
@TableLogic(value = "0",delval = "1")
private Integer deleted;
mybatis-plus:
configuration:
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
global-config:
db-config:
id-type: auto
table-prefix: xfx
logic-delete-field: deleted
logic-not-delete-value: 0
logic-delete-value: 1
@Test
void contextLoads() {
userDao.deleteById(5);
}
乐观锁是对于数据冲突保持一种乐观态度,操作数据时不会对操作的数据进行加锁,只有到数据提交的时候才通过一种机制来验证数据是否存在冲突。乐观锁是通过在表中增加一个版本(version)或时间戳(timestamp)来实现,版本最为常用。乐观锁每次在执行数据的修改操作时,都会带上一个版本号,一旦版本号和数据的版本号一致就可以执行修改操作并对版本号执行 +1 操作,否则就执行失败。
@Version
private Integer version;
@Configuration
public class MpConfig {
@Bean
//设置分页拦截器
public MybatisPlusInterceptor pageInterceptor(){
//定义MP拦截器
MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
//添加具体的拦截器
interceptor.addInnerInterceptor(new PaginationInnerInterceptor());
//乐观锁拦截器
interceptor.addInnerInterceptor(new OptimisticLockerInnerInterceptor());
return interceptor;
}
}
@Test
void contextLoads() {
User user = userDao.selectById(1);
user.setName("老大");
userDao.updateById(user);
}
SELECT id,name,age,tel,deleted,version FROM xfx_user WHERE id=?
UPDATE xfx_user SET name=?, age=?, tel=?, version=? WHERE id=? AND version=?