欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

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

程序员文章站 2024-03-13 19:49:45
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>