Mybatis中resultMap结果集的使用

一、resultMap的使用

在MyBatis的XML文件中,resultMap可以有多个。每个resultMap都有一个唯一的id,你可以根据这个id来引用不同的resultMap

选择使用哪个resultMap取决于你的查询和需要映射的结果。

示例:

假设你有以下的表结构:

CREATE TABLE user (
    id INT PRIMARY KEY,
    username VARCHAR(50),
    email VARCHAR(50)
);

CREATE TABLE order (
    order_id INT PRIMARY KEY,
    user_id INT,
    order_date DATE,
    FOREIGN KEY (user_id) REFERENCES user(id)
);

对于上面的表结构,你可能会有两个resultMap,一个用于用户,一个用于订单:

<resultMap id="userResultMap" type="com.example.User">
    <result property="id" column="id"/>
    <result property="username" column="username"/>
    <result property="email" column="email"/>
</resultMap>

<resultMap id="orderResultMap" type="com.example.Order">
    <result property="orderId" column="order_id"/>
    <result property="userId" column="user_id"/>
    <result property="orderDate" column="order_date"/>
</resultMap>

在你的查询中,你可以根据需要选择合适的resultMap。例如,如果你需要查询用户信息,你可以这样写:

<select id="getUser" resultMap="userResultMap">
    SELECT * FROM user WHERE id = #{id}
</select>

如果你需要查询订单信息,你可以这样写:

<select id="getOrder" resultMap="orderResultMap">
    SELECT * FROM order WHERE order_id = #{orderId}
</select>

总结,你可以定义多个resultMap,并在查询时通过resultMap属性引用它们。选择哪个resultMap取决于你的查询和你想如何映射查询结果。

二、使用resultMap返回HashMap

例如,对于示例表seat,具有如下结构

CREATE TABLE seat
(
	seatId VARCHAR(10),
	`status` TINYINT
);

在该表对应的mapper中有一个findAll()方法,以Map的方式返回所有表记录。其中key是seatId,value是status(转化为Boolean类型)。为了实现这个功能,可以通过MyBatis的XML或者注解来实现。这里,分别展示两种方法:

1、XML方式:

在MyBatis的XML映射文件中,你可以使用<resultMap><select>元素来定义查询和结果映射。

<resultMap id="seatResultMap" type="java.util.HashMap">
    <result property="key" column="seatId"/>
    <result property="value" column="status" javaType="boolean"/>
</resultMap>

<select id="findAll" resultType="java.util.HashMap">
    SELECT seatId as key, status as value FROM seat
</select>

注意,这里为了将TINYINT转化为Boolean,我们使用了javaType属性。并且,我们使用了别名keyvalue来匹配HashMap的key和value。

2、注解方式:

如果你更喜欢使用注解而不是XML,你可以在Mapper接口中使用@Select@ResultType注解。

@Mapper
public interface SeatMapper {
    
    @Select("SELECT seatId as key, status as value FROM seat")
    @ResultType(HashMap.class)
    Map<String, Boolean> findAll();
}

然而,请注意,上述的注解方式并不会将TINYINT转化为Boolean。因此,你可能需要在你的Service层或者DAO层进行转换。例如:

@Service
public class SeatService {
    @Autowired
    private SeatMapper seatMapper;
    
    public Map<String, Boolean> findAll() {
        Map<String, Integer> result = seatMapper.findAll();
        result.replaceAll((k, v) -> v == 1);
        return result;
    }
}

在这个例子中,我们假设status字段中,1代表true,0代表false。然后我们用replaceAll方法将所有值转化为Boolean。这只是一个简单的示例,具体的转换逻辑可能需要根据你的实际业务逻辑进行调整。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
MyBatisresultMap标签用于配置实体类与表字段的映射规则。该标签有以下作用: - 自定义映射规则,通过id属性给映射规则指定一个唯一标识,用于在其他地方引用。 - 使用id标签配置主键映射,指定实体类的属性和数据库表的字段的对应关系。 - 使用result标签配置普通字段映射,指定实体类的属性和数据库表的字段的对应关系。 举例来说,可以使用resultMap标签来配置一个名为studentmap的映射规则,该规则用于将数据库表的字段与Student实体类的属性进行映射。其,id标签用于配置主键映射,result标签用于配置普通字段映射。例如: ```xml <resultMap id="studentmap" type="student"> <id property="sid" column="s_id"></id> <result property="sname" column="s_name"></result> <result property="sage" column="s_age"></result> <result property="sphone" column="s_phone"></result> </resultMap> ``` 此外,在核心配置文件,还可以通过设置lazyLoadingEnabled属性来开启延迟加载。如果使用的是MyBatis 3.41之前的版本,还需要配置aggressiveLazyLoading为true。同时,可以使用resultMap标签来配置一对多的映射关系,将一个教师对象对应多个学生对象。例如: ```xml <resultMap id="teacher" type="Teacher"> <id property="tid" column="t_id"></id> <result property="tname" column="t_name"></result> <result property="tage" column="t_age"></result> <collection property="students" ofType="student"> <id property="sid" column="s_id"></id> <result property="sage" column="s_age"></result> <result property="sname" column="s_name"></result> <result property="sphone" column="s_phone"></result> </collection> </resultMap> ``` 此外,可以使用association标签来配置多对一的映射关系,将多个学生对象对应一个教师对象。例如: ```xml <association property="teacher" javaType="Teacher"> <result property="tid" column="t_id"></result> <result property="tname" column="t_name"></result> <result property="tage" column="t_age"></result> </association> ``` 总结起来,resultMap标签在MyBatis用于配置实体类与表字段的映射规则,可以通过id、result、collection、association等子标签来实现不同类型的映射关系的配置。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* *3* [mybatisResultmap标签的的使用](https://blog.csdn.net/weixin_43891398/article/details/125608434)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 100%"] [ .reference_list ]

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值