Mybatis07-Mybatis的各种查询功能

2024-02-29 23:32

本文主要是介绍Mybatis07-Mybatis的各种查询功能,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

Mybatis07-Mybatis的各种查询功能

  • 1、查询一个实体对象
    • 1.1、mapper接口中的方法
    • 1.2、mapper接口映射文件
    • 1.3、测试方法及其对应输出
  • 2、查询一个List集合
    • 2.1、mapper接口中的方法
    • 2.2、mapper接口映射文件
    • 2.3、测试方法及其对应输出
  • 3、查询单个数据
    • 3.1、mapper接口中的方法
    • 3.2、mapper接口映射文件
    • 3.3、测试方法及其对应输出
  • 4、查询一条数据为map集合
    • 4.1、mapper接口中的方法
    • 4.2、mapper接口映射文件
    • 4.3、测试方法及其对应输出
  • 5、查询多条数据为map集合
    • 5.1、mapper接口中的方法
    • 5.2、mapper接口映射文件
    • 5.3、测试方法及其对应输出
  • 6、总结
    • 6.1、若查询出的数据只有一条
    • 6.2、若查询出的数据有多条
    • 6.3、Mybatis中的默认类型别名

1、查询一个实体对象

1.1、mapper接口中的方法

/*** 根据id查询用户信息*/
//User getUserById(@Param("id") Integer id);
List<User> getUserById(@Param("id") Integer id);

1.2、mapper接口映射文件

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

1.3、测试方法及其对应输出

  • 测试方法
@Test
public void testGetUserById() {SqlSession sqlSession = SqlSessionUtils.getSqlSession();SelectMapper selectMapper = sqlSession.getMapper(SelectMapper.class);System.out.println(selectMapper.getUserById(3));
}
  • 结果
    在这里插入图片描述

2、查询一个List集合

2.1、mapper接口中的方法

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

2.2、mapper接口映射文件

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

2.3、测试方法及其对应输出

@Test
public void testGetAllUser() {SqlSession sqlSession = SqlSessionUtils.getSqlSession();SelectMapper selectMapper = sqlSession.getMapper(SelectMapper.class);System.out.println(selectMapper.getAllUser());
}

3、查询单个数据

3.1、mapper接口中的方法

/*** 查询用户的总记录数*/
Integer getUserCount();

3.2、mapper接口映射文件

<!-- Integer getUserCount(); -->
<!--    <select id="getUserCount" resultType="int">-->
<!--    <select id="getUserCount" resultType="java.lang.Integer">-->
<!--    <select id="getUserCount" resultType="Int">-->
<select id="getUserCount" resultType="Integer">select count(*) from mybatis.user
</select>

3.3、测试方法及其对应输出

  • 测试代码
@Test
public void testGetUserCount() {SqlSession sqlSession = SqlSessionUtils.getSqlSession();SelectMapper selectMapper = sqlSession.getMapper(SelectMapper.class);System.out.println(selectMapper.getUserCount());
}
  • 结果
    在这里插入图片描述

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

4.1、mapper接口中的方法

 /*** 根据id查询用户信息为一个map集合*/Map<String, Object> getUserByIdToMap(@Param("id") Integer id);

4.2、mapper接口映射文件

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

4.3、测试方法及其对应输出

  • 测试代码
@Test
public void testGetUserByIdToMap() {SqlSession sqlSession = SqlSessionUtils.getSqlSession();SelectMapper selectMapper = sqlSession.getMapper(SelectMapper.class);System.out.println(selectMapper.getUserByIdToMap(3));
}
  • 结果
    在这里插入图片描述

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

5.1、mapper接口中的方法

/*** 查询所有用户信息为map集合*/
//List<Map<String, Object>> getAllUserByIdToMap();
@MapKey("id")
Map<String, Object> getAllUserByIdToMap();

5.2、mapper接口映射文件

<!-- Map<String, Object> getAllUserByIdToMap(); -->
<select id="getAllUserByIdToMap" resultType="map">select * from mybatis.user
</select>

5.3、测试方法及其对应输出

  • 测试代码
@Test
public void testGetAllUserByIdToMap() {SqlSession sqlSession = SqlSessionUtils.getSqlSession();SelectMapper selectMapper = sqlSession.getMapper(SelectMapper.class);System.out.println(selectMapper.getAllUserByIdToMap());
}
  • 结果
    在这里插入图片描述

6、总结

6.1、若查询出的数据只有一条

  1. 可以通过实体类对象接收
  2. 可以通过List集合接收
  3. 可以通过map集合接收
  • map集合接收的结果:{password=123, sex=男, id=3, age=23, username=sweet}

6.2、若查询出的数据有多条

  1. 可以通过实体类类型的list集合接收
  2. 可以通过map类型的list集合接收
  3. 可以在mapper接口的方法上添加@MapKey注解,此时就可以将每条数据转换的map集合作为值,以某个字段的值作为键,放在同一个map集合中
  4. 注意:一定不能通过实体类对象接收,此时会抛出异常TooManyResultsException

6.3、Mybatis中的默认类型别名

AliasMapped Type
_bytebyte
_longlong
_shortshort
_intint
_integerint
_doubledouble
_floatfloat
_booleanboolean
stringString
byteByte
longLong
shortShort
intInteger
integerInteger
doubleDouble
floatFloat
booleanBoolean
dateDate
decimalBigDecimal
bigdecimalBigDecimal
objectObject
mapMap
hashmapHashMap
listList
arraylistArrayList
collectionCollection
iteratorIterator

示例:

<!-- Integer getUserCount(); -->
<!--    <select id="getUserCount" resultType="int">-->
<!--    <select id="getUserCount" resultType="java.lang.Integer">-->
<!--    <select id="getUserCount" resultType="Int">-->
<select id="getUserCount" resultType="Integer">select count(*) from mybatis.user
</select>

这篇关于Mybatis07-Mybatis的各种查询功能的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/760465

相关文章

springboot+redis实现订单过期(超时取消)功能的方法详解

《springboot+redis实现订单过期(超时取消)功能的方法详解》在SpringBoot中使用Redis实现订单过期(超时取消)功能,有多种成熟方案,本文为大家整理了几个详细方法,文中的示例代... 目录一、Redis键过期回调方案(推荐)1. 配置Redis监听器2. 监听键过期事件3. Redi

Mybatis对MySQL if 函数的不支持问题解读

《Mybatis对MySQLif函数的不支持问题解读》接手项目后,为了实现多租户功能,引入了Mybatis-plus,发现之前运行正常的SQL语句报错,原因是Mybatis不支持MySQL的if函... 目录MyBATis对mysql if 函数的不支持问题描述经过查询网上搜索资料找到原因解决方案总结Myb

mybatis-plus分表实现案例(附示例代码)

《mybatis-plus分表实现案例(附示例代码)》MyBatis-Plus是一个MyBatis的增强工具,在MyBatis的基础上只做增强不做改变,为简化开发、提高效率而生,:本文主要介绍my... 目录文档说明数据库水平分表思路1. 为什么要水平分表2. 核心设计要点3.基于数据库水平分表注意事项示例

Mybatis的mapper文件中#和$的区别示例解析

《Mybatis的mapper文件中#和$的区别示例解析》MyBatis的mapper文件中,#{}和${}是两种参数占位符,核心差异在于参数解析方式、SQL注入风险、适用场景,以下从底层原理、使用场... 目录MyBATis 中 mapper 文件里 #{} 与 ${} 的核心区别一、核心区别对比表二、底

Qt实现对Word网页的读取功能

《Qt实现对Word网页的读取功能》文章介绍了几种在Qt中实现Word文档(.docx/.doc)读写功能的方法,包括基于QAxObject的COM接口调用、DOCX模板替换及跨平台解决方案,重点讨论... 目录1. 核心实现方式2. 基于QAxObject的COM接口调用(Windows专用)2.1 环境

MyBatis-Plus逻辑删除实现过程

《MyBatis-Plus逻辑删除实现过程》本文介绍了MyBatis-Plus如何实现逻辑删除功能,包括自动填充字段、配置与实现步骤、常见应用场景,并展示了如何使用remove方法进行逻辑删除,逻辑删... 目录1. 逻辑删除的必要性编程1.1 逻辑删除的定义1.2 逻辑删php除的优点1.3 适用场景2.

MySQL中between and的基本用法、范围查询示例详解

《MySQL中betweenand的基本用法、范围查询示例详解》BETWEENAND操作符在MySQL中用于选择在两个值之间的数据,包括边界值,它支持数值和日期类型,示例展示了如何使用BETWEEN... 目录一、between and语法二、使用示例2.1、betwphpeen and数值查询2.2、be

MyBatis配置文件中最常用的设置

《MyBatis配置文件中最常用的设置》文章主要介绍了MyBatis配置的优化方法,包括引用外部的properties配置文件、配置外置以实现环境解耦、配置文件中最常用的6个核心设置以及三种常用的Ma... 目录MyBATis配置优化mybatis的配置中引用外部的propertis配置文件⚠️ 注意事项X

MyBatis中的两种参数传递类型详解(示例代码)

《MyBatis中的两种参数传递类型详解(示例代码)》文章介绍了MyBatis中传递多个参数的两种方式,使用Map和使用@Param注解或封装POJO,Map方式适用于动态、不固定的参数,但可读性和安... 目录✅ android方式一:使用Map<String, Object>✅ 方式二:使用@Param

MyBatis-Plus使用动态表名分表查询的实现

《MyBatis-Plus使用动态表名分表查询的实现》本文主要介绍了MyBatis-Plus使用动态表名分表查询,主要是动态修改表名的几种常见场景,文中通过示例代码介绍的非常详细,对大家的学习或者工作... 目录1. 引入依赖2. myBATis-plus配置3. TenantContext 类:租户上下文