MyBatis五、MyBatis的各种查询功能

MyBatis的各种查询功能

一、查询一个实体类对象

  1. 其结果可被接收的方式

    可通过实体类对象接收

    可通过List集合接收:即第二种情况

    可通过Map结构:即第四种情况

  2. Mapper接口
    public interface SelectMapper {
        User getUserById(@Param("id") Integer id);
    }
    
  3. Mapper映射文件
    <mapper namespace="com.atguigu.mybatis.mapper.SelectMapper">
        <select id="getUserById" resultType="user">
            select * from t_user where id = #{id}
        select>
    mapper>
    
  4. 测试
    @Test
    public void getUserByIdTest() {
        SqlSession sqlSession = SqlSessionUtils.getSqlSession();
        SelectMapper mapper = sqlSession.getMapper(SelectMapper.class);
        User userById = mapper.getUserById(1);
        // 输出结果:User{id=1, username='admin', pwd='123456', age=23, sex=男, email='[email protected]'}
        System.out.println(user);
    }
    

二、查询一个List集合

  1. 其结果可被接收的方式

    可通过List集合接收

    可通过Map结构

  2. Mapper接口
    public interface SelectMapper {
        List<User> getAllUser();
    }
    
  3. Mapper映射文件
    <mapper namespace="com.atguigu.mybatis.mapper.SelectMapper">
        <select id="getAllUser" resultType="user">
            select * from t_user 
        select>
    mapper>
    
  4. 测试
    @Test
        public void getAllUserTest() {
            SqlSession sqlSession = SqlSessionUtils.getSqlSession();
            SelectMapper mapper = sqlSession.getMapper(SelectMapper.class);
            List<User> allUser = mapper.getAllUser();
            allUser.forEach(user -> System.out.println(user));
        }
    

三、查询单个数据

  1. mybatis自带的typeAlias
    Type Integer String Map List Double Iterator
    Alias int/integer string map list double iterator
  2. Mapper接口
    public interface SelectMapper {
        int getCount();
    }
    
  3. Mapper映射文件

    注:一定要有resultType

    <select id="getCount" resultType="Integer">
        select count(*) from t_user
    select>
    
  4. 测试
    @Test
    public void getCountTest() {
        SqlSession sqlSession = SqlSessionUtils.getSqlSession();
        SelectMapper mapper = sqlSession.getMapper(SelectMapper.class);
        int count = mapper.getCount();
        System.out.println("count = " + count);
    }
    

四、查询一条数据为Map集合

  1. mapper接口
    public interface SelectMapper {
        Map<String,Object> getUserByIdToMap(@Param("id") Integer id);
    
    }
    
  2. mapper映射文件
    <select id="getUserByIdToMap" resultType="map">
        select * from t_user where id = #{id}
    select>
    
  3. 测试
    @Test
    public void getUserByIdToMapTest() {
        SqlSession sqlSession = SqlSessionUtils.getSqlSession();
        SelectMapper mapper = sqlSession.getMapper(SelectMapper.class);
        Map<String, Object> userByIdToMap = mapper.getUserByIdToMap(1);
        // 输出结果:{sex=男, id=1, pwd=123456, age=23, [email protected], username=admin}
        System.out.println(userByIdToMap);
    }
    

五、查询多条数据为Map集合

  1. @MapKey()

    用来设置Map集合的键值对

    会将查询出来的某条数据的某一个字段作为Map集合的键,

    会将查询出来的某条数据作为Map集合的值

  2. Mapper接口
    public interface SelectMapper {
        @MapKey("id")
        Map<String,Object> getAllToMap();
    }
    
  3. Mapper映射文件
    <select id="getAllToMap" resultType="map">
        select * from t_user
    select>
    
  4. 测试
    @Test
    public void getAllToMapTest() {
        SqlSession sqlSession = SqlSessionUtils.getSqlSession();
        SelectMapper mapper = sqlSession.getMapper(SelectMapper.class);
        Map<String, Object> allToMap = mapper.getAllToMap();
        /*	输出结果:
        	{1={sex=男, id=1, pwd=123456, age=23, [email protected], username=admin},
            2={sex=男, id=2, pwd=123456, age=23, [email protected], username=admin}, 
            3={sex=男, id=3, pwd=123456, age=23, [email protected], username=admin}, 
            4={sex=男, id=4, pwd=123456, age=23, [email protected], username=李四},
            10={sex=女, id=10, pwd=123456, age=23, [email protected], username=张三}}
    
        */
        System.out.println(allToMap);
    }
    

你可能感兴趣的:(MyBatis,java,mysql,MyBatis,SSM)