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. ArcGIS for qml -关于空间参考如何选择设置

    作者: 狐狸家的鱼 Github: 八至 版权声明:如需转载请获取授权和联系作者 1.关于空间参考 空间参考可以通过众所周知的ID(WKID) - 整数值来引用. 官网指南中也有对此的专门说明 htt ...

  2. ONI无法启动: Uh oh! Unable to launch Neovim...

    问题描述 在终端中是可以打开nvim的,ONI无法正确找到位置 解决方法 修改配置文件,指定nvim的路径 终端中输入which nvim定位所在位置,这里返回的结果是/usr/local/bin/n ...

  3. Java 接口篇

    为什么使用接口? 问题 要求实现防盗门的功能 分析 门有开和关的功能,锁有上锁和开锁的功能 将门和锁分别定义为抽奖类 那么问题就是防盗门即继承了门的同时又继承了锁,而Java的继承是单继承,接口可多继 ...

  4. numpy的使用方法

    一.numpy快速入门 1.什么是numpy: numpy是python的一个矩阵类型,提供了大量矩阵处理的函数,非正式来说,就是一个使运算更容易,执行更迅速的库,因为它的内部运算是通过c语言而不是p ...

  5. linux:逐行合并两文件(paste命令)

    存在file1.txt 1 2 3 4 5 6 file2.txt a b c d e f 现希望生成file3.txt 1 2 a b 3 4 c d 5 6 e f 则可以用到如下命令: past ...

  6. java统计指定目录中文件的个数和总的大小

    转: 统计指定目录中文件的个数和总的大小 package file; import java.io.File; import java.util.ArrayList; public class Fil ...

  7. java 网络编程 TCP协议 java 服务器和客户端 java socket编程

    一个 HelloWord 级别的 Java Socket 通信的例子.通讯过程:        先启动 Server 端,进入一个死循环以便一直监听某端口是否有连接请求.然后运行 Client 端,客 ...

  8. Tomcat 启动时 警告: [SetPropertiesRule]{Server/Service/Engine/Host/Context}

    在Eclipse 中,启动Tomcat 时,出现: 警告: [SetPropertiesRule]{Server/Service/Engine/Host/Context} Setting proper ...

  9. Windows下MySQL下载安装、配置与使用

    用过MySQL之后,不论容量的话,发现比其他两个(sql server .oracle)好用的多,一下子就喜欢上了.下面给那些还不知道怎么弄的童鞋们写下具体的方法步骤. (我这个写得有点太详细了,甚至 ...

  10. php中加密和解密

    项目要和第三方进行接口对接,所以数据的安全很重要.第一次自己设计并实现,学习记录下 网上查了很多资料,真的很深奥 对称加密: 双方共用一个约定好的密钥进行数据的加密和解密,但是当密匙丢失,数据将有泄露 ...