玖叶教程网

前端编程开发入门

mybatis中子查询对象及对象集合(子查询中带多参数)



项目中mybatis使用的较多,因为方便灵活,上手快,会写sql就能用好mybatis,另外sql优化等简单易做,遇到慢sql了比hibernate更好排查。除了一大痛点,因为项目短平快,所以开发过程中大概率会出现发现某个表要加字段要删字段要改字段等头疼的事。

create table if not exists tb_station(
 id bigint not null constraint pk_tb_station primary key,
 code varchar(18),
 geo_id bigint, //分组id
 label_id bigint, // 标牌id
 employee_id bigint, //员工id
 status integer
);

需要获取的结果对象为
public class StationVO {
 private Long id;//主键
 private String code;//工位编号
 private Long stationGeoId;//StationGeoVO,所属工位分区对象
 private Long labelId;//工位绑定的标牌id
 private String labelCode;//标牌code
 private String eslCode;//标牌eslcode
 private EmployeeVO employee;//EmployeeVO,分配的员工对象
 private String status;//工位状态(0:空闲,1:正在使用)
 private List<Biz> bizList;//工位分组推送vo
}
biz对象说明:biz对象对应的表里有一个geo_id字段进行关联

此处的需求是要根据分组id(即geoId)查询stationVO对象

mybatis中对应的映射及代码如下:

 <resultMap id="StationVOResultMap" type="org.sit.cto.smmrepository.vo.StationVO">
 <id column="id" jdbcType="BIGINT" property="id" />
 <result column="code" jdbcType="VARCHAR" property="code" />
 <result column="status" jdbcType="INTEGER" property="status" />
 <result column="label_id" jdbcType="BIGINT" property="labelId" />
 <result column="esl_code1" jdbcType="VARCHAR" property="eslCode" />
 <result column="lcode" jdbcType="VARCHAR" property="labelCode" />
 <result column="geo_id" jdbcType="BIGINT" property="stationGeoId" />
 <association property="employee" column="employee_id" select="selectEmployeeByEmployeeId"/>
 <collection property="bizList" column="{geoId=geo_id,eslCode=esl_code1}" ofType="map" select="selectBizVOByStationId"/>
 </resultMap>
 
 <resultMap id="EmployeeResultMap" type="org.sit.cto.smmrepository.model.Employee">
 <id column="id" jdbcType="BIGINT" property="id" />
 <result column="code" jdbcType="VARCHAR" property="code" />
 <result column="real_name" jdbcType="VARCHAR" property="realName" />
 <result column="company" jdbcType="VARCHAR" property="company" />
 <result column="dept_name" jdbcType="VARCHAR" property="deptName" />
 <result column="position" jdbcType="VARCHAR" property="position" />
 <result column="phone" jdbcType="VARCHAR" property="phone" />
 <result column="status" jdbcType="INTEGER" property="status" />
 </resultMap>
 
 <resultMap id="BizVOResultMap" type="org.sit.cto.smmrepository.vo.BizVO">
 <result column="esl_code" jdbcType="VARCHAR" property="eslCode" />
 <result column="temp_id" jdbcType="BIGINT" property="labelTempId" />
 <result column="key" jdbcType="VARCHAR" property="key" />
 <result column="value" jdbcType="VARCHAR" property="value" />
 </resultMap>
 
 <select id="selectEmployeeByEmployeeId" resultMap="EmployeeResultMap">
 select
 id, 
 code, 
 real_name, 
 company, 
 dept_name, 
 position, 
 phone, 
 status
 from tb_employee
 where id=#{employeeId}
 </select>
 
 <select id="selectBizVOByStationId" parameterType="java.util.Map" resultMap="BizVOResultMap">
 select
 esl_code, 
 temp_id, 
 key, 
 value
 from tb_biz
 where associated_id=#{geoId}
 and esl_code=#{eslCode}
 and biz_type=2
 </select>
 

 <select id="getStationVOListByGeoId" resultMap="StationVOResultMap">
 SELECT
 ts.ID,
 ts.code,
 ts.geo_id,
 ts.label_id,
 ts.employee_id,
 ts.status,
 tl.code lcode,
 tl.esl_code1
 FROM
 tb_station ts
 LEFT JOIN tb_label tl ON ts.label_id = tl.ID
 WHERE
 ts.geo_id =#{geoId}
 ORDER BY
 ts.code
 </select>

子查询获取一个关联对象
例子中的employee

 <association property="employee" column="employee_id" select="selectEmployeeByEmployeeId"/>
子查询对象,标签使用 <association />
property的内容对应目标对象中的属性名 
column对应表中的关联字段的名字
select对应获取关联对象子查询的查询名

子查询获取一个关联对象的集合
示例中Biz对象的集合

<collection property="bizList" column="{geoId=geo_id,eslCode=esl_code1}" ofType="map" select="selectBizVOByStationId"/>
子查询,标签使用<collection />
property的内容对应目标对象中的属性名
column对应表中的关联字段的名字
select对应获取对象子查询的查询名
ofType 此处子查询需要传入其他参数,故用map传入,若无其他参数则该标签中不需要此属性,column处直接填写关联字段名(如column="geo_id")
 需要传入其他参数时,写法如上例,在对应的子查询的#{}占位符处需要与该map中的key对应

发表评论:

控制面板
您好,欢迎到访网站!
  查看权限
网站分类
最新留言