spring和mybatis的整合开发(传统Dao开发方式)
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开发方式)的更多相关文章
- spring和mybatis的整合开发(基于MapperFactoryBean的整合开发(方便简单不复杂))
MapperFactoryBean是mybati-spring团队提供的一个用于根据mapper接口生成mapper对象的类. 在spring配置文件中可以配置以下参数: 1.mapperInterf ...
- SSM(Spring+SpringMVC+MyBatis)框架整合开发流程
回忆了 Spring.SpringMVC.MyBatis 框架整合,完善一个小demo,包括基本的增删改查功能. 开发环境 IDEA MySQL 5.7 Tomcat 9 Maven 3.2.5 需要 ...
- Spring+Spring MVC+Mybatis 框架整合开发(半注解半配置文件)
项目结构: (代码里面都有注释) 一.在pom文件中依赖jar包 因为我这里分了模块,所以有父子级的共两个pom文件 父级: <?xml version="1.0" enco ...
- spring和mybatis的整合开发(基于MapperScannerConfigurer的整合开发(适用于复杂项目,接口较多的情况))
在实际项目中,Dao层会包含很多接口,这样会导致spring配置文件过于臃肿.这时就需要采用扫描包的形式来配置mybaits中的映射器. 采用MapperScannerConfigurer来实现. M ...
- 七 MyBatis整合Spring,DAO开发(传统DAO&动态代理DAO)
整合思路: 1.SQLSessionFactory对象应该放到Spring中作为单例存在 2.传统dao开发方式中,应该从Spring容器中获得SqlSession对象 3.Mapper代理行驶中,应 ...
- spring与mybatis的整合
整合的思路 SqlSessionFactory对象放到spring容器中作为单例存在. 传统dao的开发方式中,从spring容器中获得sqlsession对象. Mapper代理形式中,从sprin ...
- Spring+SpringMVC+MyBatis+easyUI整合基础篇(八)mysql中文查询bug修复
写在前面的话 在测试搜索时出现的问题,mysql通过中文查询条件搜索不出数据,但是英文和数字可以搜索到记录,中文无返回记录.本文就是写一下发现问题的过程及解决方法.此bug在第一个项目中点这里还存在, ...
- Spring+SpringMVC+MyBatis的整合
1.基本概念 1.1.Spring Spring是一个开源框架,Spring是于2003 年兴起的一个轻量级的Java 开发框架,由Rod Johnson 在其著作Expert One-On-On ...
- Spring+SpringMVC+MyBatis+easyUI整合进阶篇(六)一定要RESTful吗?
作者:13 GitHub:https://github.com/ZHENFENG13 版权声明:本文为原创文章,未经允许不得转载. 写在前面的话 这个问题看起来就显得有些萌,或者说类似的问题都有些不靠 ...
随机推荐
- bzoj3331 压力(圆方树)
题目链接 圆方树 圆方树就是对于联通无向图中的每一个点双新建一个方点,与点双中的每个点连一条边,然后将原来的边删去.将原来的点看作圆点,新建的点看作方点.所以叫做圆方树. 性质 1.圆方树肯定是棵树( ...
- ImageMagick:获取一行文字的宽与高
double *fm = MagickQueryFontMetrics(mw_temp, dw_wand, text_utf8); //获取文字在指定字体和字号下的宽度和高度 double textW ...
- px转换成bp单位的工具函数
import {Dimensions} from 'react-native' //当前屏幕的高度 const deviceH = Dimensions.get('window').height // ...
- TestNg1. 基本介绍注解介绍和如何让在maven中引用
1.更适合测试人员,有很多的套件. maven中引用: <!-- https://mvnrepository.com/artifact/org.testng/testng --><d ...
- zlib库交叉编译
zlib-1.2.11 开发板:arm9 交叉编译器arm-fsl-linux-gnueabihf-gcc 编译方式: ./configure -h可以发现zlib并没有提供CC配置,所以 (1)ex ...
- POJ 2976 Dropping tests(01分数规划)
Dropping tests Time Limit: 1000MS Memory Limit: 65536K Total Submissions:17069 Accepted: 5925 De ...
- 枚举类 enum,结构体类 struct
1.枚举类型的值,直观易于理解,见词知意. 格式: enum 枚举类名:值类型 { 值1, 值2, 值n } 每个值默认(省略“:值类型”)以int型数据存储,从0开始. 使用格式:枚举类名 变量=枚 ...
- keepalived+LVS实现网站负载均衡和HA
如上图所示,102和103是内网nginx服务器,100和101是边界LB,clinet是1,这个实验是为了实现在LB上虚拟出一个VIP,client通过访问该VIP,来动态负载到两台内网nginx服 ...
- 快速傅里叶变换(FFT)_转载
FFTFFT·Fast Fourier TransformationFast Fourier Transformation快速傅立叶变换 P3803 [模板]多项式乘法(FFT) 参考上文 首 ...
- 使用 windows 下的 secureCRT 软件的 通过 sftp 上传和下载文件到远端 linux 设备
secureCRT 按下ALT+P就开启新的会话进行ftp操作. 输入:help命令,显示该FTP提供所有的命令 pwd: 查询linux主机所在目录(也就是远程主机目录) lpwd: 查询本地目录 ...