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会自动根据 ...
随机推荐
- Node学习笔记(一):stream流操作
NodeJs中谈及较多的可能就是Stream模块了,先写一个简单的ajax回调 $.post("index.php",{data:'aaa',order:'ccc'},functi ...
- WPF入门:数据绑定
上一篇我们将XAML大概做了个了解 ,这篇将继续学习WPF数据绑定的相关内容 数据源与控件的Binding Binding作为数据传送UI的通道,通过INotityPropertyChanged接口的 ...
- helios架构详解(二)客户端架构和cluster
helios的客户端架构和服务器端类似,只有部分类有所区别(黄色的),下图是客户端的架构 可以看出实际上只有IConnection的俩个实例(TcpConnection.UdpConnection)是 ...
- 【WCF】如何将WCF部署到远程服务器
一.前言 最近需要将自己写的WCF服务部署到远程服务器上,也就是公网上.宿主是IIS,在配置成功之前遇到了很多问题,问题如下: 1. WCF该怎么宿主在IIS上,为何会出现 400 Bad ...
- Connect to the DSP on C6A8168/DM8168/DM8148 using CCS
转自ti-wiki 这份wiki,我曾经就收藏过,但是没有加以重视,以至于绕了一大圈的ccs开发环境的配置,现在正式收藏于自己的博客中...总结良多啊 Connecting to DSP on C6 ...
- [连载]《C#通讯(串口和网络)框架的设计与实现》- 6.通讯控制器的设计
目 录 第六章 通讯控制器的设计... 2 6.1 控制器接口... 2 6.2 串口控制器... 3 6.3 ...
- window环境下将solr6.3部署到tomcat中
1.我下载的solr是6.3版本的,需要jdk1.8及以上,tomcat8 JDK1.8的下载地址:http://www.Oracle.com/technetwork/Java/javase/down ...
- JavaScript 中的变量命名方法
三种命名方法 在程序语言中,通常使用的变量命名方法有三种:骆驼命名法(CamelCase),帕斯卡命名法(PascalCase)和匈牙利命名法. 依靠单词的大小写拼写复合词的做法,叫做"骆驼 ...
- js判断网页是否加载完毕 包括图片
<script type="text/javascript" language="JavaScript"> //: 判断网页是否加载完成 docum ...
- 解决motools和jquery之间的冲突
在同一个页面需要同时使用motools和jquery,对于$,发生了冲突,以下是解决的办法. <head> <script src="./Scripts/lib/jquer ...