1、问题
数据库中的字段
新建一个项目,拷贝之前的,测试实体类对象不一致的情况
public class User {
private int id;
private String name;
private String password;
}
测试出现问题

//select * from mybaties.user1 where id=#{id};
//类型处理器
//select id,name,pws from mybaties.user1 where id=#{id};
解决方法:
起别名
<select id="getUserById" parameterType="int" resultType="User">
select id,name,pws as password from mybaties.user1 where id=#{id};
</select>
2、resultMap
结果集映射
id name pws
id name pswword
<!--结果集映射-->
<resultMap id="UserMap" type="User">
<!--column是数据库里面的字段,property是实体类里面的属性-->
<result column="id" property="id"/>
<result column="name" property="name"/>
<result column="pws" property="password"/>
</resultMap>
<select id="getUserById" parameterType="int" resultMap="UserMap">
select * from mybaties.user1 where id=#{id};
</select>
3、自动开启驼峰命名规则(在核心配置文件中)
<!-- 配置日志文件-->
<settings>
<!-- 是否开启自动驼峰命名规则,将经典的数据库列名A_COLUMN到Java属性名aColumn的类似映射-->
<setting name="mapUnderscoreToCamelCase" value="true"/>
</settings>
resultMap 元素是 MyBatis 中最重要最强大的元素
ResultMap 的设计思想是,对简单的语句做到零配置,对于复杂一点的语句,只需要描述语句之间的关系就行了
ResultMap 最优秀的地方在于,虽然你已经对它相当了解了,但是根本就不需要显示地用到他们

本文介绍了在Java项目中遇到属性名与数据库字段名不一致的问题,提出通过设置resultMap、使用驼峰命名规则转换以及在核心配置文件中自动开启驼峰命名规则来解决这一问题。重点讨论了ResultMap在MyBatis中的重要性和工作原理,旨在简化复杂SQL语句的配置。
1454

被折叠的 条评论
为什么被折叠?



