MybatisPlus的查询select

MybatisPlus查询

1、根据id查询一条记录:selectById()
    @Test
    public void selectById(){
        tb_user user = userMapper.selectById(1);
        System.out.println(user);
    }
输出的日志
JDBC Connection [HikariProxyConnection@1108136194 wrapping com.mysql.cj.jdbc.ConnectionImpl@c7f4457] will not be managed by Spring
==>  Preparing: SELECT user_id,create_time,psw,name,update_time,version FROM tb_user WHERE user_id=? 
==> Parameters: 1(Integer)
<==    Columns: user_id, create_time, psw, name, update_time, version
<==        Row: 1, 2021-11-21 14:00:47, 123, 李四四, 2021-11-21 22:00:47, null
<==      Total: 1
Closing non transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@77c10a5f]
tb_user(userId=1, name=李四四, psw=123, createTime=Sun Nov 21 22:00:47 CST 2021, updateTime=Mon Nov 22 06:00:47 CST 2021, version=null)

这是就会想,如果想通过多个id值,查询一个List集合咋办呢?不慌MybatisPlus还是有方法

2、根据多个id查询: selectBatchIds(数组)
    @Test
    public void selectBatchById(){
        List<tb_user> user = userMapper.selectBatchIds(Arrays.asList(1, 2, 3, 4, 6));
        user.forEach(System.out::println);
    }
输出日志信息
==>  Preparing: SELECT user_id,create_time,psw,name,update_time,version FROM tb_user WHERE user_id IN ( ? , ? , ? , ? , ? ) 
==> Parameters: 1(Integer), 2(Integer), 3(Integer), 4(Integer), 6(Integer)
<==    Columns: user_id, create_time, psw, name, update_time, version
<==        Row: 1, 2021-11-21 14:00:47, 123, 李四四, 2021-11-21 22:00:47, null
<==        Row: 2, null, 123, 张三, null, null
<==        Row: 3, null, 123, 张三, null, null
<==        Row: 4, null, 123, 张三, null, null
<==        Row: 6, 2021-11-21 22:05:11, 123, 李四四, 2021-11-22 12:26:29, null
<==      Total: 5
Closing non transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@795f8317]
tb_user(userId=1, name=李四四, psw=123, createTime=Sun Nov 21 22:00:47 CST 2021, updateTime=Mon Nov 22 06:00:47 CST 2021, version=null)
tb_user(userId=2, name=张三, psw=123, createTime=null, updateTime=null, version=null)
tb_user(userId=3, name=张三, psw=123, createTime=null, updateTime=null, version=null)
tb_user(userId=4, name=张三, psw=123, createTime=null, updateTime=null, version=null)
3、Map条件查询 :selectByMap(map)
    @Test
    public void selectByMap(){
        Map<String,Object> map = new HashMap<>();
        map.put("name","李四");
        map.put("psw","123");
        List<tb_user> user = userMapper.selectByMap(map);
        user.forEach(System.out::println);
    }
输出日志信息
=>  Preparing: SELECT user_id,create_time,psw,name,update_time,version FROM tb_user WHERE psw = ? AND name = ? 
==> Parameters: 123(String), 李四(String)
<==      Total: 0
4、分页查询

在之前直接使用最传统:limit offset,number
后面使用分页插件:如PageHelper
在MybatisPlus非常简单,只需要两步
第一步:在配置文件中加入配置信息

@MapperScan("sz.kingdom.testmybatisplus.mapper")
@Configuration
public class MyConfiguration {
    // 注册乐观锁插件
    @Bean
    public OptimisticLockerInterceptor optimisticLockerInterceptor(){
        return new OptimisticLockerInterceptor();
    }
    @Bean
    public PaginationInterceptor paginationInterceptor(){
        PaginationInterceptor paginationInterceptor = new PaginationInterceptor();
        // 设置请求的页面大于最大页后操作, true调回到首页,false 继续请求  默认false
        // paginationInterceptor.setOverflow(false);
        // 设置最大单页限制数量,默认 500 条,-1 不受限制
        // paginationInterceptor.setLimit(500);
        // 开启 count 的 join 优化,只针对部分 left join
        paginationInterceptor.setCountSqlParser(new JsqlParserCountOptimize(true));
        return paginationInterceptor;
    }
}

第二步

    @Test
    public void TestLimit(){
        Page<tb_user> page = new Page<>(1,10);
        userMapper.selectPage(page,null);
        List<tb_user> userLists = page.getRecords();

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

    }

new Page(1,5):一般需要两个参数,第一个参数:第几页,第二个参数:每页多少条记录

输出日志

JDBC Connection [HikariProxyConnection@1510312933 wrapping com.mysql.cj.jdbc.ConnectionImpl@4eaf7902] will not be managed by Spring
==>  Preparing: SELECT COUNT(1) FROM tb_user 
==> Parameters: 
<==    Columns: COUNT(1)
<==        Row: 17
==>  Preparing: SELECT user_id,create_time,psw,name,update_time,version FROM tb_user LIMIT ?,? 
==> Parameters: 0(Long), 10(Long)
<==    Columns: user_id, create_time, psw, name, update_time, version
<==        Row: 1, 2021-11-21 14:00:47, 123, 李四四, 2021-11-21 22:00:47, null
<==        Row: 2, null, 123, 张三, null, null
<==        Row: 3, null, 123, 张三, null, null
<==        Row: 4, null, 123, 张三, null, null
<==        Row: 5, null, 123, 张三, null, null
<==        Row: 6, 2021-11-21 22:05:11, 123, 李四四, 2021-11-22 12:26:29, null
<==        Row: 7, null, yps0401, sxf, 2021-11-22 12:48:46, 9
<==        Row: 1462266553855746050, null, 123, 张三1, null, null
<==        Row: 1462270263956729858, null, 123, 张三1, null, null
<==        Row: 1462270263956729859, null, 123, 张三1, null, null
<==      Total: 10
Closing non transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@ae73c80]
tb_user(userId=1, name=李四四, psw=123, createTime=Sun Nov 21 22:00:47 CST 2021, updateTime=Mon Nov 22 06:00:47 CST 2021, version=null)
tb_user(userId=2, name=张三, psw=123, createTime=null, updateTime=null, version=null)
tb_user(userId=3, name=张三, psw=123, createTime=null, updateTime=null, version=null)
tb_user(userId=4, name=张三, psw=123, createTime=null, updateTime=null, version=null)
tb_user(userId=5, name=张三, psw=123, createTime=null, updateTime=null, version=null)
tb_user(userId=6, name=李四四, psw=123, createTime=Mon Nov 22 06:05:11 CST 2021, updateTime=Mon Nov 22 20:26:29 CST 2021, version=null)
tb_user(userId=7, name=sxf, psw=yps0401, createTime=null, updateTime=Mon Nov 22 20:48:46 CST 2021, version=9)
tb_user(userId=1462266553855746050, name=张三1, psw=123, createTime=null, updateTime=null, version=null)
tb_user(userId=1462270263956729858, name=张三1, psw=123, createTime=null, updateTime=null, version=null)
tb_user(userId=1462270263956729859, name=张三1, psw=123, createTime=null, updateTime=null, version=null)

你可能感兴趣的:(Mybatis-Plus,java,mybatis)