深入浅出Mybatis系列(八)---mapper映射文件配置之select、resultMap good
2016-07-19 00:33
816 查看
上篇《深入浅出Mybatis系列(七)---mapper映射文件配置之insert、update、delete》介绍了insert、update、delete的用法,本篇将介绍select、resultMap的用法。select无疑是我们最常用,也是最复杂的,mybatis通过resultMap能帮助我们很好地进行高级映射。下面就开始看看select 以及 resultMap的用法:
先看select的配置吧:
View Code
相信通过以上demo, 大家也能够使用mybatis的select 和 resultMap的用法了。上面demo只演示了一对多的映射,其实多对一、多对多也与它类似,所以我就没演示了,有兴趣的可以自己动手再做做。
好啦,本次就写到这儿了。(PS,生病一周了,所以到现在才更新博客)。
另附上demo, 需要的童鞋可以前往下载:
demo 下载地址:http://pan.baidu.com/s/1qWjsDzA
http://www.cnblogs.com/dongying/p/4073259.html
先看select的配置吧:
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<Course> 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; } }
View Code
相信通过以上demo, 大家也能够使用mybatis的select 和 resultMap的用法了。上面demo只演示了一对多的映射,其实多对一、多对多也与它类似,所以我就没演示了,有兴趣的可以自己动手再做做。
好啦,本次就写到这儿了。(PS,生病一周了,所以到现在才更新博客)。
另附上demo, 需要的童鞋可以前往下载:
demo 下载地址:http://pan.baidu.com/s/1qWjsDzA
http://www.cnblogs.com/dongying/p/4073259.html
相关文章推荐
- 转 android 侧滑实现
- log4j:WARN No appenders could be found for logger报错问题解决方案
- Android NDK : 基本类型,字符串与数组
- android MVP 模式介绍与实战
- Android百度地图(二)结合方向传感器我们自己定位哪里走
- android CoordinatorLayout使用
- android 开发之画布初步探索
- Unity3d 销毁
- Unity3d 碰撞检测
- unity3d 鼠标事件
- Unity3d 刚体
- Android App应用启动分析与优化
- 又一款产品因微信封杀“一夜归零”,朋友圈营销动了谁的奶酪?
- 创业做一个App究竟要花多少钱?
- Android五布局——线性布局LinearLayout
- 关于iOS 录音并且转码上传的相关问题
- Android 验证码倒计时 与 getDrawable(int) 方法过期问题
- 直接调用android原生接口实现点击直接拨打电话功能
- Android String占位符功能[笔记]
- android 显示特殊符号[笔记]