mybatis关联映射
2017-08-16 08:58
169 查看
场景:以学生和班级为例,一个学生只有一个班级,一个班级包含多个学生
说明:mapper标签中我没有添加namespace
1.StudentMapper.xml
2.ClazzMapper.xml
说明:mapper标签中我没有添加namespace
1.StudentMapper.xml
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" > <mapper> <resultMap id="studentResultMap" type="com.demo.domain.Student"> <id property="id" column="id"/> <result property="name" column="name"/> <result property="sex" column="sex"/> <result property="age" column="age"/> <!--关联映射:一个学生关联一个班级--> <association property="clazz" column="clazz_id" javaType="com.demo.domain.Clazz" select="selectClazzWithId"/> </resultMap> <!--查询关联班级--> <select id="selectClazzWithId" resultType="com.demo.domain.Clazz"> SELECT * FROM TB_CLAZZ WHERE id = #{id} </select> <!--查询学生--> <select id="selectStudent" resultMap="studentResultMap"> SELECT * FROM TB_STUDENT </select> </mapper>
2.ClazzMapper.xml
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" > <mapper> <resultMap id="clazzResultMap" type="com.demo.domain.Clazz"> <id property="id" column="id"/> <result property="code" column="code"/> <!--关联映射:一个班级有多个学生,返回一个List集合--> <collection property="students" javaType="ArrayList" column="id" ofType="com.demo.domain.Student" select="selectStudentWithId"/> </resultMap> <!--查询关联的学生--> <select id="selectStudentWithId" resultType="com.demo.domain.Student"> SELECT * FROM TB_STUDENT WHERE clazz_id = #{id} </select> <!--查询班级--> <select id="selectClazz" resultMap="clazzResultMap"> SELECT * FROM TB_CLAZZ </select> </mapper>
相关文章推荐
- MyBatis框架的一对多关联映射
- MyBatis学习(二)之双向一对多关联映射查询
- MyBatis高级映射之-一对一(一对多)关联映射
- MyBatis框架的多对多关联映射
- mybatis(五)——mybatis表与表之间的关联映射
- MyBatis映射文件的resultMap如何做表关联
- MyBatis映射文件的resultMap如何做表关联
- mybatis的一对多双向关联映射
- MyBatis框架的一对一关联映射
- MyBatis映射文件的resultMap如何做表关联
- spring boot(9)-mybatis关联映射
- 0049 MyBatis关联映射--一对一关系
- Mybatis 动态SQL和关联映射
- MyBatis 3(4)关联映射:一对一,一对多
- MyBatis框架基于Annotation注解的一对一关联映射
- Mybatis一对一、一对多关联映射
- java的orm框架 mybatis 多对多 一对多关系的关联映射和查询--简单易懂,理解才是王道
- mybatis实现一对一关联映射实例代码
- mybatis 自身关联 映射
- 0050 MyBatis关联映射--一对多关系