mybatis 對映實體類不完整的一些解決問題
阿新 • • 發佈:2018-12-12
一,基本環境
自己寫個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"
第二種解決辦法
修改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>
這樣,查詢到的欄位資料直接對映成屬性,也能解決這個問題。
效果如下:
注意:
這兩種辦法只能二選一,不能同時使用。