Spring整合MyBatis

1. 整合 Spring

【整合目标:在spring的配置文件中配置SqlSessionFactory以及让mybatis用上spring的声明式事务】

1). 加入 Spring 的 jar 包和配置文件

<1>、Spring框架需要的jar包:

com.springsource.net.sf.cglib-2.2.0.jar
com.springsource.org.aopalliance-1.0.0.jar
com.springsource.org.aspectj.weaver-1.6.8.RELEASE.jar
commons-logging-1.1.1.jar
spring-aop-4.0.0.RELEASE.jar
spring-aspects-4.0.0.RELEASE.jar
spring-beans-4.0.0.RELEASE.jar
spring-context-4.0.0.RELEASE.jar
spring-core-4.0.0.RELEASE.jar
spring-expression-4.0.0.RELEASE.jar
spring-jdbc-4.0.0.RELEASE.jar
spring-orm-4.0.0.RELEASE.jar
spring-tx-4.0.0.RELEASE.jar
spring-web-4.0.0.RELEASE.jar
spring-webmvc-4.0.0.RELEASE.jar

<2>、Spring的配置文件Spring.xml:

<?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:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:jdbc="http://www.springframework.org/schema/jdbc" xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-4.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd"> <!-- 扫描包 -->
<context:component-scan base-package="com.neuedu">
<context:exclude-filter type="annotation"
expression="org.springframework.stereotype.Controller" />
<!-- 处理全局异常,可以标记在类上 -->
<context:exclude-filter type="annotation"
expression="org.springframework.web.bind.annotation.ControllerAdvice" />
</context:component-scan> <!-- 加载外部属性文件(数据库驱动) -->
<context:property-placeholder location="classpath:jdbc.properties" /> <!-- 配置数据库加载内容(c3p0) -->
<bean id="ComboPooledDataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="user" value="${jdbc.user}"></property>
<property name="password" value="${jdbc.password}"></property>
<property name="driverClass" value="${jdbc.driver}"></property>
<property name="jdbcUrl" value="${jdbc.url}"></property>
</bean>      <!-- 对于 mybatis 而言,使用的事务管理器是 DataSourceTransactionManager -->
     <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
         <property name="dataSource" ref="ComboPooledDataSource" />
     </bean>      <tx:annotation-driven /> <!-- 配置SqlSessionFactory -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="ComboPooledDataSource"></property>
          <!-- 配置mybatis配置文件的位置和名称 -->
<property name="configLocation" value="classpath:mytabis-config.xml"></property>
</bean>
<!-- 扫描接口和sql配置文件 -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.neuedu.mapper"></property>
</bean>
</beans>

  

2). 加入 mybatis 的 jar 包和配置文件:实际上需要配置的就是 settings 的部分。

<1>、mybatis需要的jar包:

mybatis-3.2.8.jar

<2>、mybatis的配置文件mytabis-config.xml:

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<!-- 总控配置文件 -->
<configuration>
<settings>
          <!-- 支持数据库中下划线命名的参数向项目对象中驼峰式命名的属性自动匹配 -->
<setting name="mapUnderscoreToCamelCase" value="true" />
          <!-- 支持懒加载 -->
          <setting name="lazyLoadingEnabled" value="true"/> </settings> </configuration>
     </settings>
</configuration>

3). 加入数据库驱动和数据库连接池的 jar 包

<1>、加入数据库驱动jdbc.properties:

jdbc.user=root
jdbc.password=123456
jdbc.url=jdbc:mysql://localhost:3306/mytabis
jdbc.driver=com.mysql.jdbc.Driver

<2>、加入数据库连接池的jar包:  

c3p0-0.9.1.2.jar
mysql-connector-java-5.1.7-bin.jar

4). 加入其他需要的jar包:

<1>、动态代理需要的jar包:

cglib-2.2.2.jar
javassist-3.17.1-GA.jar
asm-3.3.1.jar

<2>、Spring_MyBatis框架整合jar包

mybatis-spring-1.2.2.jar

<3>、ajax需要的包

jackson-all-1.9.11.jar

<4>、日志需要的jar包

log4j-1.2.17.jar

共需要24个jar包!

5). 在 Spring 的配置文件中配置数据源(Spring.xml已配置).

     <!-- 加载外部属性文件 -->
<context:property-placeholder location="classpath:jdbc.properties" /> <!-- 配置数据库加载内容(c3p0) -->
<bean id="ComboPooledDataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="user" value="${jdbc.user}"></property>
<property name="password" value="${jdbc.password}"></property>
<property name="driverClass" value="${jdbc.driver}"></property>
<property name="jdbcUrl" value="${jdbc.url}"></property>
</bean>

  

6). 在 Spring 的配置文件中配置 SqlSessionFactory(Spring.xml已配置)

     <!-- 配置SqlSessionFactory -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="ComboPooledDataSource"></property>
          <!-- 配置mybatis配置文件的位置和名称 -->
<property name="configLocation" value="classpath:mytabis-config.xml"></property>
</bean>

7)在mybatis的全局配置文件中配置settings属性

<configuration>
<settings>
          <!-- 支持数据库中下划线命名的参数向项目对象中驼峰式命名的属性自动匹配 -->
<setting name="mapUnderscoreToCamelCase" value="true" />
          <!-- 支持懒加载 -->
          <setting name="lazyLoadingEnabled" value="true"/> </settings> </configuration>
     </settings>
</configuration>

  

8). 最终整合的结果:可以从 IOC 容器中获取 Mapper,然后直接调用 Mapper 的方法。

注意:几乎不直接调用 SqlSession 的任何方法.

需要注意的是:Spring的事务处理的是:runtimeException:而编译时异常是没有处理的,所以需要
自己单独设置RollBackFor=Exception.class
eg:FileInputStream input = new FileInputStream(new File("D:\\2323\23.txt"))

例:简单的对数据库的操作

EmployeeMapper.java:

public interface EmployeeMapper {

	public employee getEmployeeById(int id); 

}

EmployeeMapper.xml:

	<select id="getEmployeeById" parameterType="Integer" resultType="com.neuedu.Bean.employee">
select id,last_name,email,gender
from tbl_employee
where id = #{id}
</select>

测试类:

public class smTest {

	@Test
public void test() {
ApplicationContext ioc =new ClassPathXmlApplicationContext("Spring.xml");
EmployeeMapper bean = ioc.getBean(EmployeeMapper.class);
employee employee = bean.getEmployeeById(10);
System.out.println(employee);
}

输出结果为:

employee [id=10, lastName=hah, gender=1, email=email]

框架整合——Spring与MyBatis框架整合的更多相关文章

  1. OSGI企业应用开发(九)整合Spring和Mybatis框架(二)

    上篇文章中,我们完成了在OSGI应用中整合Spring和Mybatis框架的准备工作,本节我们继续Spring和Mybatis框架的整合. 一.解决OSGI整合Spring中的Placeholder问 ...

  2. OSGI企业应用开发(八)整合Spring和Mybatis框架(一)

    到目前为止,我们已经学习了如何使用Blueprint將Spring框架整合到OSGI应用中,并学习了Blueprint&Gemini Blueprint的一些使用细节.本篇文章开始,我们將My ...

  3. OSGI企业应用开发(十)整合Spring和Mybatis框架(三)

    上篇文章中,我们已经完成了OSGI应用中Spring和Mybatis框架的整合,本文就来介绍一下,如何在其他Bundle中,使用Mybatis框架来操作数据库. 为了方便演示,我们新建一个新的Plug ...

  4. ssm框架(Spring Springmvc Mybatis框架)整合及案例增删改查

    三大框架介绍 ssm框架是由Spring springmvc和Mybatis共同组成的框架.Spring和Springmvc都是spring公司开发的,因此他们之间不需要整合.也可以说是无缝整合.my ...

  5. Spring+SpringMVC+MyBatis+easyUI整合

    进阶篇 Spring+SpringMVC+MyBatis+easyUI整合进阶篇(一)设计一套好的RESTful API 优化篇 Spring+SpringMVC+MyBatis+easyUI整合优化 ...

  6. Spring+SpringMVC+MyBatis+easyUI整合优化篇

    优化篇 Spring+SpringMVC+MyBatis+easyUI整合优化篇(一)System.out.print与Log Spring+SpringMVC+MyBatis+easyUI整合优化篇 ...

  7. java实现微信支付宝等多个支付平台合一的二维码支付(maven+spring springmvc mybatis框架)

    首先申明,本人实现微信支付宝等支付平台合多为一的二维码支付,并且实现有效时间内支付有效,本人采用的框架是spring springmvc mybatis 框架,maven管理.其实如果支付,不需要my ...

  8. 《经久不衰的Spring框架:Spring+SpringMVC+MyBatis 整合》

    前言 主角即Spring.SpringMVC.MyBatis,即所谓的SSM框架,大家应该也都有所了解,概念性的东西就不写了,有万能的百度.之前没有记录SSM整合的过程,这次刚刚好基于自己的一个小项目 ...

  9. SSM(Spring+SpringMVC+Mybatis)框架环境搭建(整合步骤)(一)

    1. 前言 最近在写毕设过程中,重新梳理了一遍SSM框架,特此记录一下. 附上源码:https://gitee.com/niceyoo/jeenotes-ssm 2. 概述 在写代码之前我们先了解一下 ...

随机推荐

  1. python3之微信文章爬虫

    前提: python3.4 windows 作用:通过搜狗的微信搜索接口http://weixin.sogou.com/来搜索相关微信文章,并将标题及相关链接导入Excel表格中 说明:需xlsxwr ...

  2. iOS耗电量测试

    iOS耗电量测试 本文主要介绍的是使用Energy Diagnostics Instruments来进行iOS耗电量测试. 一.使用方法: 1)iOS 设置选项 ->开发者选项 ->log ...

  3. yii2 request文件简介

    1. 在yii2项目中经常用到下面这些方法: 方法 解释 Yii::$app->request->get($key,$defaultValue) GET方式获取key的值,若key空,则k ...

  4. NlpirParser智能挖掘平台词性标注新算法

    词性标注是自然语言浅层理解的一个重要环节,它可帮助系统自动判定词语所属的语法范畴,为进一步处理提供更高层面的支持.词性标注主要任务是消除词性兼类歧义,对于新信息检测来说,它的实际意义还在于: (1)能 ...

  5. Android中的服务

    Android中的服务 四大组件都是运行在主线程 Android中的服务,是在后台运行 .可以理解成是在后台运行并且是没有界面的Activity. Foreground process 前台进程 ,用 ...

  6. unable to create …

    问题描述: 在新建Android Application时会出现unable to create the selected property page 解决方法: 将用户PATH路径中的jdk路径放到 ...

  7. Git时光机穿梭之工作区和暂存区

    Git和其他版本控制系统如SVN的一个不同之处就是有暂存区的概念. 先来看名词解释. 工作区(Working Directory) 就是你在电脑里能看到的目录,比如我的learngit文件夹就是一个工 ...

  8. 使用我的编译器,下面的代码 int i=7; printf("%d\n", i++ * i++); 返回 49?不管按什么顺序计算, 难道不该打印出56吗?

    尽管后缀自加和后缀自减操作符 ++ 和 -- 在输出其旧值之后才会执行运算, 但这里的"之后"常常被误解.没有任何保证确保自增或自减会在输出变量原值之 后和对表达式的其它部分进行计 ...

  9. js实时获取input数据

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  10. 让你快速了解并掌握如何进行iOS开发技能

    首先你要花点时间针对objective-c语言的学习:毕竟这个是iOS开发的基础(你也可以尝试用Swift,但此项目只是针对OC),编程套路其实都是差不多,多写多想多实践:关于环境的搭建就不在本文进行 ...