上一节测试好了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. .Net语言 APP开发平台——Smobiler学习日志:如何快速实现快递信息流的效果

    最前面的话:Smobiler是一个在VS环境中使用.Net语言来开发APP的开发平台,也许比Xamarin更方便 样式一 一.目标样式 我们要实现上图中的效果,需要如下的操作: 1.从工具栏上的&qu ...

  2. 和Java相关的书籍,想成为架构师的请收藏一下啊

    1.<<Effective Java 中文第二版>> 2.<<Java并发编程实践>> 3.<<Java核心技术(原书第8版)卷I_基础知识 ...

  3. winForm连接数据库(sqlserver2005)

    帮同学搞个课程设计winform连接sqlserver2005 具体方法: .添加App.config文件 2.在App.config文件中添加节点 <?xml version="1. ...

  4. C#开发微信门户及应用(1)--开始使用微信接口

    微信应用如火如荼,很多公司都希望搭上信息快车,这个是一个商机,也是一个技术的方向,因此,有空研究下.学习下微信的相关开发,也就成为日常计划的重要事情之一了.本系列文章希望从一个循序渐进的角度上,全面介 ...

  5. GJM : Unity3D HIAR -【 快速入门 】 二、搭建开发环境

    感谢您的阅读.喜欢的.有用的就请大哥大嫂们高抬贵手"推荐一下"吧!你的精神支持是博主强大的写作动力以及转载收藏动力.欢迎转载! 版权声明:本文原创发表于 [请点击连接前往] ,未经 ...

  6. SQL联合查询:子表任一记录与主表联合查询

    今天有网友群里提了这样一个关于SQL联合查询的需求: 一.有热心网友的方案: 二.我的方案: select * from ( select a.*,(select top 1 Id from B as ...

  7. 北京54全国80及WGS84坐标系的相互转换

    这三个坐标系统是当前国内较为常用的,它们均采用不同的椭球基准.其中北京54坐标系,属三心坐标系,大地原点在苏联的普而科沃,长轴6378245m,短轴6356863,扁率1/298.3:西安80坐标系, ...

  8. React Native 之 TextInput使用

    前言 学习本系列内容需要具备一定 HTML 开发基础,没有基础的朋友可以先转至 HTML快速入门(一) 学习 本人接触 React Native 时间并不是特别长,所以对其中的内容和性质了解可能会有所 ...

  9. javascript 设置input框只读属性 获取disabled后的值并传给后台

    input只读属性   有两种方式可以实现input的只读效果:disabled 和 readonly. 自然两种出来的效果都是只能读取不能编辑,可是两者有很大不同. Disabled说明该input ...

  10. freeswitch对接其它SIP设备

    这几天用到freeswitch对接其它设备方面的知识,这里整理下,也方便我以后查阅. 操作系统:debian8.5_x64 freeswitch 版本 : 1.6.8 一.freeswitch作为被叫 ...