上一节测试好了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整合的更多相关文章

  1. SSM整合(三):Spring4与Mybatis3与SpringMVC整合

    源码下载 SSMDemo 上一节整合了Mybatis3与Spring4,接下来整合SpringMVC! 说明:整合SpringMVC必须是在web项目中,所以前期,新建的就是web项目! 本节全部采用 ...

  2. SSM框架整合环境构建——基于Spring4和Mybatis3

    目录 环境 配置说明 所需jar包 配置db.properties 配置log4j.properties 配置spring.xml 配置mybatis-spring.xml 配置springmvc.x ...

  3. Mybatis整合通用Dao,Mybatis整合通用Mapper,MyBatis3.x整合通用 Mapper3.5.x

    Mybatis整合通用Dao,Mybatis整合通用Mapper,MyBatis3.x整合通用 Mapper3.5.x ============================== 蕃薯耀 2018年 ...

  4. Spring3+MyBatis3整合log4j无法输出SQL语句问题的解决

    今天遇到了跟下面文章一模一样的问题,下面文章的解决方案很好,在这里记录保存一下. Spring3+MyBatis3整合无法输出SQL语句问题的解决

  5. 【j2ee spring】27、巴巴荆楚网-整合hibernate4+spring4(2)

    巴巴荆楚网-整合hibernate4+spring4(2) 1.图文项目 2.首先我们引入对应的jar包 这里用的是oracle 11g,所以我们使用的数据库连接jar包是ojdbc6, 的区别就是支 ...

  6. [置顶] Java Web学习总结(24)——SSM(Spring+SpringMVC+MyBatis)框架快速整合入门教程

    1.基本概念 1.1.Spring Spring是一个开源框架,Spring是于2003 年兴起的一个轻量级的Java 开发框架,由Rod Johnson 在其著作Expert One-On-One  ...

  7. spring和mybatis的整合开发(基于MapperFactoryBean的整合开发(方便简单不复杂))

    MapperFactoryBean是mybati-spring团队提供的一个用于根据mapper接口生成mapper对象的类. 在spring配置文件中可以配置以下参数: 1.mapperInterf ...

  8. MyBatis笔记----SSM框架mybatis3整合springmvc spring4

    上节 无springmvc框架 http://www.cnblogs.com/tk55/p/6661786.html 结构 jar包 web.xml 与index.jsp <?xml versi ...

  9. 使用Maven完成SSM框架整合环境构建——基于Spring4和Mybatis3

    只言片语 使用Maven来搭建一个SSM环境,其实和使用手工倒入jar的过程没有多大区别,所用的jar包都是一样的,但是区别在与不用你手动导入jar包了,而是只修改pom.xml,maven会自动根据 ...

随机推荐

  1. 基于Metronic的Bootstrap开发框架经验总结(14)--条码和二维码的生成及打印处理

    在很多项目里面,对条形码和二维码的生成和打印也是一种很常见的操作,在Web项目里面,我们可以利用JS生成条形码和二维码的组件有很多.本文引入两个比较广泛使用的JS组件,用来处理条形码和二维码的生成处理 ...

  2. GridView中显示时间日期格式问题

    以下都是GridView基本常用的日期,时间格式 形式 语法 结果 注释 数字 {0:N2} 12.36   数字 {0:N0} 13   货币 {0:c2} $12.36   货币 {0:c4} $ ...

  3. 初识ASP.NET MVC

    我们首先从创建ASP.NET MVC项目开始.打开Visual Studio,在文件菜单中选择新建-> 项目,然后在模板中选择Web,接着选择ASP.Net Web应用程序,更改项目名称,点击确 ...

  4. Aspose.Cells导出Excel(1)

    利用Aspose.Cells导出excel 注意的问题 1.DataTable的处理 2.进行编码,便于中文名文件下载 3.别忘了Aspose.Cells.dll(可以自己在网上搜索) public ...

  5. C# - 多线程 之 进程与线程

    并行~并发 并发 Concurrency,逻辑上的同时发生,一个处理器(在不同时刻或者说在同一时间间隔内)"同时"处理多个任务.宏观上是并发的,微观上是按排队等待.唤醒.执行的步骤 ...

  6. linux中~和/的区别

  7. intellij中不能导入jar包

    ①选中项目点击右键,选择open modual settings(或者直接按F4): ②在弹出的窗口左端选择Libraries: ③点击左边加号,选择From Maven Repository; ④将 ...

  8. 你必须知道的Microsoft SQL Server一

    不知道为什么我Win10环境下安装的Sqlserver2012,智能提示的功能基本上没有用,没办法,我还是选择安装插件SQL Prompt 5吧.下载地址:http://www.uzzf.com/so ...

  9. datatables中的Options总结(3)

    datatables中的Options总结(3) 十.ColReorder colReorder.fixedColumnsLeft 不允许x列重新排序(从左数) colReorder.fixedCol ...

  10. 3.2 js六大数据类型

    js中有六种数据类型,包括五种基本数据类型(Number,String,Boolean,Null,Undefined),和一种混合数据类型(Object). 前面说到js中变量是松散类型的,因此有时候 ...