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 版权声明:本文为原创文章,未经允许不得转载. 写在前面的话 这个问题看起来就显得有些萌,或者说类似的问题都有些不靠 ...
随机推荐
- GNOME禁用GDM中night-light功能
Night-light feature is enabled also in GDM screen, see here : https://bugzilla.gnome.org/show_bug.cg ...
- HDU/HDOJ 4864 Task
贪心题. 贪心方法很是naive...... 首先我们就能注意到一个性质:优先选择时间(x)长的,然后才是等级(y). 所以我们把机器和任务排好序,从大到小枚举任务.对于每一个x满足的机器,x也一定满 ...
- P1637 三元上升子序列
thair 好,这个naive的东西因为只有三元,很好求解.只要把每个数之前小的L[i]与之后大的R[i]求一下即可. 求两次逆序对即可.那么答案便是∑(L[i]*R[i]); 对于更高元的,胡雨菲写 ...
- A1009. Product of Polynomials
This time, you are supposed to find A*B where A and B are two polynomials. Input Specification: Each ...
- MySQL填充字符串函数 LPAD(str,len,padstr),RPAD(str,len,padstr)
转: MySQL填充字符串函数 LPAD(str,len,padstr),RPAD(str,len,padstr) LPAD(str,len,padstr) 用字符串 padstr对 str进行左边填 ...
- Spring 学习笔记一
1.IOC,DI. 2.装配bean基于xml(实例化,声明周期,后处理bean,属性注入).3.装配bean基于注解 1 spring框架概述 1.1 什么是spring l Sp ...
- TestNg 2.套件测试
看一下我的目录结构,新建一个包,名字叫做suite,主要为了做套件的测试用.然后在resource下新建一个文件,一般的叫做testng.xml,我这里随便起个名字,叫做suite.xml. 运行的时 ...
- noi.openjudge 1.12.6
http://noi.openjudge.cn/ch0112/06/ 总时间限制: 2000ms 内存限制: 65536kB 描述 传说很遥远的藏宝楼顶层藏着诱人的宝藏.小明历尽千辛万苦终于找到传 ...
- Nginx 性能优化有这篇就够了!
目录: 1.Nginx运行工作进程数量 Nginx运行工作进程个数一般设置CPU的核心或者核心数x2.如果不了解cpu的核数,可以top命令之后按1看出来,也可以查看/proc/cpuinfo文件 g ...
- rabbitMQ 3.6.15生产环境
服务器配置 系统环境:CentOS 7 由于RabbitMQ依赖erlang, 所以需要先安装erlang 下载erlang 下载地址 http://erlang.org/download/ linu ...