mybatis spring xml设置
2016-09-26 13:17
190 查看
所有的properties文件在最初入口xml文件中定义。
typeHandlersPackage的value是一个package,可以保存多个class
配置事务
<property name="dataSource" ref="accgDataSource" />定义的bean在下方
再如下贴出配置文件。
test.resources.config/mysql.properties
定义了所有的字段。
另外,在test中有一个spring-config-mysql.xml
<bean id="configProperties" class="org.springframework.beans.factory.config.PropertiesFactoryBean"> <property name="locations"> <list> <value>classpath*:*.properties</value> </list> </property> </bean>
<!-- 创建SqlSessionFactory,同时指定数据源 --> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="accgDataSource" /> <property name="mapperLocations"> <array> <value>classpath*:META-INF/mybatis/mapper/*.xml</value> </array> </property> <!--typeHandlersPackage 节点保存了数据库字段类型和java字段类型之间的映射 --> <property name="typeHandlersPackage" value="common.mybatis.type" /> <property name="configurationProperties"> <map> <entry key="__dbPrefix" value="${db.prefix}" /> </map> </property> </bean>
typeHandlersPackage的value是一个package,可以保存多个class
<!-- 事务相关配置 --> <tx:annotation-driven transaction-manager="transactionManager" order="1" /> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="accgDataSource" /> </bean>
配置事务
<property name="dataSource" ref="accgDataSource" />定义的bean在下方
<!-- Mybatis Mapper扫描 --> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="annotationClass" value="org.springframework.stereotype.Repository" /> <property name="basePackage" value="com.fcore.**.dao" /> <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" /> </bean>
<!-- 数据源配置 --> <bean id="accgDataSource" parent="abstractDataSource"> <property name="driverClassName" value="${accg.0.mysql.driver}" /> <property name="maxActive" value="${accg.0.jdbc.maxActive}" /> <property name="url" value="${accg.0.jdbc.url}" /> <property name="username" value="${accg.0.jdbc.username}" /> <property name="password" value="${accg.0.jdbc.password}" /> <property name="numTestsPerEvictionRun" value="${accg.0.jdbc.num 4000 TestsPerEvictionRun}" /> </bean> <bean id="abstractDataSource" class="com.alibaba.druid.pool.DruidDataSource" destroy-method="close"> <property name="filters" value="${accg.datasource.filters}" /> <property name="defaultAutoCommit" value="${accg.datasource.defaultAutoCommit}" /> <property name="initialSize" value="${accg.datasource.initialSize}" /> <property name="minIdle" value="${accg.datasource.minIdle}" /> <property name="maxWait" value="${accg.datasource.maxWait}" /> <property name="testWhileIdle" value="${accg.datasource.testWhileIdle}" /> <property name="testOnBorrow" value="${accg.datasource.testOnBorrow}" /> <property name="testOnReturn" value="${accg.datasource.testOnReturn}" /> <property name="validationQuery" value="${accg.datasource.validationQuery}" /> <property name="timeBetweenEvictionRunsMillis" value="${accg.datasource.timeBetweenEvictionRunsMillis}" /> <property name="minEvictableIdleTimeMillis" value="${accg.datasource.minEvictableIdleTimeMillis}" /> <property name="logAbandoned" value="${accg.datasource.logAbandoned}" /> <property name="removeAbandoned" value="${accg.datasource.removeAbandoned}" /> <property name="removeAbandonedTimeout" value="${accg.datasource.removeAbandonedTimeout}" /> </bean> <bean id="batchSqlSession" class="org.mybatis.spring.SqlSessionTemplate"> <constructor-arg index="0" ref="sqlSessionFactory" /> <constructor-arg index="1" value="BATCH" /> </bean>
再如下贴出配置文件。
test.resources.config/mysql.properties
定义了所有的字段。
另外,在test中有一个spring-config-mysql.xml
<?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: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/context http://www.springframework.org/schema/context/spring-context.xsd"> <!-- 确保可在@Value中, 使用SeEL表达式获取资源属性 --> <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <property name="properties" ref="configProperties" /> </bean> <bean id="configProperties" class="org.springframework.beans.factory.config.PropertiesFactoryBean"> <property name="locations"> <list> <value>classpath:/config/*.properties</value> </list> </property> </bean> <!-- 初始化Flogger Container --> <bean id="initLogContainerBean" class="com.common.logger.InitLogContainerBean"> <property name="logLocation" value="classpath*:META-INF/log/*.log.xml" /> <property name="errorLocation" value="classpath*:META-INF/error/*.error.xml" /> </bean> <!-- Spring annotation扫描 --> <context:component-scan base-package="com.fbu.fcore.accounting.common" /> <!-- 日切服务 --> <bean id="dayCutService" class="com.common.daycut.service.impl.DayCutServiceImpl" /> <bean id="accountingEntryGenerator" class="com.accounting.common.service.AccountingEntryGenerator" /> <bean id="accountingRuleEngine" class="com.accounting.common.service.AccountingRuleEngine" init-method="init" /> <!-- Imports --> <import resource="classpath:/spring/spring-mybatis.xml" /> <import resource="classpath:/spring/spring-cache.xml" /> <import resource="classpath:/spring/spring-uid-generator.xml" /> <import resource="classpath:/spring/spring-jobs.xml" /> </beans>
相关文章推荐
- JPA+Spring下,applicationContext.xml中的entityManagerFactory中的class设置问题
- Spring、MyBatis的整合数据映射器类(UserMapper->iocContext.xml)配置文件详解
- MyBatis与Spring设置callSettersOnNulls
- springMVC+Mybatis的maven-web项目的pom.xml文件内容
- MyBatis在非Spring环境下第三方DataSource设置
- JPA+Spring下,applicationContext.xml中的entityManagerFactory中的class设置问题
- Maven+Mybatis+Spring配置之第一步:配置pom.xml依赖
- 也来设置下Spring+MyBatis的事务控制
- Spring+Hibernate配置文件-applicationContext.xml设置
- spring和mybatis集成(二) 设置spring的动态数据源
- SqlMapConfig.xml --- 在spring配置文件中自动加载的mybatis文件
- spring+springmvc+druid+oscache+mybatis整合web.xml 配置内容
- SpringMVC+MyBatis 整合遇到错误 Configuration problem: Unable to locate Spring NamespaceHandler for XML
- MyBatis和Spring设置callSettersOnNulls
- Spring+Mybatis整合配置xml
- spring应用中web.xml的设置
- SqlMapConfig.xml---在spring配置文件中自动加载的mybatis文件
- springmvc + spring + mybatis 框架设置
- Spring中关于ClassPathXmlApplicationContext和FileSystemXmlApplicationContext的路径设置
- class path resource [src/main/resources/conf/spring-mybatis.xml] cannot be opened because it does no