Spring和Mybatis整合,配置文件
整合时除了未整合前spring、mybatis的必须包外还需要加入两个包
spring-jdbc-4.2.5.RELEASE.jar
mybatis-spring-1.2.5.jar
spring-jdbc-4.2.5.RELEASE.jar 提供了Mybatis所用数据源的管理
mybatis-spring-1.2.5.jar 提供了myBatis所用的sqlSessionFactionBean 和SqlSessionTemplate
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<!-- 自动装配配置 -->
<bean
class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor" />
<!-- 数据源Bean配置 -->
<bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://localhost:3306/appproject" />
<property name="username" value="root" />
<property name="password" value="12345678" />
</bean>
<!-- MyBatis session工厂bean配置 -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"></property>
<property name="mapperLocations" value="classpath*:mappers/mapper-*.xml"></property>
</bean>
<!-- 创建session bean,自动装配 -->
<bean class="org.mybatis.spring.SqlSessionTemplate" autowire="byType">
<constructor-arg ref="sqlSessionFactory"></constructor-arg>
</bean>
<import resource="beans-dao.xml" />
<import resource="beans-service.xml" /> </beans>
org.springframework.jdbc.datasource.DriverManagerDataSource类是spring-jdbc提供的数据源对象,我们创建SqlSessionTemplate时用的是这个类的构造方法,传入的参数的类型是SqlSessionFactionBean,这个Bean是由mybatis-spring提供的。
SqlSessionTemplate使用了自动装配的方式(代码如下),我的Dao层有一个基类,基类中的SqlSessionTemplate 已经用@Autowired。Spring启动时,会自动注入,想要了解自动装配的方式请点击我的另外一篇博客@Autowired的使用
public class BaseDao {
@Autowired
protected SqlSessionTemplate session;
}
当然,整合的方式有很多种,此处是其中之一。
Spring和Mybatis整合,配置文件的更多相关文章
- spring和mybatis整合配置文件
查看所有springmvc spring mybatis配置文件见下链接: https://my.oschina.net/sherwayne/blog/262616 <?xml versio ...
- 简单Spring和mybatis整合配置文件
<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.spr ...
- Spring+springmvc+Mybatis整合案例 annotation版(myeclipse)详细版
Spring+springmvc+Mybatis整合案例 Version:annotation版 文档结构图: 从底层开始做起: 01.配置web.xml文件 <?xml version=&qu ...
- Spring+springmvc+Mybatis整合案例 xml配置版(myeclipse)详细版
Spring+springmvc+Mybatis整合案例 Version:xml版(myeclipse) 文档结构图: 从底层开始做起: 01.配置web.xml文件 <?xml version ...
- Mybatis学习--spring和Mybatis整合
简介 在前面写测试代码的时候,不管是基于原始dao还是Mapper接口开发都有许多的重复代码,将spring和mybatis整合可以减少这个重复代码,通过spring的模板方法模式,将这些重复的代码进 ...
- 九 spring和mybatis整合
1 spring和mybatis整合 1.1 整合思路 需要spring通过单例方式管理SqlSessionFactory. spring和mybatis整合生成代理对象,使用Sq ...
- Mybatis学习(7)spring和mybatis整合
整合思路: 需要spring通过单例方式管理SqlSessionFactory. spring和mybatis整合生成代理对象,使用SqlSessionFactory创建SqlSession.(spr ...
- 框架篇:Spring+SpringMVC+Mybatis整合开发
前言: 前面我已搭建过ssh框架(http://www.cnblogs.com/xrog/p/6359706.html),然而mybatis表示不服啊. Mybatis:"我抗议!" ...
- SpringMVC, Spring和Mybatis整合案例一
一 准备工作 包括:spring(包括springmvc).mybatis.mybatis-spring整合包.数据库驱动.第三方连接池. 二 整合思路 Dao层: 1.SqlMapConfig. ...
- MyBatis学习七:spring和MyBatis整合
<\mybatis\day02\16mybatis和spring整合-sqlSessionFactory配置.avi;> MyBatis学习七:spring和MyBatis整合.逆向工程 ...
随机推荐
- C#——this关键字(2,3)(含求助贴)
这次来看一看this关键字的第二个用法:将对象作为参数传递到其他方法 ----------------------------------------------------------------- ...
- 记录一次bug解决过程:可维护性和性能优化
一.总结 使用某些变量的地方在2次以上的,强烈建议使用枚举值来维护变量,日后方便扩展. 查数据库的方法调用,能合并就净量去合并. 二.Bug描述 枚举变量的维护以及方法使用: public class ...
- jdk源码分析红黑树——插入篇
红黑树是自平衡的排序树,自平衡的优点是减少遍历的节点,所以效率会高.如果是非平衡的二叉树,当顺序或逆序插入的时候,查找动作很可能会遍历n个节点 红黑树的规则很容易理解,但是维护这个规则难. 一.规则 ...
- html标签学习
1. 标题 : <h1> - <h6> 2. 分割线 : <hr> 3. 加粗: <strong> 4. 斜体: <em> <i> ...
- 如何利用BI实现人力资源可视化管理
随着通信行业改革的不断深化,行业的发展形势和生存环境正发生巨大变化,通信和信息的边界越来越模糊,市场竞争也随之愈演愈烈.近年来,某通讯运营商在业务的转型.网络的转型取得了巨大的突破,但人力资源管理的转 ...
- SharePoint2016母版页的直接依赖项的数目限制超过10的限制解决方案Direct Dependencies Limit with Master page User Controls
前言 最近母版制作站点模板,根据站点模板新建后发现如下错误: 解决方案 1. 打开C:\inetpub\wwwroot\wss\VirtualDirectories\,如下图: 2. 打开web.co ...
- 微软要如何击败Salesforce?Office365、Azure、Dynamics365 全面布局AI | 双语
微软在上月宣布组建自己的 AI 研究小组.该小组汇集了超过 5000 名计算机科学家和工程师,加上微软内部研究部门,将共同挖掘 AI 技术. 与此同时,亚马逊,Facebook,Google,IBM ...
- no identity found Command /usr/bin/codesign failed with exit code 1 报错解决方法
stackoverflow 的解决方法是 xcode->preference->account->view detail -> refresh the provisioning ...
- Android 手机卫士--绑定sim卡序列号
现在开始具体 处理每一个导航页面的逻辑,首先看第二个导航页 本文地址:http://www.cnblogs.com/wuyudong/p/5949775.html,转载请注明出处. 这里需要实现绑定s ...
- 【代码笔记】iOS-自定义导航条颜色
一,效果图. 二,工程图. 三,代码. AppDelegate.m - (BOOL)application:(UIApplication *)application didFinishLaunchin ...