博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
深入浅出Mybatis系列(八)---mapper映射文件配置之select、resultMap
阅读量:7058 次
发布时间:2019-06-28

本文共 6163 字,大约阅读时间需要 20 分钟。

原文地址:http://www.cnblogs.com/dongying/p/4073259.html

上篇《》 介绍了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 List
courseList; 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"); List
courseList = 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

原文地址:http://www.cnblogs.com/dongying/p/4073259.html

转载于:https://www.cnblogs.com/jerrylz/p/5486360.html

你可能感兴趣的文章
bzoj千题计划316:bzoj3173: [Tjoi2013]最长上升子序列(二分+树状数组)
查看>>
python 3 中建立可迭代对象(making object iterable)
查看>>
linux: 堆排序和快速排序的整理
查看>>
请求数据传入(SpringMVC)
查看>>
第七篇 PHP编码规范
查看>>
队列(queue)
查看>>
jsHint-静态代码检查工具eclipse中使用
查看>>
SDE面试技巧之二:System Design
查看>>
测试中的小事情
查看>>
8.spring:事务管理(上):Spring的数据库编程、编程式事务管理
查看>>
PAT 1014
查看>>
Python异或加密字符串
查看>>
Cesium实现背景透明的方法
查看>>
大数据系列修炼-Scala课程06
查看>>
windows下注册和取消pg服务的方法
查看>>
Dijkstra
查看>>
android webView 播放优酷视频
查看>>
退回win7后无法上网 的解决方法
查看>>
实验4 可视化简历生成
查看>>
【转】解决深入学习PHP的瓶颈?
查看>>