06 sql映射之返回值类型(resultType)


resultType属性就是指定返回值类型,这一章节主要介绍resultType属性

1 返回集合

如果返回值是一个集合,resultType并非指定为集合类型,而是指定为集合内元素的类型

比如:

List<TbUser> selectAll();
<!--
 如果返回值是一个集合,resultType并非指定为集合类型,`而是指定为集合内元素的类型`
 -->
<select id="selectAll" resultType="TbUser">
   select
       *
   from
       tb_user
</select>

测试:

@Test
public void test01() {
    List<TbUser> list = tbUserMapper.selectAll();
    System.out.println(list);
}

2 返回Map

  1. key就是列名,value就是对应的数据,resultType指定为map

比如:

Map<String, Object> selectById(@Param("id") Long id);
<select id="selectById" resultType="map">
    select
        *
    from
        tb_user
    where
        id = #{id}
</select>

测试:

@Test
public void test02() {
    Map<String, Object> map = tbUserMapper.selectById(1L);
    System.out.println(map);
}

输出结果:

{password=123456, update_time=2019-04-04 22:58:26.0, create_time=2019-04-04 22:58:26.0, phone=13100001111, id=1, username=kobe}
  1. 返回的map,key是主键,value是这个主键对应的数据呢
    • resultType指定为map
    • 在mapper接口中使用MapKey注解指定,返回map的key
@MapKey("id")
Map<Long, TbUser> selectMap();
<select id="selectMap" resultType="map">
   select
       *
   from
       tb_user
</select>

测试:

@Test
public void test03() {
    Map<Long, TbUser> map = tbUserMapper.selectMap();
    for (Long id : map.keySet()) {
        System.out.println(id + "--->" + map.get(id));
    }
}

输出:

1--->{password=123456, update_time=2019-04-04 22:58:26.0, create_time=2019-04-04 22:58:26.0, phone=13100001111, id=1, username=kobe}
5--->{password=123456, update_time=2021-01-31 11:44:22.0, create_time=2021-01-31 11:44:22.0, phone=13011112222, id=5, username=t-mac}
6--->{password=123456, update_time=2021-01-31 11:44:56.0, create_time=2021-01-31 11:44:56.0, phone=13011112222, id=6, username=t-mac}
  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在Spring Boot中使用MyBatis,需要按照以下步骤进行配置和编写SQL映射文件: 1. 添加MyBatis和MyBatis-Spring依赖: ```xml <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>2.2.0</version> </dependency> ``` 2. 配置数据源: 在application.properties文件中添加数据库连接信息,例如: ```properties spring.datasource.url=jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf8 spring.datasource.username=root spring.datasource.password=123456 spring.datasource.driver-class-name=com.mysql.jdbc.Driver ``` 3. 配置MyBatis: 在application.properties文件中添加MyBatis配置信息,例如: ```properties mybatis.mapper-locations=classpath:mapper/*.xml mybatis.type-aliases-package=com.example.demo.entity ``` 其中,mapper-locations指定了SQL映射文件的路径,type-aliases-package指定了实体类的包路径。 4. 编写SQL映射文件: 在mapper目录下创建一个XML文件,例如UserMapper.xml,编写SQL语句和对应的映射关系,例如: ```xml <?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.demo.mapper.UserMapper"> <select id="getUserById" resultType="com.example.demo.entity.User"> select * from user where id=#{id} </select> <insert id="addUser" parameterType="com.example.demo.entity.User"> insert into user(name, age) values(#{name}, #{age}) </insert> <update id="updateUser" parameterType="com.example.demo.entity.User"> update user set name=#{name}, age=#{age} where id=#{id} </update> <delete id="deleteUserById" parameterType="int"> delete from user where id=#{id} </delete> </mapper> ``` 其中,namespace指定了Mapper接口的全路径,id指定了方法名,parameterType指定了方法参数类型resultType指定了返回值类型。 5. 编写Mapper接口: 在com.example.demo.mapper包下创建一个UserMapper.java接口,例如: ```java package com.example.demo.mapper; import com.example.demo.entity.User; import org.apache.ibatis.annotations.*; @Mapper public interface UserMapper { @Select("select * from user where id=#{id}") User getUserById(int id); @Insert("insert into user(name, age) values(#{name}, #{age})") @Options(useGeneratedKeys = true, keyProperty = "id") int addUser(User user); @Update("update user set name=#{name}, age=#{age} where id=#{id}") int updateUser(User user); @Delete("delete from user where id=#{id}") int deleteUserById(int id); } ``` 其中,@Mapper注解标识了该接口是MyBatis的Mapper接口,@Select/@Insert/@Update/@Delete注解分别对应了SQL映射文件中的select/insert/update/delete标签。 至此,MyBatis的配置和SQL映射文件的编写完成。在业务代码中使用UserMapper的方法即可操作数据库。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值