六、MyBatis的各种查询功能

本文介绍了使用MyBatis实现各种查询功能的方法,包括查询单个实体、查询列表、查询单个数据、查询单条记录为Map集合以及查询多条记录为Map集合的不同方式。

准备

数据库表
t_person表
在这里插入图片描述

实体类对象
在这里插入图片描述

1、查询一个实体类对象

       通过name=“王五”进行查询一条数据,并封装在Person对象中。

mapper接口

public interface PersonMapper {

    //通过name查询对象
    Person selectPersonByName(@Param("name") String name);

}

映射文件

<mapper namespace="com.xxx.mapper.PersonMapper"><!--接口-->

<!--    Person selectPersonByName(@Param("name") String name);-->
    <select id="selectPersonByName" resultType="com.xxx.pojo.Person">
        select * from t_person where name=#{name}
    </select>


</mapper>

测试

		PersonMapper mapper = sqlSession.getMapper(PersonMapper.class);
		
        Person zs = mapper.selectPersonByName("张三");
        System.out.println(zs);

输出结果

在这里插入图片描述

2、查询一个list集合

       查询所有age=20的人。

mapper接口

public interface PersonMapper {

    //查询所有age=20的人
    List<Person> selectPersonByAge(@Param("age") Integer age);
}

映射文件

<mapper namespace="com.xxx.mapper.PersonMapper"><!--接口-->

<!--    List<Person> SelectPersonByAge(@Param("age") Integer age);-->
    <select id="selectPersonByAge" resultType="com.xxx.pojo.Person">
        select * from t_person where age=20
    </select>


</mapper>

测试

		PersonMapper mapper = sqlSession.getMapper(PersonMapper.class);

        List<Person> people = mapper.selectPersonByAge(20);
        System.out.println(people);

输出结果

在这里插入图片描述

3、查询单个数据

       查询年龄等于xx的人的个数。

mapper接口

public interface PersonMapper {

    //查询年龄等于20的人的个数
    Integer selectAgeCount(Integer age);
}

映射文件

<mapper namespace="com.xxx.mapper.PersonMapper"><!--接口-->

<!--    Integer selectAgeCount();-->
    <select id="selectAgeCount" resultType="int">
        select count(*) from t_person where age=#{age}
    </select>


</mapper>

测试

		PersonMapper mapper = sqlSession.getMapper(PersonMapper.class);

        //查询年龄是20岁的人的个数
        Integer integer = mapper.selectAgeCount(20);
        System.out.println(integer);

输出结果

在这里插入图片描述

4、查询一条数据为map集合

       根据用户name查询用户信息为map集合。

mapper接口

public interface PersonMapper {
    //根据用户name查询用户信息为map集合
    Map<String,Object> selectPersonByIdMap(@Param("name")String name);
}

映射文件

<mapper namespace="com.xxx.mapper.PersonMapper"><!--接口-->

<!--     Map<String,Object> selectPersonByIdMap(@Param("name")String name);-->

    <select id="selectPersonByIdMap" resultType="Map">
        select * from t_person where name=#{name}
    </select>

</mapper>

测试

        PersonMapper mapper = sqlSession.getMapper(PersonMapper.class);

        //根据用户name查询用户信息为map集合
        Map<String, Object> map = mapper.selectPersonByIdMap("王五");
        System.out.println(map)

输出结果

在这里插入图片描述

5、查询多条数据为map集合

       查询所有用户信息为map集合。
       将表中的数据以map集合的方式查询,一条数据对应一个map;若有多条数据,就会产生多个map集合,此 时可以将这些map放在一个list集合中获取。

方式一:List

mapper接口

public interface PersonMapper {

    //查询所有用户信息为map集合01。
    List<Map<String,Object>> selectAllPerson01();
}

映射文件

<?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"><!--约束,约束不同xml中所写的标签也不同-->
<mapper namespace="com.xxx.mapper.PersonMapper"><!--接口-->

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

</mapper>

测试

        PersonMapper mapper = sqlSession.getMapper(PersonMapper.class);
        
        //查询所有用户信息为map集合01
        List<Map<String, Object>> maps = mapper.selectAllPerson01();
        System.out.println(maps);

输出结果

在这里插入图片描述

方式二:@MapKey注解

       将表中的数据以map集合的方式查询,一条数据对应一个map;若有多条数据,就会产生多个map集合,并 且最终要以一个map的方式返回数据,此时需要通过@MapKey注解设置map集合的键,值是每条数据所对应的 map集合。

mapper接口

public interface PersonMapper {

   //查询所有用户信息为map集合02。
    @MapKey("name")
    Map<String,Object> selectAllPerson02();
}

映射文件

<?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"><!--约束,约束不同xml中所写的标签也不同-->
<mapper namespace="com.xxx.mapper.PersonMapper"><!--接口-->

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

</mapper>

测试

        PersonMapper mapper = sqlSession.getMapper(PersonMapper.class);

        //查询所有用户信息为map集合02
        Map<String, Object> maps = mapper.selectAllPerson02();
        System.out.println(maps);

输出结果

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

齊 天 大 聖

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值