版权声明:柠萌 https://blog.csdn.net/Hack_Different/article/details/83004682
注:本次以 SyEmp员工 、SyDept部门 测试 , 一个部门对应多个员工。
一、一方放集合
private List<SyEmp> syEmpList;
public List<SyEmp> getSyEmpList() {
return syEmpList;
}
public void setSyEmpList(List<SyEmp> syEmpList) {
this.syEmpList = syEmpList;
}
二、一方mapper放配置
<第一种,联查映射>
<!--映射Student对象的resultMap(结果映射集)-->
<resultMap type="cn.kaxlm6.mybatis.pojo.SyDept" id="SyDeptMapper">
<result property="deptId" column="Dept_ID" jdbcType="INTEGER"/>
<result property="deptName" column="Dept_Name" jdbcType="VARCHAR"/>
<result property="deptRemark" column="Dept_Remark" jdbcType="VARCHAR"/>
<result property="deptDisabled" column="Dept_Disabled" jdbcType="INTEGER"/>
<!--一对多关联映射:
Collection表示关联的是集合
Property="syEmpList"对应的是Clazz类中List<SyEmp> syEmpList
ofType="cn.kaxlm6.mybatis.pojo.SyEmp"表示映射的类为List<SyEmp>中的SyEmp
fetchType="lazy"表示懒加载,即使用时才实例化
-->
<collection property="syEmpList" ofType="cn.kaxlm6.mybatis.pojo.SyEmp" fetchType="lazy">
<result property="empId" column="Emp_ID" jdbcType="INTEGER"/>
<result property="empName" column="Emp_Name" jdbcType="VARCHAR"/>
<result property="empEmpno" column="Emp_EmpNo" jdbcType="VARCHAR"/>
<result property="empPwd" column="Emp_Pwd" jdbcType="VARCHAR"/>
<result property="empDeptid" column="Emp_DeptID" jdbcType="INTEGER"/>
<result property="empRemark" column="Emp_Remark" jdbcType="VARCHAR"/>
<result property="empDisabled" column="Emp_Disabled" jdbcType="INTEGER"/>
</collection>
</resultMap>
<!--查询语句-->
<select id="queryById" resultMap="SyDeptMapper">
select
*
from tapwater.sy_emp m,tapwater.sy_dept d
where m.Emp_DeptID = d.Dept_ID and d.Dept_ID = #{deptId}
</select>
<第二种,嵌套映射>
<!--映射Student对象的resultMap(结果映射集)-->
<resultMap type="cn.kaxlm6.mybatis.pojo.SyDept" id="SyDeptMapper">
<result property="deptId" column="Dept_ID" jdbcType="INTEGER"/>
<result property="deptName" column="Dept_Name" jdbcType="VARCHAR"/>
<result property="deptRemark" column="Dept_Remark" jdbcType="VARCHAR"/>
<result property="deptDisabled" column="Dept_Disabled" jdbcType="INTEGER"/>
<!--一对多关联映射:
Collection表示关联的是集合
Property="syEmpList"对应的是Clazz类中List<SyEmp> syEmpList
ofType="cn.kaxlm6.mybatis.pojo.SyEmp"表示映射的类为List<SyEmp>中的SyEmp
fetchType="lazy"表示懒加载,即使用时才实例化
column为主表的列名
select为查询的语句,我们调用的是接口代理方法
-->
<collection property="syEmpList" column="Dept_ID"
ofType="cn.kaxlm6.mybatis.pojo.SyEmp" fetchType="lazy"
select="cn.kaxlm6.mybatis.dao.SyEmpDao.queryById" >
</collection>
</resultMap>
<!--查询语句-->
<select id="queryById" resultMap="SyDeptMapper">
select
*
from tapwater.sy_dept
where Dept_ID = #{deptId}
</select>
注:从表mapper也必须有以下select
<!--查询语句-->
<select id="queryById" resultMap="com.baidu.mybatis.pojo.SyEmp">
select
*
from tapwater.sy_emp
where Emp_DeptID = #{deptId}
</select>
三、测试
public class Tests {
public static void main(String[] args) throws Exception {
/**
* 获得SqlSession对象,通过SqlSession操作CRUD
*
*/
SqlSession sqlSession;
//通过Resources类加载核心配置文件,得到文件的输入流
InputStream inputStream = Resources.getResourceAsStream("mybatisConfig.xml");
//创建会话工厂,编译配置文件流,获得sqlsessionfactory
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
//通过sqlSessionFactory得到sqlsession对象
sqlSession = sqlSessionFactory.openSession();
SyDeptDao mapper = sqlSession.getMapper(SyDeptDao.class);
System.out.println(mapper.queryById(9));
sqlSession.commit();
sqlSession.close();
}
}