mybatis对于加深一对一、一对多的理解

对于加深一对一、一对多的理解
1、一对一

<!--测试用:检索employee为主,及其它两个关联表,一对一对一,员工对部门对银行卡 -->
	<resultMap id="testOneToOne" type="com.hywl.entity.Employee">
		<id column="empId" property="empId" />
		<result column="empName" property="empName" />
		<result column="password" property="password" />
		<result column="age" property="age" />
		<result column="status" property="status" />
		<result column="sex" property="sex" />
		<result column="job" property="job" />
		<result column="status" property="status" />
		<result column="remark" property="remark" />
		<result column="depId" property="depId" />
		<result column="did" property="did" />
		<result column="depName" property="depName" />
		<!-- property就是Employee中Account的变量名称,如写成result节点那样也可以,下面这种是以集合的形式表现出来 -->
		<association property="acc"
			javaType="com.hywl.entity.Account">
			<id column="empId" property="empId" />
			<result column="bankName" property="bankName" />
			<result column="bankAccount" property="bankAccount" />
		</association>
		<!-- <result column="bankName" property="bankName" /> <result column="bankAccount" 
			property="bankAccount" /> -->
	</resultMap>
	<select id="testSelectAllEmpAndAccAndDep"
		resultMap="testOneToOne" parameterType="Integer">
		<![CDATA[
			select e.*,e.depId AS did,a.bankName,a.bankAccount,d.depName from emp e  
			inner join dep d on e.depid=d.depid 
			left join account a 
			on e.empId=a.empId
			where e.empId = #{empId}
		]]>
		ORDER BY empId DESC
	</select>

2、一对多

<!-- <resultMap id="testOneToMany" type="com.hywl.entity.Department"> -->
	<select id="testSelectAllAccAndEmp" parameterType="Integer"
		resultMap="testManyToOne">
		select a.*,e.* from account a
		inner join emp e
		on a.empId = e.empId
		 where a.empId = #{empId}
	</select>
	<resultMap type="com.hywl.entity.Account" id="testOneToMany"
		autoMapping="true">
		<!-- 一对多并以集合形式展现 1、sql里必须是多表联查 2、实体类里必须有此list的get方法 3、实体类里的toString方法必须有此list输出) -->
		<collection property="empList" autoMapping="true"
			javaType="list" ofType="com.hywl.entity.Employee"></collection>
	</resultMap>

3、一对多

<!-- 做为test使用 -->
	<resultMap id="testOneToMany"
		type="com.hywl.entity.Department">
		<id column="depId" property="depId" />
		<result column="depName" property="depName" />
		<!-- 一对多并以集合形式展现 1、sql里必须是多表联查 2、实体类里必须有此list的get方法 3、实体类里的toString方法必须有此list输出) -->
		<!-- 
			一对多的配置
			1.type:指定集合类型,一般情况下都用list
			2.property:对应depVo中的List类型的成员变量
			3.ofType:指定List中元素的类型
		 -->
		<collection javaType="list" property="empList"
			ofType="com.hywl.entity.Employee">
			<id column="empId" property="empId" />
			<result column="empName" property="empName" />
			<result column="password" property="password" />
			<result column="age" property="age" />
			<result column="status" property="status" />
			<result column="sex" property="sex" />
			<result column="job" property="job" />
			<result column="status" property="status" />
			<result column="remark" property="remark" />
			<result column="depId" property="depId" />
		</collection>
	</resultMap>
	<select id="testSelectAllDepAndEmp" resultMap="testOneToMany"
		parameterType="Integer">
		<![CDATA[
			select d.depName,e.* from emp e  
			inner join dep d on e.depid=d.depid 
			where d.depId = #{depId}
		]]>
		ORDER BY depId DESC
	</select>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值