mybatis可以使用discriminator鉴别器判断某列的值,然后根据某列的值改变封装行为
举例:
封装Employee,如果查出的是女生,就把部门信息查询出来,否则不查询;如果是男生,把last_name这一列的值,赋值给email。
映射文件
discriminator标签
column:制定判定的列名
javaType:列值对应的java类型
case标签中,对女生查询的resultType:指定封装的结果类型;不能缺少。resultMap二选一就行
<resultMap type="com.mybatis.bean.Employee" id="MyEmpDis">
<id column="id" property="id" />
<result column="last_name" property="lastName" />
<result column="email" property="email" />
<result column="gender" property="gender" />
<discriminator javaType="string" column="gender">
<case value="0" resultType="com.mybatis.bean.Employee">
<association property="dept"
select="com.mybatis.dao.DepartmentMapper.getDeptById"
column="d_id">
</association>
</case>
<case value="1" resultType="com.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>
<select id="getEmpByIdStep" resultMap="MyEmpDis">
select * from tbl_employee where id=#{id}
</select>
结果
查询出来是女生的情况:部门信息查询出来
查询出来是男生的情况:部门信息没有,email信息更替为lastName的信息
鉴别器用的比较少,了解一下就行。
在例子中遇到一个有趣的问题,就是把查询出的男生中email信息更替为lastName的信息,通过测试可以深入了解resultMap中column和property之间的关系
以数据库中id=5的数据信息为例
创建student的javabean类
public class Student {
private int id;
private String name;
private int age;
private int number;
public int getNumber() {
return number;
}
public void setNumber(int number) {
this.number = number;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
service层
public interface StudentService {
/**
* 通过id查看学生信息
* @param id
* @return
*/
Student selectStu(int id);
}
service实现层
@Service("StudentService")
public class StudentServiceImpl implements StudentService {
@Autowired
private StudentDao studentDao;
@Override
public Student selectStu(int id) {
return studentDao.selectInfo(id);
}
}
Dao层
public interface StudentDao {
/**
* 根据学生id查询学生信息
* @param id
* @return
*/
Student selectInfo (int id);
}
映射文件
<?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.yscredit.mjlyx.dao.StudentDao">
<resultMap id="ResultMap" type="com.yscredit.mjlyx.entity.Student">
<id column="id" jdbcType="INTEGER" property="id" />
<result column="name" jdbcType="VARCHAR" property="name"/>
<result column="age" jdbcType="INTEGER" property="age"/>
<result column="number" jdbcType="INTEGER" property="number"/>
</resultMap>
<select id="selectInfo" resultMap="ResultMap">
SELECT * FROM test_student WHERE id=#{id}
</select>
</mapper>
controller层
@Controller
@RequestMapping("/test")
public class StudentController {
@Autowired
private StudentService studentService;
@RequestMapping("findInfo")
@ResponseBody
public Student findInfo(int id){
return studentService.selectStu(id);
}
}
以上是正常测试情况,会得到如下所示结果,与数据库中一致:
修改映射文件中resultMap集合中column值
<result column="age" jdbcType="INTEGER" property="age"/>
<result column="age" jdbcType="INTEGER" property="number"/>
测试结果为
操作流程是:
column获取到的列名,去库里查询该列的数据信息,然后把查询到的结果返回给property这个属性,然后通过这个属性对应的返回类型,再显示在页面上。
比如在上诉测试中:
第一行返回值类型,在DB中查询出来age=11,赋值给javabean中的age,所以显示的age=11;
第二行返回值类型,再在DB中查询出来的age=11,赋值给javabean中的number,所以显示的number=11;
修改映射文件中resultMap集合中property值
<result column="age" jdbcType="INTEGER" property="age"/>
<result column="number" jdbcType="INTEGER" property="age"/>
测试结果为
比如在上诉测试中:
第一行返回值类型,在DB中查询出来age=11,赋值给javabean中的age,所以显示的age=11;
第二行返回值类型,再在DB中查询出来的number=222,又赋值给javabean中的age,所以显示的age=222;
而javabean中的number这一属性,并没有和DB中有任何匹配,所有显示的是int的默认值——0。