DeptDao接口:
public interface DeptDao {
//根据部门编号,检索部门名称以及该部门下所有员工的信息 单条SQL
public Dept getDeptNO(int deptNO); //一对一单条SQL
public Dept MoseDetpNo(int deptNO); //一对多,多条SQL
}
DaptDao.xml小配置:<?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="cn.bdqn.dao.DeptDao">
<!--根据部门编号,查询部门对象,对象里面包含着员工的集合-->
<resultMap id="depeMper" type="Dept">
<id column="deptNo" property="deptNo"></id>
<result column="deptName" property="deptName"/>
<collection property="emps" ofType="Emp">
<id column="empNo" property="empNo"></id>
<result column="empName" property="empName"/>
</collection>
</resultMap>
有下划线的是属于一个
<resultMap id="deprmerr" type="Dept">
<id column="deptNo" property="deptNo"></id>
<result column="deptName" property="deptName"/>
<collection property="emps" ofType="Emp" select="MoseDetpNoss" column="deptNo"></collection>
</resultMap>
<select id="getDeptNO" resultMap="depeMper">
select dept.deptNo,deptName,empNo,empName
from dept,emp
where dept.deptNo=emp.deptNo
and dept.deptNo=#{deptNo}
</select>
<select id="MoseDetpNo" resultMap="deprmerr">
select deptNo,deptName
from dept
where deptNo=#{deptNo}
</select>
<select id="MoseDetpNoss" resultType="Emp">
select * from emp where deptNo=#{deptNo}
</select>
</mapper>
Depttest测试类
//单条SQl
@Test
public void testDept() throws IOException {
Userinfoutil user=new Userinfoutil();
SqlSession sqlSession=user.sqls();
DeptDao dept=sqlSession.getMapper(DeptDao.class);
Dept dent=dept.getDeptNO(3);
System.out.println(dent.getDeptName());
for (Emp emp:dent.getEmps()) {
System.out.println(emp.getEmpName());
}
sqlSession.close();
}
//多条SQL
@Test
public void testDeptmuil() throws IOException {
Userinfoutil user=new Userinfoutil();
SqlSession sqlSession=user.sqls();
DeptDao dept=sqlSession.getMapper(DeptDao.class);
Dept dent=dept.MoseDetpNo(3);
System.out.println(dent.getDeptName());
for (Emp emp:dent.getEmps()) {
System.out.println(emp.getEmpName());
}
sqlSession.close();
}
EmpDao接口
public interface EmpDao {
//根据员工的编号, 获取到员工姓名 和所属部门名称
public Emp getEmopNo(int empNo); //多对一单条SQL
public Emp MostEmp(int empno); //多对多,多条SQL
}
EmpDao.xml小配置
<?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="cn.bdqn.dao.EmpDao">
<resultMap id="empmerr" type="Emp">
<id column="empNo" property="empNo"></id>
<result column="empName" property="empName"></result>
<association property="dept" javaType="Dept">
<id column="deptNo" property="deptNo"></id>
<result column="deptName" property="deptName"></result>
</association>
</resultMap>
具有下划线的是属于一个方法的
<resultMap id="mostemmper" type="Emp">
<id column="empNo" property="empNo"></id>
<result column="empName" property="empName"/>
<result column="deptNo" property="deptNo"/>
<association property="dept" javaType="Dept" select="mosetmers" column="deptNo"/>
</resultMap>
<select id="mosetmers" resultType="Dept">
select * from dept where deptNo=#{deptNo}
</select>
<select id="MostEmp" resultMap="mostemmper">
select empNo,empName,deptNo
from emp
where empNo=#{empNo}
</select>
<select id="getEmopNo" resultMap="empmerr">
select dept.deptNo,deptName,empNo,empName
from dept,emp
where dept.deptNo=emp.deptNo
and empNo=#{empNo}
</select>
</mapper>
Emptest测试类
package cn.bdqn.test;
import cn.bdqn.dao.EmpDao;
import cn.bdqn.entity.Emp;
import cn.bdqn.util.Userinfoutil;
import org.apache.ibatis.session.SqlSession;
import org.junit.Test;
import java.io.IOException;
/**
* Created by java on 2017/7/14.
*/
public class Emptest {
//多对一单条数据
@Test
public void emputil() throws IOException {
Userinfoutil user=new Userinfoutil();
SqlSession sqlSession=user.sqls();
EmpDao emp=sqlSession.getMapper(EmpDao.class);
Emp em=emp.getEmopNo(3);
System.out.println(em.getEmpName());
System.out.println(em.getDept().getDeptName());
}
@Test
public void testemptmuil() throws IOException {
Userinfoutil user=new Userinfoutil();
SqlSession sqlSession=user.sqls();
EmpDao dept=sqlSession.getMapper(EmpDao.class);
Emp dent=dept.MostEmp(3);
System.out.println(dent.getEmpName());
System.out.println(dent.getDept().getDeptName());
sqlSession.close();
}
}
CategoryDao 接口 自关联
public interface CategoryDao {
//根据pid=查询所有子分类的集合
public List<Category> getChileBypid(int pid);
}
CategoryDao.xml小配置
<?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="cn.bdqn.dao.CategoryDao">
<resultMap id="CteatMapper" type="Category">
<id column="cid" property="cid"></id>
<result column="cname" property="cname"></result>
<result column="pid" property="pid"></result>
<collection property="cates" ofType="Category" select="getChileBypid" column="cid"></collection>
</resultMap>
<select id="getChileBypid" resultMap="CteatMapper">
SELECT * FROM category where pid=#{pid}
</select>
</mapper>
Categorytest测试类
//自关联
@Test
public void testSel() throws IOException {
Userinfoutil user=new Userinfoutil();
SqlSession session=user.sqls();
CategoryDao ct=session.getMapper(CategoryDao.class);
List<Category> list=ct.getChileBypid(1);
for (Category c:list) {
System.out.println(c.getCname());
}
session.close();
}