spring整合mybatis(hibernate)配置
一、Spring整合配置Mybatis
spring整合mybatis可以不需要mybatis-config.xml配置文件,直接通过spring配置文件一步到位。一般需要具备如下几个基本配置。
1.配置数据源(连接数据库最基本的属性配置,如数据库url,账号,密码,和数据库驱动等最基本参数配置)
<!-- 导入properties配置文件 -->
<context:property-placeholder location="classpath*:/jdbc.properties"/> <!-- 数据源基本配置 -->
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="username" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
<property name="url" value="${jdbc.url}"/>
<property name="driverClassName" value="${jdbc.driverClassName}"/>
</bean>
我们将参数配置统一写入jdbc.properties文件中:
jdbc.url=jdbc:mysql://localhost:3306/mydb
jdbc.driverClassName=com.mysql.jdbc.Driver
jdbc.username=root
jdbc.password=root
2.配置SessionFactory(用于将数据源和mybatis的mapper映射文件进行管理和初始化)
<!-- 创建sessionFactory -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<!-- 扫描mapper映射文件 -->
<property name="mapperLocations" value="classpath*:dao/mapping/*.xml" />
</bean>
3.扫描mapper映射文件所对应的dao接口类(其实dao接口的是实现类就是mapper.xml映射文件,此配置是为了将接口和映射文件进行初始化)
<!-- 扫描与mapper映射文件对应的dao接口类 -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="dao.daoInterface"/>
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
</bean>
4.创建事务(事务有两种配置方式:注解方式和aop切入方式)
<!-- 创建事务管理 -->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"/>
</bean>
创建好事务管理后,我们可以选择使用注解方式实现管理,或者aop织入管理
4.1注解方式
<!-- 注解式事务配置,启动事务注解驱动 -->
<tx:annotation-driven/>
注解配置方式要先通过配置文件启动事务注解驱动,然后在要加事务的方法上面加上事务注解:@Transactional(propagation = Propagation.REQUIRED,isolation = Isolation.READ_COMMITTED) 事务相关知识可参考:http://www.cnblogs.com/caijh/p/7724964.html
@Transactional(propagation = Propagation.REQUIRED,isolation = Isolation.READ_COMMITTED)
@Override
public void insertUser(UserEntity userEntity) {
for(int i=0;i<10;i++){
userEntity.setId(111+i);
userEntity.setUsername("mybatis "+i);
userDao.insertUser(userEntity);
}
}
4.2 AOP织入方式
<!-- aop切入式事务配置 -->
<tx:advice id="trAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="*" propagation="REQUIRED" isolation="READ_COMMITTED"/>
</tx:attributes>
</tx:advice> <aop:config>
<aop:pointcut id="serviceAdvice" expression="execution(* service.serviceImpl.*.*(..))"/>
<aop:advisor advice-ref="trAdvice" pointcut-ref="serviceAdvice"/>
</aop:config>
AOP相关知识可参考:http://www.cnblogs.com/caijh/p/7710725.html
最终配置如下:
<?xml version="1.0" encoding="UTF-8"?>
<!--
~ @(#) applicationContext.xml
~ <br> Copyright: Copyright (c) 2017
~ <br> @author cjh
~ <br> 2017-10-29 15:45:16
-->
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.3.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd"> <!-- 导入properties配置文件 -->
<context:property-placeholder location="classpath*:/jdbc.properties"/> <!-- 扫描注解包 -->
<context:component-scan base-package="dao.daoInterface"/>
<context:component-scan base-package="service.serviceImpl" /> <!-- 数据源基本配置 -->
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="username" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
<property name="url" value="${jdbc.url}"/>
<property name="driverClassName" value="${jdbc.driverClassName}"/>
</bean> <!-- 创建sessionFactory -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<!-- 扫描mapper映射文件 -->
<property name="mapperLocations" value="classpath*:dao/mapping/*.xml" />
</bean> <!-- 扫描与mapper映射文件对应的dao接口类 -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="dao.daoInterface"/>
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
</bean> <!-- 创建事务管理 -->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"/>
</bean> <!-- 注解式事务配置,启动事务注解驱动 -->
<!--<tx:annotation-driven/>--> <!-- aop切入式事务配置 -->
<tx:advice id="trAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="*" propagation="REQUIRED" isolation="READ_COMMITTED"/>
</tx:attributes>
</tx:advice> <aop:config>
<aop:pointcut id="serviceAdvice" expression="execution(* service.serviceImpl.*.*(..))"/>
<aop:advisor advice-ref="trAdvice" pointcut-ref="serviceAdvice"/>
</aop:config> </beans>
SSMDemo整合配置源码位置:https://gitee.com/codecaijh/SSMDemo
二、Spring整合配置Hibernate
Spring整合配置hibernate和Mybatis的配置大同小异,主要区别在与SessionFactory和映射文件的管理配置,但目的都是一样的。
1.配置数据源(连接数据库最基本的属性配置,如数据库url,账号,密码,和数据库驱动等最基本参数配置)【同Mybatis配置】
2.配置SessionFactory(因为Hibernate对数据库操作做了封装,所以需要一些额外的属性配置)
<!-- 创建sessionFactory -->
<bean id="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="hibernateProperties">
<props>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.format_sql">true</prop>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</prop>
<!--<prop key="hibernate.current_session_context_class">org.springframework.orm.hibernate5.SpringSessionContext</prop>-->
</props>
</property>
<!-- 实体类映射文件 -->
<property name="mappingLocations">
<list>
<value>classpath*:/domain/*.hbm.xml</value>
</list>
</property>
<!-- 扫描实体类包 -->
<property name="packagesToScan">
<value>domain</value>
</property>
<!-- 实体类 -->
<property name="annotatedClasses">
<list>
<value>domain.UserEntity</value>
</list>
</property>
</bean>
Hibernate的配置中,把映射文件和是实体类映射全部配置在SessionFactory中,也就是和Mybatis第2步和第3步类似,
3.创建事务(事务有两种配置方式:注解方式和aop切入方式)【同Mybatis配置】
最终配置如下:
<?xml version="1.0" encoding="UTF-8"?>
<!--
~ @(#) applicationContext.xml
~ <br> Copyright: Copyright (c) 2017
~ <br> @author cjh
~ <br> 2017-10-29 15:45:16
-->
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.3.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd"> <!-- 导入properties配置文件 -->
<context:property-placeholder location="classpath*:/jdbc.properties"/> <!-- 扫描注解包 -->
<context:component-scan base-package="dao.daoImpl"/>
<context:component-scan base-package="service.serviceImpl" /> <!-- 数据源基本配置 -->
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="username" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
<property name="url" value="${jdbc.url}"/>
<property name="driverClassName" value="${jdbc.driverClassName}"/>
</bean> <!-- 创建sessionFactory -->
<bean id="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="hibernateProperties">
<props>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.format_sql">true</prop>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</prop>
<!--<prop key="hibernate.current_session_context_class">org.springframework.orm.hibernate5.SpringSessionContext</prop>-->
</props>
</property>
<!-- 实体类映射文件 -->
<property name="mappingLocations">
<list>
<value>classpath*:/domain/*.hbm.xml</value>
</list>
</property>
<!-- 扫描实体类包 -->
<property name="packagesToScan">
<value>domain</value>
</property>
<!-- 实体类 -->
<property name="annotatedClasses">
<list>
<value>domain.UserEntity</value>
</list>
</property>
</bean> <!-- 创建声明式事务管理 -->
<bean id="transactionManager" class="org.springframework.orm.hibernate5.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"/>
</bean>
<!-- 事务通知(注解方式) -->
<tx:annotation-driven transaction-manager="transactionManager"/> <!-- 事务通知(aop方式) -->
<!--<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<!– propagation配置传播行为,isolation配置隔离方式 –>
<tx:method name="*" propagation="REQUIRED" isolation="READ_COMMITTED" />
</tx:attributes>
</tx:advice> <!– aop织入通知 –>
<aop:config>
<aop:pointcut id="serviceOption" expression="(execution(* service.serviceImpl.*.*(..)) and (execution(* dao.daoImpl.*.*(..))))"/>
<aop:advisor advice-ref="txAdvice" pointcut-ref="serviceOption"/>
</aop:config>--> </beans>
SSHDemo整合配置源码位置:https://gitee.com/codecaijh/SSHDemo
三、可能遇到的问题
在整合mybatis的时候可能会遇到 BindingException: Invalid bound statement (not found): dao.daoInterface.UserDao.getUserInfo dao接口类和mapper文件绑定失败而找不到实现方法的异常。

查看target目录下的classes文件时发现没有任何xml文件,推断项目编译的时候可能没把它包含进去。
解决办法:
在pom.xml文件中添加如下内容:(表示让maven将以xml和properties等为后缀的文件在构建的时候从资源路径加载到目标路径)
<build>
<resources>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.properties</include>
<include>**/*.xml</include>
</includes>
<filtering>false</filtering>
</resource>
</resources>
</build>
资源往往不是代码,无需编译,而是一些properties或XML配置文件,构建过程中会往往会将资源文件从源路径复制到指定的目标路径。
配置说明:
- resources,build过程中涉及的资源文件
- targetPath,资源文件的目标路径
- filtering,构建过程中是否对资源进行过滤,默认false
- directory,资源文件的路径,默认位于${basedir}/src/main/resources/目录下
- includes,一组文件名的匹配模式,被匹配的资源文件将被构建过程处理
- excludes,一组文件名的匹配模式,被匹配的资源文件将被构建过程忽略。同时被includes和excludes匹配的资源文件,将被忽略。
- filters,给出对资源文件进行过滤的属性文件的路径,默认位于${basedir}/src/main/filters/目录下。属性文件中定义若干键值对。在构建过程中,对于资源文件中出现的变量(键),将使用属性文件中该键对应的值替换。
- testResources,test过程中涉及的资源文件,默认位于${basedir}/src/test/resources/目录下。这里的资源文件不会被构建到目标构件中
spring整合mybatis(hibernate)配置的更多相关文章
- 阶段3 3.SpringMVC·_07.SSM整合案例_09.ssm整合之Spring整合MyBatis框架配置事务
spring加入声明式的事物 配置事物 配置事物管理器 需要一个dataSource,引入上面的dataSource 配置事务通知 引入上面的transactionManager事物管理器 find开 ...
- spring整合mybatis详解
在上篇螃蟹已经说明spring注解的最经典配置,接下来开始整合mybatis,这样整个项目就相对完整了. 有关本实例的源码可以到 <spring MVC注解实例及说明文档> 下载. 如需转 ...
- spring 整合 mybatis 中数据源的几种配置方式
因为spring 整合mybatis的过程中, 有好几种整合方式,尤其是数据源那块,经常看到不一样的配置方式,总感觉有点乱,所以今天有空总结下. 一.采用org.mybatis.spring.mapp ...
- idea spring+springmvc+mybatis环境配置整合详解
idea spring+springmvc+mybatis环境配置整合详解 1.配置整合前所需准备的环境: 1.1:jdk1.8 1.2:idea2017.1.5 1.3:Maven 3.5.2 2. ...
- spring基础:什么是框架,框架优势,spring优势,耦合内聚,什么是Ioc,IOC配置,set注入,第三方资源配置,综合案例spring整合mybatis实现
知识点梳理 课堂讲义 1)Spring简介 1.1)什么是框架 源自于建筑学,隶属土木工程,后发展到软件工程领域 软件工程中框架的特点: 经过验证 具有一定功能 半成品 1.2)框架的优势 提高开发效 ...
- spring整合mybatis,ioc容器及声明式事务配置
步骤: 1.创建jdbc.properties文件,用来管理存放连接数据库的相关信息 jdbc.properties:jdbc.user=root jdbc.password=123456 jdbc. ...
- Spring学习总结(六)——Spring整合MyBatis完整示例
为了梳理前面学习的内容<Spring整合MyBatis(Maven+MySQL)一>与<Spring整合MyBatis(Maven+MySQL)二>,做一个完整的示例完成一个简 ...
- Spring学习总结(五)——Spring整合MyBatis(Maven+MySQL)二
接着上一篇博客<Spring整合MyBatis(Maven+MySQL)一>继续. Spring的开放性和扩张性在J2EE应用领域得到了充分的证明,与其他优秀框架无缝的集成是Spring最 ...
- spring整合mybatis错误:class path resource [config/spring/springmvc.xml] cannot be opened because it does not exist
spring 整合Mybatis 运行环境:jdk1.7.0_17+tomcat 7 + spring:3.2.0 +mybatis:3.2.7+ eclipse 错误:class path reso ...
随机推荐
- 学习资料分享:Python能做什么?
最近一直忙着研究学习Python,很久没更新博客了,整理了一些Python学习资料,和大家分享一下!每天更新一篇~ 一.Python 特点 1.易于学习:Python有相对较少的关键字,结构简单,和一 ...
- js中的isNaN()函数
<html> <head> <script type="text/javascript" src="function.js"> ...
- PHP调用外部命令
------------------------------------------------------------------ 一.PHP调用外部命令总结 ...
- C语言_初步了解一下指针
指针的基本概念 在计算机中,所有的数据都是存放在存储器中的. 一般把存储器中的一个字节称为一个内存单元, 不同的数据类型所占用的内存单元数不等,如整型量占2个单元,字符量占1个单元等.为了正确地访问这 ...
- 静态编译程序 依赖于 Qt 和 Opencv 静态库 会出现 jpeg jpg 图像格式保存崩溃的情况,这是什么原因?
双方依赖的 jpeg 静态库源码版本冲突,重新静态编译库,都采用一致版本即可.
- the c programing language 学习过程8
glean 捡拾落穗; glean insight 深入了解 modeled模型化 peripheral外围的 himogeneous匀称的 intents 意图 excerpt摘录 intende ...
- linux_group总结
group_name:passwd:GID:user_list 在/etc/group 中的每条记录分四个字段: 第一字段:用户组名称: 第二字段:用户组密码: 第三字段:GID 第四字段:用户列表, ...
- win10 , JAVA安装 环境搭建
一.关于下载地址及方式: 1.百度搜索JDK,进入下载官网,具体网址如下: click me!!! 2.根据你电脑的不同系统选择对应的下载,当然首先你要点击Accept License Agreeme ...
- 一句Python,一句R︱数据的合并、分组、排序、翻转
先学了R,最近刚刚上手python,所以想着将python和R结合起来互相对比来更好理解python.最好就是一句python,对应写一句R. python中的numpy模块相当于R中的matirx矩 ...
- php和java中的加密和解密
遇到的java代码如下: Cipher cipher=Cipher.getInstance("DESede/CBC/PKCS5Padding"); 在php中使用des算法 始终校 ...