SSM整合&单元测试集成备忘

ssm.jpg

ssm整合


  阅读原文请访问我的博客brightloong's blog
  SSM框架,既是Sping + Spring MVC + Mybatis,本篇博文主要是作为本人的备忘,记录如何整合SSM框架,以及如何集成单元测试,关于每一个配置的作用大都加上了注释,至于如何搭建项目这里不在赘述(我使用的是web项目),可以自己查找相关资料。我使用的Spring版本是4.1.3.RELEASE,Mybatis版本是3.3.0,使用Maven进行构建。

注意:首先申明如下的配置是根据我的目录结构来配置的,我的目录结构如下所示:

directory%20structure%20.jpg

目录结构

使用Maven添加相关jar包。

  关于pom.xml的配置如下。

 
  1. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

  2. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

  3. <modelVersion>4.0.0</modelVersion>

  4. <groupId>ssm_test</groupId>

  5. <artifactId>ssm_test</artifactId>

  6. <version>0.0.1-SNAPSHOT</version>

  7. <packaging>war</packaging>

  8. <name>ssm_test</name>

  9. <description />

  10. <properties>

  11. <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>

  12. </properties>

  13. <!--配置依赖包 -->

  14. <dependencies>

  15. <!-- https://mvnrepository.com/artifact/junit/junit -->

  16. <dependency>

  17. <groupId>junit</groupId>

  18. <artifactId>junit</artifactId>

  19. <version>4.10</version>

  20. </dependency>

  21. <!-- 1.日志,slf4j规范 日志实现:log4j,logback,commin-logging 使用:slf4j + logback -->

  22. <dependency>

  23. <groupId>org.slf4j</groupId>

  24. <artifactId>slf4j-api</artifactId>

  25. <version>1.7.12</version>

  26. </dependency>

  27. <dependency>

  28. <groupId>ch.qos.logback</groupId>

  29. <artifactId>logback-core</artifactId>

  30. <version>1.1.1</version>

  31. </dependency>

  32. <!-- 实现slf4j接口并整合 -->

  33. <dependency>

  34. <groupId>ch.qos.logback</groupId>

  35. <artifactId>logback-classic</artifactId>

  36. <version>1.1.1</version>

  37. </dependency>

  38. <!-- 2.数据库相关依赖 -->

  39. <dependency>

  40. <groupId>mysql</groupId>

  41. <artifactId>mysql-connector-java</artifactId>

  42. <version>5.1.7</version>

  43. </dependency>

  44. <dependency>

  45. <groupId>c3p0</groupId>

  46. <artifactId>c3p0</artifactId>

  47. <version>0.9.1.2</version>

  48. </dependency>

  49.  
  50. <!-- 3.dao mybatis依赖 -->

  51. <dependency>

  52. <groupId>org.mybatis</groupId>

  53. <artifactId>mybatis</artifactId>

  54. <version>3.3.0</version>

  55. </dependency>

  56. <!-- mybatis自身实现的整合spring -->

  57. <dependency>

  58. <groupId>org.mybatis</groupId>

  59. <artifactId>mybatis-spring</artifactId>

  60. <version>1.2.3</version>

  61. </dependency>

  62.  
  63. <!-- 4.servelet web 相关依赖 -->

  64. <dependency>

  65. <groupId>jstl</groupId>

  66. <artifactId>jstl</artifactId>

  67. <version>1.2</version>

  68. </dependency>

  69. <dependency>

  70. <groupId>taglibs</groupId>

  71. <artifactId>standard</artifactId>

  72. <version>1.1.2</version>

  73. </dependency>

  74. <dependency>

  75. <groupId>com.fasterxml.jackson.core</groupId>

  76. <artifactId>jackson-databind</artifactId>

  77. <version>2.5.4</version>

  78. </dependency>

  79. <dependency>

  80. <groupId>javax.servlet</groupId>

  81. <artifactId>javax.servlet-api</artifactId>

  82. <version>3.1.0</version>

  83. </dependency>

  84.  
  85. <!-- 5.spring的依赖 -->

  86. <!-- 1)spring核心依赖 -->

  87. <dependency>

  88. <groupId>org.springframework</groupId>

  89. <artifactId>spring-core</artifactId>

  90. <version>4.1.3.RELEASE</version>

  91. </dependency>

  92. <dependency>

  93. <groupId>org.springframework</groupId>

  94. <artifactId>spring-beans</artifactId>

  95. <version>4.1.3.RELEASE</version>

  96. </dependency>

  97. <dependency>

  98. <groupId>org.springframework</groupId>

  99. <artifactId>spring-context</artifactId>

  100. <version>4.1.3.RELEASE</version>

  101. </dependency>

  102.  
  103. <!-- spring aop 相关 -->

  104. <dependency>

  105. <groupId>org.springframework</groupId>

  106. <artifactId>spring-aop</artifactId>

  107. <version>4.1.3.RELEASE</version>

  108. </dependency>

  109.  
  110. <dependency>

  111. <groupId>org.aspectj</groupId>

  112. <artifactId>aspectjrt</artifactId>

  113. <version>1.6.11</version>

  114. </dependency>

  115. <dependency>

  116. <groupId>org.aspectj</groupId>

  117. <artifactId>aspectjweaver</artifactId>

  118. <version>1.6.11</version>

  119. </dependency>

  120. <!-- 2)spring dao层依赖 -->

  121. <dependency>

  122. <groupId>org.springframework</groupId>

  123. <artifactId>spring-jdbc</artifactId>

  124. <version>4.1.3.RELEASE</version>

  125. </dependency>

  126. <dependency>

  127. <groupId>org.springframework</groupId>

  128. <artifactId>spring-tx</artifactId>

  129. <version>4.1.3.RELEASE</version>

  130. </dependency>

  131. <!-- 3)spring web依赖 -->

  132. <dependency>

  133. <groupId>org.springframework</groupId>

  134. <artifactId>spring-web</artifactId>

  135. <version>4.1.3.RELEASE</version>

  136. </dependency>

  137. <dependency>

  138. <groupId>org.springframework</groupId>

  139. <artifactId>spring-webmvc</artifactId>

  140. <version>4.1.3.RELEASE</version>

  141. </dependency>

  142. <!-- 4)spring test依赖 -->

  143. <dependency>

  144. <groupId>org.springframework</groupId>

  145. <artifactId>spring-test</artifactId>

  146. <version>4.1.3.RELEASE</version>

  147. </dependency>

  148. <!-- junit依赖 -->

  149. <dependency>

  150. <groupId>junit</groupId>

  151. <artifactId>junit</artifactId>

  152. <version>4.11</version>

  153. </dependency>

  154. </dependencies>

  155. <build>

  156. <sourceDirectory>src</sourceDirectory>

  157. <resources>

  158. <resource>

  159. <directory>src</directory>

  160. <excludes>

  161. <exclude>**/*.java</exclude>

  162. </excludes>

  163. </resource>

  164. </resources>

  165. <plugins>

  166. <plugin>

  167. <artifactId>maven-compiler-plugin</artifactId>

  168. <version>2.3.2</version>

  169. <configuration>

  170. <source>1.6</source>

  171. <target>1.6</target>

  172. </configuration>

  173. </plugin>

  174. <plugin>

  175. <artifactId>maven-war-plugin</artifactId>

  176. <version>2.2</version>

  177. <configuration>

  178. <warSourceDirectory>${basedir}/WebRoot</warSourceDirectory>

  179. <version>3.0</version>

  180. <failOnMissingWebXml>false</failOnMissingWebXml>

  181. </configuration>

  182. </plugin>

  183. </plugins>

  184. </build>

  185. </project>

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133
  • 134
  • 135
  • 136
  • 137
  • 138
  • 139
  • 140
  • 141
  • 142
  • 143
  • 144
  • 145
  • 146
  • 147
  • 148
  • 149
  • 150
  • 151
  • 152
  • 153
  • 154
  • 155
  • 156
  • 157
  • 158
  • 159
  • 160
  • 161
  • 162
  • 163
  • 164
  • 165
  • 166
  • 167
  • 168
  • 169
  • 170
  • 171
  • 172
  • 173
  • 174
  • 175
  • 176
  • 177
  • 178
  • 179
  • 180
  • 181
  • 182
  • 183
  • 184
  • 185

SSM整合配置

  为了使配置层次清楚,结构清晰,我把配置按照web、service和dao分别分为spring-web.xml、spring-service.xml和spirng-dao.xml,下面依次介绍这几个配置文件。

spring-web.xm配置

  该配置中主要是一些资源映射的相关配置。

 
  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:aop="http://www.springframework.org/schema/aop"

  5. xmlns:tx="http://www.springframework.org/schema/tx"

  6. xmlns:context="http://www.springframework.org/schema/context"

  7. xmlns:mvc="http://www.springframework.org/schema/mvc"

  8. xsi:schemaLocation="http://www.springframework.org/schema/beans

  9. http://www.springframework.org/schema/beans/spring-beans.xsd

  10. http://www.springframework.org/schema/aop

  11. http://www.springframework.org/schema/aop/spring-aop.xsd

  12. http://www.springframework.org/schema/tx

  13. http://www.springframework.org/schema/tx/spring-tx.xsd

  14. http://www.springframework.org/schema/context

  15. http://www.springframework.org/schema/context/spring-context.xsd

  16. http://www.springframework.org/schema/mvc

  17. http://www.springframework.org/schema/mvc/spring-mvc.xsd">

  18. <!-- 配置springMVC -->

  19. <!-- 1.开启springMVC注解模式 -->

  20. <!-- 简化配置:

  21. 1)自动注册defaultAnnotationHandlerMapping,AnnotationMethodHandlARadpter

  22. 2)提供一系列:数据绑定,数字和日期的format @NumberFoamat,@DataTiemFormat

  23. xml,json默认读写支持-->

  24. <mvc:annotation-driven></mvc:annotation-driven>

  25. <!--2.servelt-mapping 映射路径:"/"

  26. 静态资源默认servlet配置

  27. 1.加入对静态资源的处理:js,gif,png

  28. 2.允许用/做整体映射-->

  29. <mvc:default-servlet-handler/>

  30.  
  31. <!-- 3. 配置jsp显示viewResolver-->

  32. <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">

  33. <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"></property>

  34. <property name="prefix" value="/WEB-INF/jsp/"></property>

  35. <property name="suffix" value=".jsp"></property>

  36. </bean>

  37.  
  38. <!-- 4.扫描web相关的包 -->

  39. <context:component-scan base-package="com.chenlong.study.web"></context:component-scan>

  40.  
  41. <!-- 获取properties -->

  42. <bean id="propertyConfigurer" class="com.chenlong.study.utils.PropertyUtil">

  43. <property name="locations" >

  44. <list>

  45. <value>classpath:*.properties</value>

  46. </list>

  47. </property>

  48. </bean>

  49. <aop:aspectj-autoproxy/>

  50. </beans>

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50

  在上述配置中最后关于获取properties的配置,我创建了一个PropertyUtil类用来获取properties文件,并读取里面的属性。PropertyUtil.java如下:

 
  1. public class PropertyUtil extends PropertyPlaceholderConfigurer {

  2.  
  3. private static Map<String,String> propertyMap;

  4.  
  5. @Override

  6. protected void processProperties(ConfigurableListableBeanFactory beanFactoryToProcess, Properties props) throws BeansException {

  7. super.processProperties(beanFactoryToProcess, props);

  8. propertyMap = new HashMap<String, String>();

  9. for (Object key : props.keySet()) {

  10. String keyStr = key.toString();

  11. String value = props.getProperty(keyStr);

  12. propertyMap.put(keyStr, value);

  13. }

  14. }

  15.  
  16. //static method for accessing context properties

  17. public static Object getProperty(String name) {

  18. return propertyMap.get(name);

  19. }

  20. }

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20

spring-service.xml配置

  配置自动扫描,用于自动注入;并配置注解事务。

 
  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" xmlns:aop="http://www.springframework.org/schema/aop"

  4. xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context"

  5. xsi:schemaLocation="http://www.springframework.org/schema/beans

  6. http://www.springframework.org/schema/beans/spring-beans.xsd

  7. http://www.springframework.org/schema/aop

  8. http://www.springframework.org/schema/aop/spring-aop.xsd

  9. http://www.springframework.org/schema/tx

  10. http://www.springframework.org/schema/tx/spring-tx.xsd

  11. http://www.springframework.org/schema/context

  12. http://www.springframework.org/schema/context/spring-context.xsd">

  13. <!-- 扫描service包下的所有注解类型 -->

  14. <context:component-scan base-package="com.chenlong.study.service"></context:component-scan>

  15. <!-- 配置事务管理器,dataSource在spring-dao.xml中配置 -->

  16. <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">

  17. <property name="dataSource" ref="dataSource"></property>

  18. </bean>

  19. <!-- 配置基于注解的声明事务

  20. 默认使用注解来管理事务行为-->

  21. <tx:annotation-driven transaction-manager="transactionManager"/>

  22. <aop:aspectj-autoproxy/>

  23. </beans>

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23

配置spring-dao.xml

  在配置sprign-dao.xml之前,先配置mybatis的全局文件mybatis-config.xml如下:

 
  1. <?xml version="1.0" encoding="UTF-8" ?>

  2. <!DOCTYPE configuration

  3. PUBLIC "-//mybatis.org//DTD Config 3.0//EN"

  4. "http://mybatis.org/dtd/mybatis-3-config.dtd">

  5. <configuration>

  6. <!-- 配置全局属性 -->

  7. <settings>

  8. <!-- 获取数据库自增主键值 -->

  9. <setting name="useGeneratedKeys" value="true"></setting>

  10. <!-- 使用列命替换别名 -->

  11. <setting name="useColumnLabel" value="true"></setting>

  12. <!-- 开启驼峰命名转换 -->

  13. <setting name="mapUnderscoreToCamelCase" value="true"></setting>

  14. </settings>

  15. </configuration>

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15

  在jdbc.properties中配置数据库信息如下:

 
  1. jdbc.username=root

  2. jdbc.password=

  3. jdbc.driverClass=com.mysql.jdbc.Driver

  4. jdbc.jdbcUrls=jdbc:mysql://localhost:3306/test?useUnicode\=true&characterEncoding\=UTF-8

  • 1
  • 2
  • 3
  • 4

  然后配置spring-dao.xml如下:

 
  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" xmlns:aop="http://www.springframework.org/schema/aop"

  4. xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context"

  5. xsi:schemaLocation="http://www.springframework.org/schema/beans

  6. http://www.springframework.org/schema/beans/spring-beans.xsd

  7. http://www.springframework.org/schema/aop

  8. http://www.springframework.org/schema/aop/spring-aop.xsd

  9. http://www.springframework.org/schema/tx

  10. http://www.springframework.org/schema/tx/spring-tx.xsd

  11. http://www.springframework.org/schema/context

  12. http://www.springframework.org/schema/context/spring-context.xsd">

  13. <!-- 配置整合mybatis过程 -->

  14. <!-- 1.配置数据库相关参数 -->

  15. <!-- 引入属性文件 -->

  16. <context:property-placeholder location="classpath:jdbc.properties" />

  17. <!-- 2.配置数据库连接池 -->

  18. <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">

  19. <property name="driverClass" value="${jdbc.driverClass}"></property>

  20. <property name="jdbcUrl" value="${jdbc.jdbcUrls}"></property>

  21. <property name="user" value="${jdbc.username}"></property>

  22. <property name="password" value="${jdbc.password}"></property>

  23. <!-- 配置连接池私有属性 -->

  24. <property name="maxPoolSize" value="30"></property>

  25. <property name="minPoolSize" value="10"></property>

  26. <!-- 关闭连接后不自动提交 -->

  27. <property name="autoCommitOnClose" value="false"></property>

  28. <!--获取连接超时时间 -->

  29. <property name="checkoutTimeout" value="1000"></property>

  30. <!-- 重试次数 -->

  31. <property name="acquireRetryAttempts" value="2"></property>

  32. </bean>

  33. <!-- 3.配置sqlsessionfactory -->

  34. <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">

  35. <!-- 注入数据库连接池 -->

  36. <property name="dataSource" ref="dataSource"></property>

  37. <!-- 配置mybatis全局文件 -->

  38. <property name="configLocation" value="classpath:mybatis-config.xml"></property>

  39. <!-- 扫描entity包使用别名 -->

  40. <property name="typeAliasesPackage" value="com.chenlong.study.entity"></property>

  41. <!-- 扫描mapper文件 -->

  42. <property name="mapperLocations" value="classpath:com/chenlong/study/dao/*.xml"></property>

  43. </bean>

  44. <!-- 4.配置扫描dao接口包,动态实现dao接口,自动注入到spring容器中 -->

  45. <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">

  46. <!-- 注入sqlsessionfactory -->

  47. <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property>

  48. <!-- 给出扫描Dao接口包 -->

  49. <property name="basePackage" value="com.chenlong.study.dao"></property>

  50. </bean>

  51. <aop:aspectj-autoproxy/>

  52. </beans>

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52

配置web.xml

  在完成上述配置后,配置web.xml,配置springmvc和spring的servlet

 
  1. <?xml version="1.0" encoding="UTF-8"?>

  2. <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

  3. xmlns="http://java.sun.com/xml/ns/javaee"

  4. xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_1.xsd"

  5. id="WebApp_ID" version="3.1">

  6. <display-name>sh_student</display-name>

  7. <!-- 配置springmvc dispatcherServlet -->

  8. <servlet>

  9. <servlet-name>dispacherservlet</servlet-name>

  10. <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>

  11. <init-param>

  12. <param-name>contextConfigLocation</param-name>

  13. <param-value>classpath:spring-*.xml</param-value>

  14. </init-param>

  15. </servlet>

  16. <servlet-mapping>

  17. <servlet-name>dispacherservlet</servlet-name>

  18. <url-pattern>/</url-pattern>

  19. </servlet-mapping>

  20. <context-param>

  21. <param-name>contextConfigLocation</param-name>

  22. <param-value>classpath:spring-*.xml </param-value>

  23. </context-param>

  24. <listener>

  25. <listener-class> org.springframework.web.context.ContextLoaderListener</listener-class>

  26. </listener>

  27. <welcome-file-list>

  28. <welcome-file>index.html</welcome-file>

  29. <welcome-file>index.htm</welcome-file>

  30. <welcome-file>index.jsp</welcome-file>

  31. <welcome-file>default.html</welcome-file>

  32. <welcome-file>default.htm</welcome-file>

  33. <welcome-file>default.jsp</welcome-file>

  34. </welcome-file-list>

  35. </web-app>

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35

单元测试集成

  在pom.xm文件中我们曾加入如下的jar包用来做单元测试。

 
  1. <!-- 4)spring test依赖 -->

  2. <dependency>

  3. <groupId>org.springframework</groupId>

  4. <artifactId>spring-test</artifactId>

  5. <version>4.1.3.RELEASE</version>

  6. </dependency>

  7. <!-- https://mvnrepository.com/artifact/junit/junit -->

  8. <dependency>

  9. <groupId>junit</groupId>

  10. <artifactId>junit</artifactId>

  11. <version>4.11</version>

  12. </dependency>

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12

  下面我给出一个测试示例:

 
  1. //整合junit和spring,让junit在启动时候加载springIOC容器

  2. @RunWith(SpringJUnit4ClassRunner.class)

  3. //告诉junit spring的配置文件,需要用到的配置文件,如果是dao的话可以不用spring-service.xml

  4. @ContextConfiguration({ "classpath:spring-dao.xml",

  5. "classpath:spring-service.xml" })

  6. public class SeckillServiceTest {

  7. @Autowired

  8. private SeckillService seckillService;

  9.  
  10. private Logger logger = LoggerFactory.getLogger(this.getClass());

  11.  
  12. @Test

  13. public void testGetSeckillList() {

  14. List<Seckill> seckills = seckillService.getSeckillList();

  15. logger.info("list={}", seckills);

  16. }

  17. }

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17

  关于SSM框架的整合和单元测试的集成就到此结束。

转载自https://blog.csdn.net/cl534854121/article/details/76121140

猜你喜欢

转载自blog.csdn.net/qq_40374604/article/details/81253643