六、MyBatis的各种查询功能

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

准备

数据库表

t_person表

2345_image_file_copy_105.jpg

实体类对象

2345_image_file_copy_106.jpg

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);

输出结果

2345_image_file_copy_107.jpg

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);

输出结果

2345_image_file_copy_108.jpg

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);

输出结果

2345_image_file_copy_109.jpg

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)

输出结果

2345_image_file_copy_110.jpg

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);

输出结果

2345_image_file_copy_111.jpg

方式二:@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);

输出结果

2345_image_file_copy_112.jpg

相关文章
|
1月前
|
供应链 前端开发 Java
服装库存管理系统 Mybatis+Layui+MVC+JSP【完整功能介绍+实现详情+源码】
该博客文章介绍了一个使用Mybatis、Layui、MVC和JSP技术栈开发的服装库存管理系统,包括注册登录、权限管理、用户和货号管理、库存管理等功能,并提供了源码下载链接。
服装库存管理系统 Mybatis+Layui+MVC+JSP【完整功能介绍+实现详情+源码】
|
1月前
|
druid Java 数据库连接
SpringBoot项目整合MybatisPlus持久层框架+Druid数据库连接池,以及实现增删改查功能
SpringBoot项目整合MybatisPlus和Druid数据库连接池,实现基本的增删改查功能。
153 0
|
2月前
|
Java 数据库连接 mybatis
Mybatis查询传递单个参数和传递多个参数用法
Mybatis查询传递单个参数和传递多个参数用法
46 11
MybatisPlus-标准CRUD制作,新增boolean save(T t),删除 ~ delete(int id),修改 ~ update(T t),根据id查询,T getById....
MybatisPlus-标准CRUD制作,新增boolean save(T t),删除 ~ delete(int id),修改 ~ update(T t),根据id查询,T getById....
|
3月前
|
缓存 NoSQL Java
在 SSM 架构(Spring + SpringMVC + MyBatis)中,可以通过 Spring 的注解式缓存来实现 Redis 缓存功能
【6月更文挑战第18天】在SSM(Spring+SpringMVC+MyBatis)中集成Redis缓存,涉及以下步骤:添加Spring Boot的`spring-boot-starter-data-redis`依赖;配置Redis连接池(如JedisPoolConfig)和连接工厂;在Service层使用`@Cacheable`注解标记缓存方法,指定缓存名和键生成策略;最后,在主配置类启用缓存注解。通过这些步骤,可以利用Spring的注解实现Redis缓存。
67 2
MyBatisPlus如何根据id批量查询?Required request parameter ‘id‘ for method 解决方法是看青戈大佬MybatisPlus的教程
MyBatisPlus如何根据id批量查询?Required request parameter ‘id‘ for method 解决方法是看青戈大佬MybatisPlus的教程
MybatisPlus介绍新增用户,根据id查询,引入MybatisPlus的起步依赖,增删改查最简单的写法
MybatisPlus介绍新增用户,根据id查询,引入MybatisPlus的起步依赖,增删改查最简单的写法
|
3月前
|
存储 Java 关系型数据库
留言板——增添功能(持久化存储数据,使用MyBatis)
留言板——增添功能(持久化存储数据,使用MyBatis)
37 4
|
3月前
|
Java 数据库连接 mybatis
MyBatisPlus分页功能制作未完
MyBatisPlus分页功能制作未完
|
3月前
|
SQL Java 数据库连接
MyBatis插件深度解析:功能、原理、使用、应用场景与最佳实践
MyBatis插件深度解析:功能、原理、使用、应用场景与最佳实践