Mybatis框架-12:resultMap(domain属性和数据库字段名不一致时如何封装数据)

本文详细介绍如何使用MyBatis的resultMap进行数据库查询结果与Java对象的映射,通过具体示例展示如何配置resultMap来实现字段之间的精确匹配,以及如何在Mapper中调用这些配置来获取数据。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

第一步:使用resoultMap写好封装模板

column是数据库中的字段名,property是domain的字段名,这样写了之后就相当于将他们做了一个匹配,和hibernate的映射文件有点相似

<resultMap id="Mycustomer" type="com.ctbu.domain.Customer">
        <result column="cust_id" property="cust_ids"/>
        <result property="cust_names" column="cust_name"/>
        <result property="cust_phones" column="cust_phone"/>
        <result property="cust_professions" column="cust_profession"/>
        <result property="email" column="email"/>
    </resultMap>

第二步:执行的语句返回结果使用上面个自己写的那个模板

<select id="getCustomer" resultMap="Mycustomer">
        select * from customer where cust_id=#{id}
    </select>

第三步:执行测试,查看是否能够成功查询

 @Test
    public void test3(){
        SqlSession session = MyUtils.openSession();
        CustomerMapper mapper = session.getMapper(CustomerMapper.class);
        Customer customer = mapper.getCustomer(2);
        System.out.println(customer);
        session.close();
    }

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值