SpringMVC + MyBatis 配置文件
web.xml
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 <?xml version="1.0" encoding="UTF-8"?> 2 <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4 xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" 5 version="3.1"> 6 <filter> 7 <filter-name>encodingFilter</filter-name> 8 <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class> 9 <init-param> 10 <param-name>encoding</param-name> 11 <param-value>UTF-8</param-value> 12 </init-param> 13 </filter> 14 <filter-mapping> 15 <filter-name>encodingFilter</filter-name> 16 <url-pattern>/*</url-pattern> 17 </filter-mapping> 18 19 <servlet> 20 <servlet-name>dispatcherServlet</servlet-name> 21 <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> 22 <init-param> 23 <param-name>contextConfigLocation</param-name> 24 <param-value>classpath:spring.xml</param-value> 25 </init-param> 26 </servlet> 27 <servlet-mapping> 28 <servlet-name>dispatcherServlet</servlet-name> 29 <url-pattern>*.action</url-pattern> 30 </servlet-mapping> 31 </web-app>
spring.xml
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 <?xml version="1.0" encoding="UTF-8"?> 2 <beans xmlns="http://www.springframework.org/schema/beans" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4 xmlns:context="http://www.springframework.org/schema/context" 5 xmlns:tx="http://www.springframework.org/schema/tx" 6 xmlns:mvc="http://www.springframework.org/schema/mvc" 7 xsi:schemaLocation="http://www.springframework.org/schema/beans 8 http://www.springframework.org/schema/beans/spring-beans.xsd 9 http://www.springframework.org/schema/context 10 http://www.springframework.org/schema/context/spring-context.xsd 11 http://www.springframework.org/schema/tx 12 http://www.springframework.org/schema/tx/spring-tx.xsd 13 http://www.springframework.org/schema/mvc 14 http://www.springframework.org/schema/mvc/spring-mvc.xsd 15 "> 16 17 <!--扫描注解,目的就是对相关的javabean实例化--> 18 <context:component-scan base-package="com.iotek"/> 19 20 <!--启用springmvc的注解模式--> 21 <mvc:annotation-driven /> 22 23 <!--DataSource--> 24 <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"> 25 <property name="driverClass" value="com.mysql.jdbc.Driver"></property> 26 <property name="jdbcUrl" value="jdbc:mysql://localhost:3306/newsdb"></property> 27 <property name="user" value="root"></property> 28 <property name="password" value="root"></property> 29 <!--连接池启动的时候默认创建的连接数量--> 30 <property name="initialPoolSize" value="3"></property> 31 <!--连接池最多可以管理的连接对象个数--> 32 <property name="maxPoolSize" value="100"></property> 33 <!--连接池中最多能够管理的statement对象--> 34 <property name="maxStatements" value="1000"></property> 35 <!--一旦连接池中现有的连接数量不够,每次增长的连接数目:5 ,但是连接池中的连接数量--> 36 <!--最多不可超过maxPoolSize中设置的连接数目--> 37 <property name="acquireIncrement" value="5"></property> 38 </bean> 39 40 <!--Factory--> 41 <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> 42 <property name="dataSource" ref="dataSource"/> 43 <!--配置mapper中使用的实体类别名--> 44 <property name="typeAliasesPackage" value="com.iotek.entity"/> 45 <!--mapper文件的路径 46 classpath*:com/iotek/mapper/**/*.xml : classpath,从源文件角度就是src,实际上是运行时候的classes目录 47 com.iotek.mapper包,以及它所有的子包里面的xml文件,都会认为是mapper文件 48 --> 49 <property name="mapperLocations" value="classpath*:com/iotek/mapper/**/*.xml"/> 50 </bean> 51 52 <!-- 53 DAO层位置的确定 54 --> 55 <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> 56 <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/> 57 <!--指定dao接口的位置--> 58 <property name="basePackage" value="com.iotek.dao"/> 59 </bean> 60 61 <!--TransactionManager--> 62 <bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> 63 <property name="dataSource" ref="dataSource"></property> 64 </bean> 65 66 <!--注解或AOP的方式使用声明式事务--> 67 <tx:annotation-driven transaction-manager="txManager"></tx:annotation-driven> 68 69 70 </beans>
1 <tx:advice id="txAdvice" transaction-manager="transactionManager"> 2 <tx:attributes> 3 <tx:method name="create*"/> 4 <tx:method name="query*"/> 5 <tx:method name="update*"/> 6 <tx:method name="delete*"/> 7 </tx:attributes> 8 </tx:advice> 9 10 <aop:config> 11 <!--切面的配置 id表示切面的唯一id,ref表示切面的支持类--> 12 <aop:pointcut id="operate" expression="execution(* com.iotek.service.*.*(..))"/><!--切点( 切入点)--> 13 <aop:advisor advice-ref="txAdvice" pointcut-ref="operate"/> 14 </aop:config>