本文阐述了在
MyBatis
中使用ResultMap
映射实体类的具体操作步骤,并包含详细的代码,主要是学习过程中的笔记,版权所有,转载请注明出处
示例工程:mybatis-demo2
1)我们在实际开发的过程中,往往会经常遇到实体类的属性和数据库表字段名称不一致的情况,
例如:数据库字段是下划线方式的,而实体类一般是驼峰命名法
2)这时就需要使用
ResultMap
将数据库的字段与实体类进行映射
MySql
数据库1)我们使用之前的订单信息表
tb_order_info
作为演示,数据库的字段采用下划线的方式进行命名,具体的建表语句如下
-- 订单信息表
create table tb_order_info(
order_id varchar(10) not null comment '订单id',
payment decimal(18,2) default 0.00 comment '支付金额',
goods_name varchar(256) default '' comment '商品名称',
payment_type char(1) default '1' comment '支付类型 : 1-在线支付,2-货到付款',
post_fee decimal(18,2) default 0.00 comment '邮费',
status char(1) default '1' comment '订单状态:1-未付款,2-已付款,3-未发货,4-已发货,5-交易成功,6-交易关闭',
create_time date comment '订单创建时间',
constraint pk_order_id primary key(order_id)
) engine=InnoDB AUTO_INCREMENT=19 DEFAULT CHARSET=utf8 comment '订单信息表';
OrderInfo
1)新建数据库实体类
OrderInfo
,将实体类的属性字段采用驼峰命名法,用以区别
OrderInfo
package com.iambest.study.entity;
import java.math.BigDecimal;
import java.util.Date;
/**
*
* 订单信息表的实体类
*
* @author zhangwei
* @since 1.0.0
* @version 1.0.0
*/
public class OrderInfo {
private String orderId;
private BigDecimal payment;
private String goodsName;
private String paymentType;
private BigDecimal postFee;
private String status;
private Date createTime;
public String getOrderId() {
return orderId;
}
public void setOrderId(String orderId) {
this.orderId = orderId;
}
public BigDecimal getPayment() {
return payment;
}
public void setPayment(BigDecimal payment) {
this.payment = payment;
}
public String getGoodsName() {
return goodsName;
}
public void setGoodsName(String goodsName) {
this.goodsName = goodsName;
}
public String getPaymentType() {
return paymentType;
}
public void setPaymentType(String paymentType) {
this.paymentType = paymentType;
}
public BigDecimal getPostFee() {
return postFee;
}
public void setPostFee(BigDecimal postFee) {
this.postFee = postFee;
}
public String getStatus() {
return status;
}
public void setStatus(String status) {
this.status = status;
}
public Date getCreateTime() {
return createTime;
}
public void setCreateTime(Date createTime) {
this.createTime = createTime;
}
@Override
public String toString() {
return "OrderInfo [orderId=" + orderId + ", payment=" + payment
+ ", goodsName=" + goodsName + ", paymentType=" + paymentType
+ ", postFee=" + postFee + ", status=" + status
+ ", createTime=" + createTime + "]";
}
}
SqlMapper
的配置1)首先使用
标签定义数据库字段和实体类的属性之间的映射关系
2)然后在执行
sql
查询时将返回结果指定为该resultMap
即可3)如下示例:
OrderInfoMapper.xml
<resultMap id="baseResultMap" type="com.iambest.study.entity.OrderInfo">
<result property="orderId" column="order_id" jdbcType="VARCHAR" javaType="string">result>
<result property="payment" column="payment" jdbcType="DECIMAL" javaType="java.math.BigDecimal">result>
<result property="goodsName" column="goods_name" jdbcType="VARCHAR" javaType="string">result>
<result property="paymentType" column="payment_type" jdbcType="VARCHAR" javaType="string">result>
<result property="postFee" column="post_fee" jdbcType="DECIMAL" javaType="java.math.BigDecimal">result>
<result property="status" column="status" jdbcType="VARCHAR" javaType="string">result>
<result property="createTime" column="create_time" jdbcType="DATE">result>
resultMap>
<sql id="baseColumn">order_id,payment,goods_name,payment_type,post_fee,status,create_timesql>
<select id="selectOne" resultMap="baseResultMap" parameterType="string">
select
<include refid="baseColumn"/>
from tb_order_info
where order_id=#{order_id}
select>
1)在Mapper接口中新增
selectOne
方法,返回结果为OrderInfo
实体类
/**
*
* 根据订单的ID查询订单的信息
*
* @param order_id
* @return
*/
public OrderInfo selectOne(String order_id);
1)在单元测试中编写如下的代码:
@Test
public void testResultMap() throws IOException{
String config = "config/SqlMapConfig-insert.xml";
Reader reader = Resources.getResourceAsReader(config);
// 创建Builder对象
SqlSessionFactoryBuilder sfb = new SqlSessionFactoryBuilder();
// 创建Factory对象
SqlSessionFactory factory = sfb.build(reader);
// 通过factory对象生成session对象
SqlSession session = factory.openSession();
// 获取到Mapper接口对象
OrderInfoMapper mapper = session.getMapper(OrderInfoMapper.class);
OrderInfo entity = mapper.selectOne("102000000");
System.out.println(entity);
session.close();
}
1)运行单元测试代码,控制台可以正确的输出查询出的数据,并且数据库的实体对象的各个属性都能争取的赋值,表示成功,如下图所示: