mybatis一对一关系 association的使用
来源:互联网 发布:python except break 编辑:程序博客网 时间:2024/06/05 05:11
废话不多说直接上代码
Employee.java
public class Employee { private Integer id; private String lastName; private String gender; private String email; private Department dept; }
接口文件EmployeeMapperPlus.java
public interface EmployeeMapperPlus { /** * 根据id查询Employee对象,使用resultMap * @param id * @return */ public Employee getEmpById(Integer id); /** * 根据id查询Employee对象,同时要查询出Deptment对象信息 方式一 * @param id * @return */ public Employee getEmpAndDeptById(Integer id); /** * 根据id查询Employee对象,同时要查询出Deptment对象信息 方式二 * @param id * @return */ public Employee getEmpAndDeptById2(Integer id); }Mapper配置文件EmployeeMapperPlus.xml
<?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.du.mybatis.dao.EmployeeMapperPlus"> <!-- type:自定义规则的java类型 id:唯一标识,以便引用 --> <resultMap type="com.du.mybatis.bean.Employee" id="MySimpleEmp"> <!-- 主键列使用id,其他列使用result property java属性字段名 column 数据库字段名 --> <id property="id" column="id"/> <result property="lastName" column="last_name"/> <result column="email" property="email"/> <result column="gender" property="gender"/> </resultMap> <!-- public Employee getEmpById(Integer id); --> <select id="getEmpById" resultMap="MySimpleEmp"> select id, last_name, email, gender from tbl_employee where id= #{id} </select> <!-- 一对一关系写法1 级联属性封装结果集--> <resultMap type="com.du.mybatis.bean.Employee" id="MyDifEmp1"> <id column="id" property="id"/> <result property="lastName" column="last_name"/> <result column="email" property="email"/> <result column="gender" property="gender"/> <result property="dept.id" column="dept_id"/> <result property="dept.departmentName" column="dept_name"/> </resultMap> <!-- 一对一关系写法2 使用association --> <resultMap type="com.du.mybatis.bean.Employee" id="MyDifEmp2"> <id column="id" property="id"/> <result property="lastName" column="last_name"/> <result column="email" property="email"/> <result column="gender" property="gender"/> <!-- association指定级联的Javabean对象 property指定该对象的字段名称 javaType指定对象类型(必填) --> <association property="dept" javaType="com.du.mybatis.bean.Department"> <id column="id" property="id"/> <result column="dept_name" property="departmentName"/> </association> </resultMap> <!-- 查询Employee同时查出所属部门Department resultMap:可以选择MyDifEmp1和MyDifEmp2其中一种实现方式 --> <!-- public Employee getEmpAndDeptById(Integer id); --> <select id="getEmpAndDeptById" resultMap="MyDifEmp1"> <!-- 注意查询字段如果不写*,则要把所有需要的字段列出 --> SELECT e.id,e.last_name,e.email,e.gender,e.dept_id,d.dept_name FROM tbl_employee e, tbl_dept d WHERE e.dept_id=d.id and e.id=#{id} </select> <!-- 一对一关系,实现方式三 --> <resultMap type="com.du.mybatis.bean.Employee" id="MyDifEmp3"> <id column="id" property="id"/> <result property="lastName" column="last_name"/> <result column="email" property="email"/> <result column="gender" property="gender"/> <!-- 定义关联对象 column:将该字段作为参数传递给select作为入参 --> <association property="dept" column="dept_id" select="com.du.mybatis.dao.DepartmentMapper.getDeptById"></association> </resultMap> <!-- 一对一关系,实现方式四 --> <resultMap type="com.du.mybatis.bean.Employee" id="MyDifEmp4"> <id column="id" property="id"/> <result property="lastName" column="last_name"/> <result column="email" property="email"/> <result column="gender" property="gender"/> <!-- 定义关联对象 column:将该字段作为参数传递给select作为入参 --> <association property="dept" column="dept_id" select="queryDeptById"></association> </resultMap> <!-- 分步骤查询: 1、根据员工号查询员工信息 2、根据员工信息的dept_id信息,在部门表里查询部门信息 3、将部门信息放入到员工属性的Department里 --> <!-- public Employee getEmpAndDeptById2(Integer id); --> <select id="getEmpAndDeptById2" resultMap="MyDifEmp4"> select id, last_name, email, gender, dept_id from tbl_employee where id=#{id} </select> <select id="queryDeptById" resultType="com.du.mybatis.bean.Department"> select id,dept_name departmentName from tbl_dept where id=#{id} </select> <!-- ================================ --> <!-- resultMap中有一个discriminator标签,鉴别器:可以使用discriminator判断某列的值,然后根据某列的值改变封装行为 <discriminator javaType=""></discriminator> 例如:Employee 如果查出是女生,查出部门信息, 如果是男生,不查询部门信息,同时把last_Name这一列赋值给email; 参见MyDifEmp5配置 --> <resultMap type="com.du.mybatis.bean.Employee" id="MyDifEmp5"> <id column="id" property="id"/> <result property="lastName" column="last_name"/> <result column="email" property="email"/> <result column="gender" property="gender"/> <!-- column:需要判断的列 javaType:列值对应的java类型 --> <discriminator javaType="string" column="gender"> <!-- 女生 resultType:指定封装的结果类型 --> <case value="0" resultType="com.du.mybatis.bean.Employee"> <association property="dept" column="dept_id" select="com.du.mybatis.dao.DepartmentMapper.getDeptById"></association> </case> <!-- 男生 resultType:指定封装的结果类型 --> <case value="1" resultType="com.du.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> </mapper>
阅读全文
0 0
- mybatis一对一关系 association的使用
- mybatis 一对一关系取值association
- mybatis 一对一与一对多collection和association的使用
- mybatis 一对一关系取值association 通过resultMap实现
- 使用mybatis建立一对一的关系
- mybatis association的使用
- Mybatis中使用Association元素进行一对一级联查询
- mybatis中association的使用
- springmvc mybatis 配置一对多的关系使用 <association> 及实体类写法1
- mybatis collection和association的简单使用
- MyBatis的ResultMaps之一对一关系
- myBatis 中查询多表数据时使用association处理一对一关联
- mybatis <association>标签使用
- Mybatis 一对一关系映射
- mybatis一对一关系表
- MyBatis一对一关系映射
- mybatis一对一关联关系
- MyBatis一对一关系映射
- 单链表元素定位
- 读《跟我一起学Makefile》有感
- 常用的DOS命令
- 今天复习javaSE的一些知识
- 类是什么?
- mybatis一对一关系 association的使用
- 51nod 1717 好数(找规律)
- 【学习笔记六】
- HGDB-pgbouncer配置
- Linux--第一天 vmware centos6.5 查看网络ip 127.0.0.1
- OpenCV 实现canny边缘检测
- java中 final、finally、finalize的区别
- HDOJ HDU 1087 Super Jumping! Jumping! Jumping!
- 4sum