dao模块配置
db.properties
# key=value
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://127.0.0.1:3306/saas-export?characterEncoding=utf-8
jdbc.username=root
jdbc.password=123456
applicationContext-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
https://www.springframework.org/schema/context/spring-context.xsd">
<context:property-placeholder location="classpath:properties/db.properties"/>
<!--连接池配置-->
<bean class="com.alibaba.druid.pool.DruidDataSource" id="dataSource">
<property name="url" value="${jdbc.url}"/>
<property name="driverClassName" value="${jdbc.driver}"/>
<property name="username" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
</bean>
<!--sqlsession配置-->
<bean class="org.mybatis.spring.SqlSessionFactoryBean" id="factory">
<property name="dataSource" ref="dataSource"/>
<property name="typeAliasesPackage" value="com.glc.domain"/>
<property name="configuration">
<bean class="org.apache.ibatis.session.Configuration">
<property name="mapUnderscoreToCamelCase" value="true"/>
</bean>
</property>
</bean>
<!--dao包扫描器-->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer" id="mapperScanner">
<property name="basePackage" value="com.glc.dao"/>
<property name="sqlSessionFactoryBeanName" value="factory"/>
</bean>
</beans>
service模块配置
service主要进行事务处理
<?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"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
https://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/tx
https://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/aop
https://www.springframework.org/schema/aop/spring-aop.xsd">
<context:component-scan base-package="com.glc.service"/>
<bean class="org.springframework.jdbc.datasource.DataSourceTransactionManager" id="transactionManager">
<property name="dataSource" ref="dataSource"/><!--这里会报红,datasource在dao模块配置了-->
</bean>
<tx:advice id="txAdvice" transaction-manager="transactionManager" >
<!--配置事务细节特征-->
<tx:attributes>
<!--查询方法,使用默认的隔离级别 及 SUPPORTS传播行为-->
<tx:method name="find*" isolation="DEFAULT" propagation="SUPPORTS"/>
<tx:method name="query*" isolation="DEFAULT" propagation="SUPPORTS"/>
<tx:method name="select*" isolation="DEFAULT" propagation="SUPPORTS"/>
<tx:method name="get*" isolation="DEFAULT" propagation="SUPPORTS"/>
<!--增删改方法,使用默认的隔离级别 及 REQUIRED传播行为-->
<tx:method name="*" isolation="DEFAULT" propagation="REQUIRED"/>
</tx:attributes>
</tx:advice>
<aop:config>
<!--配置切入点-->
<aop:pointcut id="pt" expression="execution(* com.glc.service.*.impl.*.*(..))"/>
<!--切面=通知+切入点-->
<aop:advisor advice-ref="txAdvice" pointcut-ref="pt"/>
</aop:config>
</beans>
web模块配置
log4j.properties
log4j.rootLogger=debug, stdout,file
# Redirect log messages to console
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target=System.out
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n
log4j.appender.stdout.Encoding=UTF-8
# Rirect log messages to a log file
log4j.appender.file=org.apache.log4j.RollingFileAppender
log4j.appender.file.File=test.log
log4j.appender.file.MaxFileSize=5MB
log4j.appender.file.MaxBackupIndex=10
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n
springmvc.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.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd">
<!--1.扫描Controller所在包-->
<context:component-scan base-package="com.glc.web"/>
<!--2.视图解析器-->
<!-- success 查找文件 /WEB-INF/pages/success.jsp -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<!--2.1 前缀-->
<property name="prefix" value="/WEB-INF/pages/"/>
<!--2.2 后缀-->
<property name="suffix" value=".jsp"/>
</bean>
<!--3.mvc注解驱动-->
<!--3. 把转换器工厂放入到注解驱动,才会生效的 @RequestMapping @ResponseBody @RequestBody-->
<mvc:annotation-driven />
</beans>
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
version="4.0">
<!--设置配置文件的路径 service dao-->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath*:applicationContext-*.xml</param-value>
</context-param>
<!--解决中文乱码的过滤器-->
<filter>
<filter-name>characterEncodingFilter</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>characterEncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!--配置Spring的监听器,默认只加载WEB-INF目录下的applicationContext.xml配置文件-->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!--配置前端控制器 controller-->
<servlet>
<servlet-name>dispatcherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!--加载springmvc.xml配置文件-->
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:springmvc.xml</param-value>
</init-param>
<!--启动服务器,创建该servlet-->
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcherServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
注意事项:
在模块单元测试时,service模块需要使用到两个spring配置文件,所以需要在@ContextConfiguration的值设置为
@ContextConfiguration("classpath*:spring/applicationContext-*.xml")
如果出现applicationContext异常,
@ContextConfiguration("classpath*:spring/applicationContext-*.xml")
classpath:
加载当前maven工程的resources目录下的配置文件
classpath*
: 加载当前maven工程及其依赖工程的resources目录下的配置文件
applicationContext-*.xml
: 读取所有符合规则的文件