多对一
<?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.kl.repository.StudentRepository">
<!-- <select id="getAll" resultType="com.kl.entity.Student">-->
<!-- select * from Student;-->
<!-- </select>-->
<resultMap id="studentMap" type="com.kl.entity.Student">
<id column="sid" property="id"></id>
<result column="sname" property="name"></result>
<association property="tid"
javaType="com.kl.entity.Teacher">
<id property="id" column="tid"></id>
<result property="name" column="tname"></result>
</association>
</resultMap>
<select id="getById" parameterType="java.lang.Integer" resultMap="studentMap">
select s.id sid,s.name sname,t.id tid,t.name tname from student s,teacher t where s.id =#{id} and s.tid = t.id
</select>
</mapper>
一对多
<?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.kl.repository.TeacherRepository">
<resultMap id="classesMap" type="com.kl.entity.Teacher">
<id property="id" column="tid"></id>
<result property="name" column="tname"></result>
<collection property="student" ofType="com.kl.entity.Student">
<id property="id" column="sid"></id>
<result property="name" column="sname"></result>
</collection>
</resultMap>
<select id="getById" parameterType="java.lang.Integer" resultMap="classesMap">
select t.id tid,t.name tname,s.id sid,s.name sname from student s,teacher t where t.id =#{id} and t.id = s.tid
</select>
</mapper>
多对多
<?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.southwind.repository.AccountRepository">
<resultMap id="accoutMap" type="com.southwind.entity.Account">
<id column="aid" property="id"></id>
<result column="aname" property="name"></result>
<collection property="courses" ofType="com.southwind.entity.Course">
<id column="cid" property="id"/>
<result column="cname" property="name"/>
</collection>
</resultMap>
<select id="findById" parameterType="java.lang.Integer"
resultMap="accoutMap">
select a.id aid,a.name aname,c.id cid,c.name cname from t_account
a,account_course ac,t_course c where a.id = #{id} and a.id = ac.aid and c.id =
ac.cid
</select>
</mapper>