持续创作,加速成长!这是我参与「掘金日新计划 · 6 月更文挑战」的第27天,点击查看活动详情
collection
==collection定义关联集合类型的属性的封装规则==
ofType:指定集合里面元素的类型
collection一般用于封装一个实体类中有个属性是一个的值的集合这种类型
一、实体类
@Data
@ToString
public class Dept {
private Integer id;
private String departName;
private List<Account> accounts;
}
@Data
@ToString
public class Account {
private Integer id;
private String name;
private String money;
private Integer dId;
}
二、Mapper接口
public interface DepartMapper {
Dept getDeptList(Integer 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.caq.study.mapper.DepartMapper">
<resultMap id="getDeptListResultMap" type="com.caq.study.entity.Dept">
<id column="id" property="id"></id>
<result column="dept_name" property="departName"></result>
<collection property="accounts" ofType="com.caq.study.entity.Account">
<id column="id" property="id"/>
<result column="name" property="name"/>
<result column="money" property="money"/>
<result column="d_id" property="dId"/>
</collection>
</resultMap>
<select id="getDeptList" resultMap="getDeptListResultMap">
SELECT
depart.`id`,
depart.`dept_name`,
account.`id`,
account.`name`,
account.`money`,
account.`d_id`
FROM
`depart`
LEFT JOIN `account` ON account.d_id = depart.id
WHERE
depart.id = #{id}
</select>
</mapper>
分步查询
分步查询的思路很简单,如下:
SELECT * FROM depart where id = 1
SELECT * FROM account WHERE d_id=1
先根据id查询depart表信息,再根据第一步查询查出来的id
当做第二个的限制条件查询account表
一、mapper接口
第一个要查的接口
Dept stepCollection(Integer id);
第二个要查的接口
Account selectById(Integer id);
二、映射文件
第一个要查的接口映射文件
select标签指定分步查询的mapper接口
column标签指定第一步查询出来的列的值作为限制条件
<?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.caq.study.mapper.DepartMapper">
<resultMap id="stepCollectionResultMap" type="com.caq.study.entity.Dept">
<id column="id" property="id"></id>
<result column="dept_name" property="departName"></result>
<collection property="accounts"
select="com.caq.study.mapper.AccountMapper.selectById"
column="id">
</collection>
</resultMap>
<select id="stepCollection" resultMap="stepCollectionResultMap">
SELECT * FROM depart where id = #{id}
</select>
</mapper>
第二个要查的接口映射文件
<select id="selectById" resultType="com.caq.study.entity.Account">
SELECT * FROM account WHERE d_id=#{id}
</select>
多列值传递
将多列的值封装map传递 column="{key1=column1,key2=column2}"
==这里的key1是第二步需要的参数,column1是第二步查询需要的参数对应的列==
延迟加载
==fetchType="lazy“:表示使用延迟加载==
直接用fetchType指定即可,lazy开启,eager为关闭,默认是开启
discriminator鉴别器
有时候,一个数据库查询可能会返回多个不同的结果集(但总体上还是有一定的联系的)。 鉴别器(discriminator)元素就是被设计来应对这种情况的,另外也能处理其它情况,例如类的继承层次结构。 鉴别器的概念很好理解——它很像 Java 语言中的 switch 语句。
column指定判定的列名,javaType列值对应的java类型
男生,如果是男生,把last_name这一列的值赋给email,女生则查询出所在部门信息
javaType列值对应的java类型
<resultMap id="MyEmpDis" type="com.caq.mybatis.bean.Employee">
<id column="id" property="id"/>
<result column="last_name" property="lastName"/>
<result column="email" property="email"/>
<result column="gender" property="gender"/>
<!-- column指定判定的列名,javaType列值对应的java类型-->
<discriminator javaType="string" column="gender">
<!-- 女生-->
<case value="0" resultType="com.caq.mybatis.bean.Employee">
<association property="dept"
select="com.caq.mybatis.dao.DepartmentMapper.getDeptById"
column="d_id">
</association>
</case>
<!-- 男生,如果是男生,把last_name这一列的值赋给email-->
<case value="1" resultType="com.caq.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>
这个很好理解的,我还是把测试结果写出来
// 根据id分布查员工信息
@Test
public void test03() throws IOException {
SqlSessionFactory sqlSessionFactory = getSqlSessionFactory();
SqlSession openSession = sqlSessionFactory.openSession();
try {
EmployeeMapperPlus mapper = openSession.getMapper(EmployeeMapperPlus.class);
Employee empByIdStep = mapper.getEmpByIdStep(4);
System.out.println(empByIdStep);
System.out.println(empByIdStep.getDept());
} finally {
openSession.close();
}
}
我们查询id为4的员工,根据我们前面写的sql映射文件,如果它的性别是男则他的邮件地址就是它的姓名,如果他的性别是女那么就能打印出她的部门信息(查部门信息又是一个新的对象我们用的association分步查询)
显示结果分别如下:
Employee{id=4, lastName='jerry2', email='jerry2', gender='1'} null
Employee{id=1, lastName='tom', email='tom@caq.com', gender='0'} Department{id=1, departmentName='开发'}