Mybatis' mapping file writing methods are diverse, and different writing methods and usages have a great difference in the development time and maintenance time consumed during the actual development process. Today I will record a relatively simple mapping file writing method for everyone to modify and recommend, and try to find the best writing method~~:
Take the User object and UserMap.xml as an example to explain, the code is as follows:
User is a user entity class (for explanation only, you can only focus on reference type variables, and the get/set method is omitted):
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;/*** User 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 office; // private String loginName;// login name private String password;// password private String no; // work number private String name; // name private String email; // email private String phone; // phone private String mobile; // mobile private String userType;// user type private String loginIp; // last login IPprivate Date loginDate; // Last login date private String loginFlag; // Whether to allow login to private String photo; // Avatar private String qrCode; // QR code private String oldLoginName;// Original login name private String newPassword; // New password private String oldLoginIp; // Last login IPprivate Date oldLoginDate; // Last login date private Dept dept; // Department private Staff staff; // Position private Role role; // Query user conditions based on role private List<Role> roleList = Lists.newArrayList(); // Owner role list private School school; // Attributor private String remarks; // Notes private User createBy; // Creator private Date createDate; // Creation date private User updateBy; // Updater private Date updateDate; // Updated date private String delFlag; // Delete mark (0: normal; 1: delete; 2: review)}For member variables of reference types, in order to directly assign values during the query process, the query results can be assigned to the returned result set in the mapping file:
<?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"> <!-- The key point is this section, directly assigning the id field saved by the database to an object's member variable, such as a.company_id AS "company.id", at this time, the properties in the List or User object queryed back in the background can be obtained directly through user.getCompany().getId() --> <!-- Of course, the reference type variables in Company can also be assigned using this method. Multiple tables can be checked through LEFT JOIN. This is an operation in database query. This is not discussed here-><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",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><!-- Query statement, according to the Id query result, the return type can be written directly by User, while different configuration resultMap omits the time to write the xml --><select id="getUserById" resultType="User">SELECT<include refid="userColumns"/>FROM sys_user a<include refid="userJoins"/>WHERE a.id = #{id}</select><!-- Query statement, query based on the User object, the parameters here are the User variable--><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><!-- Insert statement, the parameters must be User object--><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}, #{updateDate}, #{remarks}, #{loginFlag}, #{photo}, #{qrCode},#{delFlag},#{dept.id},#{staff.id},#{school.id})</insert><!-- Update statement, the parameters are also User objects--><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><!-- Physical deletion of user--><update id="delete">DELETE FROM sys_user WHERE id = #{id}</update><!-- Logical deletion of user--><update id="deleteByLogic">UPDATE sys_user SET del_flag = #{DEL_FLAG_DELETE}WHERE id = #{id}</update></mapper>