springmvc(五)集成阿⾥druid数据库连接池和事务等配置,
集成mybatis
感谢我们的⼩领导,他在研究,我们在套⽤,他⾛了以后再没有完善过,⼀直沿⽤⾄今。如果看这⾥的朋友有什么需要集成进来的,不吝赐教,谢谢各位了。
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="/schema/beans"
xmlns:xsi="/2001/XMLSchema-instance"
xmlns:aop="/schema/aop"
xmlns:context="/schema/context"
xmlns:mvc="/schema/mvc"
xmlns:tx="/schema/tx"
xsi:schemaLocation="/schema/beans
/schema/beans/spring-beans.xsd
/schema/aop
/schema/aop/spring-aop.xsd
/schema/context
/schema/context/spring-context-4.0.xsd
/schema/mvc
/schema/mvc/spring-mvc-4.0.xsd
/schema/tx
/schema/tx/spring-tx.xsd
">
<!-- 如下配置中,如果配置了include-filter type=annotation 那么只有在其中配置的注解会被扫描,前提是-->
<context:component-scan base-package="com"></context:component-scan>
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/pages/"></property>
<property name="suffix" value=""></property>
</bean>
<!-- 只有配置了如下标签,@RequestMapping中的produces才会起作⽤,跟其注册的消息体转换类有关 -->
<mvc:annotation-driven>
<mvc:message-converters register-defaults="true">
<bean
class="com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter">
<property name="supportedMediaTypes" value="application/json" />
</bean>
</mvc:message-converters>
</mvc:annotation-driven>
<mvc:default-servlet-handler />
<!-- location中的内容可以写多个地址,⽐如可以这么写location="/,/**"前⼀个表⽰在根⽬录查 -->
<mvc:resources mapping="/upload/**" location="/upload/,/CKEditorUploadImage/" />
<mvc:resources mapping="/images/**" location="/WEB-INF/images/" />
<mvc:resources mapping="/css/**" location="/WEB-INF/css/" />
<mvc:resources mapping="/plugins/**" location="/WEB-INF/plugins/" />
<mvc:resources mapping="/js/**" location="/WEB-INF/js/" />
<mvc:resources mapping="/*.html" location="/WEB-INF/static_pages/" />
<mvc:resources mapping="/*.xml" location="/WEB-INF/" />
<mvc:interceptors>
<bean class="com.intercpter.MyInterceptor"></bean>
</mvc:interceptors>
<bean name="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"></property>
</bean>
<!-- 99框架赋值写法有问题,web请求可以注⼊属性⽂件的值,代码测试⽆法注⼊,导致junit测试Dao层报加载配置⽂件出错 --> <bean id="propertyConfigurer"
class="org.springframework.fig.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>classpath:dbconfig.properties</value>
</list>
</property>
</bean>
<!-- 阿⾥ druid数据库连接池 -->
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource"druid连接池配置详解
destroy-method="close">
<!-- 数据库基本信息配置 -->
<property name="url" value="${url}" />
<property name="username" value="${username}" />
<property name="password" value="${password}" />
<property name="driverClassName" value="${driverClassName}" />
<property name="filters" value="${filters}" />
<!-- 最⼤并发连接数 -->
<property name="maxActive" value="${maxActive}" />
<!-- 初始化连接数量 -->
<property name="initialSize" value="${initialSize}" />
<!-- 配置获取连接等待超时的时间 -->
<property name="maxWait" value="${maxWait}" />
<!-- 最⼩空闲连接数 -->
<property name="minIdle" value="${minIdle}" />
<!-- 配置间隔多久才进⾏⼀次检测,检测需要关闭的空闲连接,单位是毫秒 -->
<property name="timeBetweenEvictionRunsMillis" value="${timeBetweenEvictionRunsMillis}" />
<!-- 配置⼀个连接在池中最⼩⽣存的时间,单位是毫秒 -->
<property name="minEvictableIdleTimeMillis" value="${minEvictableIdleTimeMillis}" />
<property name="validationQuery" value="${validationQuery}" />
<property name="testWhileIdle" value="${testWhileIdle}" />
<property name="testOnBorrow" value="${testOnBorrow}" />
<property name="testOnReturn" value="${testOnReturn}" />
<property name="maxOpenPreparedStatements" value="${maxOpenPreparedStatements}" />
<!-- 打开removeAbandoned功能 -->
<property name="removeAbandoned" value="${removeAbandoned}" />
<!-- 1800秒,也就是30分钟 -->
<property name="removeAbandonedTimeout" value="${removeAbandonedTimeout}" />
<!-- 关闭abanded连接时输出错误⽇志 -->
<property name="logAbandoned" value="${logAbandoned}" />
</bean>
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="delete*" propagation="REQUIRED" read-only="false"
rollback-for="java.lang.Exception"/>
<tx:method name="insert*" propagation="REQUIRED" read-only="false"
rollback-for="java.lang.Exception" />
<tx:method name="update*" propagation="REQUIRED" read-only="false"
rollback-for="java.lang.Exception" />
<tx:method name="save*" propagation="REQUIRED" read-only="false"
rollback-for="java.lang.Exception" />
</tx:attributes>
</tx:advice>
<aop:aspectj-autoproxy proxy-target-class="true"/>
<!-- 事物处理 -->
<aop:config>
<aop:pointcut id="pc" expression="execution(* ud.service..*(..))" /> <!--⽬录结构,该⽬录下类注解:
@Service("xxxService")-->
<aop:advisor pointcut-ref="pc" advice-ref="txAdvice" />
</aop:config>
<!-- mybatis配置 -->
<bean id="sqlSessionFactory" class="batis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="configLocation" value="classpath:l"></property>
<!-- mapper扫描 -->
<property name="mapperLocations" value="classpath:mybatis/*/*.xml"></property>
</bean>
<bean id="sqlSessionTemplate" class="batis.spring.SqlSessionTemplate">
<constructor-arg ref="sqlSessionFactory" />
</bean>
<!-- ⾃定义上传拦截,如最⼤上传值及最⼩上传值 单个⽂件4M,解决原始⽂件上传和spring上传request被解析的问题-->
<!-- url中带有excludeUrls的http请求就不会被multipartResolver先解析-->
<bean id="multipartResolver" class="solver.MyMultipartResolver">
<property name="excludeUrls">
<list>
<value>ckupload</value>
<value>UEditorConfig</value>
<!--
使⽤springmvc的⽂件上传实现了,所有暂时不需要转换request,
疑问是不知道百度编辑器的jar中是否有⽂件上传的代码,因为看到
有⽂件上传需要的jar包,⽽且也没有得到config.json中配置的⽂件格式名称
-->
<!-- <value>UEditorUploadImage</value>-->
</list>
</property>
<!-- 指定所上传⽂件的总⼤⼩不能超过16MB。注意maxUploadSize属性的限制不是针对单个⽂件,⽽是所有⽂件的容量之和 --> <property name="maxUploadSize">
<value>647772160</value>
</property>
<property name="maxInMemorySize">
<value>4096</value>
</property>
<property name="defaultEncoding">
<value>utf-8</value>
</property>
</bean>
</beans>
⽂件结构:
mybatis配置
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE configuration PUBLIC "-////DTD SQL Map Config 3.0//EN"
"/dtd/mybatis-3-config.dtd">
<configuration>
<!-- 配置实体类的别名,在实体类对应的l中返回的类型就可以写类名,⽽不需要全类名了 --> <typeAliases>
<package name="dal" />
</typeAliases>
</configuration>
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论