SSM框架(一)

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 配置如下:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
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">
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
<!--Spring 容器的启动入口ContextLoaderListener-->
<listener>
<listenerclass>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!--由Spring MVC处理的URL 路径-->
<servlet>
<servlet-name>SpringMVC</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servletclass>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:springmvc.xml</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>SpringMVC</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<!--配置一个Spring MVC提供的防止中文乱码的过滤器。-->
<filter>
<filter-name>characterEncodingFilter</filter-name>
<filterclass>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
<init-param>
<param-name>forceEncoding</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>characterEncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
</web-app>

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 的类和视图解析,配置如下:

<?xml version="1.0" encoding="UTF-8"?>
<beans default-lazy-init="true"
       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">
    <!-- 扫描带注解的包下的类,注册Bean-->
    <context:component-scan base-package="com.study.controller" />
    <!-- 通过注解,把URL映射到Controller上,该标签默认注册
    DefaultAnnotationHandlerMapping和AnnotationMethodHandlerAdapter -->
    <mvc:annotation-driven />
    <!-- 视图解析器 -->
    <bean id="viewResolver"
          class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="viewClass"
                  value="org.springframework.web.servlet.view.JstlView" />
        <property name="prefix" value="/WEB-INF/" />
        <property name="suffix" value=".jsp"></property>
    </bean>
</beans>

applicationContext.xml 文件是 Spring 的配置文件,配置 DI 所需要搜索的包和读取数据库的连接信息, 配置数据库连接池的数据源和集成MyBatis 并对 SqlSession 进行管理,对事务做声明式配置。

<beans xmlns="http://www.springframework.org/schema/beans"
       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 ">
  <!-- 加载db.properties-->
  <context:property-placeholder location="classpath:db.properties" />
  <!-- 扫描包,将标注Spring注解的类自动转化对象,同时完成Bean的注入 -->
  <context:component-scan base-package="com.study"/>
  <!-- 启动对@AspectJ注解的支持 -->
  <aop:aspectj-autoproxy></aop:aspectj-autoproxy>
  <!-- 配置数据源 ,使用dbcp数据库连接池 -->
  <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
        destroy-method="close">
    <property name="driverClassName" value="${jdbc.driver}" />
    <property name="url" value="${jdbc.url}" />
    <property name="username" value="${jdbc.username}" />
    <property name="password" value="${jdbc.password}" />
    <property name="maxActive" value="30" />
    <property name="maxIdle" value="20" />
  </bean>
  <!--配置sqlSessionFactory -->
  <bean id="sqlSessionFactory"
        class="org.mybatis.spring.SqlSessionFactoryBean">
    <property name="dataSource" ref="dataSource" />
    <property name="mapperLocations" value="classpath:mapper/*.xml">
    </property>
  </bean>
  <!-- mapper扫描器 -->
  <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
    <property name="basePackage" value="edu.hbfu.hrsys.dao"></property>
    <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" />
  </bean>
  <!-- 事务管理器 对mybatis操作数据库事务控制,spring使用jdbc的事务控制类 -->
  <bean id="transactionManager"
        class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
    <property name="dataSource" ref="dataSource" />
  </bean>
  <!-- 通知 -->
  <tx:advice id="txAdvice" transaction-manager="transactionManager">
    <tx:attributes>
      <!-- 传播行为 -->
      <tx:method name="save*" propagation="REQUIRED" />
      <tx:method name="delete*" propagation="REQUIRED" />
      <tx:method name="add*" propagation="REQUIRED" />
      <tx:method name="modify*" propagation="REQUIRED" />
      <tx:method name="update*" propagation="REQUIRED" />
      <tx:method name="find*" propagation="SUPPORTS" read-only="true" />
      <tx:method name="get*" propagation="SUPPORTS" read-only="true" />
      <tx:method name="select*" propagation="SUPPORTS" read-only="true" />
      <tx:method name="search*" propagation="SUPPORTS" read-only="true" />
    </tx:attributes>
  </tx:advice>
  <!-- aop -->
  <aop:config>
    <aop:advisor advice-ref="txAdvice"
                 pointcut="execution(* com.study.service.impl.*.*(..))"
    />
  </aop:config>
</beans>

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<Emp> search(Emp condition);
    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 复制代码
<mapper namespace="com.study.dao.EmpDao">
    <select id="search" resultMap="EmpAndDep">
        select e.*,d.name as depName from
        employee as e left join department as d on
        e.dep_id=d.id
        where 1=1
        <if test="number!=null">
            and e.number=#{number}
        </if>
        <if test="name!=null and name!=''">
            and e.name like '%${name}%'
        </if>
        <if test="gender!=null and gender!=''">
            and e.gender=#{gender}
        </if>
        <if test="age!=null">
            and e.age=#{age}
        </if>
        <if test="dep!=null and dep.id!=null">
            and e.dep_id=#{dep.id}
        </if>
        order by e.id
    </select>
    <select id="searchById" resultMap="EmpAndDep">
        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}
    </select>
    <resultMap type="com.study.entity.Emp" id="EmpAndDep">
        <id property="id" column="id" />
        <result property="number" column="number" />
        <result property="name" column="name" />
        <result property="gender" column="gender" />
        <result property="age" column="age" />
        <association property="dep" javaType="com.study.entity.Dep">
            <id property="id" column="dep_id" />
            <result property="numb" column="depNumber" />
            <result property="name" column="depName" />
        </association>
    </resultMap>
    <insert id="add">
        insert into employee
            (number,name,gender,age,dep_id)values(#{number},#{name},#{gender},#
            {age},#{dep.id})
    </insert>
    <update id="update">
        update employee set
                            number=#{number},name=#{name},gender=#{gender},age=#
            {age},dep_id=#{dep.id} where
            id=#{id}
    </update>
    <delete id="delete">
        delete from employee where id=#{id}
    </delete>
    <update id="updateByDep">
        update employee set
            dep_id=null where
            dep_id=#{depId}
    </update>
</mapper>

DepartmentDao 代码清单:

java 复制代码
    public List<Dep> search();
    public Dep searchById(Integer id);
    public int add(Dep dep);
    public int update(Dep dep);
    public int delete(int id);

DepartmentDao.xml 核心代码清单:

java 复制代码
<select id="search" resultType="com.study.entity.Dep">
    select * from department
</select>
    <select id="searchById" resultType="com.study.entity.Dep">
        select * from department where
            id=#{id}
    </select>
    <insert id="add">
        insert into department
            (number,name)values(#{number},#{name})
    </insert>
    <update id="update">
        update department set number=#{number},
                              name=#{name} where id=#{id}
    </update>
    <delete id="delete">
        delete from department where id=#{id}
    </delete>

剩下内容请看SSM框架(二)

SSM架构(二)-CSDN博客​​​​​​​

相关推荐
【D'accumulation】18 分钟前
典型的MVC设计模式:使用JSP和JavaBean相结合的方式来动态生成网页内容典型的MVC设计模式
java·设计模式·mvc
试行32 分钟前
Android实现自定义下拉列表绑定数据
android·java
茜茜西西CeCe38 分钟前
移动技术开发:简单计算器界面
java·gitee·安卓·android-studio·移动技术开发·原生安卓开发
救救孩子把43 分钟前
Java基础之IO流
java·开发语言
小菜yh44 分钟前
关于Redis
java·数据库·spring boot·redis·spring·缓存
ggdpzhk1 小时前
Mybatis 快速入门(maven)
oracle·maven·mybatis
宇卿.1 小时前
Java键盘输入语句
java·开发语言
浅念同学1 小时前
算法.图论-并查集上
java·算法·图论
立志成为coding大牛的菜鸟.1 小时前
力扣1143-最长公共子序列(Java详细题解)
java·算法·leetcode
鱼跃鹰飞1 小时前
Leetcode面试经典150题-130.被围绕的区域
java·算法·leetcode·面试·职场和发展·深度优先