mybatis的映射文件寫法多種多樣,不同的寫法和用法,在實際開發過程中所消耗的開發時間、維護時間有很大差別,今天我就把我認為比較簡單的一種映射文件寫法記錄下來,供大家修改建議,爭取找到一個最優寫法~~: 以User對象和UserMap.xml為例講解,代碼如下: User為用戶實體類(僅作為講解, ...
mybatis的映射文件寫法多種多樣,不同的寫法和用法,在實際開發過程中所消耗的開發時間、維護時間有很大差別,今天我就把我認為比較簡單的一種映射文件寫法記錄下來,供大家修改建議,爭取找到一個最優寫法~~:
以User對象和UserMap.xml為例講解,代碼如下:
User為用戶實體類(僅作為講解,可以只關註引用類型變數,get/set方法省略):
import com.google.common.collect.Lists; import com.gukeer.common.persistence.DataEntity; import com.gukeer.modules.personal.entity.Dept; import com.gukeer.modules.personal.entity.Staff; import com.gukeer.modules.school.entity.School; import java.util.Date; /** * 用戶Entity * * auther:cc * date:2016/9/2 */ public class User extends DataEntity<User> { private static final long serialVersionUID = 1L; private String id; private Office company; // 歸屬公司 private Office office; // 歸屬部門 private String loginName;// 登錄名 private String password;// 密碼 private String no; // 工號 private String name; // 姓名 private String email; // 郵箱 private String phone; // 電話 private String mobile; // 手機 private String userType;// 用戶類型 private String loginIp; // 最後登陸IP private Date loginDate; // 最後登陸日期 private String loginFlag; // 是否允許登陸 private String photo; // 頭像 private String qrCode; // 二維碼 private String oldLoginName;// 原登錄名 private String newPassword; // 新密碼 private String oldLoginIp; // 上次登陸IP private Date oldLoginDate; // 上次登陸日期 private Dept dept; //部門 private Staff staff; //職位 private Role role; // 根據角色查詢用戶條件 private List<Role> roleList = Lists.newArrayList(); // 擁有角色列表 private School school; //歸屬學校 private String remarks; // 備註 private User createBy; // 創建者 private Date createDate; // 創建日期 private User updateBy; // 更新者 private Date updateDate; // 更新日期 private String delFlag; // 刪除標記(0:正常;1:刪除;2:審核) }
針對引用類型的成員變數,為了可以在查詢過程中直接賦值,在映射文件中可以直接將查詢結果賦值給返回的結果集:
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <mapper namespace="com.gk.modules.sys.dao.UserDao">
<!-- 重點就是這一段,將資料庫保存的id欄位直接賦值給一個對象的成員變數中,比如a.company_id AS "company.id",此時後臺查詢回的List或User對象中的屬性可以直接通過user.getCompany().getId()來獲取到-->
<!-- 當然Company中的引用類型變數也可以使用這種方法來賦值,通過LEFT JOIN可以聯查多表,這是資料庫查詢方面的操作,這裡不作討論 --> <sql id="userColumns"> a.id, a.company_id AS "company.id", a.office_id AS "office.id", a.login_name, a.password, a.no, a.name, a.email, a.phone, a.mobile, a.user_type, a.login_ip, a.login_date, a.remarks, a.login_flag, a.photo, a.qrcode, a.create_by AS "createBy.id", a.create_date, a.update_by AS "updateBy.id", a.update_date, a.del_flag, c.name AS "company.name", c.parent_id AS "company.parent.id", c.parent_ids AS "company.parentIds", ca.id AS "company.area.id", ca.name AS "company.area.name", ca.parent_id AS "company.area.parent.id", ca.parent_ids AS "company.area.parentIds", o.name AS "office.name", o.parent_id AS "office.parent.id", o.parent_ids AS "office.parentIds", oa.id AS "office.area.id", oa.name AS "office.area.name", oa.parent_id AS "office.area.parent.id", oa.parent_ids AS "office.area.parentIds", cu.id AS "company.primaryPerson.id", cu.name AS "company.primaryPerson.name", cu2.id AS "company.deputyPerson.id", cu2.name AS "company.deputyPerson.name", ou.id AS "office.primaryPerson.id", ou.name AS "office.primaryPerson.name", ou2.id AS "office.deputyPerson.id", ou2.name AS "office.deputyPerson.name", sc.xxlx AS "school.xxlx", sc.xxmc AS "school.xxmc" </sql> <sql id="userJoins"> LEFT JOIN sys_office c ON c.id = a.company_id LEFT JOIN sys_area ca ON ca.id = c.area_id LEFT JOIN sys_office o ON o.id = a.office_id LEFT JOIN sys_area oa ON oa.id = o.area_id LEFT JOIN sys_user cu ON cu.id = c.primary_person LEFT JOIN sys_user cu2 ON cu2.id = c.deputy_person LEFT JOIN sys_user ou ON ou.id = o.primary_person LEFT JOIN sys_user ou2 ON ou2.id = o.deputy_person LEFT JOIN xj_school sc ON sc.id = a.school </sql> <!-- 查詢語句,根據Id查詢結果,返回類型可以直接寫User,而不同配置resultMap省略編寫xml的時間 --> <select id="getUserById" resultType="User"> SELECT <include refid="userColumns"/> FROM sys_user a <include refid="userJoins"/> WHERE a.id = #{id} </select> <!-- 查詢語句,根據User對象來查詢,這裡的參數即為User變數 --> <select id="getByLoginName" resultType="User" parameterType="User"> SELECT <include refid="userColumns"/> FROM sys_user a <include refid="userJoins"/> WHERE
a.login_name = #{loginName}
AND a.del_flag = #{DEL_FLAG_NORMAL} </select> <!-- 插入語句,參數肯定是User對象 --> <insert id="insert"> INSERT INTO sys_user( id, company_id, office_id, login_name, password, no, name, email, phone, mobile, user_type, create_by, create_date, update_by, update_date, remarks, login_flag, photo, qrcode, del_flag, dept_id, staff_id, school ) VALUES ( #{id}, #{company.id}, #{office.id}, #{loginName}, #{password}, #{no}, #{name}, #{email}, #{phone}, #{mobile}, #{userType}, #{createBy.id}, #{createDate}, #{updateBy.id}, #{updateDate}, #{remarks}, #{loginFlag}, #{photo}, #{qrCode}, #{delFlag}, #{dept.id}, #{staff.id}, #{school.id} ) </insert> <!-- 更新語句,參數也是User對象 --> <update id="update"> UPDATE sys_user SET company_id = #{company.id}, office_id = #{office.id}, login_name = #{loginName}, password = #{password}, no = #{no}, name = #{name}, email = #{email}, phone = #{phone}, mobile = #{mobile}, user_type = #{userType}, update_by = #{updateBy.id}, update_date = #{updateDate}, remarks = #{remarks}, login_flag = #{loginFlag}, photo = #{photo}, qrcode = #{qrCode}, school = #{school.id} WHERE id = #{id} </update> <!-- 物理刪除用戶 --> <update id="delete"> DELETE FROM sys_user WHERE id = #{id} </update> <!-- 邏輯刪除用戶 --> <update id="deleteByLogic"> UPDATE sys_user SET del_flag = #{DEL_FLAG_DELETE} WHERE id = #{id} </update>
</mapper>
整體就是這樣,如果後續有什麼補充,我會在之後的章節增加;如果有錯誤歡迎指出並修改。