深入淺出MyBatis中對應檔和實體類的關聯性_java

來源:互聯網
上載者: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; // 最後登陸IPprivate Date loginDate; // 最後登陸日期private String loginFlag; // 是否允許登陸private String photo; // 頭像private String qrCode; // 二維碼private String oldLoginName;// 原登入名稱private String newPassword; // 新密碼private String oldLoginIp; // 上次登陸IPprivate 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_idLEFT JOIN sys_area ca ON ca.id = c.area_idLEFT JOIN sys_office o ON o.id = a.office_idLEFT JOIN sys_area oa ON oa.id = o.area_idLEFT JOIN sys_user cu ON cu.id = c.primary_personLEFT JOIN sys_user cu2 ON cu2.id = c.deputy_personLEFT JOIN sys_user ou ON ou.id = o.primary_personLEFT JOIN sys_user ou2 ON ou2.id = o.deputy_personLEFT 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>

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.