整合思路

1、SqlSessionFactory对象应该放到spring容器中作为单例存在。

2、传统dao的开发方式中,应该从spring容器中获得sqlsession对象。

3、Mapper代理形式中,应该从spring容器中直接获得mapper的代理对象。

4、数据库的连接以及数据库连接池事务管理都交给spring容器来完成。

整合需要的jar包

1、spring的jar包

2、Mybatis的jar包

3、Spring+mybatis的整合包。

4、Mysql的数据库驱动jar包。

5、数据库连接池的jar包。

jar包位置如下所示:

加入配置文件

1. mybatisSpring的配置文件sqlmapConfig.xml

a)        数据库连接及连接池

<!-- 加载配置文件 -->
<context:property-placeholder location="classpath:db.properties" /> <!-- 数据库连接池 -->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close">
<property name="driverClassName" value="${jdbc.driver}" />
<property name="url" value="${jdbc.url}" />
<property name="username" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" />
<property name="maxActive" value="10" />
<property name="maxIdle" value="5" />
</bean>

b)        事务管理(暂时可以不配置)

c)        sqlsessionFactory对象,配置到spring容器中

    <!-- 配置SqlSessionFactory -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<!-- 配置mybatis核心配置文件 -->
<property name="configLocation" value="classpath:SqlMapConfig.xml" />
<!-- 配置数据源 -->
<property name="dataSource" ref="dataSource" />
</bean>

d)        mapeer代理对象或者是dao实现类配置到spring容器中。

1.dao实现类配置到spring容器中:

  原始的DAO开发是  接口+实现类来完成。

  需要dao实现类需要继承SqlsessionDaoSupport类

  因为SqlSessionDaoSupport提供getSqlSession()方法来获取SqlSession

  例如:

public class UserDaoImpl extends SqlSessionDaoSupport implements UserDao {
@Override
public User queryUserById(int id) {
// SqlSessionDaoSupport提供getSqlSession()方法来获取SqlSession
SqlSession sqlSession = super.getSqlSession(); // 使用SqlSession执行操作
User user = sqlSession.selectOne("queryUserById", id); // 不要关闭sqlSession return user;
}
}

接着把dao实现类配置到spring容器中,如下

<!-- 原始的DAO开发,将实现类配到spring中 -->
<bean id="userDao" class="com.xk.mybatis.dao.impl.UserDaoImpl">
     <!-- 配置sqlSessionFactory-->
    <property name="sqlSessionFactory" ref="sqlSessionFactoryBean"/>
  </bean>

编写测试类:

public class UserDaoTest {
private ApplicationContext context; @Before
public void setUp() throws Exception {
this.context = new ClassPathXmlApplicationContext("classpath:applicationContext.xml");
} @Test
public void testQueryUserById() {
// 获取userDao
UserDao userDao = this.context.getBean(UserDao.class); User user = userDao.queryUserById(1);
System.out.println(user);
}
}

  2.mapeer代理对象配置到spring容器中:

    (1)编写UserMapper.xml配置文件

    (2)实现UserMapper接口

    (3)在applicationContext.xml添加配置

    MapperFactoryBean也是属于mybatis-spring整合包

<!-- Mapper代理的方式开发方式一,配置Mapper代理对象(一个mapper一个mapper的配) -->
<bean id="userMapper" class="org.mybatis.spring.mapper.MapperFactoryBean">
<!-- 配置Mapper接口 -->
<property name="mapperInterface" value="com.xk.mybatis.mapper.UserMapper" />
<!-- 配置sqlSessionFactory -->
<property name="sqlSessionFactory" ref="sqlSessionFactory" />
</bean>

或者

<!-- Mapper代理的方式开发方式二,扫描包方式配置代理(一下扫描全部) -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<!-- 配置Mapper接口 -->
<property name="basePackage" value="com.xk.mybatis.mapper" />
</bean>
<!--每个mapper代理对象的id就是类名,首字母小写 -->

Mybaits整合Spring的更多相关文章

  1. Mybaits整合Spring自动扫描 接口,Mybaits配置文件.xml文件和Dao实体类

    1.转自:https://blog.csdn.net/u013802160/article/details/51815077 <?xml version="1.0" enco ...

  2. 使用maven整合spring+springmvc+mybatis

    使用maven整合spring+springmvc+mybatis 开发环境: 1. jdk1.8 2. eclipse4.7.0 (Oxygen) 3. mysql 5.7 在pom.xml文件中, ...

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

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

  4. 【Java EE 学习 81】【CXF框架】【CXF整合Spring】

    一.CXF简介 CXF是Apache公司下的项目,CXF=Celtix+Xfire:它支持soap1.1.soap1.2,而且能够和spring进行快速无缝整合. 另外jax-ws是Sun公司发布的一 ...

  5. Mybatis整合Spring

    根据官方的说法,在ibatis3,也就是Mybatis3问世之前,Spring3的开发工作就已经完成了,所以Spring3中还是没有对Mybatis3的支持.因此由Mybatis社区自己开发了一个My ...

  6. mybatis入门_一对多,多对多映射以及整合spring框架

    一.一对多映射. 1.1 一对多映射之根据多的一方关联查询一的一方 示例:查询出具体的订单信息,同时也查询出来订单的用户信息. 引入的订单表如下所示: 框选出来的为具体的外键. 订单的Pojo类如下所 ...

  7. 《SSM框架搭建》三.整合spring web

    感谢学习http://blog.csdn.net/zhshulin/article/details/37956105#,还是修改了spring到最新的版本和接口开发示例 根据前一篇日志,已经有了myb ...

  8. Maven 整合 spring profile实现多环境自动切换

    Maven 整合 spring profile实现多环境自动切换 时间:2014-03-19 15:32来源:Internet 作者:Internet 点击:525次 profile主要用在项目多环境 ...

  9. TinyFrame续篇:整合Spring IOC实现依赖注入

    上一篇主要讲解了如何搭建基于CodeFirst的ORM,并且在章节末我们获取了上下文对象的实例:BookContext.这节主要承接上一篇,来讲解如何整合Spring IOC容器实现控制反转,依赖注入 ...

随机推荐

  1. 安全管理器SecurityManager

    一.文章的目的 这是一篇对Java安全管理器入门的文章,目的是简单了解什么是SecurityManager,对管理器进行简单配置,解决简单问题. 比如在阅读源码的时候,发现这样的代码,想了解是做什么的 ...

  2. UVA120-Stacks of Flapjacks(思维)

    Problem UVA120-Stacks of Flapjacks Accept: 9232  Submit: 38455Time Limit: 10000 mSec Problem Descrip ...

  3. Visual Studio Code 支持TensorFlow配置支持

    首先选择解释器 选择TensorFlow版本的conda版本 (当然你如果是通过python单独安装的TensorFlow也可以) 编辑器输入代码,进行测试 import tensorflow as ...

  4. Unexpected end of JSON input while parsing near

    运行 npm cache clean --force 即可解决pm install出现”Unexpected end of JSON input while parsing near”错误.

  5. esp8266 免费wifi强推广告神器(0) 项目介绍

    某宝产品 WIFI SSID广告终端路由推广宝 简单来说,手机连接免费wifi,自动弹出广告页面,有二维码和电话,点击电话直接打电话给商家客服,用户点击链接跳转到商家网页. 同时存在设置页面,使用者可 ...

  6. C++ —— 返回数组指针的函数 和 返回指向函数的指针的函数

    返回数组指针的函数 基础知识:数组不能被拷贝,函数不能返回数组,只能返回数组的指针或者引用. 定义一个 返回数组指针的函数 的方法,以 一个接收参数为 含有10个整型元素的数组的引用  和 返回一个含 ...

  7. Docker资源限制与Cgroups

    一.Linux control groups 简介     Linux CGroup全称Linux Control Group, 是Linux内核的一个功能,用来限制,控制与分离一个进程组群的资源(如 ...

  8. 两个select之间的元素互相移动并保持顺序

    <html> <head> <meta http-equiv="Content-Type" content="text/html; char ...

  9. Westore 1.0 正式发布 - 小程序框架一个就够

    世界上最小却强大的小程序框架 - 100多行代码搞定全局状态管理和跨页通讯 Github: https://github.com/dntzhang/westore 众所周知,小程序通过页面或组件各自的 ...

  10. python之间的基础

    编程第一步 print('hello,world!') 变量名的命名的规则: 1:变量由字母,数字,下划线组成 2:变量不能以数字开头 3:禁止使用python中的关键字,如 'alse', 'Non ...