spring和mybatis整合开发有三种整合方式1.传统DAO方式的开发整合(现在基本上不会用这种方式了,不推荐使用这种方式),2.mapper接口方式的开发整合(基于MapperFactoryBean的整合和基于MapperScannerConfigurer的整合)

mybatis和spring的开发整合环境:

1.mybatis核心jar包和解压过后lib目录下的所有jar包

2.spring核心jar包,以及aop开发要用到的jar包,关于事务的jar包,jdbc jar包,这些jar包都在下载的spring框架包中能找到

3.日志jar包,数据库驱动jar包,DBCP数据源jar包,以及连接池jar包,junit测试jar包

4.spring和mybatis整合的中间件,mybatis-spring jar包

整合所需要的配置文件

1.mybatis-config.xml 2.applicationContext.xml 3.db.properties 4.log4j.properties 5.xxxMapper.xml

传统DAO方式的开发整合

采用传统DAO方式的开发整合需要编写DAO接口以及接口的实现类 ,并且需要向DAO实现类中注入SqlSessionFactory,然后在方法体内通过SqlSessionFactory创建sqlsession。所以可以使用mybatis-spring包中的SqlSessionTemplate类和SqlSessionDaoSupport类来实现。

SqlSessionTemplate类是mybatis-spring的核心类,他负责管理mybatis的sqlsession,调用mybatis的sql方法。当调用sql方法时,SqlSessionTemplate将会保证使用的SqlSession和当前的spring的事务是相关的。并且他还管理bean的生命周期,包含必要的关闭,提交和回滚等。

SqlSessionDaoSupport类:是一个抽象支持类,他继承了DaoSupport类,主要是作为DAO的基类来使用。可以通过SqlSessionDaoSupport类的getSqlSession()方法来获取需要的SqlSession。

例如

/*客户持久化类*/

public class Customer {
private Integer id;
private String username;
private String jobs;
private String phone;
  setter/getter......
   @override
    toString()
} CustomerMapper映射文件
根据id查找客户
<mapper namespace="com.itheima.po.CustomerMapper">
<select id="findCustomerById" parameterType="Integer" resultType="customer">
select * from t_customer where id=#{id}
</select>
</mapper>
记得在mybatis-config.xml中配置mapper的路径

接口CustomerDao
public interface CustomerDao {
public Customer findCustomerById(Integer id);
} 接口实现类CustomerDaoImpl
CustomerDaoImpl需要先继承SqlSessionDaoSupport类,然后实现CustomerDao接口
public class CustomerDaoImpl extends SqlSessionDaoSupport implements CustomerDao {
@Override
public Customer findCustomerById(Integer id) {
return this.getSqlSession().selectOne("com.itheima.po.CustomerMapper.findCustomerById", id);
} }
在applicationContext.xml中实例化customerDao
<!--实例化dao-->
<bean id="customerDao" class="com.itheima.po.dao.impl.CustomerDaoImpl">
<!--注入sqlsessionFactory对象实例-->
<property name="sqlSessionFactory" ref="sqlSessionFactory"/>
</bean>
测试
public class FindCustomerByIdTest {
@Test
public void findCustomerById(){
ApplicationContext applicationContex = new ClassPathXmlApplicationContext("applicationContext.xml");
CustomerDao customerDao=applicationContex.getBean(CustomerDao.class);
Customer customer=customerDao.findCustomerById(2);
System.out.println(customer);
}
}
测试结果
Customer{id=2, username='chen', jobs='java', phone='456789'}
application.xml中完整配置
<!--读取db.properties配置信息-->
<context:property-placeholder location="classpath:db.properties"/>
<!--配置数据源-->
<bean id="dataSource" class="org.apache.commons.dbcp2.BasicDataSource">
<!--数据驱动-->
<property name="driverClassName" value="${jdbc.driver}"/>
<!--连接数据库的url地址-->
<property name="url" value="${jdbc.url}"/>
<!--连接数据库的用户名-->
<property name="username" value="${jdbc.username}"/>
<!--连接数据库的密码-->
<property name="password" value="${jdbc.password}"/>
<!--最大连接数-->
<property name="maxTotal" value="${jdbc.maxTotal}"/>
<!--最大空闲连接-->
<property name="maxIdle" value="${jdbc.maxIdle}"/>
<!--初始化连接数-->
<property name="initialSize" value="${jdbc.initialSize}"/>
</bean>
<!--事务管理器,依赖于数据源-->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"/>
</bean>
<!--开启事务注解-->
<tx:annotation-driven transaction-manager="transactionManager"/>
<!--配置Mybatis工厂-->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<!--注入数据源-->
<property name="dataSource" ref="dataSource"/>
<!--指定核心配置文件位置-->
<property name="configLocation" value="classpath:mybatis-config.xml"/>
</bean>
<!--实例化dao-->
<bean id="customerDao" class="com.itheima.po.dao.impl.CustomerDaoImpl">
<!--注入sqlsessionFactory对象实例-->
<property name="sqlSessionFactory" ref="sqlSessionFactory"/>
</bean>

spring和mybatis的整合开发(传统Dao开发方式)的更多相关文章

  1. spring和mybatis的整合开发(基于MapperFactoryBean的整合开发(方便简单不复杂))

    MapperFactoryBean是mybati-spring团队提供的一个用于根据mapper接口生成mapper对象的类. 在spring配置文件中可以配置以下参数: 1.mapperInterf ...

  2. SSM(Spring+SpringMVC+MyBatis)框架整合开发流程

    回忆了 Spring.SpringMVC.MyBatis 框架整合,完善一个小demo,包括基本的增删改查功能. 开发环境 IDEA MySQL 5.7 Tomcat 9 Maven 3.2.5 需要 ...

  3. Spring+Spring MVC+Mybatis 框架整合开发(半注解半配置文件)

    项目结构: (代码里面都有注释) 一.在pom文件中依赖jar包 因为我这里分了模块,所以有父子级的共两个pom文件 父级: <?xml version="1.0" enco ...

  4. spring和mybatis的整合开发(基于MapperScannerConfigurer的整合开发(适用于复杂项目,接口较多的情况))

    在实际项目中,Dao层会包含很多接口,这样会导致spring配置文件过于臃肿.这时就需要采用扫描包的形式来配置mybaits中的映射器. 采用MapperScannerConfigurer来实现. M ...

  5. 七 MyBatis整合Spring,DAO开发(传统DAO&动态代理DAO)

    整合思路: 1.SQLSessionFactory对象应该放到Spring中作为单例存在 2.传统dao开发方式中,应该从Spring容器中获得SqlSession对象 3.Mapper代理行驶中,应 ...

  6. spring与mybatis的整合

    整合的思路 SqlSessionFactory对象放到spring容器中作为单例存在. 传统dao的开发方式中,从spring容器中获得sqlsession对象. Mapper代理形式中,从sprin ...

  7. Spring+SpringMVC+MyBatis+easyUI整合基础篇(八)mysql中文查询bug修复

    写在前面的话 在测试搜索时出现的问题,mysql通过中文查询条件搜索不出数据,但是英文和数字可以搜索到记录,中文无返回记录.本文就是写一下发现问题的过程及解决方法.此bug在第一个项目中点这里还存在, ...

  8. Spring+SpringMVC+MyBatis的整合

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

  9. Spring+SpringMVC+MyBatis+easyUI整合进阶篇(六)一定要RESTful吗?

    作者:13 GitHub:https://github.com/ZHENFENG13 版权声明:本文为原创文章,未经允许不得转载. 写在前面的话 这个问题看起来就显得有些萌,或者说类似的问题都有些不靠 ...

随机推荐

  1. redux源码解析-函数式编程

    提到redux,会想到函数式编程.什么是函数式编程?是一种很奇妙的函数式的编程方法.你会感觉函数式编程这么简单,但是用起来却很方便很神奇. 在<functional javascript> ...

  2. 通过WebChannel/WebSockets与QML中的HTML交互

    来源:通过WebChannel/WebSockets与QML中的HTML交互 GitHub:八至 作者:狐狸家的鱼 本文链接:QML与HTML交互 在查询QML与HTML之间通信交互时资料很少,这篇文 ...

  3. Jupyter Notebook添加Ruby支持

    安装步骤 gem install iruby iruby register --force 参考资料:http://devopspy.com/linux/ruby-kernel-jupyter-not ...

  4. 详解 清除浮动 的多种方式(clearfix)

    说明 本文适合知道HTML 与 CSS基础知识的读者,或者想要了解清除浮动背后原理的读者! 1.什么是浮动 首先我们需要知道定位 元素在页面中的位置就是定位,解决问题之前我们先来了解下几种定位方式 : ...

  5. Python之常用模块--collections模块

    认识模块 什么是模块? 常见的场景:一个模块就是一个包含了python定义和声明的文件,文件名就是模块名字加上.py的后缀. 但其实import加载的模块分为四个通用类别: 1 使用python编写的 ...

  6. 第六节,Neural Networks and Deep Learning 一书小节(下)

    4.神经网络可以计算任何函数的可视化证明 神经网络拥有一定的普遍性,即包含一个隐藏层的神经网络可以被用来按照任意给定的精度来近似任何连续函数. 这一章使用一个实例来阐述神经网络是如何来近似一个一元函数 ...

  7. TestNg 12. extentReport测试报告

    直接上代码:以下是我的目录结构,首先新建一个包名字叫 com.tester.extent.demo,直接新建两个类就可以,名字叫  ExtentTestNGIReporterListener  和 T ...

  8. tomcat部署-手动启动tomcat部署,添加网页,

    公司的内网什么都不能往外传,于是自己用公司的网络搭了一个网页,在网上抄了一堆upload,用来来回传输数据.... 但是每次用ideaJ启动服务器太费时. 研究了一下怎么手动启动tomcat,部署网页 ...

  9. spring的设计模式

    spring中用到哪些设计模式   1.工厂模式,这个很明显,在各种BeanFactory以及ApplicationContext创建中都用到了: 2.模版模式,这个也很明显,在各种BeanFacto ...

  10. css+div基本知识;

    1.居中: <div class="test"></div> css: .test{ margin: auto; //一行中居中: } 2.IE与其他浏览器 ...