resultMap

用来解决实体类中的属性与数据库表中列名不匹配的问题

1. 实体类

package com.wy.bean;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.util.Date;

/**
 * @author HelloWorld
 * @create 2021-06-22-19:07
 * @email 154803771@qq.com
 */
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Order {
    private Integer id;
    private String name;
    private Date date;
}

2. 数据库表列名

mybatis---resultMap标签_# mybatis

Order getOrderById(int id);
<mapper namespace="com.wy.dao.OrderDao">

    <select id="getOrderById" resultType="com.wy.bean.Order" resultMap="OrderResultMap">
        select * from orders where order_id = #{id}
    </select>
<!--    id 唯一标识符  type:返回的类型-->
    <resultMap id="OrderResultMap" type="com.wy.bean.Order">
        <id column="order_id" property="id"/>
        <result column="order_name" property="name"/>
        <result column="order_date" property="date"/>
    </resultMap>

<!--    id 标签:用于指定主键字段-->
<!--    result 标签:用于指定非主键字段-->
<!--    column 属性:用于指定数据库列名-->
<!--    property 属性:用于指定实体类属性名称-->
</mapper>
public void testGetOrderById() {
    SqlSession session = MyBatisUtils.getSession();
    OrderDao orderDao = session.getMapper(OrderDao.class);
    System.out.println(orderDao.getOrderById(1));
    MyBatisUtils.close(session);
}