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. ...
随机推荐
- perl处理含有中文字符的json编码
例子:1. 有php的 json函数生成的中文串 [root@tts177:/tmp]$/opt/php/bin/php -r 'echo json_encode(Array("a" ...
- [Flex] ButtonBar系列——flex3 ButtonBar各项之间的间距调整
<?xml version="1.0" encoding="utf-8"?> <mx:Application xmlns:mx="h ...
- position中多次用到了relative和absolute,能不能具体介绍一下这两者的区别?
position中多次用到了relative和absolute,能不能具体介绍一下这两者的区别? 一个是相对定位,一个是绝对定位. absolute生成绝对定位的元素,相对于 static 定位以外的 ...
- [SQL]SQL中把一个字段的数据分多行显示
其实你完全可以写个自定义函数就OK了 . =================================================================== create func ...
- Arch xfce4 安装解压缩软件
>>>安装方法 pacman -S thunar-archive-plugin xarchiver zip unzip p7zip arj lzop cpio unrar >& ...
- Android——inflate 将一个xml中定义的布局找出来
通俗的说,inflate就相当于将一个xml中定义的布局找出来. 因为在一个Activity里如果直接用findViewById()的话,对应的是setConentView()的那个layout里的组 ...
- app与服务器对接
如何做ios的app与服务器的数据传输
- oracle 监听动态和静态的配置
[oracle@oracledb1 admin]$ vi listener.ora # listener.ora Network Configuration File: /u01/app/oracle ...
- ofstream的问题
ofstream在多字节编码的项目中, 写入中文目录写不了, 英文目录可以 换成c库的fopen, fwrite可以
- 解决PHP生成校验码时“图像因其本身有错无法显示”的错误
今天使用http://crazymud.iteye.com/blog/452293给出的代码进行PHP生成校验码功能的实现,发现firefox一直提示“图像.......因其本身有错无法显示”的问题, ...