brightloong’s blog
I used Spring version 4.1.3.RELEASE and Mybatis version 3.3.0 to build with Maven.
Note: First declare that the following configuration is based on my directory structure, which looks like this:
! [] directory structure (https://brightloong.github.io/images/directory structure. JPG)
Use Maven to add relevant JAR packages.
The configuration of pom.xml is as follows.
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>ssm_test</groupId>
<artifactId>ssm_test</artifactId>
<version>0.0.1 - the SNAPSHOT</version>
<packaging>war</packaging>
<name>ssm_test</name>
<description />
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<! -- Configure dependencies -->
<dependencies>
<! -- https://mvnrepository.com/artifact/junit/junit -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.10</version>
</dependency>
<! Slf4j + logback --> slf4j + logback -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.12</version>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-core</artifactId>
<version>1.1.1</version>
</dependency>
<! Implement slF4J interface and integrate
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>1.1.1</version>
</dependency>
<! -- 2. Database dependencies -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.7</version>
</dependency>
<dependency>
<groupId>c3p0</groupId>
<artifactId>c3p0</artifactId>
<version>0.9.1.2</version>
</dependency>
<! -- 3. Dao Mybatis dependencies -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.3.0</version>
</dependency>
<! -- Mybatis implementation of spring integration -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>1.2.3</version>
</dependency>
<! -- 4. Servelet dependencies -->
<dependency>
<groupId>jstl</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<dependency>
<groupId>taglibs</groupId>
<artifactId>standard</artifactId>
<version>1.1.2</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.5.4</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
</dependency>
<! -- 5. Spring dependencies -->
<! -- 1) Spring core dependencies -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>4.1.3. RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>4.1.3. RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.1.3. RELEASE</version>
</dependency>
<! -- Spring AOP related -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>4.1.3. RELEASE</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjrt</artifactId>
<version>1.6.11</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.6.11</version>
</dependency>
<! Spring DAO layer dependencies -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>4.1.3. RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>4.1.3. RELEASE</version>
</dependency>
<! -- 3) Spring Web dependencies -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>4.1.3. RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>4.1.3. RELEASE</version>
</dependency>
<! -- 4) Spring test dependencies -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>4.1.3. RELEASE</version>
</dependency>
<! -- Junit dependency -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
</dependency>
</dependencies>
<build>
<sourceDirectory>src</sourceDirectory>
<resources>
<resource>
<directory>src</directory>
<excludes>
<exclude>**/*.java</exclude>
</excludes>
</resource>
</resources>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.3.2</version>
<configuration>
<source>1.6</source>
<target>1.6</target>
</configuration>
</plugin>
<plugin>
<artifactId>maven-war-plugin</artifactId>
<version>2.2</version>
<configuration>
<warSourceDirectory>${basedir}/WebRoot</warSourceDirectory>
<version>3.0</version>
<failOnMissingWebXml>false</failOnMissingWebXml>
</configuration>
</plugin>
</plugins>
</build>
</project>
Copy the code
Integrated SSM configuration
In order to make the configuration hierarchy and structure clear, I divided the configuration into Spring-web. XML, spring-service. XML and spirng-dao.xml respectively according to web, service and DAO. The following configuration files are introduced in turn.
Spring – web. Xm configuration
This configuration is mainly related to some resource mapping configurations.
<?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:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
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/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.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">
<! -- Configure springMVC -->
<! -- 1. Enable springMVC annotation pattern -->
<! - simplify configuration: 1) automatic registration defaultAnnotationHandlerMapping, AnnotationMethodHandlARadpter 2) provides a range of: Data binding, number and date format @numberFOamat, @datatiemFormat XML, JSON default read and write support -->
<mvc:annotation-driven></mvc:annotation-driven>
<! --2. Servelt-mapping mapping path :"/" Static resource Default servlet configuration 1. Add static resource processing: js, GIF, PNG
<mvc:default-servlet-handler/>
<! -- 3. Configure JSP to display viewResolver-->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass" value="org.springframework.web.servlet.view.JstlView"></property>
<property name="prefix" value="/WEB-INF/jsp/"></property>
<property name="suffix" value=".jsp"></property>
</bean>
<! -- 4. Scan for Web-related packages -->
<context:component-scan base-package="com.chenlong.study.web"></context:component-scan>
<!-- 获取properties -->
<bean id="propertyConfigurer" class="com.chenlong.study.utils.PropertyUtil">
<property name="locations" >
<list>
<value>classpath:*.properties</value>
</list>
</property>
</bean>
<aop:aspectj-autoproxy/>
</beans>
Copy the code
In the last configuration above for getting properties, I created a PropertyUtil class to get the properties file and read the properties inside. PropertyUtil. Java is as follows:
public class PropertyUtil extends PropertyPlaceholderConfigurer {
private static Map<String,String> propertyMap;
@Override
protected void processProperties(ConfigurableListableBeanFactory beanFactoryToProcess, Properties props) throws BeansException {
super.processProperties(beanFactoryToProcess, props);
propertyMap = new HashMap<String, String>();
for(Object key : props.keySet()) { String keyStr = key.toString(); String value = props.getProperty(keyStr); propertyMap.put(keyStr, value); }}//static method for accessing context properties
public static Object getProperty(String name) {
returnpropertyMap.get(name); }}Copy the code
Spring – service. The XML configuration
Configure automatic scan for automatic injection; And configure the annotation transaction.
<?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:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx" 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/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<! -- Scan all annotation types under service package -->
<context:component-scan base-package="com.chenlong.study.service"></context:component-scan>
<! DataSource = 'dataSource';
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"></property>
</bean>
<! By default, annotations are used to manage transaction behavior -->
<tx:annotation-driven transaction-manager="transactionManager"/>
<aop:aspectj-autoproxy/>
</beans>
Copy the code
Configure spring – dao. XML
Before configuring sprign-dao.xml, configure mybatis global file mybatis-config. XML as follows:
<?xml version="1.0" encoding="UTF-8" ? >
<configuration>
<! -- Configure global properties -->
<settings>
<! SQL > select primary key from database;
<setting name="useGeneratedKeys" value="true"></setting>
<! -- Replace aliases with columns -->
<setting name="useColumnLabel" value="true"></setting>
<! -- Enable hump name conversion -->
<setting name="mapUnderscoreToCamelCase" value="true"></setting>
</settings>
</configuration>
Copy the code
Configure the database information in jdbc.properties as follows:
jdbc.username=root jdbc.password= jdbc.driverClass=com.mysql.jdbc.Driver jdbc.jdbcUrls=jdbc:mysql://localhost:3306/test? useUnicode\=true&characterEncoding\=UTF-8Copy the code
Then configure spring-dao.xml as follows:
<?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:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx" 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/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<! Configure and integrate mybatis process
<! -- 1. Set database parameters -->
<! -- Import properties file -->
<context:property-placeholder location="classpath:jdbc.properties" />
<! 2. Configure the database connection pool -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="${jdbc.driverClass}"></property>
<property name="jdbcUrl" value="${jdbc.jdbcUrls}"></property>
<property name="user" value="${jdbc.username}"></property>
<property name="password" value="${jdbc.password}"></property>
<! -- Set connection pool private property -->
<property name="maxPoolSize" value="30"></property>
<property name="minPoolSize" value="10"></property>
<! -- Not automatically commit after closing connection -->
<property name="autoCommitOnClose" value="false"></property>
<! Get connection timeout time -->
<property name="checkoutTimeout" value="1000"></property>
<! -- Retry times -->
<property name="acquireRetryAttempts" value="2"></property>
</bean>
<! Sqlsessionfactory -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<! Insert into database pool -->
<property name="dataSource" ref="dataSource"></property>
<! -- Configure mybatis global file -->
<property name="configLocation" value="classpath:mybatis-config.xml"></property>
<! -- Scan entity package using alias -->
<property name="typeAliasesPackage" value="com.chenlong.study.entity"></property>
<! -- Scan mapper files -->
<property name="mapperLocations" value="classpath:com/chenlong/study/dao/*.xml"></property>
</bean>
<! -- 4. Configure the scan DAO interface package, dynamically implement the DAO interface, and automatically inject it into the Spring container -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<! Sqlsessionfactory -->
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property>
<! Dao interface package -->
<property name="basePackage" value="com.chenlong.study.dao"></property>
</bean>
<aop:aspectj-autoproxy/>
</beans>
Copy the code
Configure web. XML
After the above configuration, configure web.xml, springMVC, and Spring servlets
<?xml version="1.0" encoding="UTF-8"? >
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_1.xsd"
id="WebApp_ID" version="3.1">
<display-name>sh_student</display-name>
<! Springmvc dispatcherServlet -->
<servlet>
<servlet-name>dispacherservlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring-*.xml</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>dispacherservlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring-*.xml </param-value>
</context-param>
<listener>
<listener-class> org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
</web-app>
Copy the code
Unit test integration
In the pom.xm file we included the following JAR package for unit testing.
<! -- 4) Spring test dependencies -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>4.1.3. RELEASE</version>
</dependency>
<! -- https://mvnrepository.com/artifact/junit/junit -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
</dependency>
Copy the code
Here is a test example:
// Integrate junit and Spring so that junit loads the springIOC container at startup
@RunWith(SpringJUnit4ClassRunner.class)
// Tell junit the spring configuration file to use. If it is a DAO, you can skip spring-service.xml
@ContextConfiguration({ "classpath:spring-dao.xml"."classpath:spring-service.xml" })
public class SeckillServiceTest {
@Autowired
private SeckillService seckillService;
private Logger logger = LoggerFactory.getLogger(this.getClass());
@Test
public void testGetSeckillList(a) {
List<Seckill> seckills = seckillService.getSeckillList();
logger.info("list={}", seckills); }}Copy the code
This concludes the integration of the SSM framework and unit tests.