mybatis可以使用鉴别器判断某列的值,然后根据某列的值改变封装行为。
比如说:
如果是女生(gender=0)我们将部门信息提取出来,否则不提取;
如果是男生(gender=1),把last_name的值赋值给email;
EmployeeMapper.xml
<resultMap type="com.gong.mybatis.bean.Employee" id="MyEmpDis"> <id column="id" property="id"/> <result column="last_name" property="lastName"/> <result column="gender" property="gender"/> <result column="email" property="email"/> <discriminator javaType="string" column="gender"> <case value="0" resultType="com.gong.mybatis.bean.Employee"> <association property="dept" select="com.gong.mybatis.dao.DepartmentMapper.getDeptById" column="d_id"> </association> </case> <case value="1" resultType="com.gong.mybatis.bean.Employee"> <id column="id" property="id"/> <result column="last_name" property="lastName"/> <result column="gender" property="gender"/> <result column="last_name" property="email"/> </case> </discriminator> </resultMap> <select id="getEmpByIdStep" resultMap="MyEmpDis"> select * from tbl_employee where id=#{id} </select>
说明:distriminator中的javaType指定数据库中字段对应着java中的属性的类型,column属性为列名称。case标签中value属性标识数据库中列的值,resultType指定封装的结果类型,不可少。
DepartmentMapper.xml
<select id="getDeptById" resultType="com.gong.mybatis.bean.Department"> select * from tbl_department where id=#{id} </select>
进行测试:
package com.gong.mybatis.test; import java.io.IOException; import java.io.InputStream; import java.util.List; import java.util.Map; import org.apache.ibatis.io.Resources; import org.apache.ibatis.session.SqlSession; import org.apache.ibatis.session.SqlSessionFactory; import org.apache.ibatis.session.SqlSessionFactoryBuilder; import org.junit.Test; import com.gong.mybatis.bean.Department; import com.gong.mybatis.bean.Employee; import com.gong.mybatis.dao.DepartmentMapper; import com.gong.mybatis.dao.EmployeeMapperPlus; public class TestMybatis2 { public SqlSessionFactory getSqlSessionFactory() throws IOException { String resource = "mybatis-config.xml"; InputStream is = Resources.getResourceAsStream(resource); return new SqlSessionFactoryBuilder().build(is); } @Test public void test() throws IOException { SqlSessionFactory sqlSessionFactory = getSqlSessionFactory(); SqlSession openSession = sqlSessionFactory.openSession(); try { EmployeeMapperPlus mapper = openSession.getMapper(EmployeeMapperPlus.class); Employee employee = mapper.getEmpByIdStep(3); System.out.println(employee.getLastName()); System.out.println(employee.getDept()); openSession.commit(); } finally { openSession.close(); } } }
首先测试的是gender=0的情况:
DEBUG 01-20 20:47:37,503 ==> Preparing: select * from tbl_employee where id=? (BaseJdbcLogger.java:145)
DEBUG 01-20 20:47:37,590 ==> Parameters: 3(Integer) (BaseJdbcLogger.java:145)
DEBUG 01-20 20:47:37,757 <== Total: 1 (BaseJdbcLogger.java:145)
小红
DEBUG 01-20 20:47:37,759 ==> Preparing: select * from tbl_department where id=? (BaseJdbcLogger.java:145)
DEBUG 01-20 20:47:37,760 ==> Parameters: 1(Integer) (BaseJdbcLogger.java:145)
DEBUG 01-20 20:47:37,763 <== Total: 1 (BaseJdbcLogger.java:145)
Department [id=1, deptName=开发部]
可以查到部门的信息。
接下来是gender=1的情况:
Employee employee = mapper.getEmpByIdStep(1);
DEBUG 01-20 20:53:32,581 ==> Preparing: select * from tbl_employee where id=? (BaseJdbcLogger.java:145)
DEBUG 01-20 20:53:32,647 ==> Parameters: 1(Integer) (BaseJdbcLogger.java:145)
DEBUG 01-20 20:53:32,708 <== Total: 1 (BaseJdbcLogger.java:145)
xiximayou
null
发现部门信息是查不到的。
再进行gender=1时的情况:
Employee employee = mapper.getEmpByIdStep(1);
System.out.println(employee);
System.out.println(employee.getDept());
DEBUG 01-20 20:54:39,440 ==> Preparing: select * from tbl_employee where id=? (BaseJdbcLogger.java:145)
DEBUG 01-20 20:54:39,480 ==> Parameters: 1(Integer) (BaseJdbcLogger.java:145)
DEBUG 01-20 20:54:39,555 <== Total: 1 (BaseJdbcLogger.java:145)
Employee [id=1, lastName=xiximayou, gender=1, email=xiximayou, dept=null]
null
email被修改,且部门信息查不到。
gender=0的情况:
Employee employee = mapper.getEmpByIdStep(3);
System.out.println(employee);
System.out.println(employee.getDept());
DEBUG 01-20 20:56:19,177 ==> Preparing: select * from tbl_employee where id=? (BaseJdbcLogger.java:145)
DEBUG 01-20 20:56:19,221 ==> Parameters: 3(Integer) (BaseJdbcLogger.java:145)
DEBUG 01-20 20:56:19,384 <== Total: 1 (BaseJdbcLogger.java:145)
DEBUG 01-20 20:56:19,385 ==> Preparing: select * from tbl_department where id=? (BaseJdbcLogger.java:145)
DEBUG 01-20 20:56:19,386 ==> Parameters: 1(Integer) (BaseJdbcLogger.java:145)
DEBUG 01-20 20:56:19,389 <== Total: 1 (BaseJdbcLogger.java:145)
Employee [id=3, lastName=小红, gender=0, email=xiaohong@qq.com, dept=Department [id=1, deptName=开发部]]
Department [id=1, deptName=开发部]
email未被修改,且可以查到部门的信息。