spring和mybatis广泛应用于java后端开发,在一些规模比较大的系统中,设计的东西太多,在本机测试需要的配置比较繁琐,甚至难以完成,这时可以根据spring和mybatis的工作原理在本机配置出简易的测试环境,仅对关键的功能点测试,从而减小线上测试的工作量。
一、数据库的配置文件。该文件通常命名为jdbc.properties,内容通常如下示例:
driver=com.mysql.jdbc.Driver url=jdbc:mysql://localhost:3306/crm_statistic username=root password=linux2014 #定义初始连接数 initialSize=0 #定义最大连接数 maxActive=20 #定义最大空闲 maxIdle=20 #定义最小空闲 minIdle=1 #定义最长等待时间 maxWait=60000
二、数据库访问的日志文件。通常命名为,内容如下示例:
#定义LOG输出级别 log4j.rootLogger=INFO,Console,File #定义日志输出目的地为控制台 log4j.appender.Console=org.apache.log4j.ConsoleAppender log4j.appender.Console.Target=System.out #可以灵活地指定日志输出格式,下面一行是指定具体的格式 log4j.appender.Console.layout = org.apache.log4j.PatternLayout log4j.appender.Console.layout.ConversionPattern=[%c] - %m%n #文件大小到达指定尺寸的时候产生一个新的文件 log4j.appender.File = org.apache.log4j.RollingFileAppender #指定输出目录 log4j.appender.File.File = logs/ssm.log #定义文件最大大小 log4j.appender.File.MaxFileSize = 10MB # 输出所以日志,如果换成DEBUG表示输出DEBUG以上级别日志 log4j.appender.File.Threshold = ALL log4j.appender.File.layout = org.apache.log4j.PatternLayout log4j.appender.File.layout.ConversionPattern =[%p] [%d{yyyy-MM-dd HH\:mm\:ss}][%c]%m%n三、spring和mybatis的整合配置文件:
<?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-3.1.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd 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"> <!--自动扫描--> <context:component-scan base-package="com.qunar.scm.watchdog.service" /> <!--引入配置文件--> <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer" > <property name="location" value="classpath:jdbc.properties" /> </bean> <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close" > <property name="driverClassName" value="${driver}" /> <property name="url" value="${url}" /> <property name="username" value="${username}" /> <property name="password" value="${password}" /> <property name="initialSize" value="${initialSize}" /> </bean> <!--spring和mybatis融合--> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean" > <property name="dataSource" ref="dataSource" /> <property name="mapperLocations" value="classpath:mapper/EmployeeListOndutyDao.xml" /> </bean> <!--spring扫描dao--> <bean id="mapperScannerConfigurer" class="org.mybatis.spring.mapper.MapperScannerConfigurer" > <property name="basePackage" value="com.qunar.scm.watchdog.dao" /> <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" /> </bean> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource" /> </bean> </beans>
四、创建spring容器,并注册bean的编程实现。
基于java注解的bean创建文件(java文件):
@Configuration @ComponentScan(basePackageClasses = {StringSplitterImpl.class,EmployeeListOndutyService.class}) public class EmployeeListOndutyServiceConfig { }
应用spring的方法,编程实现:
String xmlPath = "wjt/service_test.xml"; ctx = new ClassPathXmlApplicationContext(xmlPath);
总结,对于复杂的本机测试,可用该方法,从而减少复杂的本机配置就可实现本机测试。