1.历史发展变革
在 2004 年开始开发 Struts, Spring 和 Hibernate 组成的 SSH。
2011 年开始,更加轻量级Java 开发组合架出现了,即由 Spring MVC 、Spring,MyBatis 组的 SSM 架,相比 SSH ,SSM 架更加轻量级、更加人性。
在2016 年左右,SSM 框架取代了 SSH 的地位,为 Java Web 企业级开发最行的架。即使到现在,尤其 是在国内仍有很多公司在使用 SSM 框架,而且还有不少公司即使在使用 Spring Boot,也只是用 Spring Boot 减少配置、可直接运行的优点,将它作为一个壳子套在SSM 框架上,并没有使用 Spring Boot 推荐 的技术组合(如 Spring Boot推荐的持久层框楚是Spring Data JPA)。 这也充分说明 SSM 架在行业内的影响力之大,未来也很有可能会出现 MyBati和 Spring Data JPA 两套 持层架并存的局面。鉴于其在行业中的重要性,本章先对SSM 框架结合JSP 术进行重点讲解,Alan 人事 管理系统先使用 SSM 实现,后使用Spring Boot 及 Spring Boot推荐技术实现,以便我们更清楚地了解 Spirng Boot框架的优势。
2.1创建SSM项目
2.1.1 创建Maven Module
1.File->New->Module
2.选择Maven项目
3.设置项目名
4.Maven配置
5.添加工程目录文件
右击main->new->Directory
单击选中java,回车
右击webapp->点击new->JSP
输入index,新建首页
2.1.2 配置SSM依赖
配置ssm依赖,主要使用Spring MVC,Spring,MyBatis,JSP,JSTL等技术
2.2 SSM****整合
2.2.1****项目结构
符合 MVC 三层架构对 java 包进行设计
entity包:实体包 controller 包:控制器包,即 Spring MVC类存放的位置。
service 包:业务逻辑接口包。
service.impl包:业务逻辑实现类包。
dao包:数据库访问接口包,即MyBatis 接口存放的位置.
util包:工具包。
项目使用 Maven 工具管理,这不会减少配置文件,但会统一项目目录。 SSM 项目涉及的多个配置文件 统一放到 resources 文件夹下。
mapper 文件夹 :MyBatis 映射文件。
applicationContext.xml: Spring 核心配置文件。
db.properties: 数据库链接信息配置文件。
springmvc.xml: Spring MVC 配置文件。
webapp 文件:存放 web 资源。
2.2.2****配置文件详解
web.xml 文件是 Java Web 项目的核心配置文件,主要包括三个配置:
(1)Spring 容器的启动入口 ContextLoaderListener 。
(2)由 Spring MVC 处理的 URL 路径
(3)配置一个 Spring MVC 提供的防止中文乱码的过滤器。
web.xml 配置如下:
复制代码
xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
db.properties 配置( diver 与 mysql 版本有关系, 5 版本的: com.mysql.jdbc.Driver , 8 版本的:
com.mysql.cj.jdbc.Driver ) :
复制代码
jdbc.driver=com.mysql.cj.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/hrsys?characterEncoding=utf-8
jdbc.username=
jdbc.password=
springmvc.xml 文件主要是配置了扫描 @Controller 的类和视图解析,配置如下:
复制代码
xmlns="http://www.springframework.org/schema/beans" xmlns:p="http://www.springframework.org/schema/p" 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/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd"> class="org.springframework.web.servlet.view.InternalResourceViewResolver"> value="org.springframework.web.servlet.view.JstlView" />
applicationContext.xml 文件是 Spring 的配置文件,配置 DI 所需要搜索的包和读取数据库的连接信息, 配置数据库连接池的数据源和集成MyBatis 并对 SqlSession 进行管理,对事务做声明式配置。
复制代码
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd "> destroy-method="close"> class="org.mybatis.spring.SqlSessionFactoryBean"> class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> pointcut="execution(* com.study.service.impl.*.*(..))" />
2.3 MyBatis**(持久层)**
2.3.1 DAO****层设计
参考entity 设计数据库并创建数据库和数据库表:
Employee 实体设计:
java
复制代码
private Integer id;
private Integer number;
private String name;
private String gender;
private Integer age;
private Dep dep;
Department 实体设计代码:
java
复制代码
private Integer id;
private String name;
private Integer numb;
EployeeDao 代码清单:
java
复制代码
public List
public Emp searchById(int id);
public int add(Emp emp);
public int update(Emp emp);
public int delete(int id);
public int updateByDep(int depId);
EployeeDao.xml核心代码清单:
java
复制代码
select e.*,d.name as depName from
employee as e left join department as d on
e.dep_id=d.id
where 1=1
and e.number=#{number}
and e.name like '%${name}%'
and e.gender=#{gender}
and e.age=#{age}
and e.dep_id=#{dep.id}
order by e.id
select e.*,d.name as depName,d.number as depNumber from
employee as e left join department as d on e.dep_id=d.id where
e.id=#{id}
insert into employee
(number,name,gender,age,dep_id)values(#{number},#{name},#{gender},#
{age},#{dep.id})
update employee set
number=#{number},name=#{name},gender=#{gender},age=#
{age},dep_id=#{dep.id} where
id=#{id}
delete from employee where id=#{id}
update employee set
dep_id=null where
dep_id=#{depId}
DepartmentDao 代码清单:
java
复制代码
public List
public Dep searchById(Integer id);
public int add(Dep dep);
public int update(Dep dep);
public int delete(int id);
DepartmentDao.xml 核心代码清单:
java
复制代码
select * from department
select * from department where
id=#{id}
insert into department
(number,name)values(#{number},#{name})
update department set number=#{number},
name=#{name} where id=#{id}
delete from department where id=#{id}
剩下内容请看SSM框架(二)
SSM架构(二)-CSDN博客