Mybatis级联,使用JOIN和Associa,以及一些ID覆盖和自动变换。
先说下坑,比如数据库的字段是 DW_ID ,用generator讲mybatis自动转换的时候,会省略下表_变成dwId,所以我们之后自己手动设计的时候也尽量换成dwId;
generate的mybatis不是重新生成而是覆盖原来文字
Mybatis的级联
<resultMap id="BaseResultMap" type="com.ylzinfo.apps.rsdl.domain.RSDL_SHXX">
<id column="ID" jdbcType="VARCHAR" property="id" />
<result column="DW_ID" jdbcType="VARCHAR" property="dwId" />
<result column="DY_TYPE" jdbcType="VARCHAR" property="dyType" />
<result column="DWNAME" jdbcType="VARCHAR" property="dwname" />
<result column="ZJHM" jdbcType="VARCHAR" property="zjhm" />
<result column="SBSJ" jdbcType="VARCHAR" property="sbsj" />
<result column="YDLJG_ID" jdbcType="VARCHAR" property="ydljgId" />
<result column="YDLSTATE" jdbcType="VARCHAR" property="ydlstate" />
<result column="YDLOPNION" jdbcType="VARCHAR" property="ydlopnion" />
<result column="XDLJG_ID" jdbcType="VARCHAR" property="xdljgId" />
<result column="XDLSTATE" jdbcType="VARCHAR" property="xdlstate" />
<result column="XDLOPNION" jdbcType="VARCHAR" property="xdlopnion" />
<result column="JYSTATE" jdbcType="VARCHAR" property="jystate" />
<result column="JYREASON" jdbcType="VARCHAR" property="jyreason" />
<result column="JYOPNION" jdbcType="VARCHAR" property="jyopnion" />
<result column="SCSQ" jdbcType="VARCHAR" property="scsq" />
<result column="YLZD1" jdbcType="VARCHAR" property="ylzd1" />
<result column="YLZD2" jdbcType="VARCHAR" property="ylzd2" />
<result column="YLZD3" jdbcType="VARCHAR" property="ylzd3" />
<result column="YLZD4" jdbcType="VARCHAR" property="ylzd4" />
<result column="YLZD5" jdbcType="VARCHAR" property="ylzd5" />
<result column="SYS_CREATE_TIME" jdbcType="VARCHAR" property="sysCreateTime" />
<result column="SYS_CREATE_USER" jdbcType="VARCHAR" property="sysCreateUser" />
<result column="SYS_UPDATE_TIME" jdbcType="VARCHAR" property="sysUpdateTime" />
<result column="SYS_UPDATE_USER" jdbcType="VARCHAR" property="sysUpdateUser" />
<association column="YDLJG_ID" select="getYdwjg" property="ydwjg"/>
<association column="XDLJG_ID" select="getXdwjg" property="xdwjg"/> <result column="SYS_CREATE_USER" jdbcType="VARCHAR" property="sysCreateUser" />
<result column="SYS_UPDATE_TIME" jdbcType="VARCHAR" property="sysUpdateTime" />
<result column="SYS_UPDATE_USER" jdbcType="VARCHAR" property="sysUpdateUser" />
<association column="YDLJG_ID" select="getYdwjg" property="ydwjg"/>
</resultMap>
<sql id="Base_Column_List">
ID, DW_ID, DY_TYPE, DWNAME, ZJHM, SBSJ, YDLJG_ID, YDLSTATE, YDLOPNION, XDLJG_ID,
XDLSTATE, XDLOPNION, JYSTATE, JYREASON, JYOPNION, SCSQ, YLZD1, YLZD2, YLZD3, YLZD4,
YLZD5, SYS_CREATE_TIME, SYS_CREATE_USER, SYS_UPDATE_TIME, SYS_UPDATE_USER
</sql>
<select id="selectByPrimaryKey" parameterType="java.lang.String" resultMap="BaseResultMap">
select
<include refid="Base_Column_List" />
from RSDL_SHXX
where ID = #{id,jdbcType=VARCHAR}
</select>
<select id="getYdwjg" parameterType="java.lang.String" resultType="com.ylzinfo.apps.unit.domain.UnitBaseInfo">
SELECT NAME FROM Unit_Baseinfo WHERE id=#{ydwjg,jdbcType=VARCHAR}
</select> <select id="selectByDwId" parameterType="java.lang.String" resultMap="BaseResultMap">
select
<include refid="Base_Column_List" />
from RSDL_SHXX
where DW_ID = #{dwId,jdbcType=VARCHAR}
</select>
里面是级联部分代码,其中resultMap用来映射实体类对应的字段,用于返回数据,其中如果原本的实体类里面又包含实体类,我们就可以使用级联。在resultMap里面跟上级联实体类的名字,后面加入方法,
之后专门添加对应的select进行级联。
因为上个方法在项目中不知道为什么原因,如果名字一样就只能显示一个名字,而且如果只是要名字的话却封装一个对象,有点大材小用,故用到JOIN 来替换。
<select id="queryAndName" parameterType="java.lang.String"
resultMap="BaseResultMap2">
select r.id as ID,
r.dw_id as DW_ID,
r.dwname as DWNAME,
r.zjhm as ZJHM,
r.sbsj as SBSJ,
r.ydljg_id as YDLJG_ID,
r.ydlstate as YDLSTATE,
r.xdljg_id as XDLJG_ID,
r.xdlstate as XDLSTATE,
r.ylzd1 as YLZD1,
u1.dwname as YDLJG_NAME,
u2.dwname as XDLJG_NAME
from RSDL_SHXX r
left outer join RSDL_DLJG u1 on r.ydljg_id = u1.dwid
left outer join RSDL_DLJG u2 on r.xdljg_id = u2.dwid
where 1=1
<if test="dwId != null and dwId != ''">
and DW_ID like CONCAT(CONCAT('%',#{dwId}),'%')
</if>
<if test="dyType != null and dyType != ''">
and DY_TYPE like CONCAT(CONCAT('%',#{dyType}),'%')
</if>
<if test="dwname != null and dwname != ''">
and DWNAME like CONCAT(CONCAT('%',#{dwname}),'%')
</if>
<if test="zjhm != null and zjhm != ''">
and ZJHM like CONCAT(CONCAT('%',#{zjhm}),'%')
</if>
<if test="sbsj != null and sbsj != ''">
and SBSJ like CONCAT(CONCAT('%',#{sbsj}),'%')
</if>
<if test="ydljgId != null and ydljgId != ''">
and YDLJG_ID like CONCAT(CONCAT('%',#{ydljgId}),'%')
</if>
<if test="ydlstate != null and ydlstate != ''">
and YDLSTATE like CONCAT(CONCAT('%',#{ydlstate}),'%')
</if>
<if test="ydlopnion != null and ydlopnion != ''">
and YDLOPNION like CONCAT(CONCAT('%',#{ydlopnion}),'%')
</if>
<if test="xdljgId != null and xdljgId != ''">
and XDLJG_ID like CONCAT(CONCAT('%',#{xdljgId}),'%')
</if>
<if test="xdlstate != null and xdlstate != ''">
and XDLSTATE like CONCAT(CONCAT('%',#{xdlstate}),'%')
</if>
<if test="ydlopnion != null and ydlopnion != ''">
and XDLOPNION like CONCAT(CONCAT('%',#{ydlopnion}),'%')
</if>
<if test="jystate != null and jystate != ''">
and JYSTATE like CONCAT(CONCAT('%',#{jystate}),'%')
</if>
<if test="jyreason != null and jyreason != ''">
and JYREASON like CONCAT(CONCAT('%',#{jyreason}),'%')
</if>
<if test="jyopnion != null and jyopnion != ''">
and JYOPNION like CONCAT(CONCAT('%',#{jyopnion}),'%')
</if>
<if test="ylzd1 != null and ylzd1 != ''">
and YLZD1 like CONCAT(CONCAT('%',#{ylzd1}),'%')
</if>
<if test="ylzd2 != null and ylzd2 != ''">
and YLZD2 like CONCAT(CONCAT('%',#{ylzd2}),'%')
</if>
<if test="ylzd3 != null and ylzd3 != ''">
and YLZD3 like CONCAT(CONCAT('%',#{ylzd3}),'%')
</if>
<if test="ylzd4 != null and ylzd4 != ''">
and YLZD4 like CONCAT(CONCAT('%',#{ylzd4}),'%')
</if>
<if test="ylzd5 != null and ylzd5 != ''">
and YLZD5 like CONCAT(CONCAT('%',#{ylzd5}),'%')
</if>
order by r.sbsj desc
<!-- select <include refid="Base_Column_List" /> from RSDL_SHXX where DW_ID
= #{dwId,jdbcType=VARCHAR} order by sbsj desc -->
</select>
resultmap和java实体类
<resultMap id="BaseResultMap2" type="com.ylzinfo.apps.rsdl.domain.RSDL_SHXX">
<id column="ID" jdbcType="VARCHAR" property="id" />
<result column="DW_ID" jdbcType="VARCHAR" property="dwId" />
<result column="DY_TYPE" jdbcType="VARCHAR" property="dyType" />
<result column="DWNAME" jdbcType="VARCHAR" property="dwname" />
<result column="ZJHM" jdbcType="VARCHAR" property="zjhm" />
<result column="SBSJ" jdbcType="VARCHAR" property="sbsj" />
<result column="YDLJG_ID" jdbcType="VARCHAR" property="ydljgId" />
<result column="YDLSTATE" jdbcType="VARCHAR" property="ydlstate" />
<result column="YDLOPNION" jdbcType="VARCHAR" property="ydlopnion" />
<result column="XDLJG_ID" jdbcType="VARCHAR" property="xdljgId" />
<result column="XDLSTATE" jdbcType="VARCHAR" property="xdlstate" />
<result column="XDLOPNION" jdbcType="VARCHAR" property="xdlopnion" />
<result column="JYSTATE" jdbcType="VARCHAR" property="jystate" />
<result column="JYREASON" jdbcType="VARCHAR" property="jyreason" />
<result column="JYOPNION" jdbcType="VARCHAR" property="jyopnion" />
<result column="SCSQ" jdbcType="VARCHAR" property="scsq" />
<result column="YLZD1" jdbcType="VARCHAR" property="ylzd1" />
<result column="YLZD2" jdbcType="VARCHAR" property="ylzd2" />
<result column="YLZD3" jdbcType="VARCHAR" property="ylzd3" />
<result column="YLZD4" jdbcType="VARCHAR" property="ylzd4" />
<result column="YLZD5" jdbcType="VARCHAR" property="ylzd5" />
<result column="SYS_CREATE_TIME" jdbcType="VARCHAR" property="sysCreateTime" />
<result column="SYS_CREATE_USER" jdbcType="VARCHAR" property="sysCreateUser" />
<result column="SYS_UPDATE_TIME" jdbcType="VARCHAR" property="sysUpdateTime" />
<result column="SYS_UPDATE_USER" jdbcType="VARCHAR" property="sysUpdateUser" />
<result column="YDLJG_NAME" jdbcType="VARCHAR" property="ydwjg" />
<result column="XDLJG_NAME" jdbcType="VARCHAR" property="xdwjg" />
</resultMap>
/**
* 创建时间,一旦创建无法改变。
*/
private String sysCreateTime; /**
* 创建用户,一旦创建无法改变。
*/
private String sysCreateUser; /**
* 更改时间
*/
private String sysUpdateTime; /**
* 更改用户
*/
private String sysUpdateUser; /**
* 原单位机构,用来取名字。
*/
private String ydwjg; /**
* 新单位机构,用来取名字。
*/
private String xdwjg;
Mybatis级联,使用JOIN和Associa,以及一些ID覆盖和自动变换。的更多相关文章
- mybatis 级联
级联是一个数据库实体的概念.一对多的级联,一对多的级联,在MyBatis中还有一种被称为鉴别器的级联,它是一种可以选择具体实现类的级联. 级联不是必须的,级联的好处是获取关联数据十分便捷,但是级联过多 ...
- mybatis ---- 级联查询 一对多 (集合映射)
关联有嵌套查询和嵌套结果两种方式,本文是按照嵌套结果这种方式来说明的 上一章介绍了多对一的关系,用到了<association></association>,这是一个复杂类型的 ...
- (三)mybatis级联的实现
mybatis级联的实现 开篇 级联有三种对应关系: 1.一对一(association):如学号与学生 2.一对多(collection):如角色与用户 3.多对多(discri ...
- mybatis级联查询
1.定义四个实体.User Role Privilege Resource,他们之间的对于关系为 2.需求:我通过用户名username查找出该用户对应的角色以及角色对应的权限和资源 3 ...
- 【mybatis】mybatis中insert操作,返回自增id
需求是这样的: mybatis中insert操作,返回自增id,因为这个自增id需要给后续业务用到. 原本是这样的: 将insert语句传入,正常执行insert操作,返回int永远是 0[失败] 或 ...
- mybatis mapper xml文件配置resultmap时,id行和result行有什么区别?
mybatis mapper xml文件配置resultmap时,id行和result行有什么区别? <resultMap id = "CashInvoiceMap" typ ...
- mybatis的插入与批量插入的返回ID的原理
目录 背景 底层调用方法 单个对象插入 列表批量插入 完成 背景 最近正在整理之前基于mybatis的半ORM框架.原本的框架底层类ORM操作是通过StringBuilder的append拼接的,这次 ...
- Mybatis 级联查询 (一对多 )
后台系统中 涉及到添加试卷 问题 答案的一个模块的.我需要通过试卷 查询出所有的试题,以及试题的答案.这个主要要使用到Mybatis的级联查询. 通过试卷 查询出与该试卷相关的试题(一对多),查询出试 ...
- Mybatis级联:关联、集合和鉴别器的使用
Mybatis中级联有关联(association).集合(collection).鉴别器(discriminator)三种.其中,association对应一对一关系.collection对应一对多 ...
随机推荐
- 4.1.4 Nim
Problem description: 有n堆石子,每堆各有ai颗石子.A和B轮流从非空的石子堆中取走至少一颗石子.A先取,取光所有石子的一方获胜.当双方都采用最佳策略时,谁会获胜? 1<=n ...
- FileZilla 客户端连接 FlieZilla 服务器 连接成功读取目录列表却失败的解决办法
解决过程: 第一步: 第二步:
- 『PyTorch』第三弹_自动求导
torch.autograd 包提供Tensor所有操作的自动求导方法. 数据结构介绍 autograd.Variable 这是这个包中最核心的类. 它包装了一个Tensor,并且几乎支持所有的定义在 ...
- Python遍历文件个文件夹
在读文件的时候往往需要遍历文件夹,python的os.path包含了很多文件.文件夹操作的方法.下面列出: os.path.abspath(path) #返回绝对路径 os.path.basename ...
- zend framwork项目基本操作
1.首先,我们做项目是采用db的方式来编写sql语句的. 2.查询: fetchOne() 查询一个字段,如果没有指定就只查询第一个字段,只能得到一个值. fetchRow() 查询一行数据 ...
- 水题系列一:Circle
问题描述:Circle 小明在玩游戏,他正在玩一个套圈圈的游戏.他手里有 L 种固定半径的圆圈,每一种圆 圈都有其固定的数量.他要把这些圆圈套进 N 个圆形槽中的一个.这些圆形槽都有一个最 小半径和最 ...
- 维护一个旧程序 linq2sql,出现row not found or changed的异常
维护一个旧程序 linq2sql,出现row not found or changed的异常, 查博客园,文章都是一大抄,都不对. 想想之前都能保存的.这个异常是在加了字段之后出现的. 因为用vs.n ...
- oracle create tablespace
ALTER PROFILE DEFAULT LIMIT PASSWORD_LIFE_TIME UNLIMITED; sqlplus shybt/shybt@127.0.0.1:1521/orcl Cr ...
- Oracle 如何循环查询结果集,进行新增或修改
Oracle的PL/SQL中怎样循环查询的结果集,然后根据查询结果进行判断,是新增或修改操作 loop循环例子 for item in (select a,b,c from table_a where ...
- Ubuntu 14.04下如何更换更新源(更新为163源)
之前的安装ubuntu桌面版的之后安装yum,vim等会遇到一些问题, 比如:Ubuntu 14.04下如何更换更新源(更新为163源) 解决: http://jingyan.baidu.com/ar ...