你的位置:首页 > Java教程

[Java教程]简单Spring和mybatis整合配置文件


<?<beans xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
">


<!-- 引入属性文件 : 请往下翻-->
<context:property-placeholder location="classpath:config.properties" />


<!-- 配置数据源 :使用的 alibaba 的数据库连接 ,要使用先下载 Druid 包 -->
<bean name="dataSource"
init-method="init" destroy-method="close">
<property name="url" value="${jdbc_url}" />
<property name="username" value="${jdbc_username}" />
<property name="password" value="${jdbc_password}" />

<!-- 初始化连接大小 -->
<property name="initialSize" value="1" />
<!-- 连接池最大使用连接数量 -->
<property name="maxActive" value="100" />
<!-- 连接池最大空闲 -->
<property name="maxIdle" value="1" />
<!-- 连接池最小空闲 -->
<property name="minIdle" value="10" />
<!-- 获取连接最大等待时间 -->
<property name="maxWait" value="60000" />

<property name="validationQuery" value="${validationQuery}" />
<property name="testOnBorrow" value="false" />
<property name="testOnReturn" value="false" />
<property name="testWhileIdle" value="true" />

<!-- 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒 -->
<property name="timeBetweenEvictionRunsMillis" value="60000" />
<!-- 配置一个连接在池中最小生存的时间,单位是毫秒 -->
<property name="minEvictableIdleTimeMillis" value="14400000" />

<!-- 打开removeAbandoned功能 -->
<property name="removeAbandoned" value="true" />
<!-- 600秒,也就是10分钟 -->
<property name="removeAbandonedTimeout" value="600" />
<!-- 关闭abanded连接时输出错误日志 -->
<property name="logAbandoned" value="true" />

<!-- 监控数据库 -->
<!-- <property name="filters" value="stat" /> -->
<property name="filters" value="mergeStat" />
</bean>


<!-- myBatis文件 -->
<bean id="sqlSessionFactory" >
<property name="dataSource" ref="dataSource" />
<!-- 自动扫描mapping目录, 省掉Configuration. <property name="mapperLocations" value="classpath:x/x/x/x/*. </bean>

<bean >
<property name="basePackage" value="dao层包引用" />
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" />
</bean>



<!-- 配置事务管理器 -->
<bean id="transactionManager"
>
<property name="dataSource" ref="dataSource" />
</bean>

<!-- 拦截器方式配置事物 -->
<tx:advice id="transactionAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="add*" propagation="REQUIRED" />
<tx:method name="append*" propagation="REQUIRED" />
<tx:method name="insert*" propagation="REQUIRED" />
<tx:method name="save*" propagation="REQUIRED" />
<tx:method name="update*" propagation="REQUIRED" />
<tx:method name="modify*" propagation="REQUIRED" />
<tx:method name="edit*" propagation="REQUIRED" />
<tx:method name="delete*" propagation="REQUIRED" />
<tx:method name="remove*" propagation="REQUIRED" />
<tx:method name="repair" propagation="REQUIRED" />
<tx:method name="delAndRepair" propagation="REQUIRED" />

<tx:method name="get*" propagation="SUPPORTS" />
<tx:method name="find*" propagation="SUPPORTS" />
<tx:method name="load*" propagation="SUPPORTS" />
<tx:method name="search*" propagation="SUPPORTS" />
<tx:method name="datagrid*" propagation="SUPPORTS" />

<tx:method name="*" propagation="SUPPORTS" />
</tx:attributes>
</tx:advice>
<aop:config>
<aop:pointcut id="transactionPointcut"
expression="execution(* x.x.x.x.impl..*Impl.*(..))" /><!-- 切入 -->
<aop:advisor pointcut-ref="transactionPointcut"
advice-ref="transactionAdvice" />
</aop:config>


<!-- 配置druid监控spring jdbc -->
<bean id="druid-stat-interceptor"
>
</bean>
<bean id="druid-stat-pointcut"
scope="prototype">
<property name="patterns">
<list>
<value>x.x.x.x.impl.*</value>
</list>
</property>
</bean>
<aop:config>
<aop:advisor advice-ref="druid-stat-interceptor"
pointcut-ref="druid-stat-pointcut" />
</aop:config>

<!-- 自动扫描(自动注入) -->
<context:component-scan base-package="x.x.x.x.impl" />

</beans>

 

 

<!-- 引入的属性文件 注意是文件 .properties的配置文件,自己新建一个-->

driverClassName=com.mysql.jdbc.Driver
validationQuery=SELECT 1

jdbc_url=jdbc:mysql://连接服务器的地址(如:192.168.1.122):3306(端口号)/数据库名?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull
jdbc_username=数据库连接名
jdbc_password=数据库密钥

sessionInfoName=sessionInfo
uploadFieldName=filedata
uploadFileMaxSize=20971520
uploadFileExts=txt,rar,zip,doc,docx,xls,xlsx,jpg,jpeg,gif,png,swf,wmv,avi,wma,mp3,mid
uploadDirectory=attached