Mybatis-association-collection-discriminator

使用mybatis,在写映射文件mapping.xml 中的resultMap时,出现元素类型为 “resultMap” 的内容必须匹配 “(constructor?,id*,result*,association*,collection*,discriminator?)”

的错误,检查发现resultMap中元素的顺序应按照括号中内容的顺序,也就是id、result、association、collection、discriminatory,不能在result没写完的情况下插入association或者collection。
1.association用法
用法1:

<?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.queen.mybatis.mapper.UserMapper">
 
	<resultMap type="com.queen.mybatis.bean.User" id="userResultMap">
		<id property="id" column="id"/>
	    <result property="loginId" column="loginId" />
	    <result property="userName" column="userName"/>
	    <result property="note" column="note"/>
            <!--assocication可以指定联合的JavaBean对象 
                property="role"指定哪个属性是联合的对象
                javaType:指定这个属性对象的类型
            -->
	    <association property="role" javaType="com.queen.mybatis.bean.Role">
	    	<id column="role_id" property="id"/>
	    	<result column="roleName" property="roleName"/>
	    </association>
	</resultMap>
	
	<select id="getUserById" resultMap="userResultMap">
		select m.id id, m.loginId loginId, m.userName userName, m.roleId roleId,m.note note, n.id role_id, n.roleName roleName 
			from t_user m left join t_role n on m.roleId=n.id
			where m.id=#{id}
	</select>

用法二

<resultMap type="com.queen.mybatis.bean.User" id="userResultMapStep">
	<id property="id" column="id"/>
	<result property="loginId" column="loginId" />
	<result property="userName" column="userName"/>
	<result property="note" column="note"/>
	<!-- association定义关联对象的封装规则 
	    select:表明当前属性是调用select指定的方法查询出结果
	    column:指定将那一列的值传递给这个方法
	    整个流程:使用Select指定的方法(传入column指定的这列参数的值)查出对象,并封装给property指定的属性
	 -->
	<association property="role" select="com.queen.mybatis.mapper.RoleMapper.getRoleById"
	    	column="roleId">
	</association>
</resultMap>
<select id="getUserByIdStep" resultMap="userResultMapStep">
	select * from t_user where id=#{id}
</select>

collection 用法
用法1:

<?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.atguigu.mybatis.dao.DepartmentMapper">
 
	<!-- collection嵌套结果集的方式,定义关联的集合类型元素的封装规则-->
	
	<!-- 嵌套结果集的方式,使用collection标签定义关联的集合类型的属性封装规则 -->
	<resultMap type="com.atguigu.mybatis.bean.Department" id="MyDept">
		<id column="did" property="id"/>
		<result column="dept_name" property="departmentName"/>
		<!-- 
		collection定义关联结合类型的属性的封装规则
		ofType:指定集合里面元素的类型
		-->
		<collection property="emps" ofType="com.atguigu.mybatis.bean.Employee">
			<!-- 定义这个集合中元素的封装规则 -->
			<id column="eid" property="id"/>
			<result column="last_name" property="lastName"/>
			<result column="email" property="email"/>
			<result column="gender" property="gender"/>
		</collection>
	
	</resultMap>
	
	<!-- public Department getDeptByIdPlus(Integer id); -->
	<select id="getDeptByIdPlus" resultMap="MyDept" >
		SELECT
			d.id did,
			d.dept_name dept_name,
			e.id,
			e.last_name last_name,
			e.email email,
			e.gender gender
		FROM
			tbl_dept d
		LEFT JOIN tbl_employee e ON d.id = e.d_id
		WHERE
			d.id = #{id}
	</select>
	
</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.atguigu.mybatis.dao.EmployeeMapperPlus">
	
<resultMap type="com.atguigu.mybatis.bean.Department" id="MyDeptStep">
		<id column="id" property="id"/>
		<id column="dept_name" property="departmentName"/>
		<collection property="emps"
			select="com.atguigu.mybatis.dao.EmployeeMapperPlus.getEmpsByDeptId"
			column="{deptId=id}" fetchType="lazy"></collection>
</resultMap>
	<!-- public Department getDeptByIdStep(Integer id); -->
	<select id="getDeptByIdStep" resultMap="MyDeptStep">
		select id,dept_name departmentName from tbl_dept where id=#{id}
	</select>
 
<mapper/>

3.discriminator

<?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.mybatis.dao.EmployeeMapper">
    <!-- <discriminator javaType=""></discriminator>
        鉴别器:mybatis可以使用discriminator判断某列的值,然后根据某列的值改变封装行为
        封装Employee:
            如果查出的是女生:就把部门信息查询出来,否则不查询;
            如果是男生,把last_name这一列的值赋值给email;
     -->
    <resultMap type="com.mybatis.bean.Employee" id="MyEmpDis">
        <id column="id" property="id"/>
        <result column="last_name" property="lastName"/>
        <result column="email" property="email"/>
        <result column="gender" property="gender"/>
        <!--
            column:指定判定的列名
            javaType:列值对应的java类型  -->
        <discriminator javaType="java.lang.Integer" column="gender">
            <!--女生  resultType:指定封装的结果类型;不能缺少。/resultMap-->
            <case value="0" resultType="com.mybatis.bean.Employee">
                <association property="dept"
                             select="com.mybatis.dao.DeptmentMapper.getDeptById"
                             column="d_id">
                </association>
            </case>
            <!--男生 ;如果是男生,把last_name这一列的值赋值给email; -->
            <case value="1" resultType="com.mybatis.bean.Employee">
                <id column="id" property="id"/>
                <result column="last_name" property="lastName"/>
                <result column="last_name" property="email"/>
                <result column="gender" property="gender"/>
            </case>
        </discriminator>
    </resultMap>
 
    <select id="getEmpByIdWithDept" resultMap="MyEmpDis">
       select * from tbl_employee where id=#{id}
    </select>
</mapper>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值