目录
- 一、乐观锁
- 二、查询构造器
- 三、分页查询
- 四、逻辑删除
- 五、Mybatis的应用
一、乐观锁
配置示例:
- 在数据库表中加入version字段,表示数据版本号
- 修改实体类,在使用类中加入对应的version字段,并使用是乐观锁
//乐观锁
@Version
private int version;
- 配置乐观锁
package com.xnx.mp280.config;
import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.OptimisticLockerInnerInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class MybatisPlusConfig {
@Bean
public MybatisPlusInterceptor mybatisPlusInterceptor() {
MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
// 乐观锁
interceptor.addInnerInterceptor(new OptimisticLockerInnerInterceptor());
// 分页
interceptor.addInnerInterceptor(new PaginationInnerInterceptor());
return interceptor;
}
}
- 测试
/测试乐观锁
@Test
public void testLock01() {
User user01 = userMapper.selectById(66L);
User user02 = userMapper.selectById(66L);
user01.setName("leguansuo03");
userMapper.updateById(user01);
user02.setName("leguansuo04");
userMapper.updateById(user02);
// QueryWrapper qu = new QueryWrapper();
// qu.eq("id",66L);
select * from user where id = 66L
// qu.ne("name","xnx");
select * from user where name <> 'xnx'
// String sql = " email = ewew@qq.com and version = 2";
// qu.apply(sql);
}
二、查询构造器
- 批量查询多个对象
@Test
public void testSelectList() {
//一次查询多个ID
List<User> users = userMapper.selectBatchIds(Arrays.asList(1L, 2L, 3L));
users.forEach(t -> {
System.out.println(t);
});
}
@Test
public void testSelectByMap() {
//使用map进行查询
Map<String, Object> map = new HashMap<>();
map.put("name", "Tom");
map.put("age", 28);
List<User> users = userMapper.selectByMap(map);
users.forEach(t -> System.out.println(t));
}
@Test
public void testWrapperLike(){
QueryWrapper<User> wrapper = new QueryWrapper<>();
wrapper.apply(" hadkjhwajdksahd");
List<User> users = userMapper.selectList(wrapper);
users.forEach(t-> System.out.println(t));
}
@Test
public void testUpdateWrapper(){
UpdateWrapper u = new UpdateWrapper();
// u.set("name","TT");
u.eq(true,"id",6L);
User user = userMapper.selectById(6L);
// user = new User();
user.setName("TTT");
userMapper.update(user,u);
}
三、分页查询
- 分页配置
package com.xnx.mp280.config;
import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.OptimisticLockerInnerInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class MybatisPlusConfig {
@Bean
public MybatisPlusInterceptor mybatisPlusInterceptor() {
MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
// 乐观锁
interceptor.addInnerInterceptor(new OptimisticLockerInnerInterceptor());
// 分页
interceptor.addInnerInterceptor(new PaginationInnerInterceptor());
return interceptor;
}
}
- 分页演示
@Test
public void testSelectPage() {
PageDTO<User> page = new PageDTO<>();
page.setCurrent(3);
page.setSize(3);
PageDTO<User> pageDTO = userMapper.selectPage(page, null);
System.out.println(pageDTO.getTotal());
List<User> records = pageDTO.getRecords();
records.forEach(t-> System.out.println(t));
}
四、逻辑删除
物理删除使用起来比较简单,仿照查询功能即可,不再赘述。
什么是逻辑删除?
即:标记删除,并不是真的从数据库中删除,而是做个删除标记,在查询时,过滤掉标记为删除的记录
即可。
- 数据库表结构调整
- 修改实体类
//标记该字段为逻辑删除字段
@TableLogic
private int deleted;
- 在application.properties (或application.yml)中加入如下配置,
#逻辑删除字段名
mybatis-plus.global-config.db-config.logic-delete-field=deleted
# 1表示逻辑删除
mybatis-plus.global-config.db-config.logic-delete-value=1
# 0 表示未删除
mybatis-plus.global-config.db-config.logic-not-delete-value=0
- 测试
@Test
public void testDeleteLogic() {
int i = userMapper.deleteById(66L);
System.out.println(i);
}
五、Mybatis的应用
mybatis-plus是mybatis的升级版,所以在mybatis-plus中使用xml的配置比较简单
- 在application.properties配置文件中加入如下配置
mybatis-plus.mapper-locations=classpath:/mapper/**/*.xml
- 定义mapper接口,继承BaseMapper接口,以便于使用mybatis-plus提供的方法
package com.xnx.mp280.mapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.xnx.mp280.model.User;
import org.apache.ibatis.annotations.Select;
import org.springframework.stereotype.Repository;
import java.util.List;
@Repository
public interface UserMapperXml extends BaseMapper<User> {
List<User> list();
@Select("select * from user u,goods g where u.id = 66L")
List<User> list2();
}
- 根据接口生成xml配置文件,IDE一般有辅助的插件,例如IDEA中的Free Mybatis plugin
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="com.xnx.mp280.mapper.UserMapperXml">
<select id="list" resultType="com.xnx.mp280.model.User">
select * from user;
</select>
</mapper>
也可以不使用xml配置文件,使用注解来定义sql语句
如UserMapperXml的list2()
4. 测试
package com.xnx.mp280;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.PageDTO;
import com.xnx.mp280.mapper.UserMapper;
import com.xnx.mp280.mapper.UserMapperXml;
import com.xnx.mp280.model.User;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@SpringBootTest
public class SampleTest2 {
@Autowired
private UserMapperXml userMapper;
@Test
public void test1(){
List<User> list = userMapper.list();
list.forEach(System.out::println);
}
}