mybatis-plus高级操作

lambda条件构造器 使用

 

  /**
* lambda 条件构造器
* 生成的sql语句 SELECT id,name,age,email,manager_id,create_time FROM user WHERE name LIKE ? AND age < ?
*/
@Test
public void selectLambda() {
//创建lambda 条件构造器 的三种方法
// LambdaQueryWrapper<User> lambda2 = new LambdaQueryWrapper<>();
// LambdaQueryWrapper<User> lambda = new QueryWrapper<User>().lambda();
LambdaQueryWrapper<User> lambda3 = Wrappers.<User>lambdaQuery();
lambda3.like(User::getName, "雨").lt(User::getAge, 40);
List<User> users = userMapper.selectList(lambda3);
users.forEach(System.out::println);


}


/**
* 名字为王姓并且(年龄小于40并且邮箱不为空)
* name like '王%' and (age <40 or email in not null)
* 生成的sql语句 SELECT id,name,age,email,manager_id,create_time FROM user WHERE name LIKE ? AND age < ?
*/
@Test
public void selectLambda2() {
LambdaQueryWrapper<User> lambda3 = Wrappers.<User>lambdaQuery();
lambda3.likeRight(User::getName, "王").and(qw -> qw.lt(User::getAge, 40).or().isNotNull(User::getEmail));

List<User> users = userMapper.selectList(lambda3);
users.forEach(System.out::println);


}


/**
* 自定义sql 使用
* Dao层 代码:
*
* @Select("select * from ${ew.customSqlSegment}")
* List<User> selectAll(@Param(Constants.WRAPPER)Wrapper<User> wrapper);
*/
@Test
public void selectLambda4my() {
LambdaQueryWrapper<User> lambda3 = Wrappers.<User>lambdaQuery();
lambda3.likeRight(User::getName, "王").and(qw -> qw.lt(User::getAge, 40).or().isNotNull(User::getEmail));

List<User> users = userMapper.selectAll(lambda3);
users.forEach(System.out::println);


}
xml方式自定义sql 这里只是个单表演示
application.yml 添加
mybatis-plus:
mapper-locations:
- com/mp/mapper/*
UserMapper 接口 添加方法

List<User> selectAll(@Param(Constants.WRAPPER) Wrapper<User> wrapper);

IPage<User> selectUserPage(Page<User> page,@Param(Constants.WRAPPER)Wrapper<User> wrapper);


UserMpper.xml
<?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 user ${ew.customSqlSegment}
</select>

<select id="selectUserPage" resultType="com.mp.entity.User">
select * from user ${ew.customSqlSegment}
</select>
</mapper>



/**
* lambda 条件构造器第四种创建方式 MP 3.0.7 新增的方式
* 生成的sql语句 SELECT id,name,age,email,manager_id,create_time FROM user WHERE name LIKE ? AND age >= ?
* 这个例子可以看出 代码更简洁了
* 生成的sql语句 SELECT id,name,age,email,manager_id,create_time FROM user WHERE name LIKE ? AND age >= ?
*/
@Test
public void selectLambda3() {
List<User> users = new LambdaQueryChainWrapper<User>(userMapper)
.like(User::getName, "雨").ge(User::getAge, 20).list();

users.forEach(System.out::println);
}
MP的分页操作
baseMapper接口提供两种分页方法来实现物理分页(注:导包时导MP的包)
第一个返回实体对象 允许null 第二个人返回map 对象多用于在指定放回字段时使用,避免为指定字段null值出现
 IPage<T> selectPage(IPage<T> page, @Param("ew") Wrapper<T> queryWrapper);

IPage<Map<String, Object>> selectMapsPage(IPage<T> page, @Param("ew") Wrapper<T> queryWrapper);


使用MP的分页需要配置 类

package com.mp.configuration;

import com.baomidou.mybatisplus.extension.plugins.PaginationInterceptor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class MybatisPlusConfig {


@Bean
public PaginationInterceptor paginationInterceptor() {
return new PaginationInterceptor();
}
}

/**
* 分页MP 分页插件
*/
@Test
public void selectPage() {
QueryWrapper<User> queryWrapper = new QueryWrapper<User>();
queryWrapper.ge("age", 26);
//后面参数false 表示不查询总记录数
Page<User> userPage = new Page<>(1, 2, false);
/* IPage<User> iPage = userMapper.selectPage(userPage, queryWrapper);
System.out.println("总页数"+iPage.getPages());
System.out.println("总记录数"+iPage.getTotal());
List<User> records = iPage.getRecords();
records.forEach(System.out::println);
*/
IPage<Map<String, Object>> iPage = userMapper.selectMapsPage(userPage, queryWrapper);
System.out.println("总页数" + iPage.getPages());
System.out.println("总记录数" + iPage.getTotal());
List<Map<String, Object>> records = iPage.getRecords();

records.forEach(System.out::println);

}
自定义分页
UserMapper接口添加 xml方式和注解方式sql是相同的 上面有示例
IPage<User> selectUserPage(Page<User> page,@Param(Constants.WRAPPER)Wrapper<User> wrapper);

UserMpper.xml 添加
<select id="selectUserPage" resultType="com.mp.entity.User">
select * from user ${ew.customSqlSegment}
</select>

/**
* 自定义分页方法
*/
@Test
public void selectMyPage() {
QueryWrapper<User> queryWrapper = new QueryWrapper<User>();
queryWrapper.ge("age", 26);
//后面参数false 表示不查询总记录数
Page<User> page = new Page<>(1, 2);

IPage<User> iPage = userMapper.selectUserPage(page, queryWrapper);
System.out.println("总页数" + iPage.getPages());
System.out.println("总记录数" + iPage.getTotal());
List<User> records = iPage.getRecords();
records.forEach(System.out::println);
}
















posted @ 2019-07-12 12:44  爱玛999  阅读(8551)  评论(0编辑  收藏  举报