前言: 现在在企业中一般都是用SSMM框架来进行开发。
SSMM(springmvc + spring + mybatis+ mysql)
从分层角度来说
- springmvc 控制层
- spring service 层
- mybatis dao层
- mysql 数据库
从我个人而言,需要用springboot来搭建SSMM框架,前提是能先用xml配置的方式搭建起来,这样在用springboot的时候,可以思路清晰一些,下面就开始来搭建,我先搭建一份xml 格式SSMM,在由xml 转向springboot,这样就会发现springboot的配置是怎么来的。
- 思路
- 创建maven项目
- 导入pom.xml
- 配置web.xml
- 配置数据库连接,连接池,sessionFactory,事务,扫描注入的配置
- 创建数据库
- 写测试用例
- 测试添加
- 测试事物
- 完成基本搭建。
- 后续可以继续配置springmvc
按照上面的思路现在来开始搭建
(1).创建一个maven项目
(2).导入pomx.ml
<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>com.study</groupId>
<artifactId>boot-study-ssmm-xml</artifactId>
<version>1.0.0</version>
<packaging>war</packaging>
<build>
<plugins>
<plugin>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat7-maven-plugin</artifactId>
<version>2.2</version>
</plugin>
</plugins>
</build>
<properties>
<java-version>1.7</java-version>
<junit-version>4.8.2</junit-version>
<org.springframework-version>3.2.4.RELEASE</org.springframework-version>
<jackson-mapper-asl-version>1.9.12</jackson-mapper-asl-version>
<jackson-core-asl-version>1.9.12</jackson-core-asl-version>
<mysql-connector-java-version>5.1.8</mysql-connector-java-version>
<org.mybatis-version>3.2.7</org.mybatis-version>
<org.mybatis-spring-version>1.2.2</org.mybatis-spring-version>
<opensymphony-version>2.4.2</opensymphony-version>
<freemarker-version>2.3.9</freemarker-version>
<c3p0-version>0.9.1.2</c3p0-version>
<commons-collections-version>1.0</commons-collections-version>
<commons-fileupload-version>1.2.2</commons-fileupload-version>
<org.apache.commons-version>3.1</org.apache.commons-version>
<commons-codec-version>1.6</commons-codec-version>
<dom4j-version>1.6.1</dom4j-version>
<javax.servlet-version>1.2</javax.servlet-version>
<aspectjweaver-version>1.6.6</aspectjweaver-version>
<slf4j-log4j12-version>1.6.6</slf4j-log4j12-version>
<log4j-version>1.2.16</log4j-version>
<javax.servlet-jsp-version>2.0</javax.servlet-jsp-version>
<cglib-version>2.2.2</cglib-version>
<slf4j-api-version>1.6.6</slf4j-api-version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<!-- with junit4.8.2 -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>${junit-version}</version>
<type>jar</type>
</dependency>
<!-- with spring -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>${org.springframework-version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>${org.springframework-version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>${org.springframework-version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aspects</artifactId>
<version>${org.springframework-version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
<version>${org.springframework-version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>${org.springframework-version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${org.springframework-version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>${org.springframework-version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>${org.springframework-version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-instrument</artifactId>
<version>${org.springframework-version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-instrument-tomcat</artifactId>
<version>${org.springframework-version}</version>
</dependency>
<!--springmvc 转 Json-->
<dependency>
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-mapper-asl</artifactId>
<version>${jackson-mapper-asl-version}</version>
</dependency>
<dependency>
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-core-asl</artifactId>
<version>${jackson-core-asl-version}</version>
</dependency>
<!--freemarker-->
<dependency>
<groupId>freemarker</groupId>
<artifactId>freemarker</artifactId>
<version>${freemarker-version}</version>
</dependency>
<!-- with mybatis-spring -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>${org.mybatis-version}</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>${org.mybatis-spring-version}</version>
</dependency>
<!-- jdbc driver -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>${mysql-connector-java-version}</version>
</dependency>
<!-- connect pool with c3p0 -->
<dependency>
<groupId>c3p0</groupId>
<artifactId>c3p0</artifactId>
<version>${c3p0-version}</version>
</dependency>
<!-- apache commons jar -->
<dependency>
<groupId>commons-collections</groupId>
<artifactId>commons-collections</artifactId>
<version>${commons-collections-version}</version>
</dependency>
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
<version>${commons-fileupload-version}</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>${org.apache.commons-version}</version>
</dependency>
<dependency>
<groupId>commons-codec</groupId>
<artifactId>commons-codec</artifactId>
<version>${commons-codec-version}</version>
</dependency>
<!-- analyze xml use dom4j -->
<dependency>
<groupId>dom4j</groupId>
<artifactId>dom4j</artifactId>
<version>${dom4j-version}</version>
</dependency>
<!-- about servlet -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>${javax.servlet-version}</version>
</dependency>
<dependency>
<groupId>taglibs</groupId>
<artifactId>standard</artifactId>
<version>1.1.2</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.0.1</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.6.6</version>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.6.6</version>
</dependency>
<dependency>
<groupId>javax.activation</groupId>
<artifactId>activation</artifactId>
<version>1.1.1</version>
</dependency>
<!-- memcached -->
<dependency>
<groupId>com.google.code.maven-play-plugin.spy</groupId>
<artifactId>memcached</artifactId>
<version>2.4.2</version>
</dependency>
<!-- <dependency>
<groupId>com.danga</groupId>
<artifactId>memcached</artifactId>
<version>2.0.1</version>
</dependency>
<dependency>
<groupId>memcached-util</groupId>
<artifactId>memcached</artifactId>
<version>1.1</version>
</dependency> -->
<dependency>
<groupId>cglib</groupId>
<artifactId>cglib</artifactId>
<version>${cglib-version}</version>
</dependency>
<dependency>
<groupId>net.fckeditor</groupId>
<artifactId>java-core</artifactId>
<version>2.6</version>
</dependency>
<dependency>
<groupId>org.json</groupId>
<artifactId>json</artifactId>
<version>20131018</version>
</dependency>
<dependency>
<groupId>oscache</groupId>
<artifactId>oscache</artifactId>
<version>2.4.1</version>
</dependency>
<!--jerson -->
<dependency>
<groupId>com.sun.jersey</groupId>
<artifactId>jersey-client</artifactId>
<version>1.18.1</version>
</dependency>
<!--jcaptcha -->
<!-- <dependency>
<groupId>com.octo.captcha</groupId>
<artifactId>jcaptcha-my</artifactId>
<version>1.0</version>
</dependency> -->
<!--page -->
<!-- <dependency>
<groupId>cn.itcast</groupId>
<artifactId>page</artifactId>
<version>1.0</version>
</dependency> -->
</dependencies>
</project>
(3)配置web.xml
- 一般我们在配置web.xml 首先会配置web.xml的头
<?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/j2ee" xmlns:javaee="http://java.sun.com/xml/ns/javaee"
xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"
id="WebApp_ID" version="2.4">
- 配置spring监听
<!-- Spring监听 -->
<listener >
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
扩展下:
一般在这里我们会配置spring监听的spring配置文件路径,默认情况下是/WEB-INF/applicationContext.xml
这里我们可以去看下源码,到底是为什么要这么配置
监听器,发现是继承ContextLoader
继续,我们去查看下ContextLoader 源码
发现这个就是我们平时配置的Context-param,原来是这么来的,看注解发现我们可以去org.springframework.web.context.support.XmlWebApplicationContext#DEFAULT_CONFIG_LOCATION 看下监听器的默认配置
继续查看XmlWebApplicationContext
到这里,明白了吧,原来默认配置在这里哈。
继续配置我们的监听文件路径,一般我习惯不用默认配置,设置如下
<!-- spring监听文件地址 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:application-context.xml</param-value>
</context-param>
创建application-context.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:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx" xmlns:jdbc="http://www.springframework.org/schema/jdbc"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">
<import resource="config/*.xml" />
</beans>
创建文件夹 config
(3)在文件夹下配置数据库连接,连接池,sessionFactory,事务,扫描注入的配置
- 扫描
- 创建annotation.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:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx" xmlns:jdbc="http://www.springframework.org/schema/jdbc"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">
<!-- 扫描配置,排除扫描controller -->
<context:component-scan base-package="com.study">
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
</context:component-scan>
<!-- 隐式地向 Spring容器注册AutowiredAnnotationBeanPostProcessor、
RequiredAnnotationBeanPostProcessor、CommonAnnotationBeanPostProcessor
以及PersistenceAnnotationBeanPostProcessor这4个BeanPostProcessor -->
<context:annotation-config/>
</beans>
- 数据库连接 - 读取配置文件
创建properties.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:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx" xmlns:jdbc="http://www.springframework.org/schema/jdbc"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<!-- 配置Jdbc -->
<value>classpath:properties/jdbc.properties</value>
</list>
</property>
</bean>
</beans>
- jdbc连接
<?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:jdbc="http://www.springframework.org/schema/jdbc"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="${driverClass}"/>
<property name="jdbcUrl" value="${jdbcUrl}"></property>
<property name="user" value="${user}" />
<property name="password" value="${password}"/>
</bean>
</beans>
- 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:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx" xmlns:jdbc="http://www.springframework.org/schema/jdbc"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">
<bean class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="typeAliasesPackage" value="com.study.bean"/>
<!-- <property name="configLocation" value="classpath:mybatis-config.xml"/> -->
</bean>
<!-- 配置Mybatis扫描 -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.study.dao"/>
</bean>
</beans>
创建事务transation.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:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx" xmlns:jdbc="http://www.springframework.org/schema/jdbc"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">
<!-- spring 事务 -->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"/>
</bean>
<!-- 开启注解事务-->
<tx:annotation-driven transaction-manager="transactionManager"/>
</beans>
在properties文件中创建jdbc.properties
driverClass=com.mysql.jdbc.Driver
jdbcUrl=jdbc:mysql://localhost:3306/test?characterEncoding=UTF-8
user=root
password=
配置文件到这里就结束了,终于结束了。。
(4)开始创建数据库
创建 test数据库,创建下面的表
CREATE TABLE `test_tb` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(255) DEFAULT NULL,
`birthday` datetime DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;
(5)写测试用例
一般我会写一个通用的BaseTest,这样其他测试类直接继承就可以开始测试,不用去理会测试环境的配置
package com.study.test;
import org.junit.runner.RunWith;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"classpath:application-context.xml"})
public class BaseTest {
}
package com.study.test;
import java.util.Date;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import com.study.bean.TestTb;
import com.study.service.TestTbService;
public class TestTestTb extends BaseTest{
@Autowired
private TestTbService testTbService;
@Test
public void test_add(){
TestTb tb = new TestTb();
tb.setName("test");
tb.setBirthday(new Date());
testTbService.add(tb);
}
}
(6)写model,service,dao,mapper,开始测试
附xml
<?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.study.dao.TestTbDao">
<!-- 添加测试数据 -->
<insert id="addTestTb" parameterType="TestTb">
insert into test_tb
(name,birthday)
values
(#{name},#{birthday})
</insert>
</mapper>
其他代码比较简单,就不附上了
(7)测试事务,一般写测试事务的时候,我会在service层抛出异常,如果异常抛出,数据数据没有添加成功,则事务配置成功,反之则没有成功。