spring与mybatis整合
2016-04-17 08:40
369 查看
本文属于工作杂记,临时存放,待整理。
实际项目中应用是在applicationContext.xml中配置,
为了显示清晰:拆分出专门的spring配置,
与mybatis整合
至于mybatis的配置:**mapper.xml
实际项目中应用是在applicationContext.xml中配置,
为了显示清晰:拆分出专门的spring配置,
<!-- 自动扫描业务包 --> <context:component-scan base-package="com.xxx.service" /> <!-- 数据源 --> <jee:jndi-lookup id="jndiDataSource" jndi-name="java:comp/env/jdbc/datasource" /> <!-- 配置事务 --> <bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="jndiDataSource" /> </bean> <!-- 配置基于注解的事物aop --> <tx:annotation-driven transaction-manager="txManager" proxy-target-class="true"/>
与mybatis整合
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd"> <!-- 自动扫描 --> <context:component-scan base-package="com.cn.hnust" /> <!-- 引入配置文件 --> <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <property name="location" value="classpath:jdbc.properties" /> </bean> <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"> <property name="driverClassName" value="${driver}" /> <property name="url" value="${url}" /> <property name="username" value="${username}" /> <property name="password" value="${password}" /> <!-- 初始化连接大小 --> <property name="initialSize" value="${initialSize}"></property> <!-- 连接池最大数量 --> <property name="maxActive" value="${maxActive}"></property> <!-- 连接池最大空闲 --> <property name="maxIdle" value="${maxIdle}"></property> <!-- 连接池最小空闲 --> <property name="minIdle" value="${minIdle}"></property> <!-- 获取连接最大等待时间 --> <property name="maxWait" value="${maxWait}"></property> </bean> <!-- spring和MyBatis完美整合,不需要mybatis的配置映射文件 --> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="dataSource" /> <!-- 自动扫描mapping.xml文件 --> <property name="mapperLocations" value="classpath:com/cn/hnust/mapping/*.xml"></property> </bean> <!-- DAO接口所在包名,Spring会自动查找其下的类 --> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="basePackage" value="com.cn.hnust.dao" /> <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property> </bean> <!-- (事务管理)transaction manager, use JtaTransactionManager for global tx --> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource" /> </bean> </beans>
至于mybatis的配置:**mapper.xml
相关文章推荐
- java基础(2)
- 在eclipse中查看Android源码
- Java反射机制学习
- java代理理解
- Java基础--并发实用工具(2)
- mac eclipse Version 1.6.0_65 of the JVM is not suitable for this product
- JSR296 为 annotation processing 安装 Eclipse : m2e-apt 插件
- 今天学习到的Myeclipse快捷键
- JAVA技术开发规范(4)——JASMP平台的开发代码规范
- Eclipse查看Java源代码
- Java基础--并发实用工具(1)
- spring源码剖析(五)利用AOP实现自定义Spring注解
- 什么是jvm?
- Java开发web的几种开发模式
- eclipse字符集编码设置
- java xml转义方法以及中文字符的处理
- Java应用的优秀管理工具Maven的下载安装及配置
- 使用java排序的几种方式
- 类的设计技巧
- 20145322《Java程序设计》第2次实验报告