在spring项目中配置mybatis

目前的互联网项目的架构,大都会设计到spring、Mybatis,这里把在spring中Mybatis相关的配置简单整理下。

  1. 项目需要配置spring,需要配置项目的web.xml文件,相关配置如下:
    <span style="white-space:pre">	</span><!-- 配置spring的配置文件ApplicationContext.xml的位置 -->
    <span style="white-space:pre">	</span><context-param>
    		<param-name>contextConfigLocation</param-name>
    		<param-value>classpath*:/spring/applicationContext.xml</param-value>
    	</context-param>
    <span style="white-space:pre">	</span><!-- 添加spring的监听类 -->
    <span style="white-space:pre">	</span><listener>
    		<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    	</listener>
  2. 在src目录下的spring文件夹中新建applicationContext.xml文件,配置如下:
    <span style="white-space:pre">	</span><!-- 使用Annotation自动注册Bean -->
    	<context:component-scan base-package="com.tonson"/>
    	
    	<!-- 添加注解的支持 -->
    	<context:annotation-config />
    	
    	<!-- 添加Mybatis的sessionFactory -->
    	<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
    		<property name="dataSource" ref="backupDataSource" />
    		<property name="configLocation" value="classpath:spring/applicationContext-mybatis.xml"></property>  
    	</bean>
    
    	<!-- 让Mybatis自己去扫描mapper文件 -->
    	<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
    		<property name="basePackage" value="com.tonson.dao.mapper" />
    	</bean>
    	
    	<!-- 加载配置属性文件 -->
    	<context:property-placeholder ignore-unresolvable="true" location="classpath:config.properties" />
    	
    	<!-- 以下是配置事务 -->
    	<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
    		<property name="dataSource" ref="backupDataSource" />
    	</bean>
    
    	<aop:config>
    		<aop:advisor pointcut="execution(* com.tonson..*Service.*(..))" advice-ref="txAdvice"></aop:advisor>
    	</aop:config>
    
    	<tx:advice id="txAdvice" transaction-manager="transactionManager">
    		<tx:attributes>
    			<tx:method name="create*,save*,update*,delete*"
    				propagation="REQUIRED"></tx:method>
    			<tx:method name="*" read-only="true"></tx:method>
    		</tx:attributes>
    	</tx:advice>
    	
    	<!-- 加载配置属性文件 -->
    	<context:property-placeholder ignore-unresolvable="true" location="classpath:config.properties" />
    	
    	<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
    		<property name="dataSource" ref="dataSource" />
    	</bean>
    
    	<aop:config>
    		<aop:advisor pointcut="execution(* com.tonson..*Service.*(..))" advice-ref="txAdvice"></aop:advisor>
    	</aop:config>
    
    	<tx:advice id="txAdvice" transaction-manager="transactionManager">
    		<tx:attributes>
    			<tx:method name="create*,save*,update*,delete*"
    				propagation="REQUIRED"></tx:method>
    			<tx:method name="*" read-only="true"></tx:method>
    		</tx:attributes>
    	</tx:advice>
    	
    	<!-- 配置druid数据源 -->
    	<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource"
    		init-method="init" destroy-method="close">
    		<!-- 基本属性 url、user、password -->
    		<property name="url" value="${backup.jdbc.url}" />
    		<property name="username" value="${backup.jdbc.username}" />
    		<property name="password" value="${backup.jdbc.password}" />
    		<property name="connectionProperties" value="config.decrypt=true" />
    		<!-- 配置初始化大小、最小、最大 -->
    		<property name="initialSize" value="${backup.jdbc.pool.init}" />
    		<property name="minIdle" value="${backup.jdbc.pool.minIdle}" />
    		<property name="maxActive" value="${backup.jdbc.pool.maxActive}" />
    		<!-- 配置获取连接等待超时的时间 -->
    		<property name="maxWait" value="60000" />
    		<!-- 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒 -->
    		<property name="timeBetweenEvictionRunsMillis" value="60000" />
    		<!-- 配置一个连接在池中最小生存的时间,单位是毫秒 -->
    		<property name="minEvictableIdleTimeMillis" value="300000" />
    		<property name="validationQuery" value="SELECT 'x'" />
    		<property name="testWhileIdle" value="true" />
    		<property name="testOnBorrow" value="false" />
    		<property name="testOnReturn" value="false" />
    
    		<!-- 监控统计拦截的filters,日志记录
    		<property name="proxyFilters">
    			<list>
    				<ref bean="stat-filter" />
    				<ref bean="log-filter" />
    			</list>
    		</property> -->
    		<!-- 监控统计拦截的filters,日志记录 
    		<property name="filters">
    			<value>stat,wall,slf4j,config</value>
    		</property>-->
    		<property name="filters">
    			<value>config</value>
    		</property>
    	</bean>
  3. 在src目录下的spring目录里新建配置文件applicationContext-mybatis.xml,内容如下:
    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE configuration
        PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-config.dtd">
    
    <configuration>
    <settings>
      <setting name="lazyLoadingEnabled" value="true" />
    </settings>
    </configuration>
    

    到此位置,Mybatis的配置就完成了。


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值