MyBatis 一对多 多对一 自关联 例子
2017-07-14 17:14
399 查看
public interface IDepet { public Dept getAll(int id); public Dept getAllMutlSql(int id); }
<mapper namespace="cn.hello.dao.IDepet"> <resultMap id="deptMap" type="Dept"> <id column="deptNo" property="deptNo"></id> <result column="deptName" property="deptName"></result> <collection property="emps" ofType="Emp"> <id column="empNo" property="empNo"></id> <result column="empName" property="empName"></result> </collection> </resultMap> <resultMap id="deptMapMutlSql" type="Dept"> <id column="deptNo" property="deptNo"></id> <result column="deptName" property="deptName"></result> <collection property="emps" ofType="Emp" select="selectDeptNo" column="deptNo"></collection> </resultMap> <!--一对多 多条SQ--> <select id="selectDeptNo" resultType="Emp"> SELECT * FROM emp WHERE deptNo=#{deptNo} </select> <!--一对多 单条SQL--> <select id="getAll" resultMap="deptMap"> select dept.deptNo,deptName,empNo,empName from dept,emp where dept.deptNo=emp.deptNo and dept.deptNo=#{deptNo} </select> <!--一对多 多条SQ--> <select id="getAllMutlSql" resultMap="deptMapMutlSql"> select deptNo,deptName from dept where deptNo=#{deptNo} </select> </mapper>
public interface IEmp { public Emp SelectEmpNo(int id); public Emp SelectEmpNoMutile(int id); } <mapper namespace="cn.hello.dao.IEmp"> <resultMap id="empMap" 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="empMapMuit" type="Emp"> <id column="empNo" property="empNo"></id> <result column="empName" property="empName"></result> <association property="dept" javaType="Dept" select="selectDeptNo" column="deptNo"></association> </resultMap> <!--多对一 多条SQL--> <select id="selectDeptNo" resultType="Dept"> SELECT * FROM dept WHERE deptNo=#{deptNo} </select> <!--多对一 一条SQL--> <select id="SelectEmpNo" resultMap="empMap"> select dept.deptNo,deptName,empNo,empName from dept,emp where dept.deptNo=emp.deptNo and emp.empNo=#{empNo} </select> <!--多对一 多条SQL--> <select id="SelectEmpNoMutile" resultMap="empMapMuit"> SELECT deptNo,empNo,empName FROM emp WHERE empNo=#{empNo} </select> </mapper>
public interface ICategory { public List<Category> getChildreByPid(int id); } <mapper namespace="cn.hello.dao.ICategory"> <!--自关联--> <resultMap id="CateMap" 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="getChildreByPid" column="cid"></collection> </resultMap> <select id="getChildreByPid" resultMap="CateMap"> SELECT * FROM category WHERE pid=#{pid} </select> </mapper>
相关文章推荐
- MyBatis--关联关系查询--自关联--一对多--查询指定父节点的所有子节点
- mybatis 实现一对一,一对多,多对多关联查询 小结
- hibernate映射一对多,双向关系[关联自身],初学例子四
- MyBatis实现关联表查询(一对一,一对多,联合查询,嵌套查询)
- Mybatis 注解关联 (一对多、多对一)
- MyBatis-一对多关联表查询 -06
- mybatis的关联:自关联、一对一、一对多、多对多
- 关于mybatis多对多关联查询 下面的例子让新手看不错。可以改进的一点是 1、不必建立 关联对象实体,可以用map传递两个id进去 2、文章的resultMa
- MyBatis 3(4)关联映射:一对一,一对多
- MyBatis 【一对多、多对一 || 多对多也一样的】【非回环关联】
- MyBatis 关联查询 自关联 (一对多、多对一)
- Mybatis 一对一、一对多、多对多关联之级联添加
- Mybatis学习——一对多关联表查询
- Mybatis一对一,一对多,自关联
- Mybatis总结(5)--实现关联表查询(一对一、一对多)
- MyBatis 一对多 多对一关联 trim foreach
- MyBatis 一对多,多对一关联查询的时候Mapper的顺序
- MyBatis--关联关系查询--自关联--一对多--查询指定父节点及其所有子
- MyBatis高级映射之-一对一(一对多)关联映射
- 用到了yii2 hasMany() 方法,一对多关联