前言
雖然現在SpringBoot開始流行,但是SSM作為一個經典框架,還是有必要去了解一下。
項目建立
1.新建一個空白的Maven項目,如下圖。然后把IDEA自動生成的多余src目錄刪掉。
2.右鍵項目新建Module,選擇Module類型,ArtfactId為Web,如下
、
3.繼續新建空白Module,分別建立api,biz,common,repository,sal模塊,這樣基本的項目就建立起來了.
4.接下來再如下圖配置一下Tomcat服務器,就可以運行項目,在程序上看到 "Hello World!"
SpringMVC
現在主流都是前后端分離,所以control層都是以restful的形式返回json格式數據,下面寫了個查詢部門類別的例子,需要建立多個文件,如下圖
文件從上往下分別為

public class DeptInfo { private Integer id; public String getName() { return name; } public void setName(String name) { this.name = name; } private String name; public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } }

@Service public class DeptServiceImpl implements DeptService { @Override public List<DeptInfo> listDeptInfo(String name) { List<DeptInfo> deptInfos=new ArrayList<>(); for (int i=0;i<2;i++) { DeptInfo info=new DeptInfo(); info.setName("部門"+i); info.setId(i); deptInfos.add(info); } return deptInfos; } }

public interface DeptService { List<DeptInfo> listDeptInfo(String name); }

@RestController @RequestMapping(value = "/dept") public class DeptController { @Autowired private DeptService deptService; @RequestMapping(value = "/get", method = RequestMethod.GET) public String getDept(String id) { return "部門id"+id; } @RequestMapping(value = "/getList", method = RequestMethod.GET) public List<DeptInfo> getDeptList(String name) { return deptService.listDeptInfo(name); } }

<?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: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/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd"> <context:component-scan base-package="cn.com.test"> </context:component-scan> </beans>

<?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" xmlns:mvc="http://www.springframework.org/schema/mvc" xsi:schemaLocation=" http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.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 http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd "> <mvc:annotation-driven> <mvc:message-converters> <bean id="stringConverter" class="org.springframework.http.converter.StringHttpMessageConverter"> <property name="supportedMediaTypes"> <list> <value>text/html;charset=UTF-8</value> </list> </property> </bean> <bean id="jsonConverter" class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter"> <property name="supportedMediaTypes"> <list> <value>application/json;charset=UTF-8</value> </list> </property> </bean> </mvc:message-converters> </mvc:annotation-driven> <context:component-scan base-package="cn.com.test.springmvc.web"/> </beans>

<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd" > <web-app> <display-name>Archetype Created Web Application</display-name> <context-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/applicationContext.xml</param-value> </context-param> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <servlet> <servlet-name>dispatcher</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>dispatcher</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> </web-app>

<?xml version="1.0" encoding="UTF-8"?> <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"> <parent> <artifactId>springmvc</artifactId> <groupId>cn.com.test.springmvc</groupId> <version>1.0-SNAPSHOT</version> </parent> <modelVersion>4.0.0</modelVersion> <artifactId>web</artifactId> <packaging>war</packaging> <name>web Maven Webapp</name> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <maven.compiler.source>1.8</maven.compiler.source> <maven.compiler.target>1.8</maven.compiler.target> </properties> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.11</version> <scope>test</scope> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>5.0.7.RELEASE</version> </dependency> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</artifactId> <version>2.9.1</version> </dependency> <dependency> <groupId>cn.com.test.springmvc</groupId> <artifactId>biz</artifactId> <version>1.0-SNAPSHOT</version> <scope>compile</scope> </dependency> </dependencies> <build> <finalName>web</finalName> <pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) --> <plugins> <plugin> <artifactId>maven-clean-plugin</artifactId> <version>3.1.0</version> </plugin> <!-- see http://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_war_packaging --> <plugin> <artifactId>maven-resources-plugin</artifactId> <version>3.0.2</version> </plugin> <plugin> <artifactId>maven-compiler-plugin</artifactId> <version>3.8.0</version> </plugin> <plugin> <artifactId>maven-surefire-plugin</artifactId> <version>2.22.1</version> </plugin> <plugin> <artifactId>maven-war-plugin</artifactId> <version>3.2.2</version> </plugin> <plugin> <artifactId>maven-install-plugin</artifactId> <version>2.5.2</version> </plugin> <plugin> <artifactId>maven-deploy-plugin</artifactId> <version>2.8.2</version> </plugin> </plugins> </pluginManagement> </build> </project>
運行后可以看到如下
[{"id":0,"name":"部門0"},{"id":1,"name":"部門1"}]
Mybaits
接下來接入Mybaits數據庫操作,需要改以下文件

@Service public class DeptServiceImpl implements DeptService { @Autowired DeptDOMapper deptDOMapper; @Override public List<DeptInfo> listDeptInfo(String name) { List<DeptDO> deptDOList= deptDOMapper.listDept(name); List<DeptInfo> deptInfos=new ArrayList<>(); deptDOList.stream().forEach(x->{ DeptInfo info=new DeptInfo(); info.setName(x.getName()); info.setId(x.getId()); deptInfos.add(info); }); return deptInfos; } }

public class DeptDO { private Integer id; private String name; public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } }

@Repository public interface DeptDOMapper { List<DeptDO> listDept(@Param("name")String name); }

<?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="cn.com.test.springmvc.repository.mapper.DeptDOMapper" > <resultMap id="BaseResultMap" type="cn.com.test.springmvc.repository.entity.DeptDO" > <id column="id" property="id" jdbcType="BIGINT" /> <result column="name" property="name" jdbcType="VARCHAR" /> </resultMap> <select id="listDept" resultMap="BaseResultMap" > select id,name from test_dept <where> <if test="name !=null and name !=''"> and name like concat('%',#{name},'%') </if> </where> </select> </mapper>

<?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: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/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd"> <context:component-scan base-package="cn.com.test"> </context:component-scan> <bean id="dataSource" class="org.springframework.jdbc.datasource.SimpleDriverDataSource"> <property name="username" value="root"></property> <property name="password" value="111111"></property> <property name="driverClass" value="com.mysql.jdbc.Driver"></property> <property name="url" value="jdbc:mysql://localhost:3306/mysql"></property> </bean> <bean id="SqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="dataSource" /> <property name="mapperLocations" value="classpath*:*Mapper.xml"/> </bean> <bean id="dataScanner" class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="basePackage" value="cn.com.test.springmvc.repository.mapper"/> <property name="sqlSessionFactoryBeanName" value="SqlSessionFactory"/> </bean> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource"/> </bean> <tx:annotation-driven transaction-manager="transactionManager"/> </beans>

<?xml version="1.0" encoding="UTF-8"?> <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"> <parent> <artifactId>springmvc</artifactId> <groupId>cn.com.test.springmvc</groupId> <version>1.0-SNAPSHOT</version> </parent> <modelVersion>4.0.0</modelVersion> <artifactId>web</artifactId> <packaging>war</packaging> <name>web Maven Webapp</name> <url>http://www.example.com</url> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <maven.compiler.source>1.8</maven.compiler.source> <maven.compiler.target>1.8</maven.compiler.target> </properties> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.11</version> <scope>test</scope> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>5.0.7.RELEASE</version> </dependency> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</artifactId> <version>2.9.1</version> </dependency> <dependency> <groupId>cn.com.test.springmvc</groupId> <artifactId>biz</artifactId> <version>1.0-SNAPSHOT</version> <scope>compile</scope> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-jdbc</artifactId> <version>5.1.5.RELEASE</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>8.0.15</version> </dependency> </dependencies> <build> <finalName>web</finalName> <pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) --> <plugins> <plugin> <artifactId>maven-clean-plugin</artifactId> <version>3.1.0</version> </plugin> <!-- see http://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_war_packaging --> <plugin> <artifactId>maven-resources-plugin</artifactId> <version>3.0.2</version> </plugin> <plugin> <artifactId>maven-compiler-plugin</artifactId> <version>3.8.0</version> </plugin> <plugin> <artifactId>maven-surefire-plugin</artifactId> <version>2.22.1</version> </plugin> <plugin> <artifactId>maven-war-plugin</artifactId> <version>3.2.2</version> </plugin> <plugin> <artifactId>maven-install-plugin</artifactId> <version>2.5.2</version> </plugin> <plugin> <artifactId>maven-deploy-plugin</artifactId> <version>2.8.2</version> </plugin> </plugins> </pluginManagement> </build> </project>
運行后,可以得到來自我數據庫的測試數據
[{"id":1,"name":"test1"},{"id":2,"name":"我的部門"}]
補充說明
從上面項目結構看到,這個SSM應用分成了幾個模塊,如下
api | 一個大型系統肯定是不止一個應用的,而應用間通常會有數據交互,而數據交互需要應用對外的接口,而api模塊的職責就是提供對外接口,通常以jar包的形式發放出去 |
biz | 業務邏輯代碼就是寫在這里,應用內部的核心,依賴api,common,repository,sal |
common | 存放幫助類,aop類,異常類等 |
repository | 存儲層,對數據庫的操作都體現在這里 |
sal | 和api職責相反,主要是調用外部系統的服務 |
web | control層,給前端提供restful接口 |
分層的影響其實也是有好有壞,對於復雜的業務來說,分層可以使邏輯清晰,職責分明,如果以后重構代碼也能控制好邊界,降低風險。再極端一點的例子,如果數據庫要從Mysql換到Oracle,可能只需要改repository層的一點點代碼和配置信息。
壞處也有的,如果是簡單的增刪改查業務,也要在各層定義模型,層層透傳,相當費勁。
小結
上文演示了如何從零構建一個SSM項目,當然這只是一個最基礎功能,后面還會加入日志,aop,異常處理,應用間調用等功能。