I.概要:
実際の開発では、データベースの操作には多くの場合、オブジェクト指向のオブジェクト間の関連付けを含む複数のテーブルが含まれます。複数のテーブル間の操作の場合、MyBatisフレームワークは、オブジェクト間の関連付けを適切に処理できる関連付けマッピングを提供します。
2.関係:
Teacher(教師テーブル)とTeaching(教育コーステーブル)の2つのテーブルを見てみましょう。このテーブルの教師のジョブ番号(Tno)とコース番号(cno)は1対1の関係にあります。
3. 1対1の関係:
上記で分析した関係では、教師のジョブ番号をクエリすることで、教師が教えたコース番号を確認できます。また、Teacher永続クラスにteaching属性を追加する必要があります。(これは教師の観点から考えられています)
。
次に、それらのMapper.xml構成ファイルを確認し、最初に対応するインターフェースを確認します。
次に、Mapper.xmlファイルを作成します
次に、インターフェイスに従ってテストメソッドを記述できます。
コード1:
1.TeacherMapper.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="org.example.mapper.TeacherMapper">
<select id="findTeacherByTno" parameterType="string" resultMap="classNameByID">
select * from teacher where Tno=#{Tno}
</select>
<resultMap id="classNameByID" type="Teacher">
<id property="Tno" column="Tno"></id>
<result property="Tname" column="Tname"></result>
<result property="Tsex" column="Tsex"></result>
<result property="Tbirthday" column="Tbirthday"></result>
<result property="deptno" column="deptno"></result>
<association property="teaching" column="tno" javaType="Teaching"
select="org.example.mapper.TeachingMapper.findCnameByTno"/>
</resultMap>
</mapper>
2.TeachingMapper.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="org.example.mapper.TeachingMapper">
<select id="findCnameByTno" parameterType="string" resultType="Teaching">
select * from teaching where tno=#{tno}
</select>
</mapper>
方法2:
コード2:
<?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="org.example.mapper.TeacherMapper">
<select id="findTeacherByTno" parameterType="string" resultMap="classNameByID">
SELECT * FROM teacher a RIGHT JOIN teaching b on a.Tno=b.tno where a.Tno=#{Tno}
</select>
<!-- <resultMap id="classNameByID" type="Teacher">-->
<!-- <id property="Tno" column="Tno"></id>-->
<!-- <result property="Tname" column="Tname"></result>-->
<!-- <result property="Tsex" column="Tsex"></result>-->
<!-- <result property="Tbirthday" column="Tbirthday"></result>-->
<!-- <result property="deptno" column="deptno"></result>-->
<!-- <association property="teaching" column="tno" javaType="Teaching"-->
<!-- select="org.example.mapper.TeachingMapper.findCnameByTno"/>-->
<!-- </resultMap>-->
<resultMap id="classNameByID" type="Teacher">
<id property="Tno" column="Tno"></id>
<result property="Tname" column="Tname"></result>
<result property="Tsex" column="Tsex"></result>
<result property="Tbirthday" column="Tbirthday"></result>
<result property="deptno" column="deptno"></result>
<association property="teaching" javaType="Teaching">
<id property="cno" column="cno"></id>
<result property="tno" column="tno"></result>
<result property="cterm" column="cterm"></result>
</association>
</resultMap>
</mapper>
3. 1対多の関係:
アソシエーションを 使用して、JavaBeanクラスなどのJavaBeanの「複合タイプ」属性にマップします。つまり、複合データ型(JavaBean)属性は、複合タイプのアソシエーションに属するJavaBean内にネストされます。アソシエーション要素は1対1のアソシエーションのみを処理することに注意してください。
たとえば、ある部門が複数の教師に対応している場合、部門番号を照会してから、この部門番号に属する教師の名前を返す必要があります。このとき、1対多の関係を使用してからコレクションを使用して達成します。
TeacherコレクションをDepartment永続エンティティクラスに格納するList<Teacher>プロパティを追加します
TeacherMapperインターフェースにメソッドを追加します
List<Teacher> findTeacherBydeptno(String deptno);
TeacherMapper.xml
<select id="findTeacherBydeptno" parameterType="string" resultType="Teacher">
SELECT * FROM teacher WHERE deptno=#{deptno}
</select>
テスト:
コード:
<?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="org.example.mapper.DepartmentMapper">
<select id="findTeachersBydeptno" parameterType="string" resultMap="findteachsResult" >
SELECT * FROM department WHERE deptno=#{deptno}
</select>
<resultMap id="findteachsResult" type="Department">
<id property="deptno" column="deptno"></id>
<result property="deptname" column="deptname"></result>
<result property="deptheader" column="deptheader"></result>
<result property="office" column="office"></result>
<collection property="teachers" column="deptno" ofType="Teacher"
select="org.example.mapper.TeacherMapper.findTeacherBydeptno"/>
</resultMap>
</mapper>
方法2:
4.多対多:
実際の開発では、多対多の関係も非常に一般的です。学校では、学生が複数のコースを選択し、多くの学生がコースを選択します。これは多対多の関係です。以下では、多対多の関係を実装する方法を理解しています。
次に、最初に2つの永続クラスScとStudentを作成します
および対応するインターフェイス:
package org.example.mapper;
import org.apache.ibatis.annotations.Param;
import org.example.po.Student;
import java.util.List;
public interface StudentMapper {
List<Student> getStudent(@Param("cno")String cno);
}
package org.example.service;
import org.apache.ibatis.annotations.Param;
import org.example.po.Sc;
import java.util.List;
public interface ScService {
List<Sc> getSc(String cno);
}
マッパー構成ファイル:
StudentMapper.xml:
SCMapper.xml:
コード:
ScMapper.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="org.example.mapper.ScMapper">
<resultMap id="Sclist1" type="Sc">
<id property="sno" column="sno"></id>
<result property="cno" column="cno"></result>
<result property="degree" column="degree"></result>
<collection property="students" ofType="Student" column="cno"
select="org.example.mapper.StudentMapper.getStudent"/>
</resultMap>
<select id="getSc" parameterType="string" resultMap="Sclist1">
select * from sc
<if test="cno!=null and cno!=''">where cno=#{cno}</if>
</select>
</mapper>
StudentMapper.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="org.example.mapper.StudentMapper">
<select id="getStudent" resultType="Student" parameterType="string">
SELECT * from student WHERE sno IN (SELECT sno FROM sc WHERE cno=#{cno})
</select>
</mapper>
サービスパート:
package org.example.service;
import org.apache.ibatis.annotations.Param;
import org.example.po.Sc;
import java.util.List;
public interface ScService {
List<Sc> getSc(String cno);
}
package org.example.service;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.example.mapper.ScMapper;
import org.example.po.Sc;
import java.util.List;
public class ScServiceImpl implements ScService{
private SqlSessionFactory sqlSessionFactory;
public ScServiceImpl(SqlSessionFactory sqlSessionFactory){
this.sqlSessionFactory=sqlSessionFactory;
}
@Override
public List<Sc> getSc(String cno) {
SqlSession sqlSession=sqlSessionFactory.openSession();
ScMapper scMapper=sqlSession.getMapper(ScMapper.class);
List<Sc> scs=scMapper.getSc(cno);
sqlSession.close();
return scs;
}
}
試験方法:
package org.example;
import static org.junit.Assert.assertTrue;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.example.mapper.TeacherMapper;
import org.example.po.Department;
import org.example.po.Sc;
import org.example.po.Student;
import org.example.po.Teacher;
import org.example.service.DepartmentServiceImpl;
import org.example.service.ScServiceImpl;
import org.example.service.TeacherServiceImpl;
import org.junit.Before;
import org.junit.Test;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
/**
* Unit test for simple App.
*/
public class AppTest
{
private SqlSessionFactory sqlSessionFactory;
@Before
public void init() throws IOException {
InputStream in= Resources.getResourceAsStream("MyBatis_config.xml");
this.sqlSessionFactory=new SqlSessionFactoryBuilder().build(in);
}
// @Test
// public void findTeacherByTno(){
// TeacherServiceImpl teacherService=new TeacherServiceImpl(sqlSessionFactory);
// Teacher teacher=teacherService.findTeacherByTno("601");
// System.out.println(teacher);
// }
// @Test
// public void findteacherDeptno(){
// DepartmentServiceImpl departmentService=new DepartmentServiceImpl(sqlSessionFactory);
// List<Department> department=departmentService.findTeachersBydeptno("d01");
//
// }
@Test
public void getstudent(){
ScServiceImpl scService=new ScServiceImpl(sqlSessionFactory);
List<Sc> scs=scService.getSc(null);
for (Sc sc:scs){
System.out.println("课程编号为"+sc.getCno()+"的课:");
for(Student student:sc.getStudents()){
System.out.println("学生姓名为:"+student.getSname()+"学生班级为:"+student.getClassno());
}
}
}
}