当实体类的名称与数据库中不同时,如下所示
实体类中为驼峰命名法,而数据库中为下划线命名,该如何解决查询时遇到的问题?
解决方案:
-
写sql语句时起别名
select user_id from t_user where username=#{username}
-
在mybatis-config.xml开启驼峰命名规则
<!-- 开启驼峰命名规则,可以将数据库中的下划线映射为驼峰命名 ex:last_Name==>>lastName --> <settings> <setting name="mapUnderscoreToCamelCase" value="true"/> </settings>
-
在Mapper映射文件中使用resultMap来自定义规则
<resultMap id="BloggerResult" type="Blogger"> <result property="id" column="id"/> <!--property为映射的名称,column为数据库中真实的列名--> <result property="userName" column="user_Name"/> </resultMap>