程序员人生 网站导航

spring boot 学习--08---搭建ssmm-01

栏目:php教程时间:2016-11-11 09:07:48

前言: 现在在企业中1般都是用SSMM框架来进行开发。
SSMM(springmvc + spring + mybatis+ mysql)

从分层角度来讲
- springmvc 控制层
- spring service 层
- mybatis dao层
- mysql 数据库

从我个人而言,需要用springboot来搭建SSMM框架,条件是能先用xml配置的方式搭建起来,这样在用springboot的时候,可以思路清晰1些,下面就开始来搭建,我先搭建1份xml 格式SSMM,在由xml 转向springboot,这样就会发现springboot的配置是怎样来的。

  1. 思路
    • 创建maven项目
    • 导入pom.xml
    • 配置web.xml
    • 配置数据库连接,连接池,sessionFactory,事务,扫描注入的配置
    • 创建数据库
    • 写测试用例
    • 测试添加
    • 测试事物
    • 完成基本搭建。
    • 后续可以继续配置springmvc

依照上面的思路现在来开始搭建

(1).创建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⑷.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⑻</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
- 1般我们在配置web.xml 首先会配置web.xml的头

<?xml version="1.0" encoding="UTF⑻"?> <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>

扩大下:
1般在这里我们会配置spring监听的spring配置文件路径,默许情况下是/WEB-INF/applicationContext.xml
这里我们可以去看下源码,究竟是为何要这么配置

监听器,发现是继承ContextLoader
这里写图片描述

继续,我们去查看下ContextLoader 源码
这里写图片描述

发现这个就是我们平时配置的Context-param,原来是这么来的,看注解发现我们可以去org.springframework.web.context.support.XmlWebApplicationContext#DEFAULT_CONFIG_LOCATION 看下监听器的默许配置

继续查看XmlWebApplicationContext
这里写图片描述

到这里,明白了吧,原来默许配置在这里哈。

继续配置我们的监听文件路径,1般我习惯不用默许配置,设置以下

<!-- 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⑻"?> <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⑶.0.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans⑶.0.xsd http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc⑶.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx⑶.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop⑶.0.xsd"> <import resource="config/*.xml" /> </beans>

创建文件夹 config

(3)在文件夹下配置数据库连接,连接池,sessionFactory,事务,扫描注入的配置
- 扫描
- 创建annotation.xml

<?xml version="1.0" encoding="UTF⑻"?> <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⑶.0.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans⑶.0.xsd http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc⑶.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx⑶.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop⑶.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⑻"?> <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⑶.0.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans⑶.0.xsd http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc⑶.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx⑶.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop⑶.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⑻"?> <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⑶.0.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans⑶.0.xsd http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc⑶.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx⑶.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop⑶.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⑻"?> <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⑶.0.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans⑶.0.xsd http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc⑶.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx⑶.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop⑶.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⑻"?> <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⑶.0.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans⑶.0.xsd http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc⑶.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx⑶.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop⑶.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⑻ 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)写测试用例
1般我会写1个通用的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⑻" ?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis⑶-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)测试事务,1般写测试事务的时候,我会在service层抛出异常,如果异常抛出,数据数据没有添加成功,则事务配置成功,反之则没有成功。

------分隔线----------------------------
------分隔线----------------------------

最新技术推荐