public interface PeopleMapper extends BaseMapper<People> {
}
@Configuration
@MapperScan("com.gis507.mpdemp.mapper")
public class MpConfigure {}
其中@Configuration是声明为配置类,@MapperScan是声明扫描包
在Test中,自动注入PeopleMapper,用于操作表people
class Test {
@Autowired
private PeopleMapper peopleMapper;
@Test
void testPeopleInsert() {
People people = new People();
people.setId(1L);
people.setName("Leon");
people.setAge(18);
people.setEmail("leon@163.com");
peopleMapper.insert(people);
}
}
class Test {
@Test
void testPeopleTest() {
peopleMapper.selectList(null);
}
}
class Test {
@Test
void testPeopleTest() {
peopleMapper.selectById(1L);
}
}
import java.util.Arrays;
class Test {
@Test
void testPeopleTest() {
peopleMapper.selectBatchIds(Arrays.asList(1,2,3));
}
}
QueryWrapper对象
class Test{
@Test
void testSelectQuery(){
// create queryWrapper object
QueryWrapper<People> oqw = new QueryWrapper<>();
// use querwarapper set args
// ge, gt, le, lt
// select age >= 30
// oqw.ge("age", 30);
// eq, ne
// oqw.eq("age", 40);
// oqw.ne("age", 40);
// between, notBetween
// oqw.between("age", 30,80);
// allEq
// like, not like
// oqw.like("name", "ci");
// orderBy
// last
// oqw.last("limit 1");
// select confirm column
oqw.select("id", "name");
List<People> people = peopleMapper.selectList(oqw);
System.out.println(people);
}
}
class Test{
void updateTest(){
People people = new People();
people.setId(6L);
people.setAge(30);
int row = userMapper.updateById(people);
System.out.println(row);
}
}
在表中增加两个字段:create_time和update_time,在插入数据时候,可以自动的补充上create_time和update_time数据;
在people.setCreateTime(new Date())这种方法肯定是可以实现的,但是希望通过MyBatis提供的方式自动实现
import java.util.Date;
class People {
// xxxxx
private Date createTime;
private Date updateTime;
}
class People{
@TableField(fill = FieldFill.INSERT)
private Date createTime;
@TableField(fill = FieldFill.INSERT_UPDATE)
private Date updateTime;
}
@Component
public class MyMetaObjectHandle implements MetaObjectHandler {
// use mp to insert, will use this function
@Override
public void insertFill(MetaObject metaObject) {
this.setFieldValByName("createTime", new Date(), metaObject);
this.setFieldValByName("updateTime", new Date(), metaObject);
}
// use mp to update, will run this function
@Override
public void updateFill(MetaObject metaObject) {
this.setFieldValByName("updateTime", new Date(), metaObject);
}
}
当一条数据被更改后,需要把版本号放在字段内,用作标记
MyBatisPlus已经提供了Version的功能
class People{
@Version
@TableField(fill = FieldFill.INSERT)
private Integer version;
}
@Configuration
public class MpConfigure {
// 乐观锁插件
@Bean
public OptimisticLockerInterceptor optimisticLockerInterceptor() {
return new OptimisticLockerInterceptor();
}
}
@Component
public class MyMetaObjectHandle implements MetaObjectHandler {
// use mp to insert, will use this function
@Override
public void insertFill(MetaObject metaObject) {
this.setFieldValByName("createTime", new Date(), metaObject);
this.setFieldValByName("updateTime", new Date(), metaObject);
// 默认从1开始
this.setFieldValByName("version", 1, metaObject);
}
}
上述这些配置完成后,在增加数据时候就会自动加入一个version,并且每次修改这条数据后,就会version+1
注意:要先查询,再改
class Test{
@Test
void testPeopleOpLock(){
// 先查询
People people = peopleMapper.selectById(3L);
people.setName("faker");
// 再改
peopleMapper.updateById(people);
}
}
当一条数据删除的时候,物理删除的话直接delete就可以,但是当我们想要保存数据,但是还不显示的时候,就可以使用逻辑删除的方法
class People{
@TableLogic
@TableField(fill = FieldFill.INSERT)
private Integer deleted;
}
@Configuration
public class MpConfigure {
// logic delete
@Bean
public ISqlInjector sqlInjector() {
return new LogicSqlInjector();
}
}
测试:
class Test{
@Test
void testPeopleDelete(){
peopleMapper.deleteById(1L);
}
}
执行的结果语句:
-- Time:39 ms - ID:com.gis507.mpdemp.mapper.PeopleMapper.deleteById
-- Execute SQL:
UPDATE
people
SET
deleted=1
WHERE
id=1
AND deleted=0