顺着上一篇,这里介绍下spring-mybatis的配置。

我们使用mybatis去操作数据库的时候,每次都要不停地openSession,closeSession好烦躁哇~~这样工作哪里有效率可言!!!看看别人家框架的孩子从来都没有这些破事儿。

  mybatis社区的人终于看不下去了,所以他们借助spring开发了犀利的mybatis-spring。我们都知道spring是一个非常著名的java框架,它可以轻松地管理类实例对象和整合多个框架,但是Spring 3.0 的开发在 MyBatis 3.0 官方发布前就结束了,所以mybatis社区只好自己开发了mybatis-spring(实际上spring发布4的时候,spring社区依然没有支持mybatis的包,大概是觉得mybatis-spring已经足够优秀了,所以就没有选择再开发一个包了)。

准备工作

我们需要从网站上下载下面两个包

spring-mybatis下载地址:

https://github.com/mybatis/spring/releases/download/mybatis-spring-1.2.2/mybatis-spring-1.2.2.zip

spring下载地址(现在spring更加提倡使用maven来管理项目。找jar包真是太麻烦了~):

http://repo.spring.io/libs-release-local/org/springframework/spring/4.0.5.RELEASE/spring-framework-4.0.5.RELEASE-dist.zip

将下载过来的jar包放入lib目录

配置文件

上配置文件applicationContext.xml(放在src下)

<?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:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<!-- 数据库配置文件位置 -->
<context:property-placeholder location="classpath:jdbc.properties" /> <!-- 配置dbcp数据源 -->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="${jdbc.driverClassName}" />
<property name="url" value="${jdbc.url}" />
<property name="username" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" />
<!-- 队列中的最小等待数 -->
<property name="minIdle" value="${jdbc.minIdle}"></property>
<!-- 队列中的最大等待数 -->
<property name="maxIdle" value="${jdbc.maxIdle}"></property>
<!-- 最长等待时间,单位毫秒 -->
<property name="maxWait" value="${jdbc.maxWait}"></property>
<!-- 最大活跃数 -->
<property name="maxActive" value="${jdbc.maxActive}"></property>
<property name="initialSize" value="${jdbc.initialSize}"></property>
</bean> <!-- 配置mybitasSqlSessionFactoryBean,这里会读取Mybatis的配置文件Configuration.xml和数据库连接池 -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="configLocation" value="classpath:Configuration.xml"></property>
</bean> <!-- 配置SqlSessionTemplate -->
<bean id="sqlSessionTemplate" class="org.mybatis.spring.SqlSessionTemplate">
<constructor-arg name="sqlSessionFactory" ref="sqlSessionFactory" />
</bean> <!-- 事务配置 -->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource" />
</bean> <!-- 通过mybatis来扫描对应的接口,并实施注入 -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="pro.app.inter" />
<property name="sqlSessionTemplateBeanName" value="sqlSessionTemplate" />
</bean>
</beans>

上面这个配置文件中有几个类非常重要~

  首先隆重介绍了下 sqlSessionTemplate ,这个类是MyBatis-Spring的核心,从配置中可以看出它引用了一个SessionFactory类,我们可以将它理解为一个高级的SessionFactory,它负责管理MyBatis的SqlSession,通过spring优秀的资源管理,SqlSessionTemplate保证了SqlSession是和当前的事务是相关的。SqlSessionTemplate同时还管理着session的生命周期,也就是说它会自动的打开和关闭session。

之前的代码中,我们需要通过类似下面的代码获得Mapper的实例对象

UserDAO users = session.getMapper(UserDAO.class);

来获得接口的实例对象,现在把一切交给spring吧,懒人总是有这么多偷懒的办法 -_-!

<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
  <property name="basePackage" value="pro.app.inter" />
<property name="sqlSessionTemplateBeanName" value="sqlSessionTemplate" />
</bean>

通过 org.mybatis.spring.mapper.MapperScannerConfigurer类,spring为我们准备好了所有需要使用的接口类。

在同级目录下还要放入jdbc.properties配置文件

jdbc.driverClassName=org.gjt.mm.mysql.Driver
jdbc.url=jdbc:mysql://localhost:3306/blog?useUnicode=true&amp;characterEncoding=utf-8
jdbc.username=mybatis
jdbc.password=mybatis
jdbc.maxActive =10
jdbc.maxIdle =10
jdbc.minIdle=10
jdbc.initialSize=10
jdbc.maxWait =3000

测试结果

Now,让我们试试这个神奇的配置。

新建一个测试类

public class Test {
public static void main(String args[]){
//暂时通过这个类获得spring配置文件
ApplicationContext ac=new ClassPathXmlApplicationContext("applicationContext.xml");
UserMapper userDao= (UserMapper)ac.getBean("userMapper");
System.out.println(userDao);
User user = userDao.selectOne(1);
System.out.println(user.getName());
}
}

接着,它很顺利地打印出了

mybatis

相比之前各种打开关闭和异常处理,这里的代码是不是少了很多,mybatis从此赶上了别人家的孩子~

总结

使用mybatis-spring来管理SessionFactory

Mybatis-spring官网 http://mybatis.github.io/spring/zh/

Hello Mybatis 04 使用spring-mybatis的更多相关文章

  1. SpringMVC+Spring+mybatis项目从零开始--Spring mybatis mysql配置实现

    上一章我们把SSM项目结构已搭建(SSM框架web项目从零开始--分布式项目结构搭建)完毕,本章将实现Spring,mybatis,mysql等相关配置. 1.    外部架包依赖引入 外部依赖包引入 ...

  2. 第04项目:淘淘商城(SpringMVC+Spring+Mybatis) 的学习实践总结【第二天】

    淘淘商城(SpringMVC+Spring+Mybatis)  是传智播客在2015年9月份录制的,几年过去了.由于视频里课上老师敲的代码和项目笔记有些细节上存在出入,只有根据日志报错信息作出适当的调 ...

  3. 第04项目:淘淘商城(SpringMVC+Spring+Mybatis) 的学习实践总结【第一天】

    本人做过一年的MATLAB编程和简单维护过VB和C++的项目.是跟着网上获得的黑马的Java双元视频课来自学入门Java知识和常用框架的使用. 淘淘商城(SpringMVC+Spring+Mybati ...

  4. 第04项目:淘淘商城(SpringMVC+Spring+Mybatis)【第十二天】(系统架构讲解、nginx)

    https://pan.baidu.com/s/1bptYGAb#list/path=%2F&parentPath=%2Fsharelink389619878-229862621083040 ...

  5. 第04项目:淘淘商城(SpringMVC+Spring+Mybatis)【第十一天】(购物车+订单)

    https://pan.baidu.com/s/1bptYGAb#list/path=%2F&parentPath=%2Fsharelink389619878-229862621083040 ...

  6. 第04项目:淘淘商城(SpringMVC+Spring+Mybatis)【第十天】(单点登录系统实现)

    https://pan.baidu.com/s/1bptYGAb#list/path=%2F&parentPath=%2Fsharelink389619878-229862621083040 ...

  7. 第04项目:淘淘商城(SpringMVC+Spring+Mybatis)【第九天】(商品详情页面实现)

    https://pan.baidu.com/s/1bptYGAb#list/path=%2F&parentPath=%2Fsharelink389619878-229862621083040 ...

  8. 第04项目:淘淘商城(SpringMVC+Spring+Mybatis)【第八天】(solr服务器搭建、搜索功能实现)

    https://pan.baidu.com/s/1bptYGAb#list/path=%2F&parentPath=%2Fsharelink389619878-229862621083040 ...

  9. 第04项目:淘淘商城(SpringMVC+Spring+Mybatis)【第七天】(redis缓存)

    https://pan.baidu.com/s/1bptYGAb#list/path=%2F&parentPath=%2Fsharelink389619878-229862621083040 ...

  10. 第04项目:淘淘商城(SpringMVC+Spring+Mybatis) 的学习实践总结【第六天】

    https://pan.baidu.com/s/1bptYGAb#list/path=%2F&parentPath=%2Fsharelink389619878-229862621083040 ...

随机推荐

  1. C语言之数组,字符串,指针

    一. 数组的定义 1.  数组初始化 初始化方式 int a[3] = {10, 9, 6}; int a[3] = {10,9}; int a[] = {11, 7, 6}; int a[4] = ...

  2. c语言打印空心菱形

    ***算法:把菱形的中心看成坐标的原点(,),由此可以知道,如果|x| + |y| <= n;则打印输出"*"号,否则打印输出" " int mai(){ ...

  3. C语言的选择和循环上机题目(部分)

    /*(1)某市不同车牌的出租车3公里的起步价和计费分别为:夏利7元/公里,3公里以外2.1元/公里:富康8元/公里,3公里以外2.4元/公里:桑塔纳9元,3公里以外2.7元/公里.编程:从键盘输入乘车 ...

  4. java:关于继承变量的值问题

    1.在java中,如果子类继承父类的静态变量时,当你在子类面前修改这个静态变量的值,其父类的静态变量也会改变. 案例: //父类public class Animal { //静态属性 public ...

  5. knapsack problem 背包问题 贪婪算法GA

    knapsack problem 背包问题贪婪算法GA 给点n个物品,第j个物品的重量,价值,背包的容量为.应选哪些物品放入包内使物品总价值最大? 规划模型 max s.t. 贪婪算法(GA) 1.按 ...

  6. ExtJS 列表数据编辑

    在ExtJs中,GridPanel一般用于展示列表数据.同时利用一些附加的插件也能编辑数据.类似于asp.net中的DataGridView控件. 展示数据比较简单,利用Store则可以自动展示,只要 ...

  7. Spring笔记--0907

    包含ioc和aop两大核心概念 aop----事务管理 spring框架运用的设计模式(查一下) ---------------------------------------Ioc(控制反转)和Di ...

  8. java.lang.RuntimeException: Method setUp in android.test.ApplicationTestCase not mocked. See http://g.co/androidstudio/not-mocked for details.

    解决: build.gradle里加入: android { testOptions { unitTests.returnDefaultValues = true } }

  9. C语言字符串函数例子程序大全 – string相关

    关于字符串函数的应用细则,例子程序 – jerny 函数名: stpcpy 功 能: 拷贝一个字符串到另一个 用 法: char *stpcpy(char *destin, char *source) ...

  10. Java学习笔记三——数据类型

    前言 Java是强类型(strongly typed)语言,强类型包含两方面的含义: 所有的变量必须先声明后使用: 指定类型的变量只能接受预支匹配的值. 这意味着每一个变量和表达式都有一个在编译时就确 ...