MyBatis: 查询密码为123的所有用户


本节示例代码在 mybatis-demo-004

数据准备

数据准备

user 表的默认数据如下:

mysql> use blog_db;
mysql> select * from user;
+----+--------+----------------+----------+
| id | name   | email          | password |
+----+--------+----------------+----------+
|  1 | letian | letian@111.com | 123      |
|  2 | xiaosi | xiaosi@111.com | 123      |
+----+--------+----------------+----------+
2 rows in set (0.00 sec)

项目结构

项目基于 查找id为1的用户信息

UserMapper 接口

package mapper;

import bean.User;

import java.util.List;

public interface UserMapper {

    /**
     * 根据id查询用户
     * @param id
     * @return
     */
    User findById(Long id);

    /**
     * 根据密码,查询其中一个用户
     * @param password
     * @return
     */
    User findOneUserByPassword(String password); // 必须保证最多返回一条数据,否则会报 TooManyResultsException 错误。无数据,则返回null

    /**
     * 根据密码查询所有用户
     * @param password
     * @return
     */
    List<User> findByPassword(String password);

}

UserMapper.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="mapper.UserMapper">

    <select id="findById" parameterType="Long" resultType="bean.User">
        select * from blog_db.user where id=#{id}
    </select>

    <select id="findOneUserByPassword" parameterType="String" resultType="bean.User">
        select
        * from blog_db.user where password=#{password} order by id asc limit 1
    </select>

    <select id="findByPassword" parameterType="String" resultType="bean.User">
        select
        * from blog_db.user where password=#{password}
    </select>

</mapper>

运行示例

示例1

修改 Main 类如下:

import java.io.IOException;
import java.util.Iterator;
import java.util.List;

import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import bean.User;
import mapper.UserMapper;
import org.junit.Test;


@Slf4j
public class Main {

    @Test
    public void test_01() throws IOException {
        SqlSession sqlSession = getSqlSession();
        UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
        User user = userMapper.findOneUserByPassword("1234");
        log.info("{}", user);
    }

    private SqlSession getSqlSession() throws IOException {
        SqlSessionFactory sessionFactory;
        sessionFactory = new SqlSessionFactoryBuilder()
                .build(Resources.getResourceAsReader("mybatis-config.xml"));
        return sessionFactory.openSession();
    }

}

执行 test_01 函数,运行结果:

 INFO [main] - null

因为没有密码为 1234 的用户,所以 findOneUserByPassword 返回的是 null。

示例2

在 Main 类中增加 findOneUserByPassword 的示例:

@Test
public void test_02() throws IOException {
    SqlSession sqlSession = getSqlSession();
    UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
    User user = userMapper.findOneUserByPassword("123");
    log.info("{}", user);
}

运行结果是:

 INFO [main] - User(id=1, name=letian, email=letian@111.com, password=123)

密码为 123 的用户有两个,是 findOneUserByPassword 函数选择id最小的返回。

示例3

在 Main 类中增加 findByPassword 的示例:

@Test
public void test_03() throws IOException {
    SqlSession sqlSession = getSqlSession();
    UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
    List<User> userList = userMapper.findByPassword("123");
    log.info("{}", userList);
}

运行结果是:

INFO [main] - [User(id=1, name=letian, email=letian@111.com, password=123), User(id=2, name=xiaosi, email=xiaosi@111.com, password=123)]

两个密码为123的用户都被取出来了。

示例4

在 Main 类中增加 findByPassword 的示例,查密码为 1234 的所有用户:

@Test
public void test_04() throws IOException {
    SqlSession sqlSession = getSqlSession();
    UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
    List<User> userList = userMapper.findByPassword("1234");
    log.info("{}", userList);
}

运行结果是:

 INFO [main] - []

可以看到,虽然没有数据,但不会返回 null, 而是返回空 List。



(本文完)


MyBatis 教程