一、spring-web.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" xmlns:mvc="http://www.springframework.org/schema/mvc" 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-4.0.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd"> <!-- 1.开启SpringMVC注解模式 --> <!-- 简化配置: 1)自动注册DefaultAnnotationHandlerMapping、AnnotationMethodHandlerAdapter 2)提供一系列的:数据绑定、数字和日期的format(@NumberFormat、@DateTimeFormat)、xml、json默认读写支持 --> <mvc:annotation-driven> <mvc:message-converters register-defaults="true"> <!-- 配置Fastjson支持 --> <bean class="com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter" id="fastJsonHttpMessageConverter"> <property name="supportedMediaTypes"> <list> <value>text/html;charset=UTF-8</value> <value>application/json</value> </list> </property> <property name="features"> <list> <value>WriteMapNullValue</value> <value>QuoteFieldNames</value> </list> </property> </bean> </mvc:message-converters> </mvc:annotation-driven> <!-- 2.静态资源默认servlet配置 --> <!-- 1)加入对静态资源的处理的支持(js、gif、png) 2)允许使用“/”做整体映射 --> <mvc:default-servlet-handler/> <!-- 3.配置jsp的视图解析器 --> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"></property> <property name="prefix" value="/WEB-INF/jsp/"></property> <property name="suffix" value=".jsp"></property> </bean> <!-- 4.扫描web相关的包 --> <context:component-scan base-package="com.demo.controller"/> <!-- 5.文件上传 --> <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"> <property name="defaultEncoding" value="utf-8"></property> <property name="maxUploadSize" value="32505856"></property> <property name="maxInMemorySize" value="4096"></property> </bean> <!-- 6.拦截器 --> <bean id="demoInterceptor" class="com.aidilude.auction.interceptor.DemoInterceptor"/> <mvc:interceptors> <mvc:interceptor> <!-- 对所有的请求拦截使用/** ,对某个模块下的请求拦截使用:/myPath/* --> <mvc:mapping path="/**" /> <ref bean="demoInterceptor" /> </mvc:interceptor> </mvc:interceptors> </beans>
二、spring-service.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" xmlns:tx="http://www.springframework.org/schema/tx" 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-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd"> <!-- 1.配置扫描service包下的所有使用注解的类 --> <context:component-scan base-package="com.demo.service"></context:component-scan> <!-- 2.配置事务管理器 --> <bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource"></property> </bean> <!-- 3.配置使用注解管理事务 --> <tx:annotation-driven transaction-manager="txManager"/> </beans>
三、spring-dao.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-4.0.xsd"> <!-- 1.引入db.properties --> <context:property-placeholder location="classpath:jdbc.properties"/> <!-- 2.配置数据源 --> <bean name="dataSource" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close"> <!-- 一、基本项: --> <property name="url" value="${jdbc.url}" /> <property name="username" value="${jdbc.username}" /> <property name="password" value="${jdbc.password}" /> <!-- 二、连接数量配置项: --> <!-- 2.1、初始化连接数 默认值:0 --> <property name="initialSize" value="100" /> <!-- 2.2、最大使用连接数 默认值:8 --> <property name="maxActive" value="500" /> <!-- 2.3、最小空闲连接数 --> <property name="minIdle" value="100" /> <!-- 三、连接测试项: --> <!-- 3.1、获取连接的最大等待时间(毫秒) --> <property name="maxWait" value="10000" /> <!-- 3.2、用来检测连接是否有效的sql,select 1:返回1 --> <property name="validationQuery" value="select 1" /> <!-- 3.3、申请连接时执行validationQuery检测连接是否有效,做了这个配置会降低性能 默认值:true --> <property name="testOnBorrow" value="true" /> <!-- 3.4、归还连接时执行validationQuery检测连接是否有效,做了这个配置会降低性能 默认值:false --> <property name="testOnReturn" value="false" /> <!-- 3.5、建议配置为true,不影响性能,并且保证安全性。申请连接的时候检测,如果空闲时间大于timeBetweenEvictionRunsMillis,执行validationQuery检测连接是否有效 默认值:false --> <property name="testWhileIdle" value="true" /> <!-- 四、Destory线程配置项: --> <!-- 4.1、Destory线程执行检测的时间间隔,检测需要关闭的空闲连接(毫秒) --> <property name="timeBetweenEvictionRunsMillis" value="60000" /> <!-- 4.2、Destory线程如果检测到当前连接的最后活跃时间和当前时间的差值大于minEvictableIdleTimeMillis,则关闭当前连接(毫秒) --> <property name="minEvictableIdleTimeMillis" value="300000" /> <!-- 五、remove线程项: --> <!-- 5.1、对于存活时间超过removeAbandonedTimeout的连接强制关闭 --> <property name="removeAbandoned" value="true" /> <!-- 5.2、单位:秒 --> <property name="removeAbandonedTimeout" value="1800" /> <!-- 5.3、关闭abanded连接时输出错误日志 默认值:false --> <property name="logAbandoned" value="true" /> <!-- 六、监控项: --> <!-- 6.1、监控数据库 --> <property name="filters" value="mergeStat" /> <!-- 七、选配项: --> <!-- 7.1、是否缓存preparedStatement,也就是PSCache。PSCache对支持游标的数据库性能提升巨大,比如说oracle。在mysql下建议关闭 默认值:false <property name="poolPreparedStatements" value="true" /> --> <!-- 7.2、每个连接上PSCache的大小 ,要配置必须大于0,当大于0了poolPreparedStatements自动触发为true 在Druid中,不会存在Oracle下PSCache占用内存过多的问题,可以把这个数值配置大一些,比如说100 默认值:-1 <property name="maxPoolPreparedStatementPerConnectionSize" value="20" /> --> </bean> <!-- 3.配置SqlSessionFactory(SqlSessionFactoryBean) --> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <!-- 注入数据源 --> <property name="dataSource" ref="dataSource"></property> <!-- 引入mybatis全局配置文件 --> <property name="configLocation" value="classpath:mybatis-config.xml"></property> <!-- 扫描mapper映射文件 --> <property name="mapperLocations" value="classpath:mapper/*.xml"></property> <!-- 扫描entity包,使用别名 --> <!-- 当定义了别名之后,就可以在入参或结果集中直接写类名(首字母大小写都可)了,而不用写全限定名 --> <property name="typeAliasesPackage" value=""></property> </bean> <!-- 4.扫描dao接口(MapperScannerConfigurer) --> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <!-- 注入sqlSessionFactory(注意使用value引入) --> <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property> <!-- 配置dao接口位置 --> <property name="basePackage" value=""></property> </bean> </beans>
四、mybatis-config.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> <!-- 使用jdbc的getGeneratedKeys,获取数据库自增主键 --> <setting name="useGeneratedKeys" value="true"/> <!-- 使用列别名替换列名,默认:true --> <!-- select name as label from table --> <setting name="useColumnLabel" value="true"/> <!-- 开启驼峰命名转换:Table(create_time) -> Entity(createTime) --> <setting name="mapUnderscoreToCamelCase" value="true"/> <!-- 将延迟加载设置为true --> <setting name="lazyLoadingEnabled" value="true"/> <!-- 将积极加载设置为false --> <setting name="aggressiveLazyLoading" value="false"/> <!-- 二级缓存的总开关 --> <!-- 还需要在指定的mapper映射文件中开启独立的开关 --> <setting name="cacheEnabled" value="false"/> </settings> </configuration>
五、mapper.xml
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <!-- mapper接口的全限定名 --> <mapper namespace=""> <!-- 当前mapper开启二级缓存,默认使用PerpetualCache,可整合redis分布式缓存等 --> <!-- 当开启二级缓存,别忘了将对象序列化(实现Serializable) --> <!-- 查看日志中的缓存命中可以验证缓存是否成功开启 --> <!-- <cache/> --> <!-- sql片段 --> <sql id=""> <if test=""> </if> <if test=""> and id in <foreach collection="" item="" open="(" close=")" separator=","> </foreach> </if> </sql> <select id="" parameterType="" resultType=""> <!-- <where>会去掉第一个条件的and,如果没有条件,那么去掉where --> <where> <!-- 引入代码片段 --> <include refid=""></include> </where> </select> <!-- 声明结果映射,用于需要将列别名映射为po类的属性名 --> <resultMap type="" id=""> <id column="" property=""/> <result column="" property=""/> <!-- association标签用于映射引用数据类型 --> <!-- property:指定属性名,javaType:指定类型(全路径) --> <association property="" javaType=""> <id column="" property=""/> <result column="" property=""/> </association> <!-- collection标签用于映射集合数据类型 --> <!-- ofType:指定类型(全路径) --> <collection property="" ofType=""> <id column="" property=""/> <result column="" property=""/> </collection> </resultMap> <select id="" parameterType="" resultMap=""> </select> </mapper>