mybatis学习之路(四)mybatis实现一对多关系并附一对多关系失效的一种解决方案

mybatis实现一对多关系失效的解决方案
1.< resultMap id ="" type ="" ></ resultMap >标签中的属性字段在编写SQL语句时必须查询相应字段。即如果标签中有id字段,则在编写SQL语句的时候必须查询select id。

mybatis实现一对多的精髓:
失效的情况:

成功的样子:

以下是mybatis一对多关系的详细配置:
①首先建立与数据库对应的POJO
数据库的表信息:



class类
public class TestClass {
//班级ID
private int classId;
//班级名称
private String className;
//所属班级的学生
private List<TestStudent> studentList;

public int getClassId() {
return classId;
}

public void setClassId(int classId) {
this.classId = classId;
}

public String getClassName() {
return className;
}

public void setClassName(String className) {
this.className = className;
}

public List<TestStudent> getStudentList() {
return studentList;
}

public void setStudentList(List<TestStudent> studentList) {
this.studentList = studentList;
}

public TestClass() {
}

public TestClass(int classId, String className, List<TestStudent> studentList) {
this.classId = classId;
this.className = className;
this.studentList = studentList;
}

@Override
public String toString() {
return "TestClass{" +
"classId=" + classId +
", className='" + className + '\'' +
", studentList=" + studentList +
'}';
}
}
student类
public class TestStudent {
private int studentId;
private String studentName;
private int studentClassId;

public int getStudentId() {
return studentId;
}

public void setStudentId(int studentId) {
this.studentId = studentId;
}

public String getStudentName() {
return studentName;
}

public void setStudentName(String studentName) {
this.studentName = studentName;
}

public int getStudentClassId() {
return studentClassId;
}

public void setStudentClassId(int studentClassId) {
this.studentClassId = studentClassId;
}

public TestStudent() {
}

public TestStudent(int studentId, String studentName, int studentClassId) {
this.studentId = studentId;
this.studentName = studentName;
this.studentClassId = studentClassId;
}

@Override
public String toString() {
return "TestStudent{" +
"studentId=" + studentId +
", studentName='" + studentName + '\'' +
", studentClassId=" + studentClassId +
'}'; 
②mybatis的mapper配置文件
    这里要注意的是如果以class为主类,学生为子类,则是一对多的关系,选用 <collection />标签
而<association />是多对一的标签。
<mapper namespace="TestClass">

<resultMap type="algorithm.offer.pojo.TestClass" id="TestClassResult">
<id column="class_id" jdbcType="INTEGER" property="classId"/>
<result column="class_name" jdbcType="VARCHAR" property="className"/>
<!--导入Student子类-->
<collection property="studentList" resultMap="TestStudent.TestStudentResult"/>
</resultMap>

<select id="findStudent" parameterType="int" resultMap="TestClassResult">
SELECT  class.class_id,class.class_name,student.student_name FROM class
LEFT JOIN student ON class.class_id = student.student_class_id
WHERE class.class_id = #{_parameter}
</select>


</mapper>
student子类的mapper配置文件
<mapper namespace="TestStudent">

<resultMap type="algorithm.offer.pojo.TestStudent" id="TestStudentResult">
<id column="student_id" jdbcType="INTEGER" property="studentId"/>
<result column="student_name" jdbcType="VARCHAR" property="studentName"/>
<result column="student_class_id" jdbcType="INTEGER" property="studentClassId"/>
</resultMap>

</mapper>
③将mapper.xml配置到mybatis的配置文件中
<mappers>
<mapper resource="MybatisMapper/TestClassMapper.xml"/>
<mapper resource="MybatisMapper/TestStudentMapper.xml"/>
</mappers>
④DAO层(以class为主类,返回class类型的数组列表)
public class ClassDAO {
DBAccess dbAccess = new DBAccess();
SqlSession sqlSession = null;

/**
* 连表查询
* @param classId
* @return
*/
public List<TestClass> queryStudentByClassId(int classId){
List<TestClass> studentList = new ArrayList<>();

try {
sqlSession = dbAccess.getSqlSession();
studentList = sqlSession.selectList("TestClass.findStudent",classId);
} catch (IOException e) {
e.printStackTrace();
}finally {
if (sqlSession!=null){
sqlSession.close();
}
}

return studentList;
}
⑤service层
public class ClassService {
ClassDAO classDAO = new ClassDAO();

/**
* 连表查询
* @param classId
* @return
*/
public List<TestClass> queryStudentByClassId(int classId){
return classDAO.queryStudentByClassId(classId);
}
}
⑥测试结果
@Test
public void queryStudentByClassId() throws Exception {
ClassService classService = new ClassService();
List<TestClass> testClassList = classService.queryStudentByClassId(1);

for (int i=0;i<testClassList.get(0).getStudentList().size();i++) {
System.out.println(testClassList.get(0).getStudentList().get(i).getStudentName());
}

}


猜你喜欢

转载自blog.csdn.net/weixin_42228338/article/details/80411464
今日推荐