MybatisPlus简单查询

MybatisPlus简单查询

基于这几篇博客修改

[整合MybatisPlus测试]

[MybatisPlus自动填充时间]

[MybatisPlus乐观锁]

创建测试类

package com.xiang;

import com.xiang.mapper.UserMapper;
import com.xiang.pojo.User;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * Created by IntelliJ IDEA.
 * User: xiang
 * Date: 2021/10/23 1:00
 */
@SpringBootTest
public class SimpleQuery {
    @Autowired
    UserMapper userMapper;

    /**
     * MybatisPlus 实现简单查询
     */

    @Test
    //批量查询
    void BatchQuery() {
        ArrayList<Integer> list = new ArrayList<>();
        list.add(603);
        list.add(604);
        List<User> userList = userMapper.selectBatchIds(list);
        for (User user : userList) {
            System.out.println(user);
        }
    }


    @Test
        //条件查询 ,这儿是条件与查询
    void ConditionQuery() {
        HashMap<String, Object> map = new HashMap<>();
        map.put("username","json");
        map.put("age",18);
        List<User> list = userMapper.selectByMap(map);
        for (User user : list) {
            System.out.println(user);
        }
    }
}

批量查询

JDBC Connection [HikariProxyConnection@1130656047 wrapping com.mysql.cj.jdbc.ConnectionImpl@1b7a52dd] will not be managed by Spring
==>  Preparing: SELECT id,username,sex,age,birthday,create_time,update_time,version FROM user WHERE id IN ( ? , ? )
==> Parameters: 603(Integer), 604(Integer)
<==    Columns: id, username, sex, age, birthday, create_time, update_time, version
<==        Row: 603, testbox, 女, 18, 2021-10-02, null, null, null
<==        Row: 604, 小一, 男, 18, 2021-10-16, null, null, null
<==      Total: 2
Closing non transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@7d3c09ec]
User(id=603, username=testbox, sex=女, age=18, birthday=Sat Oct 02 00:00:00 CST 2021, createTime=null, updateTime=null, version=0)
User(id=604, username=小一, sex=男, age=18, birthday=Sat Oct 16 00:00:00 CST 2021, createTime=null, updateTime=null, version=0)

条件查询

JDBC Connection [HikariProxyConnection@1204030294 wrapping com.mysql.cj.jdbc.ConnectionImpl@4b039c6d] will not be managed by Spring
==>  Preparing: SELECT id,username,sex,age,birthday,create_time,update_time,version FROM user WHERE age = ? AND username = ?
==> Parameters: 18(Integer), json(String)
<==    Columns: id, username, sex, age, birthday, create_time, update_time, version
<==        Row: 1451589057287688194, json, 男, 18, 2021-10-23, 2021-10-23 00:39:35, 2021-10-23 00:40:19, 2
<==      Total: 1
Closing non transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@1d805aa1]
User(id=1451589057287688194, username=json, sex=男, age=18, birthday=Sat Oct 23 00:00:00 CST 2021, createTime=Sat Oct 23 00:39:35 CST 2021, updateTime=Sat Oct 23 00:40:19 CST 2021, version=2)
2021-10-23 01:10:43.553  INFO 26180 --- [ionShutdownHook] com.zaxxer.hikari.HikariDataSource       : HikariPool-1 - Shutdown initiated...
2021-10-23 01:10:43.562  INFO 26180 --- [ionShutdownHook] com.zaxxer.hikari.HikariDataSource       : HikariPool-1 - Shutdown completed.

posted @ 2021-10-23 01:13  阿向向  阅读(31)  评论(0编辑  收藏  举报