自定义mapper文件是为了应对一些更复杂的查询场景,首先先配置映射路径
mybatis-plus:
mapper-locations: com/mp/dao/xml/*
由于这里的mapper文件放在java文件夹中,所以要在pom.xml里进行配置
<build>
<resources>
<resource>
<!-- xml放在java目录下-->
<directory>src/main/java</directory>
<includes>
<include>**/*.xml</include>
</includes>
</resource>
<!--指定资源的位置(xml放在resources下,可以不用指定)-->
<resource>
<directory>src/main/java</directory>
</resource>
</resources>
</build>
mapper
public interface UserMapper extends BaseMapper<User> {
List<User> selectAll(@Param(Constants.WRAPPER) Wrapper<User> wrapper);
}
<?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.mp.dao.UserMapper">
<select id="selectAll" resultType="com.mp.entity.User">
select * FROM `mp_user` ${ew.customSqlSegment}
</select>
</mapper>
测试
@Test
public void selectMy() {
LambdaQueryWrapper<User> lambda= Wrappers.<User>lambdaQuery();
lambda.likeRight(User::getName,"刘")
.and(lqw->lqw.lt(User::getAge,30)
.or().isNotNull(User::getEmail));
List<User> userList = userMapper.selectAll(lambda);
userList.forEach(System.out::println);
}
在MyBatis Plus中,物理分页和内存分页是两种不同的分页方式。
**物理分页【推荐】是在数据库层面进行分页操作,通过SQL语句的LIMIT
和OFFSET
**关键字来实现。这种分页方式直接在数据库中进行数据的截取和返回,因此适用于大数据量的分页查询。
**内存分页【了解】**是在应用程序内存中进行分页操作,即先将所有数据查询出来,然后在内存中进行分页处理。这种分页方式适用于数据量较小的情况,因为需要将所有数据加载到内存中,可能会导致内存占用过高。
配置插件
@Configuration
public class MybatisPlusConfig {
@Bean
public PaginationInterceptor paginationInterceptor() {
return new PaginationInterceptor();
}
}
分页实现
@Test
public void selectPage() {
QueryWrapper<User> queryWrapper = new QueryWrapper<>();
queryWrapper.ge("age", 30);
Page<User> page = new Page<User>(1, 2,false);// fasle指的是不查询总记录数,提高性能
// IPage<User> iPage = userMapper.selectPage(page, queryWrapper);
// List<User> userList = iPage.getRecords();
// selectMapsPage
IPage<Map<String, Object>> iPage = userMapper.selectMapsPage(page, queryWrapper);
System.out.println("总页数 = " + iPage.getPages());
System.out.println("总记录数 = " + iPage.getTotal());
List<Map<String, Object>> userList = iPage.getRecords();
userList.forEach(System.out::println);
}
自定义分页
UserMapper
IPage<User> selectUserPage(Page<User> page, @Param(Constants.WRAPPER) Wrapper<User> wrapper);
}
UserMapper.xml
<select id="selectUserPage" resultType="com.mp.entity.User">
select * from mp_user ${ew.customSqlSegment}
</select>
测试
@Test
public void selectMyPage() {
QueryWrapper<User> queryWrapper = new QueryWrapper<>();
queryWrapper.ge("age", 30);
Page<User> page = new Page<User>(1, 2);
IPage<User> iPage = userMapper.selectUserPage(page, queryWrapper);
System.out.println("总页数 = " + iPage.getPages());
System.out.println("总记录数 = " + iPage.getTotal());
List<User> userList = iPage.getRecords();
userList.forEach(System.out::println);
}
@Test
public void updateById() {
User user = new User();
user.setUserId(3L);
user.setAge(28);
user.setEmail("xiao@163.com");
// 传入实体对象作为参数,自动根据id字段定位对应的记录,根据其他set的值进行更新
int rows = userMapper.updateById(user);
System.out.println("rows = " + rows);
}
/**
* 条件构造器更新
*/
@Test
public void updateByWrapper() {
UpdateWrapper<User> updateWrapper = new UpdateWrapper<>();
updateWrapper.eq("name", "小刚").eq("age", 18); // where条件
// set更新条件
User user = new User();
user.setAge(28);
user.setEmail("xiao@163.com");
// 传入实体对象作为参数,自动根据id字段定位对应的记录,根据其他set的值进行更新
int rows = userMapper.update(user, updateWrapper);
System.out.println("rows = " + rows);
}
/**
* 快速更新少量字段
*/
@Test
public void updateByWrapper2() {
UpdateWrapper<User> updateWrapper = new UpdateWrapper<>();
updateWrapper.eq("name", "李刚").eq("age", 29).set("age", 30);
int rows = userMapper.update(null,updateWrapper);
System.out.println("rows = " + rows);
}
/**
* lambda表达式
*/
@Test
public void updateByWrapper3() {
LambdaUpdateWrapper<User> lambdaUpdate = Wrappers.lambdaUpdate();
lambdaUpdate.eq(User::getName, "小刚").eq(User::getAge, 28).set(User::getAge, 31);
int rows = userMapper.update(null,lambdaUpdate);
System.out.println("rows = " + rows);
}
/**
* lambda表达式链式调用
*/
@Test
public void updateByWrapper4() {
boolean update = new LambdaUpdateChainWrapper<User>(userMapper)
.eq(User::getName, "小刚").eq(User::getAge, 31).set(User::getAge,28).update();
System.out.println("update = " + update);
}
@Test
public void deleteById() {
int rows = userMapper.deleteById(1739970502337392641L);
System.out.println("rows = " + rows);
}
@Test
public void deleteByMap() {
Map<String,Object> map = new HashMap<>();
map.put("name", "小刚");
map.put("age", "25");
// DELETE FROM mp_user WHERE name = ? AND age = ?
int rows = userMapper.deleteByMap(map);
System.out.println("rows = " + rows);
}
@Test
public void deleteBatchIds() {
int rows = userMapper.deleteBatchIds(Arrays.asList(121111123010L, 126372731123L, 127381787813L));
// 相当于 where id in(?,?,?)
System.out.println("rows = " + rows);
}
@Test
public void deleteByWrapper() {
LambdaQueryWrapper<User> lambdaQuery = Wrappers.lambdaQuery();
lambdaQuery.eq(User::getAge, 28)
.or().gt(User::getAge, 41);
int rows = userMapper.delete(lambdaQuery);
System.out.println("rows = " + rows);
}