所有XML的配置如下:
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.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-4.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd"> <!-- 引入jdbc配置文件 --> <context:property-placeholder location="classpath:jdbc.properties" />
<!-- 启用注解,并且扫描基础包以及其下的各个子包 --> <context:component-scan base-package="com.thunder.selfsite.blog" /> <!-- 对数据源进行事务管理 --> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource" /> </bean> <tx:annotation-driven transaction-manager="transactionManager"/>
<!-- 事务管理配置完成 -->
<!-- 在3.1之后注册了RequestMappingHandlerMapping以及RequestMappingHandlerAdapter两个bean,
并且给Adapter注册了常用的转换器,具体之后记录。
--> <mvc:annotation-driven />
<!-- 拦截器配置
还可以手动配置在HandlerMapping的bean里
mapping定义匹配路径: /*匹配根目录下第一级,/**匹配所有
--> <mvc:interceptors> <mvc:interceptor> <mvc:mapping path="/**"/> <bean class="com.thunder.selfsite.core.interceptor.SessionInterceptor"/> </mvc:interceptor> </mvc:interceptors> <!-- 下面注释掉的是手动注册的HandlerMapping和HandlerAdapter,
messageConverters,这个转换器对json对象进行转换。因为手动注册的话不会自动提供一些默认的转换器
<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter"> <property name="messageConverters"> <list> <bean class="com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter" /> </list> </property> </bean>
这个bean里可以配置一些拦截器 <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping"> <property name="interceptors"> <list> <bean class="com.thunder.selfsite.core.interceptor.SessionInterceptor"/> </list> </property> </bean> --> <!-- 视图处理器
返回的视图名会自动在前面加上prefix后面加上suffix得到路径
--> <bean id="viewResolver" class="org.springframework.web.servlet.view.UrlBasedViewResolver"> <property name="viewClass" value="org.springframework.web.servlet.view.JstlView" /> <property name="prefix" value="/WEB-INF/views/" /> <property name="suffix" value=".jsp" /> </bean> <!-- 定义数据源 --> <bean id="dataSource" class="org.apache.tomcat.jdbc.pool.DataSource" destroy-method="close"> <property name="driverClassName" value="${db.driverClassName}" /> <property name="url" value="${db.url}" /> <property name="username" value="${db.username}" /> <property name="password" value="${db.password}" /> <property name="initialSize" value="${db.initialSize}" /> <property name="maxActive" value="${db.maxActive}" /> <property name="maxIdle" value="${db.maxIdle}" /> <property name="maxWait" value="${db.maxWait}" /> <property name="minIdle" value="${db.minIdle}" /> <property name="testWhileIdle" value="${db.testWhileIdle}" /> <property name="validationQuery" value="${db.validationQuery}" /> </bean> <!-- 开始配置mybati --> <!-- 自动扫描mapping文件 start --> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="dataSource" /> <property name="configLocation" value="classpath:mybatis-config.xml" /> <property name="mapperLocations"> <list> <!-- 表示在com.thunder包或以下所有目录中,所有xml文件 --> <value>classpath*:/com/thunder/**/*.xml</value> </list> </property> <property name="configurationProperties"> <props> <prop key="dialect">mysql</prop> </props> </property> </bean> <!-- 配置扫描器 --> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<!-- 这种配置方式不用写dao层的实现,这里的包是dao接口的包 --> <property name="basePackage" value="com.thunder.selfsite.blog.dao" /> <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" /> </bean> <!-- 自动扫描mapping文件 end --> </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> <setting name="mapUnderscoreToCamelCase" value="true" /> <setting name="lazyLoadingEnabled" value="true"/>
<!-- 按需加载对象 --> <setting name="aggressiveLazyLoading" value="false"/> </settings> </configuration>