zoukankan      html  css  js  c++  java
  • JavaEE——Mybatis(5)--resultMap自定义结果集封装

    <!--自定义某个javaBean的封装规则
    	type:自定义规则的Java类型
    	id:唯一id方便引用
    	  -->
    	<resultMap type="com.atguigu.mybatis.bean.Employee" id="MySimpleEmp">
    		<!--指定主键列的封装规则
    		id定义主键会底层有优化;
    		column:指定哪一列
    		property:指定对应的javaBean属性
    		  -->
    		<id column="id" property="id"/>
    		<!-- 定义普通列封装规则 -->
    		<result column="last_name" property="lastName"/>
    		<!-- 其他不指定的列0"email"/>
    		<result column="gender" property="gender"/>
    	</resultMap>
    	
    	<!-- resultMap:自定义结果集映射规则;  -->
    	<!-- public Employee getEmpById(Integer id); -->
    	<select id="getEmpById"  resultMap="MySimpleEmp">
    		select * from tbl_employee where id=#{id}
    	</select>
    

      添加外键约束

    联合查询:

    <!--
      场景一:
      查询Employee的同时查询员工对应的部门
      Employee===Department
      一个员工有与之对应的部门信息;
      id last_name gender d_id did dept_name (private Department dept;)
      -->

    <!--
      联合查询:级联属性封装结果集
    -->

    <resultMap type="com.atguigu.mybatis.bean.Employee" id="MyDifEmp">
    		<id column="id" property="id"/>
    		<result column="last_name" property="lastName"/>
    		<result column="gender" property="gender"/>
    		<result column="did" property="dept.id"/>
    		<result column="dept_name" property="dept.departmentName"/>
    	</resultMap>
    
    <!--  public Employee getEmpAndDept(Integer id);-->
    	<select id="getEmpAndDept" resultMap="MyDifEmp">
    		SELECT e.id id,e.last_name last_name,e.gender gender,e.d_id d_id,
    		d.id did,d.dept_name dept_name FROM tbl_employee e,tbl_dept d
    		WHERE e.d_id=d.id AND e.id=#{id}
    	</select>
    

      

    <!--
      使用association定义关联的单个对象的封装规则;
    -->

    <!-- association可以指定联合的javaBean对象
      property="dept":指定哪个属性是联合的对象
      javaType:指定这个属性对象的类型[不能省略]
    -->

    <resultMap type="com.atguigu.mybatis.bean.Employee" id="MyDifEmp2">
    		<id column="id" property="id"/>
    		<result column="last_name" property="lastName"/>
    		<result column="gender" property="gender"/>
    		
    		<!--  association可以指定联合的javaBean对象
    		property="dept":指定哪个属性是联合的对象
    		javaType:指定这个属性对象的类型[不能省略]
    		-->
    		<association property="dept" javaType="com.atguigu.mybatis.bean.Department">
    			<id column="did" property="id"/>
    			<result column="dept_name" property="departmentName"/>
    		</association>
    	</resultMap>
    	<!--  public Employee getEmpAndDept(Integer id);-->
    	<select id="getEmpAndDept" resultMap="MyDifEmp">
    		SELECT e.id id,e.last_name last_name,e.gender gender,e.d_id d_id,
    		d.id did,d.dept_name dept_name FROM tbl_employee e,tbl_dept d
    		WHERE e.d_id=d.id AND e.id=#{id}
    	</select>
    

      

  • 相关阅读:
    Codeforces Round #614 (Div. 2) D. Aroma's Search
    Codeforces Round #614 (Div. 2) C. NEKO's Maze Game
    Kruskal最小生成树及应用
    Codeforces Round #608 (Div. 2) E. Common Number
    Codeforces Round #607 (Div. 2) D Beingawesomeism
    codeforce Hello 2020 A~E
    Codeforces Round #609 (Div. 2)
    Codeforces Round #607 (Div. 2) C. Cut and Paste
    Codeforces Round #605 (Div. 3) F. Two Bracket Sequences 三维dp
    2019-2020Nowcoder Girl初赛题解
  • 原文地址:https://www.cnblogs.com/SkyeAngel/p/8254196.html
Copyright © 2011-2022 走看看