SSM整合(二):Spring4与Mybatis3整合
上一节测试好了Mybatis3,接下来整合Spring4!
一、添加spring上下文配置
在src/main/resources/目录下的spring新建spring上下文配置文件applicationContext-dao.xml :
注:
applicationContext-dao.xml, 用于管理数据库,
applicationContext-service.xml 用于配置service,
applicationContext-mvc.xml 用于集成springmvc配置文件,以此类推
添加内容如下:
<?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"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:jdbc="http://www.springframework.org/schema/jdbc"
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
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/jdbc
http://www.springframework.org/schema/jdbc/spring-jdbc.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
http://directwebremoting.org/schema/spring-dwr/spring-dwr-3.0.xsd
">
<!--注解扫描器 -->
<context:annotation-config />
<context:component-scan base-package="com.ssm.demo2.controller">
</context:component-scan>
<!-- 配置dataSource 使用dbcp连接池 注释 -->
<!--
<bean
id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property
name="driverClassName" value="com.mysql.jdbc.Driver"
/>
<property
name="url" value="jdbc:mysql://127.0.0.1:3306/mybatis? characterEncoding=utf8"
/>
<property
name="username" value="root"
/>
<property
name="password" value="root"
/>
</bean>
-->
<!-- 加载数据源配置 -->
<context:property-placeholder location="classpath:db.properties"/>
<!-- 配置dataSource 使用dbcp连接池-->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="${jdbc.driverClass}" />
<property name="url" value="${jdbc.jdbcUrl}"/>
<property name="username" value="${jdbc.user}"/>
<property name="password" value="${jdbc.password}"/>
<!-- 设置数据库连接池的最大连接数 -->
<property name="maxActive" value="${jdbc.maxPoolSize}" />
<!--
设置数据库连接池的初始化连接数 -->
<property name="initialSize" value="${jdbc.initPoolSize}"/>
</bean>
<bean
id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<!--dataSource属性指定要用到的连接池 -->
<property name="dataSource" ref="dataSource" />
<!--configLocation属性指定mybatis的核心配置文件 -->
<property
name="configLocation" value="mybatis/mybatis_config.xml" />
</bean>
<!--注册UserMapper映射 -->
<bean
id="userMapper" class="org.mybatis.spring.mapper.MapperFactoryBean">
<!--sqlSessionFactory属性指定要用到的SqlSessionFactory实例 -->
<property name="sqlSessionFactory" ref="sqlSessionFactory" />
<!--mapperInterface属性指定映射器接口,用于实现此接口并生成映射器对象 -->
<property name="mapperInterface" value="com.ssm.demo2.mapper.UserMapper"/>
</bean>
</beans>
二、测试
把mybatis_config.xml配置中有关数据库的配置信息注释掉!
在src/test/main 目录下新建Mybatis_Spring_Test.java进行测试:
public class Mybatis_Spring_Test {
private ApplicationContext ctx;
@Before
public void setUp() throws Exception {
String configpath="spring/applicationContext-dao.xml";
ctx=new ClassPathXmlApplicationContext(configpath);
}
@Test
public void test() throws Exception {
//测试是否能正常加载配置文件
System.out.println(ctx);
UserMapper userMapper=ctx.getBean(UserMapper.class);
List<User> users=userMapper.findUserList("王");
System.out.println(users.size());
}
}
结果:

测试ok!
三、把注册mapper的方式 改为自动扫描方式
在applicationContext-dao.xml注册mapper是手动配置的,在项目中这样的配置会很多,很不方便,改成自动扫描式!
注释掉以下配置:
<!--
<bean
id="userMapper"
class="org.mybatis.spring.mapper.MapperFactoryBean">
<!--sqlSessionFactory属性指定要用到的SqlSessionFactory实例 -->
<property
name="sqlSessionFactory" ref="sqlSessionFactory"
/>
<!--mapperInterface属性指定映射器接口,用于实现此接口并生成映射器对象 -->
<property
name="mapperInterface"
value="com.ssm.demo2.mapper.UserMapper"/>
</bean>
-->
添加如下配置:
<!-- mybatis自动扫描包下的mapper -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage"value="com.ssm.demo2.mapper"/>
<!-- optional
unless there are multiple session factories defined -->
<property name="sqlSessionFactoryBeanName"value="sqlSessionFactory"/>
</bean>
最后添加事务管理
<!-- 数据库的事务管理器配置 -->
<bean id="transactionManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource" />
</bean>
<!-- 使用annotation定义数据库事务,这样可以在类或方法中直接使用@Transactional注解来声明事务 -->
<tx:annotation-driven transaction-manager="transactionManager" />
现测试通过OK
注:在src/main/resource中mybatis目录下的UserMapper.xml在前一节中改成自动扫描式后,可以去掉,同样在配置自动扫描加载mapper后,mybatis_config.xml中的功能也全部由spring进行了管理,也是可以去掉滴!!但了为结构清晰,保留这二个配置文件!
Mybatis3与Spring4整合成功!Spring4请查看相关详细文章!
最后一节整合SpringMVC!
SSM整合(二):Spring4与Mybatis3整合的更多相关文章
- SSM整合(三):Spring4与Mybatis3与SpringMVC整合
源码下载 SSMDemo 上一节整合了Mybatis3与Spring4,接下来整合SpringMVC! 说明:整合SpringMVC必须是在web项目中,所以前期,新建的就是web项目! 本节全部采用 ...
- SSM框架整合环境构建——基于Spring4和Mybatis3
目录 环境 配置说明 所需jar包 配置db.properties 配置log4j.properties 配置spring.xml 配置mybatis-spring.xml 配置springmvc.x ...
- Mybatis整合通用Dao,Mybatis整合通用Mapper,MyBatis3.x整合通用 Mapper3.5.x
Mybatis整合通用Dao,Mybatis整合通用Mapper,MyBatis3.x整合通用 Mapper3.5.x ============================== 蕃薯耀 2018年 ...
- Spring3+MyBatis3整合log4j无法输出SQL语句问题的解决
今天遇到了跟下面文章一模一样的问题,下面文章的解决方案很好,在这里记录保存一下. Spring3+MyBatis3整合无法输出SQL语句问题的解决
- 【j2ee spring】27、巴巴荆楚网-整合hibernate4+spring4(2)
巴巴荆楚网-整合hibernate4+spring4(2) 1.图文项目 2.首先我们引入对应的jar包 这里用的是oracle 11g,所以我们使用的数据库连接jar包是ojdbc6, 的区别就是支 ...
- [置顶]
Java Web学习总结(24)——SSM(Spring+SpringMVC+MyBatis)框架快速整合入门教程
1.基本概念 1.1.Spring Spring是一个开源框架,Spring是于2003 年兴起的一个轻量级的Java 开发框架,由Rod Johnson 在其著作Expert One-On-One ...
- spring和mybatis的整合开发(基于MapperFactoryBean的整合开发(方便简单不复杂))
MapperFactoryBean是mybati-spring团队提供的一个用于根据mapper接口生成mapper对象的类. 在spring配置文件中可以配置以下参数: 1.mapperInterf ...
- MyBatis笔记----SSM框架mybatis3整合springmvc spring4
上节 无springmvc框架 http://www.cnblogs.com/tk55/p/6661786.html 结构 jar包 web.xml 与index.jsp <?xml versi ...
- 使用Maven完成SSM框架整合环境构建——基于Spring4和Mybatis3
只言片语 使用Maven来搭建一个SSM环境,其实和使用手工倒入jar的过程没有多大区别,所用的jar包都是一样的,但是区别在与不用你手动导入jar包了,而是只修改pom.xml,maven会自动根据 ...
随机推荐
- angularjs学习使用分享
angularjs是一个为动态web应用设计的结构框架,它是为了克服html在构建应用上的不足而设计的. 工作原理: 1 加载html,然后解析成DOM: 2 加载angular.js脚本: 3 An ...
- php在没有登录的情况下自动跳转到登录页
<?php namespace Home\Controller; use Think\Controller; class BaseController extends Controller{ / ...
- Windows下快速安装Flask的一次经历
前提: 1.已安装python版本(一般都是2.X) 2.已安装easy_install python安装,记得配置Python的环境变量,例如:我的直接在Path上加 G:\Python 验证安装P ...
- 智能指针shared_ptr的用法
为了解决C++内存泄漏的问题,C++11引入了智能指针(Smart Pointer). 智能指针的原理是,接受一个申请好的内存地址,构造一个保存在栈上的智能指针对象,当程序退出栈的作用域范围后,由于栈 ...
- 记录一次bug解决过程:else未补全导致数据泄露和代码优化
一.总结 快捷键ctrl + alt + 四个方向键 --> 倒置屏幕 未补全else逻辑,倒置查询数据泄露 空指针是最容易犯的错误,数据的空指针,可以普遍采用三目运算符来解决 SVN冲突解决关 ...
- Vue.js 入门指南之“前传”(含sublime text 3 配置)
题记:关注Vue.js 很久了,但就是没有动手写过一行代码,今天准备入手,却发现自己比菜鸟还菜,于是四方寻找大牛指点,才终于找到了入门的“入门”,就算是“入门指南”的“前传”吧.此文献给跟我一样“白痴 ...
- Atitit java集成内嵌浏览器与外嵌浏览器attilax总结
Atitit java集成内嵌浏览器与外嵌浏览器attilax总结 HTML5将颠覆原生App世界.这听起来有点危言耸听,但若认真分析HTML5的发展史,你会发现,这个世界的发展趋势确实就是这样. 熟 ...
- H5 本地存储一
localStorage(本地存储),可以长期存储数据,没有时间限制,一天,一年,两年甚至更长,数据都可以使用.sessionStorage(会话存储),只有在浏览器被关闭之前使用,创建另一个页面时同 ...
- Javascript高性能编程-提高javascript加载速度
1.将所有<script>标签放在尽可能接近<body>标签底部的位置,以保证页面在脚本运行之前完成解析尽量减少对整个页面下载的影响 2.限制页面的<sc ...
- UICollectionViewCell--查找cell上的按钮点击后,对应的是哪个cell
实际写项目会碰到各种各样的问题,废话不多说 按钮添加到cell时,根据是直接添加到self还是self.contentView上,在点击方法里找到btn的父视图 我是直接添加到self上,所以只有一层 ...