6、Mybatis之高级查询
阅读原文时间:2023年08月21日阅读:4

++++++++++++++++++++++++++分割线++++++++++++++++++++++++++

注意namespace属性值为对应接口的全限定类名

<?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="org.rain.mybatis.mapper.SelectMapper">

</mapper>

++++++++++++++++++++++++++分割线++++++++++++++++++++++++++

6.2.1、接口方法

User getUserById(@Param("id") Integer id);

6.2.2、映射文件

    <!--User getUserById(@Param("id") Integer id);-->
    <select id="getUserById" resultType="User">
        select * from t_user where id = #{id}
    </select>

6.2.3、测试方法

    @Test
    public void testGetUserById(){
        SqlSession sqlSession = SqlSessionUtils.getSqlSession();
        SelectMapper selectMapper = sqlSession.getMapper(SelectMapper.class);
        User user = selectMapper.getUserById(1);
        System.out.println(user);
        sqlSession.close();
    }

6.2.4、执行效果

6.3.1、接口方法

List<User> getAllUser();

6.3.2、映射文件

    <!--List<User> getAllUser();-->
    <select id="getAllUser" resultType="User">
        select * from t_user
    </select>

6.3.3、测试方法

    @Test
    public void testGetAllUser(){
        SqlSession sqlSession = SqlSessionUtils.getSqlSession();
        SelectMapper selectMapper = sqlSession.getMapper(SelectMapper.class);
        List<User> users = selectMapper.getAllUser();
        for (User user : users) {
            System.out.println(user);
        }
    }

6.3.4、执行效果

6.4.1、接口方法

Integer getCount();

6.4.2、映射文件

注意:resultType属性的值除了可以是java.lang.Integer之外,还可以是Integer、integer、int或INT等;

这是因为mybatis对Java中常用的类型都设置了类型别名。

    <!--Integer getCount();-->
    <select id="getCount" resultType="java.lang.Integer">
        select count(*) from t_user
    </select>

6.4.3、测试方法

    @Test
    public void testGetCount(){
        SqlSession sqlSession = SqlSessionUtils.getSqlSession();
        SelectMapper selectMapper = sqlSession.getMapper(SelectMapper.class);
        Integer count = selectMapper.getCount();
        System.out.println(count);
        sqlSession.close();
    }

6.4.4、执行效果

6.5.1、接口方法

Map<String,Object> getUserByIdToMap(@Param("id") Integer id);

6.5.2、映射文件

    <!--Map<String,Object> getUserByIdToMap(@Param("id") Integer id);-->
    <select id="getUserByIdToMap" resultType="map">
        select * from t_user where id = #{id}
    </select>

6.5.3、测试方法

    @Test
    public void testGetUserByIdToMap(){
        SqlSession sqlSession = SqlSessionUtils.getSqlSession();
        SelectMapper selectMapper = sqlSession.getMapper(SelectMapper.class);
        Map<String, Object> userByIdToMap = selectMapper.getUserByIdToMap(1);
        System.out.println(userByIdToMap);
        sqlSession.close();
    }

6.5.4、执行效果

注意:当某个字段值为null时,该字段不会放进map集合中

6.6.1、接口方法

List<Map<String,Object>> getAllUserToMap();

6.6.2、映射文件

    <!--List<Map<String,Object>> getAllUserToMap();-->
    <select id="getAllUserToMap" resultType="map">
        select * from t_user
    </select>

6.6.3、测试方法

    @Test
    public void testGetAllUserToMap(){
        SqlSession sqlSession = SqlSessionUtils.getSqlSession();
        SelectMapper selectMapper = sqlSession.getMapper(SelectMapper.class);
        List<Map<String, Object>> allUserToMap = selectMapper.getAllUserToMap();
        for (Map<String, Object> map : allUserToMap) {
            System.out.println(map);
        }
        sqlSession.close();
    }

6.6.4、执行效果

6.6.5、嵌套map方式存储

6.6.5.1、接口方法

可以通过@MapKey注解设置大map集合的键为数据中的某个字段值(该字段值不能存在重复),值是每条数据所对应的小map集合

    @MapKey("ID")
    Map<String,Object> getAllUserToMap();

6.6.5.2、测试方法

    @Test
    public void testGetAllUserToMap(){
        SqlSession sqlSession = SqlSessionUtils.getSqlSession();
        SelectMapper selectMapper = sqlSession.getMapper(SelectMapper.class);
        Map<String, Object> allUserToMap = selectMapper.getAllUserToMap();
        System.out.println(allUserToMap);
        sqlSession.close();
    }

6.6.5.3、执行效果

手机扫一扫

移动阅读更方便

阿里云服务器
腾讯云服务器
七牛云服务器