mybatis spring xml设置


所有的properties文件在最初入口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.numTestsPerEvictionRun}" />
</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>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值