Mybatis的各种查询功能

Mybatis的各种查询功能

查询一个实体类对象

public interface SelectMapper {

    /**
     * 若sql语句查询的结果为多条时,一定不能以实体类类型作为方法的返回值
     * 否则会抛出异常TooManyResultsException
     * 若sql语句查询的结果为1条时,此时可以使用实体类类型或list集合类型作为方法的返回值
     */

    /**
     * 根据id查询用户信息
     * @param id
     * @return
     */
    User getUserById(@Param("id") Integer id);
}

**注意:**若sql语句查询的结果为多条时,一定不能以实体类类型作为方法的返回值,否则会抛出异常TooManyResultsException,若sql语句查询的结果为1条时,此时可以使用实体类类型或list集合类型作为方法的返回值

mybatis的SQL编写

<?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="com.example.mybatis.mapper.SelectMapper">
	<!--User getUserById(@Param("id") Integer id);-->
    <select id="getUserById" resultType="User">
        select * from t_user where id = #{id}
    </select>
</mapper>

查询一个list集合

public interface SelectMapper {

   /**
     * 查询所有的用户信息
     * @return
     */
    List<User> getAllUser();
}

mybatis的SQL编写

<?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="com.example.mybatis.mapper.SelectMapper">
	<!--List<User> getAllUser();-->
    <select id="getAllUser" resultType="User">
        select * from t_user
    </select>
</mapper>

当查询的数据为多条时,不能使用实体类作为返回值,否则会抛出异常

TooManyResultsException;但是若查询的数据只有一条,可以使用实体类或集合作为返回值

查询单个数据

public interface SelectMapper {

   /**
     * 查询用户的总数量
     * @return
     */
    Integer getCount();
}

mybatis的SQL编写

<?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="com.example.mybatis.mapper.SelectMapper">
	<!--Integer getCount();-->
    <!--
        MyBatis中为Java中常用的类型设置了类型别名
        Integer:Integer,int
        int:_int,_integer
        Map:map
        String:string
    -->
    <select id="getCount" resultType="int">
        select count(*) from t_user
    </select>
</mapper>

查询一条数据为map集合

public interface SelectMapper {
	/**
     * 根据id查询用户信息为map集合
     * @param id
     * @return
     */
    Map<String, Object> getUserByIdToMap(@Param("id") Integer id);
}

注意:若查询的结果有的字段的值为null,那么该字段不会存在Map集合中

mybatis的SQL编写

<?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="com.example.mybatis.mapper.SelectMapper">
	<!--Map<String, Object> getUserByIdToMap(@Param("id") Integer id);-->
    <select id="getUserByIdToMap" resultType="map">
        select * from t_user where id = #{id}
    </select>
</mapper>

查询多条数据为map集合

方式一

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

mybatis的SQL编写

<?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="com.example.mybatis.mapper.SelectMapper">
	<!--Map<String, Object> getAllUserToMap();-->
    
    <!--查询结果为:[
			  {password=123456, gender=男, id=1, age=23, email=12345@qq.com, username=admin},
			  {password=123456, gender=男, id=1, age=23, email=12345@qq.com, username=admin}
			 ]-->
    <select id="getAllUserToMap" resultType="map">
        select * from t_user
    </select>
</mapper>

方式二

public interface SelectMapper {
 /**
     * 查询所有的用户信息为map集合
     * 若查询的数据有多条时,并且要将每条数据转换为map集合
     * 此时有两种解决方案:
     * 1、将mapper接口方法的返回值设置为泛型是map的list集合
     * List<Map<String, Object>> getAllUserToMap();
     * 结果:[{password=123456, gender=男, id=1, age=23, email=12345@qq.com, username=admin},{password=123456, gender=男, id=1, age=23, email=12345@qq.com, username=admin}]
     * 2、可以将每条数据转换的map集合放在一个大的map中,但是必须要通过@MapKey注解
     * 将查询的某个字段的值作为大的map的键
     * @MapKey("id")
     * Map<String, Object> getAllUserToMap();
     * 结果:
     * {
     *  1={password=123456, gender=男, id=1, age=23, email=12345@qq.com, username=admin},
     *  2={password=123, gender=男, id=2, age=23, email=12345@qq.com, username=zhangsan},
     *  3={password=123456, gender=女, id=3, age=33, email=123@qq.com, username=root},
     *  4={password=123, id=4, username=lisi}
     *  }
     */
    //List<Map<String, Object>> getAllUserToMap();
    @MapKey("id")//此主键的作用是将查询出来的结果的id作为map集合的键,结果作为map集合的值
    Map<String, Object> getAllUserToMap();
}

mybatis的SQL编写

<?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="com.example.mybatis.mapper.SelectMapper">
	<!--Map<String, Object> getAllUserToMap();-->
    <select id="getAllUserToMap" resultType="map">
        select * from t_user
    </select>
</mapper>
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值