`MyBatis`各种查询功能

MyBatis各种查询功能【非常重要】

1.查询一个实体类对象

若查询出的数据只有一条:

  1. 可以通过实体类对象接收
  2. 可以通过List集合接收
  3. 可以通过Map集合接收,在selectresultTyperesultMap要传入别名为map【用于json数据传输】

案例:

Mapper接口

/**
     *当查询的返回只有一条记录时,
     * 1.可以用JavaBean类进行接收
     * 2.可以用List集合进行接收
     */
    // 根据ID查询用户信息 , 用User实体类进行接收
    User getUserByID(@Param("id") Integer id);
    // 根据ID查询用户信息 , 用List集合进行接收
    List<User> getUserByID2(@Param("id") Integer id);

Mapper.xml文件

 <!--getUserByID根据ID查询用户信息-->
    <select id="getUserByID" resultType="User">
        select * from t_user where id = #{id};
    </select>
    <!--List<User> getUserByID2(@Param("id") Integer id);根据ID查询用户信息-->
    <select id="getUserByID2" resultType="User">
        select * from t_user where id = #{id};
    </select>

Test

// 查询返回一条记录的情况
    @Test
    public void testGetUserByID(){
        SqlSession sqlSession = SqlSessionUtils.getSqlSession();
        SelectMapper mapper = sqlSession.getMapper(SelectMapper.class);
        //查询返回一条记录用JavaBean类进行接收
//        User user = mapper.getUserByID(1);
//        System.out.println(user);
        //查询返回一条记录用List集合进行接收
        List<User> list = mapper.getUserByID2(1);
        list.forEach(user -> System.out.println(user));
    }

2.查询一个List集合【非常重要】

若查询出的数据有多条:

  1. 可以通过实体类类型的List集合接收【就是泛型是JavaBean
  2. 可以通过Map类型的List集合接收【就是泛型是Map集合】在selectresultTyperesultMap要传入别名为map【用于json数据传输】
  3. 可以在Mapper接口的方法添加上@MapKey注解,此时就可以将每条数据转换的Map集合作为值,以某个字段的值作为键,放在同一个Map集合中

注意是:一定不能通过实体类对象接收,此时会抛出异常TooManyResultException

Mapper接口

/**
     * 当查询返回是多条记录时,
     * 1.可以用List集合进行接收
     *
     * 注意是:一定不能使用JavaBean接收,否则会报:TooManyResultsException
     */
    List<User> getAllUser();

Mapper.xml文件

<!--getAllUser查询所有用户信息-->
    <select id="getAllUser" resultType="User">
        select * from t_user;
    </select>

test

// 查询所有用户信息
    @Test
    public void testAllUser(){
        SqlSession sqlSession = SqlSessionUtils.getSqlSession();
        SelectMapper mapper = sqlSession.getMapper(SelectMapper.class);
        // 查询用户信息
        List<User> allUser = mapper.getAllUser();
        allUser.forEach(user -> System.out.println(user));
    }

3.查询单个数据【就是返回单行单列】

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-jf66abMN-1651639781714)(D:\typora笔记\mybatis\img\1651639385732.png)]

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-1IIPKlgM-1651639781716)(D:\typora笔记\mybatis\img\1651639398448.png)]

/**
* 查询用户的总记录数
* @return
* 在MyBatis中,对于Java中常用的类型都设置了类型别名
* 例如:java.lang.Integer-->int|integer
* 例如:int-->_int|_integer
* 例如:String --> string
* 例如:Map-->map,List-->list
*/
int getCount();

案例:查询所以记录条数

Mapper接口

// 查询单个数据【就是返回单行单列】
    Integer getCount();
    // 例如查询 id为1的email
    String getEmailByID(@Param("id") Integer id);

Mapper.xml文件

<!--Integer getCount();查询所有记录数-->
    <select id="getCount" resultType="java.lang.Integer">
        select count(*) from t_user;
    </select>

<!--String getEmailByID(Integer id);根据ID查询Email-->
    <select id="getEmailByID" resultType="java.lang.String">
        select email from t_user where id = #{id};
    </select>

test

// 查询所有记录数
    @Test
    public void testGetCount(){
        SqlSession sqlSession = SqlSessionUtils.getSqlSession();
        SelectMapper mapper = sqlSession.getMapper(SelectMapper.class);
        // 查询所有记录条数
        Integer count = mapper.getCount();
        System.out.println(count);
        // 根据ID查询 email
        String email = mapper.getEmailByID(1);
        System.out.println(email);
    }

4.查询一条数据为Map集合【非常重要在JSON中使用】

本质是就是将查询到结果的属性作为Map集合的键查询到的属性值作为Map集合的值

Mapper接口

/**
     * 查询返回一条记录为Map集合
     * 查询到的属性作为Map集合的键
     * 查询到的值作为Map集合的值
     */
    Map<String,Object> getUserByIdToMap(@Param("id")Integer id);

Mapper.xml文件

<!--
    Map<String,Object> getUserByIdToMap(@Param("id")Integer id);
    查询到的结果为一条记录作为Map集合,查询到属性为Map键,查询到值为Map值
   -->
    <select id="getUserByIdToMap" resultType="map">
        select * from t_user where id = #{id};
    </select>

resultType="map"说明查询到数据封装Map集合

test

// 查询到为一条记录封装为Map集合
    @Test
    public void testGetUserByIdToMap(){
        SqlSession sqlSession = SqlSessionUtils.getSqlSession();
        SelectMapper mapper = sqlSession.getMapper(SelectMapper.class);
        Map<String, Object> toMap = mapper.getUserByIdToMap(1);
        System.out.println(toMap);
    }

5.查询多条数据为Map集合【非常重要】

方式一:查询到的多条放在List集合中,泛型为Map集合

例如:

/**
* 查询所有用户信息为map集合
* @return
* 将表中的数据以map集合的方式查询,一条数据对应一个map;若有多条数据,就会产生多个map集合,此
时可以将这些map放在一个list集合中获取
*/
List<Map<String, Object>> getAllUserToMap();


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

案例

Mapper接口

/**
     * 查询返回多条记录
     * 情况一:查询到的记录每条记录都封装成一个Map集合,再将每一个Map封装List集合
     */
    //注意是:由于查询返回是多条记录不能使用一个Map集合接收,否则报错为:TooManyResultsException
    // 如:Map<String,Object> getAllUserToMap();错误,返回多条记录只用一个Map接收
    List<Map<String,Object>> getAllUserToMap();

mapper.xml文件

<!--
        List<Map<String,Object>> getAllUserToMap();
        查询到多条记录,每条都封装成Map集合,再放在List集合中
    -->
    <select id="getAllUserToMap" resultType="Map">
        select * from t_user;
    </select>

test

// 情况一:每条记录封装为Map集合,再多条记录封装为List集合泛型为Map
    @Test
    public void testGetAllUserToMap(){
        SqlSession sqlSession = SqlSessionUtils.getSqlSession();
        SelectMapper mapper = sqlSession.getMapper(SelectMapper.class);
        List<Map<String, Object>> mapList = mapper.getAllUserToMap();
        for (Map<String , Object> map:
             mapList) {
            System.out.println(map);
            /**
             * {password=123, sex=女, id=1, age=23, email=168@qq.com, username=明天}
             * {password=123, sex=男, id=2, age=22, email=123@qq.com, username=海康}
             * {password=123, sex=男, id=3, age=22, email=123@qq.com, username=湛江}
             * {password=123, sex=男, id=4, age=22, email=123@qq.com, username=西安}
             */
        }
    }
方式二:查询到的多条记录作为Map值,并且使用@MapKey注解设置Map集合的键【注意:@Mapkey的值必须是唯一】

注意是:@MapKey注解中的值必须是唯一,如果不是唯一可以将Map集合中的其他值覆盖掉,所以必须是唯一【一般选择表的ID

例如:

/**
* 查询所有用户信息为map集合
* @return
* 将表中的数据以map集合的方式查询,一条数据对应一个map;若有多条数据,就会产生多个map集合,并
且最终要以一个map的方式返回数据,此时需要通过@MapKey注解设置map集合的键,值是每条数据所对应的
map集合
*/
@MapKey("id")
Map<String, Object> getAllUserToMap();


<!--Map<String, Object> getAllUserToMap();-->
<select id="getAllUserToMap" resultType="map">
select * from t_user
</select>
结果:
<!--
{
    1={password=123456, sex=, id=1, age=23, username=admin},
    2={password=123456, sex=, id=2, age=23, username=张三},
    3={password=123456, sex=, id=3, age=23, username=张三}
}
-->

案例:

Mapper接口

/**
     * 查询每条记录都封装为Map集合,将作为Map集合的值,并且使用`@MapKey`
     * 注意:`@MapKey``@MapKey`注解中的值必须是唯一,如果不是唯一可以将`Map`集合中的其他值覆盖掉,
     * 所以必须是唯一【一般选择表的`ID`】
     */

    @MapKey("id")
    Map<Integer,Object> getAllUserToMap2();

Mapper.xml文件

<!--
    Map<String,Object> getAllUserToMap2();
    查询每条记录都封装为Map集合,将作为Map集合的值,并且使用`@MapKey`
    注意:`@MapKey``@MapKey`注解中的值必须是唯一,如果不是唯一可以将`Map`集合中的其他值覆盖掉,
    所以必须是唯一【一般选择表的`ID`】
-->
    <select id="getAllUserToMap2" resultType="map">
        select * from t_user;
    </select>

test

// 情况二:每条记录封装为Map集合,并且选取表中唯一标识作为`@MapKey`的值
    @Test
    public void testGetAllUserToMap2(){
        SqlSession sqlSession = SqlSessionUtils.getSqlSession();
        SelectMapper mapper = sqlSession.getMapper(SelectMapper.class);
        Map<Integer, Object> map = mapper.getAllUserToMap2();
        System.out.println(map);
        Set<Integer> keySet = map.keySet();
        for (Integer key:
             keySet) {
            System.out.println(key+"="+map.get(key));
        }
    }

综合:

Mapper接口

/**
 * 用于各种查询操作
 */
public interface SelectMapper {
    /**
     *当查询的返回只有一条记录时,
     * 1.可以用JavaBean类进行接收
     * 2.可以用List集合进行接收
     */
    // 根据ID查询用户信息 , 用User实体类进行接收
    User getUserByID(@Param("id") Integer id);
    // 根据ID查询用户信息 , 用List集合进行接收
    List<User> getUserByID2(@Param("id") Integer id);

    /**
     * 当查询返回是多条记录时,
     * 1.可以用List集合进行接收
     *
     * 注意是:一定不能使用JavaBean接收,否则会报:TooManyResultsException
     */
    List<User> getAllUser();

    // 查询单个数据【就是返回单行单列】
    Integer getCount();
    // 例如查询 id为1的email
    String getEmailByID(@Param("id") Integer id);

    /**
     * 查询返回一条记录为Map集合
     * 查询到的属性作为Map集合的键
     * 查询到的值作为Map集合的值
     */
    Map<String,Object> getUserByIdToMap(@Param("id")Integer id);

    /**
     * 查询返回多条记录
     * 情况一:查询到的记录每条记录都封装成一个Map集合,再将每一个Map封装List集合
     */
    //注意是:由于查询返回是多条记录不能使用一个Map集合接收,否则报错为:TooManyResultsException
    // 如:Map<String,Object> getAllUserToMap();错误,返回多条记录只用一个Map接收
    List<Map<String,Object>> getAllUserToMap();

    /**
     * 查询每条记录都封装为Map集合,将作为Map集合的值,并且使用`@MapKey`
     * 注意:`@MapKey``@MapKey`注解中的值必须是唯一,如果不是唯一可以将`Map`集合中的其他值覆盖掉,
     * 所以必须是唯一【一般选择表的`ID`】
     */

    @MapKey("id")
    Map<Integer,Object> getAllUserToMap2();

}

Mapper.xml文件

<mapper namespace="com.haikang.mybatis.mapper.SelectMapper">
    <!--getUserByID根据ID查询用户信息-->
    <select id="getUserByID" resultType="User">
        select * from t_user where id = #{id};
    </select>
    <!--List<User> getUserByID2(@Param("id") Integer id);根据ID查询用户信息-->
    <select id="getUserByID2" resultType="User">
        select * from t_user where id = #{id};
    </select>

    <!--getAllUser查询所有用户信息-->
    <select id="getAllUser" resultType="User">
        select * from t_user;
    </select>
    <!--Integer getCount();查询所有记录数-->
    <select id="getCount" resultType="java.lang.Integer">
        select count(*) from t_user;
    </select>

<!--String getEmailByID(Integer id);根据ID查询Email-->
    <select id="getEmailByID" resultType="java.lang.String">
        select email from t_user where id = #{id};
    </select>
<!--
    Map<String,Object> getUserByIdToMap(@Param("id")Integer id);
    查询到的结果为一条记录作为Map集合,查询到属性为Map键,查询到值为Map值
   -->
    <select id="getUserByIdToMap" resultType="map">
        select * from t_user where id = #{id};
    </select>

    <!--
        List<Map<String,Object>> getAllUserToMap();
        查询到多条记录,每条都封装成Map集合,再放在List集合中
    -->
    <select id="getAllUserToMap" resultType="Map">
        select * from t_user;
    </select>
<!--
    Map<String,Object> getAllUserToMap2();
    查询每条记录都封装为Map集合,将作为Map集合的值,并且使用`@MapKey`
    注意:`@MapKey``@MapKey`注解中的值必须是唯一,如果不是唯一可以将`Map`集合中的其他值覆盖掉,
    所以必须是唯一【一般选择表的`ID`】
-->
    <select id="getAllUserToMap2" resultType="map">
        select * from t_user;
    </select>
</mapper>

test

/**
 * @Author 海康
 * @Version 1.0
 */
public class TestSelectMapper {
    // 查询到多条记录封装Map集合的两种情况
    // 情况一:每条记录封装为Map集合,再多条记录封装为List集合泛型为Map
    @Test
    public void testGetAllUserToMap(){
        SqlSession sqlSession = SqlSessionUtils.getSqlSession();
        SelectMapper mapper = sqlSession.getMapper(SelectMapper.class);
        List<Map<String, Object>> mapList = mapper.getAllUserToMap();
        for (Map<String , Object> map:
             mapList) {
            System.out.println(map);
            /**
             * {password=123, sex=女, id=1, age=23, email=168@qq.com, username=明}
             * {password=123, sex=男, id=2, age=22, email=123@qq.com, username=海康}
             * {password=123, sex=男, id=3, age=22, email=123@qq.com, username=湛江}
             * {password=123, sex=男, id=4, age=22, email=123@qq.com, username=西安}
             */
        }
    }

    // 情况二:每条记录封装为Map集合,并且选取表中唯一标识作为`@MapKey`的值
    @Test
    public void testGetAllUserToMap2(){
        SqlSession sqlSession = SqlSessionUtils.getSqlSession();
        SelectMapper mapper = sqlSession.getMapper(SelectMapper.class);
        Map<Integer, Object> map = mapper.getAllUserToMap2();
        System.out.println(map);
        Set<Integer> keySet = map.keySet();
        for (Integer key:
             keySet) {
            System.out.println(key+"="+map.get(key));
        }
    }

    // 查询到为一条记录封装为Map集合
    @Test
    public void testGetUserByIdToMap(){
        SqlSession sqlSession = SqlSessionUtils.getSqlSession();
        SelectMapper mapper = sqlSession.getMapper(SelectMapper.class);
        Map<String, Object> toMap = mapper.getUserByIdToMap(1);
        System.out.println(toMap);
    }

    // 查询所有记录数
    @Test
    public void testGetCount(){
        SqlSession sqlSession = SqlSessionUtils.getSqlSession();
        SelectMapper mapper = sqlSession.getMapper(SelectMapper.class);
        // 查询所有记录条数
        Integer count = mapper.getCount();
        System.out.println(count);
        // 根据ID查询 email
        String email = mapper.getEmailByID(1);
        System.out.println(email);
    }

    // 查询返回一条记录的情况
    @Test
    public void testGetUserByID(){
        SqlSession sqlSession = SqlSessionUtils.getSqlSession();
        SelectMapper mapper = sqlSession.getMapper(SelectMapper.class);
        //查询返回一条记录用JavaBean类进行接收
//        User user = mapper.getUserByID(1);
//        System.out.println(user);
        //查询返回一条记录用List集合进行接收
        List<User> list = mapper.getUserByID2(1);
        list.forEach(user -> System.out.println(user));
    }

    // 查询所有用户信息
    @Test
    public void testAllUser(){
        SqlSession sqlSession = SqlSessionUtils.getSqlSession();
        SelectMapper mapper = sqlSession.getMapper(SelectMapper.class);
        // 查询用户信息
        List<User> allUser = mapper.getAllUser();
        allUser.forEach(user -> System.out.println(user));
    }
}
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值