MyBatis学习系列三——结合Spring
目录
MyBatis在项目中应用一般都要结合Spring,这一章主要把MyBatis结合到Spring框架中。
数据库环境:MySQL
1、源码结构图

引用Jar包:

说明:Spring的Jar包,这里不再详细说明。
MyBatis相关的Jar包:mybatis-3.2.8.jar、mybatis-spring-1.1.1.jar
MySQL相关的Jar包:mysql-connector-java-5.0.8.jar
2、MyBatis和Spring的结合
DAO和PO中的文件(DoctorMapper.java、DoctorMapper.xml、Doctor.java)我们在前两期中已经详细的说明过。
MyBatis的映射配置文件mybatis-config.xml 关联到 applicationContext.xml 中,获取sqlSessionFactory,
applicationContext.xml源码如下:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd"> <context:component-scan base-package="nankang/service" />
<bean id="dataSource"
class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close"
p:driverClassName="com.mysql.jdbc.Driver"
p:url="jdbc:mysql://localhost:3306/childrendb?useUnicode=true&characterEncoding=UTF-8&zeroDateTimeBehavior=convertToNull"
p:username="root"
p:password="world" />
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="configLocation" value="classpath:mybatis-config.xml" />
<property name="dataSource" ref="dataSource" />
</bean> </beans>
mybatis-config.xml进行了简化,去掉了连接数据库的部分,源码如下:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<mappers>
<mapper resource="nankang/dao/DoctorMapper.xml" />
</mappers>
</configuration>
3、代码中使用SqlSessionFactory
在后台提供数据服务的DoctorService中,直接使用SqlSessionFactory即可,源码如下:
package nankang.service; import java.util.List; import nankang.dao.DoctorMapper;
import nankang.po.Doctor; import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service; @Service
public class DoctorService { @Autowired
private SqlSessionFactory sqlSessionFactory; public Doctor getAllDoctors() { SqlSession sqlSession = sqlSessionFactory.openSession();
DoctorMapper doctorMapper = sqlSession.getMapper(DoctorMapper.class); List<Doctor> doctorList = doctorMapper.selectAllDoctors();
return doctorList.get(0);
}
}
4、Spring结构相关
Spring相关的,获取结果并展示,可以参考源码。
5、结果展示
浏览器中输入:http://localhost:8080/myBatis3/index.html
展示结果:

6、源码下载:http://pan.baidu.com/s/1pJmeYpX (Fish的分享>MyBatis>myBatis3.rar)
MyBatis学习系列三——结合Spring的更多相关文章
- mybatis学习系列三(部分)
1 forearch_oracle下批量保存(47) oracle批量插入 不支持values(),(),()方式 1.多个insert放在begin-end里面 begin insert into ...
- MyBatis学习系列二——增删改查
目录 MyBatis学习系列一之环境搭建 MyBatis学习系列二——增删改查 MyBatis学习系列三——结合Spring 数据库的经典操作:增删改查. 在这一章我们主要说明一下简单的查询和增删改, ...
- MyBatis学习系列一之环境搭建
目录 MyBatis学习系列一之环境搭建 MyBatis学习系列二——增删改查 MyBatis学习系列三——结合Spring 学习一个新的知识,首先做一个简单的例子使用一下,然后再逐步深入.MyBat ...
- MyBatis学习 之 三、动态SQL语句
目录(?)[-] 三动态SQL语句 selectKey 标签 if标签 if where 的条件判断 if set 的更新语句 if trim代替whereset标签 trim代替set choose ...
- 【转】MyBatis学习总结(三)——优化MyBatis配置文件中的配置
[转]MyBatis学习总结(三)——优化MyBatis配置文件中的配置 一.连接数据库的配置单独放在一个properties文件中 之前,我们是直接将数据库的连接配置信息写在了MyBatis的con ...
- mybatis入门系列三之类型转换器
mybatis入门系列三之类型转换器 类型转换器介绍 mybatis作为一个ORM框架,要求java中的对象与数据库中的表记录应该对应 因此java类名-数据库表名,java类属性名-数据库表字段名, ...
- scrapy爬虫学习系列三:scrapy部署到scrapyhub上
系列文章列表: scrapy爬虫学习系列一:scrapy爬虫环境的准备: http://www.cnblogs.com/zhaojiedi1992/p/zhaojiedi_python_00 ...
- DocX开源WORD操作组件的学习系列三
DocX学习系列 DocX开源WORD操作组件的学习系列一 : http://www.cnblogs.com/zhaojiedi1992/p/zhaojiedi_sharp_001_docx1.htm ...
- RabbitMQ学习系列三-C#代码接收处理消息
RabbitMQ学习系列三:.net 环境下 C#代码订阅 RabbitMQ 消息并处理 http://www.80iter.com/blog/1438251320680361 http://www. ...
随机推荐
- ylbtech-Recode(记录)-数据库设计
ylbtech-dbs:ylbtech-Recode(记录)-数据库设计 -- =============================================-- DatabaseName ...
- 深入研究java.lang.ThreadLocal类(转)
引用:http://lavasoft.blog.51cto.com/62575/51926/ 一.概述 ThreadLocal是什么呢?其实ThreadLocal并非是一个线程的本地实现版本,它并 ...
- Jmeter-Maven-Plugin高级应用:Selecting Tests To Run
地址:https://github.com/jmeter-maven-plugin/jmeter-maven-plugin/wiki/Advanced-Configuration Selecting ...
- JDK环境变量中dt.jar、tools.jar等变量值的作用
变量名:CLASSPATH 变量值:.;%JAVA_HOME%\lib\dt.jar;%JAVA_HOME%\lib\tools.jar; tools.jar: 工具类 库,它跟我们程序中用到的 基础 ...
- springmvc笔记(来自慕课网)
1.准备工作:springmvc相关的jar包. 2.这里我们先用eclipse来操作. 首先看一个接口编程,后面的所有知识点都是通过这个接口编程引出的. OneInterface.java pack ...
- POJ - 1159 Palindrome(dp-回文变形)
d.求对字符串最少添加几个字符可变为回文串. s. 法1:直接对它和它的逆序串求最长公共子序列长度len.N-len即为所求.(N为串长度) 因为,要求最少添加几个字符,我们可以先从原串中找到一个最长 ...
- nyoj 90 整数划分
点击打开链接 整数划分 时间限制:3000 ms | 内存限制:65535 KB 难度:3 描述 将正整数n表示成一系列正整数之和:n=n1+n2+-+nk, 其中n1≥n2≥-≥nk≥1,k≥ ...
- UIApplication及UIWindow
*:first-child { margin-top: 0 !important; } body > *:last-child { margin-bottom: 0 !important; } ...
- [原创] IIS7下顶级域名301跳转到WWW域名
百度搜索了众多方法,居然没有一个全面的IIS7下301域名跳转能用的教程,最终自己研究出了个可以用的供大家参考.1.绑定域名01ruodian.cn www.01ruodian.cn到空间: 2.在I ...
- UDKtoUE4Tool-UDKUE3资源移植UE4工具
UDKtoUE4Tool UDKtoUE4Tool 是一个把UE3/UDK资源包(T3D格式)转换成UE4(T3D格式)的工具.作者Matt3D使用C#实现,未来考虑发布到Unreal Marketp ...