mybatis中映射文件和实体类的关联性
Posted 代码书生
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了mybatis中映射文件和实体类的关联性相关的知识,希望对你有一定的参考价值。
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>
整体就是这样,如果后续有什么补充,我会在之后的章节增加;如果有错误欢迎指出并修改。
以上是关于mybatis中映射文件和实体类的关联性的主要内容,如果未能解决你的问题,请参考以下文章