一,基本环境
自己写个demo,习惯性的使用generator逆向生成代码。
这是我的mapper.xml文件。
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mapping.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.byk.rong.system.mapper.read.UserReadMapper">
<resultMap id="BaseResultMap" type="com.byk.rong.system.entity.User">
<id column="id" jdbcType="VARCHAR" property="id" />
<result column="user_name" jdbcType="VARCHAR" property="userName" />
<result column="real_name" jdbcType="VARCHAR" property="realName" />
<result column="password" jdbcType="VARCHAR" property="password" />
<result column="salt" jdbcType="VARCHAR" property="salt" />
<result column="dept_id" jdbcType="VARCHAR" property="deptId" />
<result column="email" jdbcType="VARCHAR" property="email" />
<result column="mobile" jdbcType="VARCHAR" property="mobile" />
<result column="ID_card_number" jdbcType="VARCHAR" property="idCardNumber" />
<result column="sex" jdbcType="VARCHAR" property="sex" />
<result column="pictrue" jdbcType="VARCHAR" property="pictrue" />
<result column="status" jdbcType="VARCHAR" property="status" />
<result column="city_id" jdbcType="VARCHAR" property="cityId" />
<result column="hobby" jdbcType="VARCHAR" property="hobby" />
<result column="remarks" jdbcType="VARCHAR" property="remarks" />
<result column="create_user" jdbcType="VARCHAR" property="createUser" />
<result column="create_time" jdbcType="TIMESTAMP" property="createTime" />
<result column="update_user" jdbcType="VARCHAR" property="updateUser" />
<result column="update_time" jdbcType="TIMESTAMP" property="updateTime" />
<result column="delete_user" jdbcType="VARCHAR" property="deleteUser" />
<result column="delete_time" jdbcType="TIMESTAMP" property="deleteTime" />
<result column="del_flag" jdbcType="VARCHAR" property="delFlag" />
</resultMap>
<sql id="Base_Column_List">
id,
user_name,
real_name,
password,
salt ,
dept_id,
email,
mobile,
ID_card_number,
sex,
pictrue,
status,
city_id ,
hobby,
remarks,
create_user,
create_time,
update_user,
update_time,
delete_user,
delete_time,
del_flag
</sql>
<select id="selectById" parameterType="java.lang.String" resultMap="BaseResultMap">
select
<include refid="Base_Column_List" />
from user
where id = #{id,jdbcType=VARCHAR} AND del_flag = '0'
</select>
<select id="selectByParams" resultType="com.byk.rong.system.entity.User">
select
<include refid="Base_Column_List" />
from user
<where>
<if test="id != null and id != ''">and id = #{id}</if>
<if test="userName != null and userName != ''">and user_name = #{userName}</if>
<if test="realName != null and realName != ''">and real_name = #{realName}</if>
<if test="password != null and password != ''">and password = #{password}</if>
<if test="salt != null and salt != ''">and salt = #{salt}</if>
<if test="deptId != null and deptId != ''">and dept_id = #{deptId}</if>
<if test="email != null and email != ''">and email = #{email}</if>
<if test="mobile != null and mobile != ''">and mobile = #{mobile}</if>
<if test="idCardNumber != null and idCardNumber != ''">and ID_card_number = #{idCardNumber}</if>
<if test="sex != null and sex != ''">and sex = #{sex}</if>
<if test="pictrue != null and pictrue != ''">and pictrue = #{pictrue}</if>
<if test="status != null and status != ''">and status = #{status}</if>
<if test="cityId != null and cityId != ''">and city_id = #{cityId}</if>
<if test="hobby != null and hobby != ''">and hobby = #{hobby}</if>
<if test="remarks != null and remarks != ''">and remarks = #{remarks}</if>
<if test="createUser != null and createUser != ''">and create_user = #{createUser}</if>
<if test="createTime != null and createTime != ''">and create_time = #{createTime}</if>
<if test="updateUser != null and updateUser != ''">and update_user = #{updateUser}</if>
<if test="updateTime != null and updateTime != ''">and update_time = #{updateTime}</if>
<if test="deleteUser != null and deleteUser != ''">and delete_user = #{deleteUser}</if>
<if test="deleteTime != null and deleteTime != ''">and delete_time = #{deleteTime}</if>
and del_flag = '0'
</where>
order by create_time desc
<if test="offset != null and limit != null">
limit #{offset}, #{limit}
</if>
</select>
<select id="findByUsername" parameterType="java.lang.String" resultMap="BaseResultMap">
select
<include refid="Base_Column_List" />
from user
where user_name = #{userName,jdbcType=VARCHAR} AND del_flag = '0'
</select>
</mapper>
然而,测试的时候发现实体类里面的属性映射不完整,一些字段明明有值,但是输出的实体类里面相对应的属性值为null,如下:
对比了一下,问题都出现在带有“_”的字段,也就是使用到驼峰命名的属性上。
查找资料,发现了问题所在。
二、解决办法
第一种解决办法
<select id="selectByParams" resultType="com.byk.rong.system.entity.User">
这个方法是我自己写的,使用了resultType,直接映射到实体类,没有通过mabatis提供的 BaseResultMap,因此映射失败,将这里的resultType改成 resultMap="BaseResultMap"即可,使查询到的数据通过mabatis的内部实现完整的映射到实体类。
第二种解决办法
修改sql语句,即
<sql id="Base_Column_List">
id AS id,
user_name AS userName,
real_name AS realName,
password as password,
salt as salt,
dept_id AS deptId,
email as email,
mobile as mobile,
ID_card_number as idCardNumber,
sex as sex,
pictrue as pictrue,
status as status,
city_id as cityId,
hobby as hobby,
remarks as remarks,
create_user as createUser,
create_time as createTime,
update_user as updateUser,
update_time as updateTime,
delete_user as deleteUser,
delete_time as deleteTime,
del_flag AS delFlag
</sql>
这样,查询到的字段数据直接映射成属性,也能解决这个问题。
效果如下:
注意:
这两种办法只能二选一,不能同时使用。