这个项目是由整合的SSM项目根据三层架构思想,进行的多模块的开发;通过这个整合的项目的拆分,回顾了各模块需要的资源;理顺各模块之间的关联。
此项目拆分完成后可以直接使用tomcat7插件运行,使用基本的功能模块。
实体类模块
1.建立思路
新建pojo类模块-->配置pom.xml-->创建pojo类
2.模块搭建
1.1、 pojo类型
pojo类是根据ORM映射关系,依照数据库表建立的实体类;通用返回结果类是用于表现层,统一处理返回结果的一个类;code属性也需要单独建立一个Code类,根据不同业务功能设置不同编号。
//student类
public class Student {
private Integer id;
private String name;
private Integer age;
}
//通用返回结果
public class GeneralResult<T> {
private T data;
private Boolean flag;
private String msg;
private Integer code;
}
1.2、 pom.xml
pojo类的pom.xml文件只需要配置当前模块资源
<?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">
<modelVersion>4.0.0</modelVersion>
<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>
<!--默认的模块资源-->
<groupId>com.mine</groupId>
<artifactId>ssm_pojo</artifactId>
<version>1.0-SNAPSHOT</version>
</project>
持久层模块
1.建立思路
新建dao层模块-->配置pom.xml文件-->创建dao接口-->编写接口映射文件-->配置jdbc.properties-->配置applicationContext-dao.xml文件
2.模块搭建
2.1、pom.xml
2.1.1、导入:pojo资源文件;
2.1.2、配置:spring环境依赖,mybatis环境依赖,mysql环境依赖,druid连接池依赖,分页插件
2.1.3、整合:spring整合jdbc,spring整合mybatis
<?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">
<modelVersion>4.0.0</modelVersion>
<groupId>com.mine</groupId>
<artifactId>ssm_dao</artifactId>
<version>1.0-SNAPSHOT</version>
<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>
<!--导入资源文件POJO-->
<dependency>
<groupId>com.mine</groupId>
<artifactId>ssm_pojo</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<!--spring环境-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.1.9.RELEASE</version>
</dependency>
<!--mybatis环境-->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.3</version>
</dependency>
<!--mysql环境-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.47</version>
</dependency>
<!--druid连接池-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.21</version>
</dependency>
<!--spring整合jdbc-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>5.1.9.RELEASE</version>
</dependency>
<!--spring整合mybatis-->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>2.0.3</version>
</dependency>
<!--分页插件-->
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper</artifactId>
<version>5.1.2</version>
</dependency>
</dependencies>
</project>
2.2、dao接口
2.2.1、编写基础的模块接口方法;
2.2.2、一个方法中有多个参数,使用@parm注解:
Public User selectUser(@param(“userName”) String name,@param(“userpassword”) String password);
import java.util.List;
@Repository
public interface StudentDao {
List<Student> findAll();
int saveStudent(Student student);
int updateStudent(Student student);
int deleteById(String id);
Student findById(String id);
}
2.3、dao.xml
2.3.1、路径:dao.xml文件是在resources目录下,路径同对应dao接口全限定名一样,比如:dao接口的路径:com.mine.dao.java<==> com.mine.dao(dao.xml映射路径)。
2.3.2、语句:此配置文件是写对应接口的SQL语句,具体反应数据库表与实体类之间的关系。
//dao.xml文件是在resoures文件下,对应着dao接口的全限定名
<?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.mine.dao.StudentDao">
<!--查询所有-->
<select id="findAll" resultType="com.mine.domain.Student">
select * from student
</select>
<!--查询单个-->
<select id="findById" resultType="com.mine.domain.Student">
select * from student where id=#{id}
</select>
<!--保存-->
<insert id="saveStudent" parameterType="com.mine.domain.Student">
insert into student (name,age)values (#{name},#{age})
</insert>
<!--更新-->
<update id="updateStudent" parameterType="com.mine.domain.Student">
update student set name=#{name},age=#{age} where id=#{id}
</update>
<!--删除-->
<delete id="deleteById" parameterType="java.lang.String">
delete from student where id=#{id}
</delete>
</mapper>
2.4、jdbc.properties
配置数据库连接的参数
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/数据库名?useUnicode=true&characterEncoding=utf-8
jdbc.username=用户名
jdbc.password=密码
2.5、applicationContext-dao.xml
2.5.1、开启目录扫描,将扫描的bean加入IoC容器中。
2.5.2、 整合mybatis:加载properties文件,加载druid,SqlsessionFactoryBean,MapperScannerConfigurer。
2.5.3 、配置:SqlSessionFactoryBean中配置分页插件
<?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:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd">
<!--开启目录扫描-->
<context:component-scan base-package="com.mine"/>
<!--加载perperties配置文件的信息,文件名写死,多文件加载只能单独写出-->
<context:property-placeholder location="classpath:jdbc.properties"/>
<!--加载druid资源-->
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
<property name="driverClassName" value="${jdbc.driver}"/>
<property name="url" value="${jdbc.url}"/>
<property name="username" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
</bean>
<!--spring整合mybatis后控制的创建连接用的对象-->
<bean class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="typeAliasesPackage" value="com.mine.domain"/>
<!--分页插件-->
<property name="plugins">
<array>
<bean class="com.github.pagehelper.PageInterceptor">
<property name="properties">
<props>
<prop key="helperDialect">mysql</prop>
<prop key="reasonable">true</prop>
</props>
</property>
</bean>
</array>
</property>
</bean>
<!--加载mybatis映射配置的扫描,将其作为spring的bean进行管理-->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.mine.dao"/>
</bean>
</beans>
业务层模块
1.建立思路
1)开发目录:
新建service模块-->配置pom.xml-->编写service接口-->编写service接口实现类-->配置applicationContext-service.xml文件
2)测试目录
新建service接口测试类-->编写测试方法-->配置applicationContext-service.xml文件
2.模块搭建
2.1、pom.xml
2.1.1、导入:持久层模块资源文件dao
2.1.2、配置:spring环境,junit单元测试,spring整合junit,junit测试插件
<?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">
<modelVersion>4.0.0</modelVersion>
<groupId>com.mine</groupId>
<artifactId>service</artifactId>
<version>1.0-SNAPSHOT</version>
<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>
<!--导入资源文件dao-->
<dependency>
<groupId>com.mine</groupId>
<artifactId>ssm_dao</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<!--spring环境-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.1.9.RELEASE</version>
</dependency>
<!--junit单元测试-->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
<!--spring整合junit-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>5.1.9.RELEASE</version>
</dependency>
</dependencies>
<!--juit测试插件-->
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.19.1</version>
<configuration>
<skipTests>false</skipTests>
</configuration>
</plugin>
</plugins>
</build>
</project>
2.2、service接口
模块增删改的事务管理
@Transactional(readOnly = true)
public interface StudentService {
List<Student> findAll();
@Transactional(readOnly = false)
int saveStudent(Student student);
@Transactional(readOnly = false)
int updateStudent(Student student);
@Transactional(readOnly = false)
int deleteById(String id);
Student findById(String id);
PageInfo findPage(int page, int size);
}
2.3、serviceImpl
业务层接口实现类,引入dao层对象;基础的业务功能加上分页查询方法。
@Service
public class StudentServiceImpl implements StudentService {
@Autowired
private StudentDao studentDao;
@Override
public List<Student> findAll() {
return studentDao.findAll();
}
@Override
public int saveStudent(Student student) {
return studentDao.saveStudent(student);
}
@Override
public int updateStudent(Student student) {
return studentDao.updateStudent(student);
}
@Override
public int deleteById(String id) {
return studentDao.deleteById(id);
}
@Override
public Student findById(String id) {
return studentDao.findById(id);
}
@Override
public PageInfo findPage(int page, int size) {
PageHelper.startPage(page, size);
List<Student> students = studentDao.findAll();
PageInfo pageInfo = new PageInfo(students);
return pageInfo;
}
}
2.4、applicationContext-service.xml
开启service层bean的扫描,事务注解驱动,事务管理器
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
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">
<!--开启bean注解扫描-->
<context:component-scan base-package="com.mine"/>
<!--开启注解式事务-->
<tx:annotation-driven transaction-manager="txManager"/>
<!--事务管理器-->
<bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"/>
</bean>
</beans>
2.5、serviceTest
编写测试用例,根据模块生命周期去test看是否成功。
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"classpath:applicationContext-service.xml", "classpath:applicationContext-dao.xml"})
public class StudentServiceTest {
@Autowired
private StudentService studentService;
@Test
public void testFindAll() {
List<Student> all = studentService.findAll();
System.out.println(all);
}
}
表现层模块
1.建立思路
新建controller模块-->配置pom.xml文件-->编写controller模块处理器方法-->配置spring-mvc文件-->配置web.xml文件
2.模块搭建
2.1、pom.xml
2.1.1、导入service层资源文件;配置springmvc环境,jackson依赖,servlert依赖,tomcat7插件。
2.1.2、模块资源必须 war
<?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">
<modelVersion>4.0.0</modelVersion>
<groupId>com.mine</groupId>
<artifactId>controller</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>war</packaging>
<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>
<!--导入资源文件ssm_service-->
<dependency>
<groupId>com.mine</groupId>
<artifactId>service</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<!--springmvc环境-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.1.9.RELEASE</version>
</dependency>
<!--jackson相关坐标3个-->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.9.0</version>
</dependency>
<!--servlet环境-->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
<scope>provided</scope>
</dependency>
</dependencies>
<build>
<!--设置插件-->
<plugins>
<!--tomcat7-->
<plugin>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat7-maven-plugin</artifactId>
<version>2.1</version>
<configuration>
<port>80</port>
<path>/</path>
</configuration>
</plugin>
</plugins>
</build>
</project>
2.2、controller
编写处理器方法,封装操作结果,响应给前端页面。
@Controller
@RequestMapping("/student")
public class StudentController {
@Autowired
private StudentService studentService;
@RequestMapping("findAll")
@ResponseBody
public List<Student> findAll() {
return studentService.findAll();
}
@RequestMapping("saveStudent")
@ResponseBody
public GeneralResult saveStudent(@RequestBody Student student) {
studentService.saveStudent(student);
GeneralResult generalResult = new GeneralResult();
generalResult.setFlag(true);
generalResult.setMsg("保存成功");
return generalResult;
}
@RequestMapping("updateStudent")
@ResponseBody
public GeneralResult updateStudent(@RequestBody Student student) {
studentService.updateStudent(student);
GeneralResult generalResult = new GeneralResult();
generalResult.setFlag(true);
generalResult.setMsg("更新成功");
return generalResult;
}
@RequestMapping("deleteStudent")
@ResponseBody
public GeneralResult delteById(String id) {
int i = studentService.deleteById(id);
GeneralResult generalResult = new GeneralResult();
generalResult.setFlag(true);
generalResult.setMsg("删除成功");
generalResult.setData(i);
return generalResult;
}
@RequestMapping("findStudent")
@ResponseBody
public GeneralResult findById(String id) {
Student student = studentService.findById(id);
GeneralResult<Student> result = new GeneralResult<>();
result.setData(student);
result.setFlag(true);
return result;
}
@RequestMapping("findPage")
@ResponseBody
public GeneralResult findPage() {
PageInfo page = studentService.findPage(1, 2);
GeneralResult generalResult = new GeneralResult();
generalResult.setData(page);
generalResult.setFlag(true);
return generalResult;
}
}
2.3、spring-mvc
开启注解驱动,目录扫描;放开静态资源
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<!--开启注解驱动-->
<mvc:annotation-driven/>
<!--开启目录扫描-->
<context:component-scan base-package="com.mine.controller"/>
<!--放开静态资源-->
<mvc:default-servlet-handler />
</beans>
2.3、web.xml
2.3.1、监听器:加载spring核心配置文件applicationContext.xml
2.3.2、过滤器:处理中文件乱码
2.3.3、中央处理器:加载spring-mvc核心配置文件,配置映射路径
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
version="3.1">
<!--监听器,加载spring核心配置文件-->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath*:applicationContext-*.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!--过滤器,中文乱码处理-->
<filter>
<filter-name>CharacterEncodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>CharacterEncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!--中央处理器,加载spring-mvc核心配置文件-->
<servlet>
<servlet-name>DispatcherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath*:spring-mvc.xml</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>DispatcherServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
多模块总结
1、模块中仅包含当前模块对应的功能类与配置文件。
2、spring核心配置根据功能不同进行独立制作。
3、当前模块所依赖的模块通过导入的资源文件的形式加入当前模块后才可以使用(传递依赖)
4、web.xml需要加载所有的spring核心配置文件。