Spring配置文件

  1. spring整合mybatis的配置文件,一般取名为applicationContext-mybatis.xml
  2. appapplication是核心

1.头文件

<?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:aop="http://www.springframework.org/schema/aop"  
        xmlns:p="http://www.springframework.org/schema/p"  
        xmlns:tx="http://www.springframework.org/schema/tx"  
        xmlns:context="http://www.springframework.org/schema/context"  
        xsi:schemaLocation="   
            http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd   
            http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd   
            http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd 
            http://www.springframework.org/schema/context 
            http://www.springframework.org/schema/context/spring-context.xsd">  

</beans>

2.配置自动扫描server和dao层的包,配合注解实现,就可以自动创建对象

 <context:component-scan base-package="service"/> 
 <context:component-scan base-package="dao"/>  

3.读取数据库的配置文件

<context:property-placeholder location="classpath:数据库配置文件名"/>
该文件内容:
image

<context:property-placeholder location="classpath:database.properties"/>

4.配置连接池,此处用的是dbcp

<!-- JNDI获取数据源(使用dbcp连接池) -->  
    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close" scope="singleton">
	<property name="driverClassName" value="${driver}" />  
		<property name="url" value="${url}" />  
		<property name="username" value="${user}" />  
		<property name="password" value="${password}" />
		<property name="initialSize" value="${initialSize}"/>
		<property name="maxActive" value="${maxActive}"/>
		<property name="maxIdle" value="${maxIdle}"/>
		<property name="minIdle" value="${minIdle}"/>
		<property name="maxWait" value="${maxWait}"/>
		<property name="removeAbandonedTimeout" value="${removeAbandonedTimeout}"/>
		<property name="removeAbandoned" value="${removeAbandoned}"/>
		<!-- sql 心跳 -->
		<property name= "testWhileIdle" value="true"/>
		<property name= "testOnBorrow" value="false"/>
		<property name= "testOnReturn" value="false"/>
		<property name= "validationQuery" value="select 1"/>
		<property name= "timeBetweenEvictionRunsMillis" value="60000"/>
		<property name= "numTestsPerEvictionRun" value="${maxActive}"/>
</bean>

5.配置事务管理(不是所有的都要)

<!-- 事务管理 -->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
   	<property name="dataSource" ref="dataSource"/>
</bean>

6.配置mybitas SqlSessionFactoryBean,用于创建appapplication

	<!-- 配置mybitas SqlSessionFactoryBean-->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
	<property name="dataSource" ref="dataSource"/>
	<property name="configLocation" value="classpath:mybatis-config.xml"/>
</bean>

7.开始事务

<!-- AOP 事务处理 开始 -->    
<aop:aspectj-autoproxy />
<aop:config  proxy-target-class="true">
	<aop:pointcut expression="execution(* *cn.smbms.service..*(..))" id="transService"/>
	<aop:advisor pointcut-ref="transService" advice-ref="txAdvice" />
</aop:config> 
<tx:advice id="txAdvice" transaction-manager="transactionManager">  
    <tx:attributes>  
    <tx:method name="smbms*"  propagation="REQUIRED" rollback-for="Exception"  />
	    </tx:attributes>  
</tx:advice> 
<!-- AOP 事务处理 结束 -->

8.扫描Mapper接口类所在的包,为mapper创建实现类包扫描是创建实现类的,并没有加载对应的映射文件。但是,如果映射文件和映射接口在一个包内,就会自动加载映射文件。

<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">  
    <property name="basePackage" value="dao" />  
</bean>
posted @   不再犹豫27  阅读(260)  评论(0编辑  收藏  举报
编辑推荐:
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· AI 智能体引爆开源社区「GitHub 热点速览」
· 三行代码完成国际化适配,妙~啊~
· .NET Core 中如何实现缓存的预热?
点击右上角即可分享
微信分享提示