Mybatis的自定义封装规则,分步查询,封装集合属性

之前使用Mybatis时都是使用它默认的封装规则,但是如果你的javabean的属性名称既不满足驼峰命名法,也不和数据库的

列名一致,那么就会导致封装失败,于是,我们需要自定义封装规则

resultMap标签

属性:

        type:返回的结果类型(employee是我起的别名)

        id:唯一标识符,select标签的resultMap属性填的内容,指定使用哪个封装规则

 

id标签:主键对应  

result标签:非主键的其他字段
       column:数据库表的字段名
       property:javabean中的属性

        private Integer id;
	private String name;
	private String gender;
	private String email;
	private Integer d_id;
	private Department dep;
<resultMap type="employee" id="MyResultMap">
	<id column="id" property="id"/>
	<result column="name" property="name"/>
	<result column="gender" property="gender"/>
	<result column="email" property="email"/>

	<result column="d_id" property="dep.id"/>
	<result column="de_name" property="dep.name"/>
</resultMap>

因为Employee对象含有Department对象,所以在封装dep对象的属性时,property可以使用对象.属性名的方式来封装

第二种封装对象的方式是使用association标签

<resultMap type="employee" id="MyResultMap02">
		<id column="id" property="id"/>
		<result column="name" property="name"/>
		<result column="gender" property="gender"/>
		<result column="email" property="email"/>

		<association property="dep" javaType="com.xzp.entity.Department">
			<id column="de_id" property="id"/>
			<result column="de_name" property="name"/>
		</association>
	</resultMap>

association标签

               property:包含的对象名称

               javaType:该对象的类型

它的子标签id和result就和上面一样了

分步查询

<resultMap type="employee" id="MyResultByStep">
		<id column="id" property="id"/>
		<result column="name" property="name"/>
		<result column="gender" property="gender"/>
		<result column="email" property="email"/>
		<result column="d_id" property="d_id"/>
		<association property="dep" select="com.xzp.mapper.DepartmentMapper.queryDeptOneById"
		 column="d_id"></association> -->

</resultMap>

<select id="selectOneByStep" resultMap="MyResultByStep">
	select * from employee where id=#{id}
</select>
        <resultMap type="department" id="MyDepartment">
		<id column="de_id" property="id"/>
		<result column="de_name" property="name"/>
	</resultMap>
	<select id="queryDeptOneById" resultMap="MyDepartment">
		select * from department where de_id=#{d_id}
	</select>

property:对象的名称

select:需要调用的查询方法,namespace + select的id
       column:需要传入的参数对应数据的列名

分步查询的过程就是先调用selectOneByStep的查询方法,获得了Employee的所有信息,然后将d_id这列的信息作为参数

传递给queryDeptOneById,查出Department的所有信息

我们还可以使用collection标签代替association标签

 <collection property="dep" select="com.xzp.mapper.DepartmentMapper.queryDeptOneById"
	     column="d_id">
</collection>

书写的内容都是一样的,不过,如果我们需要传递多个参数时,collection能做到这一点,association做不到

  要想传递多个参数
         将他们封装到一个Map中
         column={key1=数据库列名1,key2=数据库列名2}

<collection property="dep" select="com.xzp.mapper.DepartmentMapper.queryDeptOneById"
	    column="{id=d_id}">
</collection>

association标签的column属性不可以这么写

使用collection标签封装集合

private Integer id;
private String name;
private List<Employee> emps;
<resultMap type="department" id="MyOneToMany">
		<id column="de_id" property="id" />
		<result column="de_name" property="name"/>
	
		<collection property="emps" ofType="com.xzp.entity.Employee">
			<id column="id" property="id"/>
			<result column="name" property="name"/>
			<result column="gender" property="gender"/>
			<result column="email" property="email"/>
		</collection>
</resultMap>

 

看起来和association标签封装单个对象差不多

ofType:此集合元素所属的对象

俩者的差距就这么多

可以说association就是封装单个对象,collection封装集合多个对象

 

 

 

 

发布了29 篇原创文章 · 获赞 3 · 访问量 867

猜你喜欢

转载自blog.csdn.net/weixin_44616792/article/details/100554708