上篇《深入浅出Mybatis系列(七)---mapper映射文件配置之insert、update、delete》介绍了insert、update、delete的用法,本篇将介绍select、resultMap的用法。select无疑是我们最常用,也是最复杂的,mybatis通过resultMap能帮助我们很好地进行高级映射。下面就开始看看select 以及 resultMap的用法:
先看select的配置吧:
配置看起来总是这么多,不过实际常用的配置也就那么几个, 根据自己的需要吧,上面都已注明是否必须配置。
下面还是上个demo及时练练手吧:
------------------------------------------------------------------------下面是针对select 的练手demo---------------------------------------------------------------------------------------
数据库:新增两张表(t_course, t_student)
t_course:
t_student:
其中,1个student可选择多个course进行学习。
我们还是拿上篇文章的demo, 继续写:
增加后,项目目录如下所示:
Course.java:
package com.dy.entity; public class Course { private int id; private String name; private int deleteFlag; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getDeleteFlag() { return deleteFlag; } public void setDeleteFlag(int deleteFlag) { this.deleteFlag = deleteFlag; } }
Student.java:
package com.dy.entity; import java.util.List; public class Student { private int id; private String idCard; private String name; private ListcourseList; private int deleteFlag; public Student(int id, String idCard, String name, List courseList, int deleteFlag) { this.id = id; this.idCard = idCard; this.name = name; this.courseList = courseList; this.deleteFlag = deleteFlag; } public int getId() { return id; } public void setId(int id) { this.id = id; } public String getIdCard() { return idCard; } public void setIdCard(String idCard) { this.idCard = idCard; } public String getName() { return name; } public void setName(String name) { this.name = name; } public List getCourseList() { return courseList; } public void setCourseList(List courseList) { this.courseList = courseList; } public int getDeleteFlag() { return deleteFlag; } public void setDeleteFlag(int deleteFlag) { this.deleteFlag = deleteFlag; } }
CourseDao.java:
package com.dy.dao; import com.dy.entity.Course; public interface CourseDao { public Course findCourseById(int courseId); }
StudentDao.java:
package com.dy.dao; import com.dy.entity.Student; public interface StudentDao { public Student findStudentById(String idCard); }
courseDao.xml:
CourseDaoTest.java:
package com.dy.dao; import java.io.IOException; 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.junit.Test; import com.dy.entity.Course; public class CourseDaoTest { @Test public void findCourseById() { SqlSessionFactory sqlSessionFactory = getSessionFactory(); SqlSession sqlSession = sqlSessionFactory.openSession(); CourseDao courseDao = sqlSession.getMapper(CourseDao.class); Course course = courseDao.findCourseById(1); } //Mybatis 通过SqlSessionFactory获取SqlSession, 然后才能通过SqlSession与数据库进行交互 private static SqlSessionFactory getSessionFactory() { SqlSessionFactory sessionFactory = null; String resource = "mybatis-conf.xml"; try { sessionFactory = new SqlSessionFactoryBuilder().build(Resources .getResourceAsReader(resource)); } catch (IOException e) { e.printStackTrace(); } return sessionFactory; } }
上面的示例,我们针对course, 简单演示了 select的用法, 不过有个问题值得思考: 一个student可以对应多个course, 那么,在mybatis中如何处理这种一对多, 甚至于多对多,一对一的关系呢?
这儿,就不得不提到 resultMap 这个东西, mybatis的resultMap功能可谓十分强大,能够处理复杂的关系映射, 那么resultMap 该怎么配置呢? 别急,这就来了:
resultMap的配置:
好啦,知道resutMap怎么配置后,咱们立即接着上面的demo来练习一下吧:
------------------------------------------------------------------下面是用resultMap处理一对多关系的映射的示例-------------------------------------------------------------
一个student对应多个course, 典型的一对多,咱们就来看看mybatis怎么配置这种映射吧:
studentDao.xml:
StudentDaoTest.java:
package com.dy.dao; import java.io.IOException; import java.util.List; 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.junit.Test; import com.dy.entity.Course; import com.dy.entity.Student; public class StudentDaoTest { @Test public void findCourseById() { SqlSessionFactory sqlSessionFactory = getSessionFactory(); SqlSession sqlSession = sqlSessionFactory.openSession(); StudentDao studentDao = sqlSession.getMapper(StudentDao.class); Student student = studentDao.findStudentById("20140101"); ListcourseList = student.getCourseList(); for (Course course: courseList) { System.out.println(course.getId() + " " + course.getName()); } } //Mybatis 通过SqlSessionFactory获取SqlSession, 然后才能通过SqlSession与数据库进行交互 private static SqlSessionFactory getSessionFactory() { SqlSessionFactory sessionFactory = null; String resource = "mybatis-conf.xml"; try { sessionFactory = new SqlSessionFactoryBuilder().build(Resources .getResourceAsReader(resource)); } catch (IOException e) { e.printStackTrace(); } return sessionFactory; } }
相信通过以上demo, 大家也能够使用mybatis的select 和 resultMap的用法了。上面demo只演示了一对多的映射,其实多对一、多对多也与它类似,所以我就没演示了,有兴趣的可以自己动手再做做。
好啦,本次就写到这儿了。(PS,生病一周了,所以到现在才更新博客)。
另附上demo, 需要的童鞋可以前往下载:
demo 下载地址:http://pan.baidu.com/s/1qWjsDzA
原文地址:https://www.cnblogs.com/dongying/p/4073259.html