一、问题起因
我们经常会遇到表字段和实体类属性不一致问题,因为在sql中表的字段命名规则是每个单词用下划线隔开,而java中实体类属性的命名规则是每个单词直接采取驼峰命名,这样就会经常导致不一致问题!
二、不一致问题展示
表字段实体类字段@Data public class Test implements Serializable { private static final long serialVersionUID = 337361630075002456L; private String id; private String name; private String state; private String createTime; }
三、解决方案
注:本次只展示xml文件里的写法
之一种:起别名<select id="selectTest" resultType="com.wang.test.demo.entity.Test"> select id,name,state,create_time createTime from test </select> 第二种:使用resultmap来映射
<resultMap type="com.wang.test.demo.entity.Test" id="TestMap"> <result property="id" column="id" jdbcType="VARCHAR"/> <result property="name" column="name" jdbcType="VARCHAR"/> <result property="state" column="state" jdbcType="VARCHAR"/> <result property="createTime" column="create_time" jdbcType="VARCHAR"/> </resultMap> <select id="selectTest" resultMap="TestMap"> select * from test </select> 第三种:使用map来直接接(不建议使用,破坏了ORM映射规则)
<select id="selectTest" resultType="map"> select * from test </select>
四、总结
以上就是这三种解决方案,谢谢大家观看,有什么问题留言哦!!!