web.xml
<context-param>
<param-name>log4jConfigLocation</param-name>
<param-value>classpath:log4j.properties</param-value>
</context-param>
<listener>
<listener-class>
org.springframework.web.util.Log4jConfigListener
</listener-class>
</listener>
<!-- Spring监听器 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- 防止Spring内存溢出监听器 -->
<listener>
<listener-class>org.springframework.web.util.IntrospectorCleanupListener</listener-class>
</listener>
<!-- 编码过滤器 -->
<filter>
<filter-name>encodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<async-supported>true</async-supported>
<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>
<!-- Spring MVC servlet -->
<servlet>
<servlet-name>SpringMVC</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring-mvc.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
<async-supported>true</async-supported>
</servlet>
<servlet-mapping>
<servlet-name>SpringMVC</servlet-name>
<!-- 此处可以可以配置成*.do,对应struts的后缀习惯 -->
<url-pattern>/</url-pattern>
</servlet-mapping>
db.properties
driver=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/tedu_store
username=root
password=
initSize=1
maxActive=50
log4j.properties
#DEBUG\INFO\WARN\ERROR\FATAL
log4j.rootLogger=DEBUG,stdout,A
#\u5B9A\u4E49\u65E5\u5FD7\u8F93\u51FA\u76EE\u7684\u5730\u4E3A\u63A7\u5236\u53F0
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 = [%-5p] %d{yyyy-MM-dd HH:mm:ss,SSS} method:%l%n%m%n
#log4j.appender.CONSOLE.layout.ConversionPattern=[start]%d{DATE}[DATE]%n%p[PRIORITY]%n%x[NDC]%n%t[THREAD] n%c[CATEGORY]%n%m[MESSAGE]%n%n
###File###
log4j.appender.A = org.apache.log4j.DailyRollingFileAppender
log4j.appender.A.File = D://logs/log.log
log4j.appender.A.Append = true
log4j.appender.A.Threshold = DEBUG
log4j.appender.A.layout = org.apache.log4j.PatternLayout
log4j.appender.A.layout.ConversionPattern = %-d{yyyy-MM-dd HH:mm:ss} [ %t:%r ] - [ %p ] %m%n
<util:properties id="bd" location="classpath:db.properties"></util:properties>
<bean id="bds" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="#{bd.driver}"></property>
<property name="url" value="#{bd.url}"></property>
<property name="username" value="#{bd.username}"></property>
<property name="password" value="#{bd.password}"></property>
<property name="initialSize" value="#{bd.initSize}"></property>
<property name="maxActive" value="#{bd.maxActive}"></property>
</bean>
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="bds"></property>
<property name="mapperLocations" value="classpath:mappers/*.xml"></property>
</bean>
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="sqlSessionFactory" ref="sqlSessionFactory"></property>
<property name="basePackage" value="cn.tedu.store.mapper"></property>
</bean>
<!-- 事物控制管理器 -->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="bds" />
</bean>
<tx:annotation-driven transaction-manager="transactionManager" />
<context:component-scan base-package="cn.tedu.store.service"></context:component-scan>
<context:component-scan base-package="cn.tedu.store.controller"></context:component-scan>
<mvc:annotation-driven></mvc:annotation-driven>
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/web/"></property>
<property name="suffix" value=".jsp"></property>
</bean>
<?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 namespace="com.air.dao.StudentMapper">
<select id="getStudent" resultType="com.air.bean.Student">
select * from student
</select>
</mapper>
import org.apache.log4j.Logger;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.AbstractTransactionalJUnit4SpringContextTests;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.web.WebAppConfiguration;
@RunWith(SpringJUnit4ClassRunner.class)
@WebAppConfiguration("src/main/resouces")
@ContextConfiguration(locations = {"classpath:spring-dao.xml","classpath:spring-mybatis.xml","classpath:spring-mvc.xml","classpath:spring-service.xml"})
public class BaseTest extends AbstractTransactionalJUnit4SpringContextTests{
protected Logger log = Logger.getLogger(getClass());
// @Test
// public void test(){
// System.out.println("aaa");
// }
}
public class Juint extends BaseTest {
//private static Logger logger = Logger.getLogger(Juint.class);
// public static void main(String[] args) {
// logger.debug("debug!!!");
// logger.info("info!!!");
// logger.warn("warn!!!");
// }
@Resource
private StudentController studentController;
@Test
public void testSelectByLoginName() {
List<Student> user = studentController.getStudent();
// log.debug("selectList--------------------------【{}】"+user.size());
System.out.println(user.size());
}
}