前言
虽然现在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,异常处理,应用间调用等功能。