深入浅出MyBatis中映射文件和实体类的关联性

时间:2021-05-19

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>

声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。

相关文章