六、MyBatis的各种查询功能

准备

数据库表
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);

输出结果

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
在Spring Boot中整合MyBatis-Plus可以简化数据库操作和提高开发效率。 1. 首先,需要在Spring Boot项目的pom.xml文件中添加相关依赖: ``` <!-- MyBatis-Plus依赖 --> <dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-boot-starter</artifactId> <version>最新版本</version> </dependency> <!-- MyBatis-Plus代码生成器依赖,可选 --> <dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-generator</artifactId> <version>最新版本</version> </dependency> ``` 2. 在application.properties或application.yml文件中配置数据库连接信息,例如: ``` spring.datasource.url=jdbc:mysql://localhost:3306/mydb spring.datasource.username=root spring.datasource.password=123456 spring.datasource.driver-class-name=com.mysql.jdbc.Driver ``` 3. 创建实体类和Mapper接口。使用MyBatis-Plus的注解,如@Table、@Id、@Column等来定义实体类和字段。 4. 创建Mapper接口继承MyBatis-Plus提供的BaseMapper接口,例如: ``` import com.baomidou.mybatisplus.core.mapper.BaseMapper; public interface UserMapper extends BaseMapper<User> { // ... } ``` 5. 在需要使用数据库操作的地方,注入Mapper接口,并调用相应的方法完成数据库操作。例如: ```java @Service public class UserService { @Autowired private UserMapper userMapper; public User getUserById(Long id) { return userMapper.selectById(id); } // ... } ``` 通过以上步骤,就可以在Spring Boot项目中使用MyBatis-Plus进行数据库操作了。另外,MyBatis-Plus还提供了其他方便的功能,如分页查询、条件构造器等,可以根据实际需求进一步使用。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

齊 天 大 聖

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

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

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

打赏作者

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

抵扣说明:

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

余额充值