• 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>
    

      

  • 相关阅读:
    tomcat虚拟机配置
    日期时分秒毫秒+3位随机数
    springmvc action访问路径不带项目名
    nginx 配置模板
    AES加密
    spring-mvc里的 <mvc:resources> 及静态资源访问
    MySQL——事务(Transaction)
    mysql索引
    < aop:aspect>与< aop:advisor>的区别
    annotation-driven
  • 原文地址:https://www.cnblogs.com/SkyeAngel/p/8254196.html
Copyright © 2020-2023  润新知