程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> JAVA編程 >> 關於JAVA >> 深刻淺出MyBatis中映照文件和實體類的聯系關系性

深刻淺出MyBatis中映照文件和實體類的聯系關系性

編輯:關於JAVA

深刻淺出MyBatis中映照文件和實體類的聯系關系性。本站提示廣大學習愛好者:(深刻淺出MyBatis中映照文件和實體類的聯系關系性)文章只能為提供參考,不一定能成為您想要的結果。以下是深刻淺出MyBatis中映照文件和實體類的聯系關系性正文


mybatis的映照文件寫法多種多樣,分歧的寫法和用法,在現實開辟進程中所消費的開辟時光、保護時光有很年夜差異,明天我就把我以為比擬簡略的一種映照文件寫法記載上去,供年夜家修正建議,爭奪找到一個最優寫法~~:

以User對象和UserMap.xml為例講授,代碼以下:

User為用戶實體類(僅作為講授,可以只存眷援用類型變量,get/set辦法省略):

import com.谷歌.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>

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved