设置Mybatis(3.2.8)实体嵌套关系(一对多,多对多)遇到的问题及经验总结记录...
2019独角兽企业重金招聘Python工程师标准>>>
原始目标:
深读了mybatis 官方的文章,最后总结出一最重要的的一条,一定要好好利用官方 的autoMapping 特性,否则就得一条条写映射关系了。当然对于实体的嵌套填充, 我只做一层,再往下走就需要在程序逻辑上做一些处理
这里配置的逻辑只与表在逻辑上的连接相关,是否在数据中实现关系无关。
这里的column我们只需要考虑不包括前缀的
- INNER JOIN: Returns all rows when there is at least one match in BOTH tables
- LEFT JOIN: Return all rows from the left table, and the matched rows from the right table
- RIGHT JOIN: Return all rows from the right table, and the matched rows from the left table
- FULL JOIN: Return all rows when there is a match in ONE of the tables
2.此类别名方式下
现在来看看这个mapper的映射配置如何编写,注意示例中的以下几点:
1、constructor 实体的构造方法
2、autoMapping 自动属性映射
3、collection 集合属性的映射
4、association 关联属性的映射
5、mapUnderscoreToCamelCase 是否开启自动驼峰命名规则,全局配置.
实例一,使用下划线:
PsProjectDao.xml
<?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.thinkgem.jeesite.modules.projectschedule.dao.PsProjectDao"><!-- <typeAlias type="com.someapp.model.User" alias="User"/> --><!-- begin result map area -->
<resultMap id="psProjectResultMap" type="com.thinkgem.jeesite.modules.projectschedule.entity.PsProject" autoMapping ="true"><id property="id" column="id" /><!-- <result property="title" column="blog_title"/>--><association property="customer" column="customerid" resultMap="psCustomerResultMap" columnPrefix="customer_"/><association property="department" column="departmentid" resultMap="departmentResultMap" columnPrefix="department_"/><association property="projectManager" column="projectManagerid" resultMap="userResultMap" columnPrefix="projectManager_"/><association property="marketer" column="marketerid" resultMap="userResultMap" columnPrefix="marketer_"/><collection property="push" column="push" ofType="PsProjectPush" resultMap="psProjectPushResultMap" columnPrefix="push_"/>
</resultMap>
<resultMap id="userResultMap" type="com.thinkgem.jeesite.modules.sys.entity.User" autoMapping ="true"><id property="id" column="id"/>
</resultMap>
<resultMap id="departmentResultMap" type="com.thinkgem.jeesite.modules.sys.entity.Office" autoMapping ="true" ><id property="id" column="id"/>
</resultMap>
<resultMap id="psCustomerResultMap" type="com.thinkgem.jeesite.modules.projectschedule.entity.PsCustomer" autoMapping ="true"><id property="id" column="id"/>
</resultMap>
<resultMap id="psProjectPushResultMap" type="com.thinkgem.jeesite.modules.projectschedule.entity.PsProjectPush" autoMapping ="true"><id property="id" column="id"/>
</resultMap>
<!-- end resultmap area -->
<!-- begin sql columns area --><sql id="psProjectColumns">a.id AS "id",a.code AS "code",a.name AS "name",a.type AS "type",a.importance_degree AS "importanceDegree",a.tech_state AS "techState",a.customerid AS "customerid",a.departmentid AS "departmentid",a.project_managerid AS "projectManagerid",a.plan_starttime AS "planStarttime",a.plan_endtime AS "planEndtime",a.state AS "state",a.act_starttime AS "actStarttime",a.act_endtime AS "actEndtime",a.sending_time AS "sendingTime",a.old_plan_starttime AS "oldPlanStarttime",a.old_plan_endtime AS "oldPlanEndtime",a.delivery_time AS "deliveryTime",a.complete_status AS "completeStatus",a.marketerid AS "marketerid",a.create_by AS "createBy.id",a.create_date AS "createDate",a.update_by AS "updateBy.id",a.update_date AS "updateDate",a.remarks AS "remarks",a.del_flag AS "delFlag"</sql><sql id="departmentColumns">d.id as "department_id",d.name as "department_name"</sql><sql id="projectManagerColumns">pm.id as "projectManager_id",pm.name as "projectManager_name"</sql><sql id="marketerColumns">m.id as "marketer_id",m.name as "marketer_name"</sql><sql id="psCustomerColumns">c.id as "customer_id",c.code as "customer_code",c.name as "customer_name"</sql><sql id="psProjectPushColumns">ps.id as "push_id",ps.projectid AS "push_project.id",ps.userid AS "push_user.id",ps.is_readed AS "push_isReaded"</sql><!-- end sql columns area --><sql id="psProjectJoins">LEFT JOIN ps_customer c ON c.id = a.customerid LEFT JOIN sys_office d ON d.id = a.departmentid LEFT JOIN sys_user pm ON pm.id = a.project_managerid LEFT JOIN sys_user m ON m.id = a.marketeridLEFT JOIN ps_project_push ps ON ps.projectid=a.id</sql><select id="findAllList" resultMap="psProjectResultMap">SELECT <include refid="psProjectColumns"/>,<include refid="departmentColumns"/>,<include refid="projectManagerColumns"/>,<include refid="marketerColumns"/>,<include refid="psCustomerColumns"/>,<include refid="psProjectPushColumns"/>FROM ps_project a <include refid="psProjectJoins"/><where>a.del_flag = #{DEL_FLAG_NORMAL}</where> <choose><when test="page !=null and page.orderBy != null and page.orderBy != ''">ORDER BY ${page.orderBy}</when><otherwise>ORDER BY a.update_date DESC</otherwise></choose></select><insert id="insert">INSERT INTO ps_project(id,code,name,type,importance_degree,tech_state,customerid,departmentid,managerid,plan_starttime,plan_endtime,state,act_starttime,act_endtime,sending_time,old_plan_starttime,old_plan_endtime,delivery_time,complete_status,marketerid,create_by,create_date,update_by,update_date,remarks,del_flag) VALUES (#{id},#{code},#{name},#{type},#{importanceDegree},#{techState},#{customer.id},#{department.id},#{projectManager.id},#{planStarttime},#{planEndtime},#{state},#{actStarttime},#{actEndtime},#{sendingTime},#{oldPlanStarttime},#{oldPlanEndtime},#{deliveryTime},#{completeStatus},#{marketer.id},#{createBy.id},#{createDate},#{updateBy.id},#{updateDate},#{remarks},#{delFlag})</insert><update id="update">UPDATE ps_project SET code = #{code},name = #{name},type = #{type},importance_degree = #{importanceDegree},tech_state = #{techState},customerid = #{customer.id},departmentid = #{department.id},managerid = #{projectManager.id},plan_starttime = #{planStarttime},plan_endtime = #{planEndtime},state = #{state},act_starttime = #{actStarttime},act_endtime = #{actEndtime},sending_time = #{sendingTime},old_plan_starttime = #{oldPlanStarttime},old_plan_endtime = #{oldPlanEndtime},delivery_time = #{deliveryTime},complete_status = #{completeStatus},marketerid = #{marketer.id},update_by = #{updateBy.id},update_date = #{updateDate},remarks = #{remarks}WHERE id = #{id}</update><update id="delete">UPDATE ps_project SET del_flag = #{DEL_FLAG_DELETE}WHERE id = #{id}</update></mapper>
实例二:使用.做为前缀
<?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.thinkgem.jeesite.modules.projectschedule.dao.PsProjectDao"><!-- <typeAlias type="com.someapp.model.User" alias="User"/> --><!-- begin result map area --><resultMap id="psProjectResultMap"type="com.thinkgem.jeesite.modules.projectschedule.entity.PsProject"autoMapping="true"><id property="id" column="id" /><!-- <result property="title" column="blog_title"/> --><association property="customer" column="customerid"resultMap="psCustomerResultMap" columnPrefix="customer." /><association property="department" column="departmentid"resultMap="departmentResultMap" columnPrefix="department." /><association property="projectManager" column="projectManagerid"resultMap="userResultMap" columnPrefix="projectManager." /><association property="marketer" column="marketerid"resultMap="userResultMap" columnPrefix="marketer." /><collection property="push" column="push" ofType="PsProjectPush"resultMap="psProjectPushResultMap" columnPrefix="push." /></resultMap><resultMap id="userResultMap" type="com.thinkgem.jeesite.modules.sys.entity.User"autoMapping="true"><id property="id" column="id" /></resultMap><resultMap id="departmentResultMap"type="com.thinkgem.jeesite.modules.sys.entity.Office" autoMapping="true"><id property="id" column="id" /></resultMap><resultMap id="psCustomerResultMap"type="com.thinkgem.jeesite.modules.projectschedule.entity.PsCustomer"autoMapping="true"><id property="id" column="id" /></resultMap><resultMap id="psProjectPushResultMap"type="com.thinkgem.jeesite.modules.projectschedule.entity.PsProjectPush"autoMapping="true"><id property="id" column="id" /></resultMap><!-- end resultmap area --><!-- begin sql columns area --><sql id="psProjectColumns">a.id AS "id",a.code AS "code",a.name AS "name",a.type AS"type",a.importance_degree AS "importanceDegree",a.tech_state AS"techState",a.customerid AS "customerid",a.departmentid AS"departmentid",a.project_managerid AS "projectManagerid",a.plan_starttime AS "planStarttime",a.plan_endtime AS "planEndtime",a.state AS "state",a.act_starttime AS "actStarttime",a.act_endtime AS"actEndtime",a.sending_time AS "sendingTime",a.old_plan_starttime AS"oldPlanStarttime",a.old_plan_endtime AS "oldPlanEndtime",a.delivery_time AS "deliveryTime",a.complete_status AS"completeStatus",a.marketerid AS "marketerid",a.create_by AS"createBy.id",a.create_date AS "createDate.id",a.update_by AS"updateBy.id",a.update_date AS "updateDate.id",a.remarks AS "remarks",a.del_flag AS "delFlag"</sql><sql id="departmentColumns">department.id as "department.id",department.name as "department.name"</sql><sql id="projectManagerColumns">projectManager.id as "projectManager.id",projectManager.name as "projectManager.name"</sql><sql id="marketerColumns">marketer.id as "marketer.id",marketer.name as "marketer.name"</sql><sql id="psCustomerColumns">customer.id as "customer.id",customer.code as"customer.code",customer.name as "customer.name"</sql><sql id="psProjectPushColumns">push.id as "push.id",push.projectid AS "push.project.id",push.userid AS "push.user.id",push.is_readed AS "push.isReaded"</sql><!-- end sql columns area --><sql id="psProjectJoins">LEFT JOIN ps_customer customer ON customer.id =a.customeridLEFT JOIN sys_office department ON department.id = a.departmentidLEFT JOIN sys_user projectManager ON projectManager.id =a.project_manageridLEFT JOIN sys_user marketer ON marketer.id = a.marketeridLEFT JOIN ps_project_push push ON push.projectid=a.id</sql><select id="findAllList" resultMap="psProjectResultMap">SELECT<include refid="psProjectColumns" />,<include refid="departmentColumns" />,<include refid="projectManagerColumns" />,<include refid="marketerColumns" />,<include refid="psCustomerColumns" />,<include refid="psProjectPushColumns" />FROM ps_project a<include refid="psProjectJoins" /><where>a.del_flag = #{DEL_FLAG_NORMAL}</where><choose><when test="page !=null and page.orderBy != null and page.orderBy != ''">ORDER BY ${page.orderBy}</when><otherwise>ORDER BY a.update_date DESC</otherwise></choose></select><insert id="insert">INSERT INTO ps_project(id,code,name,type,importance_degree,tech_state,customerid,departmentid,managerid,plan_starttime,plan_endtime,state,act_starttime,act_endtime,sending_time,old_plan_starttime,old_plan_endtime,delivery_time,complete_status,marketerid,create_by,create_date,update_by,update_date,remarks,del_flag) VALUES (#{id},#{code},#{name},#{type},#{importanceDegree},#{techState},#{customer.id},#{department.id},#{projectManager.id},#{planStarttime},#{planEndtime},#{state},#{actStarttime},#{actEndtime},#{sendingTime},#{oldPlanStarttime},#{oldPlanEndtime},#{deliveryTime},#{completeStatus},#{marketer.id},#{createBy.id},#{createDate},#{updateBy.id},#{updateDate},#{remarks},#{delFlag})</insert><update id="update">UPDATE ps_project SETcode = #{code},name = #{name},type = #{type},importance_degree = #{importanceDegree},tech_state = #{techState},customerid = #{customer.id},departmentid = #{department.id},managerid = #{projectManager.id},plan_starttime = #{planStarttime},plan_endtime = #{planEndtime},state = #{state},act_starttime = #{actStarttime},act_endtime = #{actEndtime},sending_time = #{sendingTime},old_plan_starttime = #{oldPlanStarttime},old_plan_endtime = #{oldPlanEndtime},delivery_time = #{deliveryTime},complete_status = #{completeStatus},marketerid = #{marketer.id},update_by = #{updateBy.id},update_date = #{updateDate},remarks = #{remarks}WHERE id = #{id}</update><update id="delete">UPDATE ps_project SETdel_flag = #{DEL_FLAG_DELETE}WHERE id = #{id}</update></mapper>
实体文件(PsProject.java)
/*** 项目管理Entity* @author xiaohelong* @version 2016-01-06*/
public class PsProject extends DataEntity<PsProject> {private static final long serialVersionUID = 1L;private String code; // 项目编号private String name; // 项目名称private String type; // 项目类型(科研项目,还是交付)private String importanceDegree; // 重要程度(一般,重要,紧急)private String techState; // 技术状态(完全沿用,设计更改,全新设计)private PsCustomer customer; // 客户单位private Office department; // 所属部门private User projectManager; // 项目经理private Date planStarttime; // 计划开始时间private Date planEndtime; // 计划结束时间private String state; // 项目当前状态(等待开始,正常进行,交付延期,节点延期,项目暂停,项目终止,项目结束)private Date actStarttime; // 实际开始时间private Date actEndtime; // 实际结束时间private Date sendingTime; // 项目下发时间private Date oldPlanStarttime; // 计划开始时间(只读字段,初始化后不更改)private Date oldPlanEndtime; // 计划结束时间(只读字段,初始化后不更改)private Date deliveryTime; // 交付时间private String completeStatus; // 完成情况(正常完成,延期完成,未完成)private User marketer; // 市场人员IDprivate List<PsProjectPush> push; //相关领导数据表中并没有,放在这里便于表单操作,因为该字段数据送到另外一个表中了。public PsProject() {super();}public PsProject(String id){super(id);}public String getCode() {return code;}public void setCode(String code) {this.code = code;}public String getName() {return name;}public void setName(String name) {this.name = name;}public String getType() {return type;}public void setType(String type) {this.type = type;}public String getImportanceDegree() {return importanceDegree;}public void setImportanceDegree(String importanceDegree) {this.importanceDegree = importanceDegree;}public String getTechState() {return techState;}public void setTechState(String techState) {this.techState = techState;}public PsCustomer getCustomer() {return customer;}public void setCustomer(PsCustomer customer) {this.customer = customer;}public Office getDepartment() {return department;}public void setDepartment(Office department) {this.department = department;}public User getProjectManager() {return projectManager;}public void setProjectManager(User projectManager) {this.projectManager = projectManager;}public Date getPlanStarttime() {return planStarttime;}public void setPlanStarttime(Date planStarttime) {this.planStarttime = planStarttime;}public Date getPlanEndtime() {return planEndtime;}public void setPlanEndtime(Date planEndtime) {this.planEndtime = planEndtime;}public String getState() {return state;}public void setState(String state) {this.state = state;}public Date getActStarttime() {return actStarttime;}public void setActStarttime(Date actStarttime) {this.actStarttime = actStarttime;}public Date getActEndtime() {return actEndtime;}public void setActEndtime(Date actEndtime) {this.actEndtime = actEndtime;}public Date getSendingTime() {return sendingTime;}public void setSendingTime(Date sendingTime) {this.sendingTime = sendingTime;}public Date getOldPlanStarttime() {return oldPlanStarttime;}public void setOldPlanStarttime(Date oldPlanStarttime) {this.oldPlanStarttime = oldPlanStarttime;}public Date getOldPlanEndtime() {return oldPlanEndtime;}public void setOldPlanEndtime(Date oldPlanEndtime) {this.oldPlanEndtime = oldPlanEndtime;}public Date getDeliveryTime() {return deliveryTime;}public void setDeliveryTime(Date deliveryTime) {this.deliveryTime = deliveryTime;}public String getCompleteStatus() {return completeStatus;}public void setCompleteStatus(String completeStatus) {this.completeStatus = completeStatus;}public User getMarketer() {return marketer;}public void setMarketer(User marketer) {this.marketer = marketer;}public List<PsProjectPush> getPush() {return push;}public void setPush(List<PsProjectPush> push) {this.push = push;}}
另附直接用别名简单映射方式(无LIST collection)--JeeSite默认的方式,没有继续研究其List是如何实现的,因其并没有在XML中体现出来,所以个人觉得很可能是在JAVA SERVICE程进行了体现。
<?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.thinkgem.jeesite.modules.sys.dao.OfficeDao"><sql id="officeColumns">a.id,a.parent_id AS "parent.id",a.parent_ids,a.area_id AS "area.id",a.code,a.name,a.sort,a.type,a.grade,a.address, a.zip_code, a.master, a.phone, a.fax, a.email, a.remarks,a.create_by AS "createBy.id",a.create_date,a.update_by AS "updateBy.id",a.update_date,a.del_flag,a.useable AS useable,a.primary_person AS "primaryPerson.id",a.deputy_person AS "deputyPerson.id",p.name AS "parent.name",ar.name AS "area.name",ar.parent_ids AS "area.parentIds",pp.name AS "primaryPerson.name",dp.name AS "deputyPerson.name"</sql><sql id="officeJoins">LEFT JOIN sys_office p ON p.id = a.parent_idLEFT JOIN sys_area ar ON ar.id = a.area_idLEFT JOIN SYS_USER pp ON pp.id = a.primary_personLEFT JOIN SYS_USER dp ON dp.id = a.deputy_person</sql> <select id="findAllList" resultType="Office">SELECT<include refid="officeColumns"/>FROM sys_office a<include refid="officeJoins"/>WHERE a.del_flag = #{DEL_FLAG_NORMAL}ORDER BY a.code</select>
</mapper>
参考资料:
1. http://www.mybatis.org/mybatis-3/sqlmap-xml.html#Auto-mapping
2.http://leeyee.github.io/blog/2013/05/30/mybatis-association-autoMapping/
转载于:https://my.oschina.net/xiaohelong/blog/602167
设置Mybatis(3.2.8)实体嵌套关系(一对多,多对多)遇到的问题及经验总结记录...相关推荐
- sql语句之多表关系 一对多 多对多
多表的关系介绍 在实际的开发过程中,项目一定是有多张表的,且这些表之间是都有关系的 表与表之间的关系分类:一对一 一对多 多对多 一对一:A表的一行对应B表的一行,反之也成立,此时,两张表可以合并成一 ...
- 设置Mybatis(3.2.8)实体嵌套关系(一对多,多对多)遇到的问题及经验总结记录
原始目标: 配置好mapper,使得可以在实体中表示表之间的联系(一个表外键,用另一个实体表示) 深读了mybatis 官方的文章,最后总结出一最重要的的一条,一定要好好利用官方 的autoMappi ...
- 关系重叠?实体嵌套?曝光偏差?这个模型统统都搞得定!
©PaperWeekly · 作者|王雨城 学校|中国科学院信息工程研究所硕士 研究方向|自然语言处理 本文提出了一种新的实体关系联合抽取标注方案,可在一个模型中实现真正意义上的单阶段联合抽取,不存在 ...
- Mybatis中的关系映射(一对一,一对多,多对多)
在网上寻了很久,大多数讲关系性的文章都是大篇幅的去将表照搬上来,本来就很生硬,此文就不在讲述关系性映射的具体实现,转而从浅层来讲讲其概念性. 1.1 关联关系概述 在关系型数据库中,多表之间存在着三种 ...
- 用于实体和关系抽取的封装式悬空标记
Packed Levitated Marker for Entity and Relation Extraction 用于实体和关系抽取的封装式悬空标记 1. Abstract 最近的实体和关系提取工 ...
- 经典论文复现 | 基于标注策略的实体和关系联合抽取
过去几年发表于各大 AI 顶会论文提出的 400 多种算法中,公开算法代码的仅占 6%,其中三分之一的论文作者分享了测试数据,约 54% 的分享包含"伪代码".这是今年 AAAI ...
- ACL 2018论文解读 | 基于路径的实体图关系抽取模型
在碎片化阅读充斥眼球的时代,越来越少的人会去关注每篇论文背后的探索和思考. 在这个栏目里,你会快速 get 每篇精选论文的亮点和痛点,时刻紧跟 AI 前沿成果. 点击本文底部的「阅读原文」即刻加入社区 ...
- 论文浅尝 | 基于知识库的类型实体和关系的联合抽取
链接:http://hanj.cs.illinois.edu/pdf/www17_xren.pdf GitHub项目地址:https://github.com/INK-USC/DS-RelationE ...
- 论文浅尝 | 多内容实体和关系联合抽取的对抗训练
链接:https://arxiv.org/pdf/1808.06876.pdf 动机 Szegedy 在 14 年发现,对于图像分类问题的神经网络,若是在输入中人为的添加很小尺度的扰动,会让已经训练好 ...
最新文章
- @apioperation 作用_石莲子的功效与作用分享
- __GLOBAL__I_a in libUPPayPlugin.a(UPPasswordTool.o)
- 代友招中高级.NET开发工程师【上海-徐汇】
- ▽算符在球坐标系_球坐标系下的角动量算符
- C篇(一) 用Codeblocks建立一个C的工程
- 深入Go的底层,带你走近一群有追求的人
- Linq 中 Any与All
- jQuery遍历not的用法
- 小b和排序(51Nod-2484)
- linux将mysql中得配置为可读写_MySQL注入 利用系统读、写文件
- 简单的for循环,你不会就落后了
- 什么舱位_海运出口中FOB条款是什么意思?
- UniWebView3 使用中遇到的坑
- usb声卡驱动(五):声卡驱动的开始
- java pdf转png格式_如何在Java中将PDF转换为PNG或JPG
- 超级计算机计算峰值,世界运算最快计算机,中国神威·太湖之光(其峰值计算速度达每秒1...
- 交易心得(简要总结)
- 如何用AI伴侣同步测试App Inventor软件
- ntpdate离线安装
- 计算机带不起大型游戏怎么回事,电脑玩不起来大型网络游戏怎么办
热门文章
- (0008) iOS 开发之iTunes Connect 显示可供销售,AppStore 不显示新版本
- page fault in nonpaged area 蓝屏_记一次蓝屏0x00000050
- Expanded, SingleChildScrollView, CustomScrollView, container, height, width
- HUST 1588 辗转数对
- d3.js--04(enter和exit)
- JavaScript 函数声明,函数表达式,匿名函数,立即执行函数之区别
- 深入理解c#(第三版)(文摘)
- 构建之法 第6~7章读后感和对Scrum的理解
- 台北到淡水版Firefox玩网页游戏黑屏
- java求最大公约数(分解质因数)