Mybatis中实体类中的字段跟对应表的字段不一致时解决办法

解决字段名与实体类属性名不相同的冲突

实体类字段:

public class Order {
private int id;
private String orderNo;
private float price;
}

对应数据库表字段:

CREATE TABLE orders(
order_id INT PRIMARY KEY AUTO_INCREMENT,
order_no VARCHAR(20),
order_price FLOAT
);

方式一:通过在sql语句中定义别名:

<select id="selectOrder" parameterType="int" resultType="_Order">
select order_id id, order_no orderNo,order_price price from orders where order_id=#{id}
</select>

方式二: 通过<resultMap>

<select id="selectOrderResultMap" parameterType="int" resultMap="orderResultMap">
select * from orders where order_id=#{id}
</select>
<resultMap type="_Order" id="orderResultMap">
<id property="id" column="order_id"/>
<result property="orderNo" column="order_no"/>
<result property="price" column="order_price"/>
</resultMap>
上一篇:解析sql语句中left_join、inner_join中的on与where的区别


下一篇:谈谈MySQL的事务隔离级别