前言:在前面一篇文章中,介绍了单独使用MyBatis连接orace的例子,在这里分享学习下Spring和MyBatis是如何整合的,以具体工程为例子

阅读目录:

  • 1.环境准备
  • 2.搭建工程
    • 2.1.applicationContext.xml
    • 2.2.userMapper.xml
    • 2.3.测试类

MyBatis和Spring整合的官方地址,http://www.mybatis.org/spring/zh/index.html

1.环境准备

所需环境  Eclipse+maven

所需jar包    mybatis-3.4.5.jar

spring-context-4.3.1.RELEASE.jar spring相关jar包

mybatis-spring-1.3.1.jar

2.搭建工程

工程结构如下图所示

2.1 spring框架的配置文件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:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd"> <!--1 引入属性文件,在配置中占位使用 -->
<context:property-placeholder location="classpath:db.properties" /> <bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="${driver}" />
<property name="url" value="${url}" />
<property name="username" value="otatransuser" />
<property name="password" value="${password}" />
</bean> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<!-- sql映射文件路径 -->
<property name="mapperLocations" value="classpath:mapper/*.xml"></property>
</bean> <bean id="userMapper" class="org.mybatis.spring.mapper.MapperFactoryBean">
<property name="mapperInterface" value="com.ota.mapper.UserMapper" />
<property name="sqlSessionFactory" ref="sqlSessionFactory" />
</bean> <!--5 声明式事务管理 -->
<!--定义事物管理器,由spring管理事务 -->
<bean id="transactionManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"></property>
</bean> </beans>

db.properties文件放在类路径下,内容为:

driver=oracle.jdbc.driver.OracleDriver
url=jdbc\:oracle\:thin\:@(DESCRIPTION\=(ADDRESS\=(PROTOCOL\=TCP)(HOST\=10.221.129.208)(PORT\=1523))(CONNECT_DATA\=(SERVICE_NAME\=otatransuser)))
username=otatransuser
password=otatransuser123

要注意:(1)sqlSessionFactory的属性dataSource是必输项,用来指定数据源

sqlSessionFactory的属性configLocation,是用来指定 MyBatis 的 XML 配置文件路径的。这个属性是可选项

sqlSessionFactory的属性mapperLocations 属性使用一个资源位置的 list。 这个属性可以用来指定 MyBatis 的 XML 映射器文件的位置。比如:

<property name="mapperLocations" value="classpath*:sample/config/mappers/**/*.xml" />表示这会从类路径下加载在 sample.config.mappers 包和它的子包中所有的 MyBatis 映射器 XML 文件。

(2)使用MapperFactoryBean把映射器接口userMapper加入到spring中,如果你有多个映射器接口,需要一一的注入到spring。

其实没有必要在spring的xml文件中注册所有的映射器,可以采用这样一种方法,

你可以使用一个 MapperScannerConfigurer , 它 将 会 查 找 类 路 径 下 的 映 射 器 并 自 动 将 它 们 创 建 成 MapperFactoryBean。

要创建 MapperScannerConfigurer,可以在 Spring 的配置中添加如下代码:

<!-- 指定要自动扫描接口的基础包,实现接口 -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.ota.mapper" />
</bean>

注 意 , 没 有 必 要 去 指 定 SqlSessionFactory 或 SqlSessionTemplate , 因 为 MapperScannerConfigurer 将会创建 MapperFactoryBean,之后自动装配

2.2 userMapper.xml文件

userMapper.xml映射器内容如下,user还有userMapper这里也不再一一详细介绍,在前面一篇mybatis入门中有提到

<mapper namespace="com.ota.mapper.UserMapper">
<select id="findUserByName" parameterType="java.lang.String" resultType="com.ota.pojo.User">
select * from ota_user where username=#{username}
</select>
</mapper>

2.3测试类

public class MybatisTest {

    public static void main(String[] args) throws Exception {
ApplicationContext ctx = null;
ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
UserMapper userMapper = (UserMapper) ctx.getBean("userMapper");
User user = userMapper.findUserByName("wanghaixia");
System.out.println(user);
}

运行OK

Spring和MyBatis整合的更多相关文章

  1. Spring+springmvc+Mybatis整合案例 annotation版(myeclipse)详细版

    Spring+springmvc+Mybatis整合案例 Version:annotation版 文档结构图: 从底层开始做起: 01.配置web.xml文件 <?xml version=&qu ...

  2. Spring+springmvc+Mybatis整合案例 xml配置版(myeclipse)详细版

    Spring+springmvc+Mybatis整合案例 Version:xml版(myeclipse) 文档结构图: 从底层开始做起: 01.配置web.xml文件 <?xml version ...

  3. Spring与Mybatis整合的MapperScannerConfigurer处理过程源码分析

    前言 本文将分析mybatis与spring整合的MapperScannerConfigurer的底层原理,之前已经分析过java中实现动态,可以使用jdk自带api和cglib第三方库生成动态代理. ...

  4. Mybatis学习--spring和Mybatis整合

    简介 在前面写测试代码的时候,不管是基于原始dao还是Mapper接口开发都有许多的重复代码,将spring和mybatis整合可以减少这个重复代码,通过spring的模板方法模式,将这些重复的代码进 ...

  5. 九 spring和mybatis整合

    1       spring和mybatis整合 1.1     整合思路 需要spring通过单例方式管理SqlSessionFactory. spring和mybatis整合生成代理对象,使用Sq ...

  6. Mybatis学习(7)spring和mybatis整合

    整合思路: 需要spring通过单例方式管理SqlSessionFactory. spring和mybatis整合生成代理对象,使用SqlSessionFactory创建SqlSession.(spr ...

  7. 框架篇:Spring+SpringMVC+Mybatis整合开发

    前言: 前面我已搭建过ssh框架(http://www.cnblogs.com/xrog/p/6359706.html),然而mybatis表示不服啊. Mybatis:"我抗议!" ...

  8. SpringMVC, Spring和Mybatis整合案例一

    一  准备工作 包括:spring(包括springmvc).mybatis.mybatis-spring整合包.数据库驱动.第三方连接池. 二  整合思路 Dao层: 1.SqlMapConfig. ...

  9. MyBatis学习七:spring和MyBatis整合

    <\mybatis\day02\16mybatis和spring整合-sqlSessionFactory配置.avi;> MyBatis学习七:spring和MyBatis整合.逆向工程 ...

  10. Spring boot Mybatis 整合(完整版)

    个人开源项目 springboot+mybatis+thymeleaf+docker构建的个人站点开源项目(集成了个人主页.个人作品.个人博客) 朋友自制的springboot接口文档组件swagge ...

随机推荐

  1. discuz对PHP7不支持mysql的兼容性处理

    PHP7 废除了 ”mysql.dll” ,推荐使用 mysqli 或者 pdo_mysql,discuz对原生mysql函数做了如下处理,通过mysqli代替原mysql函数 http://blog ...

  2. 【React Native开发】React Native进行签名打包成Apk

    转载请标明出处: http://blog.csdn.net/developer_jiangqq/article/details/50525976 本文出自:[江清清的博客] (一)前言 [好消息]个人 ...

  3. object c中@property 的使用

    assign:  对基础的数据类型,比如NSInteger和C数据类型(int,float,char)等 copy: 针对NSString retail:   针对NSObject及其子类 nonat ...

  4. LeetCode—— Invert Binary Tree

    LeetCode-- Invert Binary Tree Question invert a binary tree. 4 / \ 2 7 / \ / \ 1 3 6 9 to 4 / \ 7 2 ...

  5. Java_WebKit_ZC01

    1. 1.1. F:\ZC_chrome_download\java_svg\__Java_call_Qt\qtjambi-master\doc\src\snippets\application.xm ...

  6. 用Heartbeat实现HA集群

    HA即高可用(high avaliable),又被叫做双机热备,用于关键性业务,简单理解就是,有两台机器A和B,正常是A提供服务,B待机闲置,当A宕机或服务宕掉,会切换到B机器继续提供服务.常用实现高 ...

  7. 图片加载之Picasso使用

    简介 Picasso是Square公司开源的一个Android图形缓存库,可以实现图片下载和缓存功能. 主要有以下一些特性: 在Adapter中回收和取消已经不在视野范围图片资源的加载,防止可能出现的 ...

  8. struts2提交文件时,出现页面重置又无法返回input现象,我没有解决这个问题

    查看资料得知,是因为我使用的tomcat是8的原因,调整到6就可以了.但我没有改变Tomcat的版本,不知道该怎么解决这个问题.

  9. 智课雅思词汇---二十三、名词性后缀mony

    智课雅思词汇---二十三.名词性后缀mony 一.总结 一句话总结:Latin: action, result of an action or condition; a suffix that for ...

  10. QT Designer 的汉化

    Designer是很好用的设计器,通过 pip 安装PyQt5之后,一般都是英文的 所以给有需要的人带来汉化的包(这个是通过替换  translations 文件夹来实现的) translations ...