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会自动根据 ...
随机推荐
- SQL Server Management Studio 无法修改表,超时时间已到 在操作完成之前超时时
在修改表时,保存的时候显示:无法修改表,超时时间已到 在操作完成之前超时时间已过或服务器未响应 这是执行时间设置过短的原因,可以修改一下设置便能把执行时间加长,以便有足够的时间执行完修改动作. 在 S ...
- 《c# 从入门经典》 (第6版) - 变量和表达式
1,程序就是对一系列数据的操作:数据最终是存储在内存中的0和1数据流:我们在桌面上的任何操作都会改变内存中的数据. 2,变量存储在计算机中占用着一定的内存空间 bit: 位,计算机的最小存储单位,存储 ...
- 用Fiddler模拟低速网络环境
有时候宽频网路用习惯了… 在开发的过程就比较少去考虑最佳化的问题… 但当有人反应说「你的网页好慢」甚至当网路速度慢,会造成你的网页跳出什么啊哩不哒的bug时要如何重现呢? 我们可以用Fiddler 这 ...
- jdk顺序表笔记
一.AbstractCollection 提供了集合的最大实现 继承该类,必须实现size()和iterator(),因为该类操作集合都是通过iterator 二.fail-fast策略 该策略在集合 ...
- MS SQL按IN()内容排序
需求:MMSQL查询结果,按查询条件中关键字IN内的列举信息的顺序一一对应排序. 分析:使用CHARINDEX 函数. 解决方法: SELECT * FROM Product WHERE 1=1 AN ...
- webstorm官网最新版激活:
2016.2.3版本的破解方式:目前最新的就是2.3版本,在打开的License Activation窗口中选择"activation code",在输入框输入下面的注册码:3B4 ...
- 360安全卫士造成Sharepoint文档库”使用资源管理器打开“异常
备注:企业用户还是少用360为妙 有客户反馈:部门里的XP SP2环境客户机全部异常,使用资源管理器打开Sharepoint文档库,看到的界面样式很老土,跟本地文件夹不一样 ...
- QQ互联登录以及非官方正版应用报100044错误
QQ第三方登录的时候,显示非官方正版应用,报100044错误:坑1:我们在QQ互联注册成功后需要设置包名和签名,签名是通过官方提供的工具生成的.注意一点:这里的签名是需要由打包签名之后APK生成,我们 ...
- C语言中的far关键字
最近看嵌入式文件系统TFFS的源码,看到far关键字,基础不好,惊呆了... /*Specify here which pointers may be far, if any. *Far pointe ...
- JavaEE开发基础
1 JavaEE简介 Java平台有三个版本,分别是JavaSE(Java Platform, Standard Edition),JavaEE(Java Platform, Enterprise E ...