mybatis可以使用鉴别器判断某列的值,然后根据某列的值改变封装行为。
比如说:
如果是女生(gender=0)我们将部门信息提取出来,否则不提取;
如果是男生(gender=1),把last_name的值赋值给email;
EmployeeMapper.xml
select * from tbl_employee where id=#{id}
说明:distriminator中的javaType指定数据库中字段对应着java中的属性的类型,column属性为列名称。case标签中value属性标识数据库中列的值,resultType指定封装的结果类型,不可少。
DepartmentMapper.xml
select * from tbl_department where id=#{id}
进行测试:
packagecom.gong.mybatis.test;importjava.io.IOException;importjava.io.InputStream;importjava.util.List;importjava.util.Map;importorg.apache.ibatis.io.Resources;importorg.apache.ibatis.session.SqlSession;importorg.apache.ibatis.session.SqlSessionFactory;importorg.apache.ibatis.session.SqlSessionFactoryBuilder;importorg.junit.Test;importcom.gong.mybatis.bean.Department;importcom.gong.mybatis.bean.Employee;importcom.gong.mybatis.dao.DepartmentMapper;importcom.gong.mybatis.dao.EmployeeMapperPlus;public classTestMybatis2 {public SqlSessionFactory getSqlSessionFactory() throwsIOException {
String resource= "mybatis-config.xml";
InputStream is=Resources.getResourceAsStream(resource);return newSqlSessionFactoryBuilder().build(is);
}
@Testpublic void test() throwsIOException {
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 protected], dept=Department [id=1, deptName=开发部]]
Department [id=1, deptName=开发部]
email未被修改,且可以查到部门的信息。
原文:https://www.cnblogs.com/xiximayou/p/12219414.html