所需配置文件
application-dao.xml
application-service.xml
application-trans.xml
db.properties
log4j.properties
springMvc.xml
SqlMapConfig.xml
web.xml
application-dao.xml
//载入配置文件
<context:property-placeholder location="classpath:db.properties"/>
//配置连接池
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="${jdbc.driver}"></property>
<property name="url" value="${jdbc.url}"></property>
<property name="username" value="${jdbc.username}"></property>
<property name="password" value="${jdbc.password}"></property>
</bean>
//给SqlSessionFactoryBean注入连接池和 SqlMapConfig.xml,spring和mybatis整合
<bean id="sqlSessionFactoryBean" class="org.mybatis.spring.SqlSessionFactoryBean" >
<property name="dataSource" ref="dataSource"></property>
<property name="configLocation" value="classpath:SqlMapConfig.xml"></property>
</bean>
//自动扫描该包下的mapper.xml文件
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="net.wanho.dao"></property>
</bean>
</beans>
application-service.xml
//自动扫描包下的service
<context:component-scan base-package="net.wanho.service"></context:component-scan>
application-trans.xml
//给事务管理注入数据源
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"></property>
</bean>
//配置我们的事务管理
<tx:advice id="myAdivce" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="save*" propagation="REQUIRED"></tx:method>
<tx:method name="insert*" propagation="REQUIRED"></tx:method>
<tx:method name="update*" propagation="REQUIRED"></tx:method>
<tx:method name="delete*" propagation="REQUIRED"></tx:method>
<tx:method name="find*" propagation="SUPPORTS" read-only="true"></tx:method>
<tx:method name="select*" propagation="SUPPORTS" read-only="true"></tx:method>
</tx:attributes>
</tx:advice>
//给我们的事务管理加到方法上去
<aop:config>
<aop:advisor advice-ref="myAdivce" pointcut="execution(* *..*.service.*.*(..))"/>
</aop:config>
db.properties
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/ssm
jdbc.username=root
jdbc.password=1234
log4j.properties
# Global logging configuration
log4j.rootLogger=DEBUG, stdout
# Console output...
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%5p [%t] - %m%n
springMvc.xml
//自动载入controller
<context:component-scan base-package="net.wanho.controller"></context:component-scan>
//自动载入handler和adapter等等
<mvc:annotation-driven></mvc:annotation-driven>
//配置view解析
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
//配置前缀和后缀
<property name="prefix" value="/WEB-INF/jsp/"></property>
<property name="suffix" value=".jsp"></property>
</bean>
//自动释放静态资源
<mvc:default-servlet-handler>
SqlMapConfig.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>
//给包里的类起别名
<typeAliases>
<package name="net.wanho.pojo"/>
<package name="net.wanho.vo"/>
</typeAliases>
<!-- <mappers>
//手动配置mapper文件的包
<package name="net.wanho.dao"/>
</mappers> -->
</configuration>
web.xml
//配置dispatcherservlet
<servlet>
<servlet-name>DispatcherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
//配置初始化参数
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:springMvc.xml</param-value>
</init-param>
</servlet>
//将dispatcher映射
<servlet-mapping>
<servlet-name>DispatcherServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
//在服务器启动时自动加载配置文件
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
//将配置文件配在Context中
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:application-*.xml</param-value>
</context-param>
//配置字符集过滤器
<filter>
<filter-name>EncodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>EncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
网友评论