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.xsdhttp://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsdhttp://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-4.0.xsdhttp://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 configurationPUBLIC "-//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,genderfrom tbl_employee where id = #{id} </select>

测试类:

public class smTest {@Testpublic 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]

转载于:https://www.cnblogs.com/Mr-zhaoz/p/7481870.html

框架整合——Spring与MyBatis框架整合相关推荐

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

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

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

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

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

    SSM(Spring+SpringMVC+Mybatis)框架环境搭建(整合步骤)(一) 1. 前言 最近在写毕设过程中,重新梳理了一遍SSM框架,特此记录一下. 附上源码:https://gitee ...

  4. SSM框架Spring+SpringMVC+MyBatis——详细整合教程

    2019独角兽企业重金招聘Python工程师标准>>> 摘要: 包括SQL Maps和Data Access Objects(DAO)MyBatis 消除了几乎所有的JDBC代码和参 ...

  5. spring boot mybatis 整合_两大热门框架 Spring 与 Mybatis 如何整合呢?

    整合的方式 新建 maven 项目 引入依赖包 配置资源文件 案例实操 新建 maven 项目 新建 maven 项目 spring_mybatis 目录结构如下: 主目录包: ​ com.xxx.d ...

  6. Spring系列(七)、Spring与MyBatis框架整合

    7 搭建Spring与MyBatis的集成环境 要实现Spring与MyBatis的整合,很明显需要这两个框架各自的jar包,以及整合两个框架的中间包mybatis-spring.jar: 我们使用m ...

  7. Spring+SpringMVC+MyBatis框架的整合详细过程

    最近,以前用ssm框架开发项目,都是前辈封装好的,自己并不是很熟悉这个ssm框架搭建过程,于是就亲自试了一把: 开发环境和工具:eclipse,jdk1.7,tomcat7,maven 1.新建一个m ...

  8. Spring和MyBatis环境整合

    2019独角兽企业重金招聘Python工程师标准>>> Spring: Spring是一个轻量级的控制反转(IoC)和面向切面(AOP)的容器框架. 两个重要模块:Spring 面向 ...

  9. Spring+SpringMVC+Mybatis框架集成搭建教程

    一.背景 最近有很多同学由于没有过SSM(Spring+SpringMvc+Mybatis , 以下简称SSM)框架的搭建的经历,所以在自己搭建SSM框架集成的时候,出现了这样或者那样的问题,很是苦恼 ...

  10. Spring+SpringMVC+MyBatis框架搭建-----详细教程

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

最新文章

  1. linux下clone一直运行,如何在Linux上使用clone()创建真正的线程?
  2. 大道至简:软件工程实践者的思想——第七、八章感想
  3. hibernate的Configuration类和SessionFactory接口
  4. 三十八、Scrapy 下载中间件Middleware
  5. linux安装python_VTK:华为笔记本电脑+深度deepin-linux+python下安装和入门
  6. 腾讯地图api修改信息窗口样式_ThingJS通过地图的信息窗口展示常见数据
  7. Magic Squares
  8. autosar网络管理_Autosar 软件框架
  9. NUnit 2.5.9 官网学游记(一) 入门
  10. fckeditor 2.6 php,fckeditor = 2.6.4 任意文件上传漏洞
  11. Matlab中sqrt函数的用法
  12. PADS输出BOM表和位号图(装配图)
  13. itools备份短信到android,【itools备份文件路径】itools备份路径_itools备份短信-系统城...
  14. Excel批量合并相同表头的表格
  15. 【论文分享】小样本图片分类方法:AwGCN:Few-Shot Learning With Attention-Weighted Graph Convolutional Networks
  16. python列表怎么比较大小_python列表怎么比较大小
  17. C++代码审阅–ice104协议从站(5)
  18. XJOI 7820 TLE
  19. GFocalV2解读
  20. iOS 蓝牙4.0开发使用(内附Demo)

热门文章

  1. 一个架构师谈什么是架构,以及怎么成为架构师
  2. 支付宝系统架构参考(架构图,最新揭秘)
  3. 阿里巴巴基于Java容器的多应用部署技术实践
  4. Memcached中Libevent和线程池使用初探
  5. 关于 Ping 的过程,你真的了解吗?
  6. Android Studio 2.0来啦
  7. sql 单引号_SQL 语句中单引号、双引号的具体用法
  8. Zabbix 数据清理
  9. 学习——java内存模型
  10. Vivado 2017封装自定义IP Core