SSM整合笔记

SSM之间的联系

SpringMVC+Spring+ MyBatis
在这里插入图片描述
Springmvc是用来控制页面的,Mybatis是用于访问底层数据库的,它们两个在整个Javaweb应用中是都有各自的职责和角色的,但是事实上Spring是没有角色的,它唯一的职责是将Springmvc和Mybatis整合起来统一管理,所以可以把Spring看作整个Javaweb应用的桥梁纽带,它是一个大容器,里面装了Springmvc和MyBatis,SpringMVC属于Spring的一个子模块,所以SSM整合实质上就是MyBatis与Spring的整合。Mybatis是可以脱离javaweb存在的,因为它的作用是将Java程序与数据库连接,但是springMVC是需要在web环境里面运行的,而spring作为管理者也应该在web里面,所以springmvc和spring是要与Javaweb联系的

SSM整合步骤(最基本的)

  1. 创建maven工程,配置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>

  <groupId>com.ssmbuild</groupId>
  <artifactId>ssm01</artifactId>
  <version>1.0-SNAPSHOT</version>
  <packaging>war</packaging>

  <name>ssm01 Maven Webapp</name>
  <!-- FIXME change it to the project's website -->
  <url>http://www.example.com</url>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <maven.compiler.source>1.7</maven.compiler.source>
    <maven.compiler.target>1.7</maven.compiler.target>
  </properties>

  <dependencies>

    <!--Springmvc相关依赖-->
    <!-- 引入springMVC依赖 -->
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-webmvc</artifactId>
      <version>5.2.0.RELEASE</version>
    </dependency>
    <!-- 引入Servlet组件依赖 -->
    <dependency>
      <groupId>javax.servlet</groupId>
      <artifactId>servlet-api</artifactId>
      <version>2.5</version>
    </dependency>
    <!-- 引入jsp核心标签库依赖 如在jsp中用到的<c:forEach 就是这里面的-->
    <dependency>
      <groupId>javax.servlet.jsp</groupId>
      <artifactId>jsp-api</artifactId>
      <version>2.2</version>
    </dependency>
    <!-- 引入EL表达式依赖 -->
    <dependency>
      <groupId>javax.servlet</groupId>
      <artifactId>jstl</artifactId>
      <version>1.2</version>
    </dependency>


    <!-- Spring相关依赖 -->
    <!-- 引入SpringJDBC依赖 用Spring来管理JDBC -->
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-jdbc</artifactId>
      <version>5.2.12.RELEASE</version>
    </dependency>
    <!-- 引入spring AOP依赖 面向切面编程 -->
    <dependency>
      <groupId>org.aspectj</groupId>
      <artifactId>aspectjweaver</artifactId>
      <version>1.9.4</version>
    </dependency>

    <!-- Mybatis相关依赖 -->
    <dependency>
      <groupId>org.mybatis</groupId>
      <artifactId>mybatis</artifactId>
      <version>3.5.2</version>
    </dependency>
    <!-- 引入mysql驱动依赖 -->
    <dependency>
      <groupId>mysql</groupId>
      <artifactId>mysql-connector-java</artifactId>
      <version>8.0.25</version>
    </dependency>
    <!-- 引入spring整合Mybatis的依赖 -->
    <dependency>
      <groupId>org.mybatis</groupId>
      <artifactId>mybatis-spring</artifactId>
      <version>2.0.2</version>
    </dependency>
    <!-- 引入c3p0数据库连接池依赖 -->
    <dependency>
      <groupId>com.mchange</groupId>
      <artifactId>c3p0</artifactId>
      <version>0.9.5.2</version>
    </dependency>



    <!-- 测试依赖 -->
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.11</version>
      <scope>test</scope>
    </dependency>
  </dependencies>

  <build>
    <finalName>ssm01</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>

    <!--这里是把java和resources下的配置文件导入到target/classes中,程序在运行时都是执行target/classes中的文件
    对于MAVEN项目,Eclipse会自动把项目src\main\java\目录下的配置文件(.xml)和资源文件(.properties)搬运到target/classes目录下,
    而intellij idea默认是不会帮我们做这件事的,所以使用IDEA这是必须的操作。-->
    <resources>
      <resource>
        <directory>src/main/java</directory><!--所在的目录-->
        <includes><!--包括目录下的.properties.xml 文件都会扫描到-->
          <include>**/*.properties</include>
          <include>**/*.xml</include>
        </includes>
        <filtering>false</filtering> <!--关闭文件过滤器-->
      </resource>
      <resource>
        <directory>src/main/resources</directory><!--所在的目录-->
        <includes><!--包括目录下的.properties.xml 文件都会扫描到-->
          <include>**/*.properties</include>
          <include>**/*.xml</include>
        </includes>
        <filtering>false</filtering> <!--关闭文件过滤器-->
      </resource>
    </resources>

  </build>
</project>

  1. 在web.xml(web.xml是整个Javaweb程序的启动入口)中配置Spring、SpringMVC、字符编码乱码过滤器、静态资源加载,注意:因为在前面SSM关系中讲过mybatis是Javaweb之外的,也就是mybatis是不需要web环境的,所以在web.xml中没有关于mybatis的配置
<!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会标红 因为以下配置顺序并不符合web-app的配置规则 但是不影响程序的运行 可做更改 -->
<web-app>
  <display-name>Archetype Created Web Application</display-name>

  <!-- 配置Spring 读取Spring的配置文件 -->
  <!-- context-param表示全局配置 -->
  <context-param>
    <param-name>contextConfigLocation</param-name>
    <!-- 这里的classpath对应的就是main/resources Spring要整合其他两个框架 那么它的配置文件就是总的配置文件 这里的ApplicationContext.xml就是各配置文件的集合 -->
    <param-value>classpath:ApplicationContext.xml</param-value>
  </context-param>
  <!--
    Listener监听器
    启动Web容器时即读取web.xml文件时,读取在contextConfigLocation中定义的xml文件(默认的是命名为ApplicationContext.xml的spring配置文件),初始化IOC容器,自动装配
    因为SpringMVC和Mybatis都是要放在Spring的配置文件中的,那么这个监听器的目的就是通过加载Spring配置文件并得到里面的关于Springmvc和Myabtis的配置信息,这一步就说明Spring在mybatis与Springmvc之间起了连接整合的作用
  -->
  <listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>


  <!-- Springmvc的相关配置  -->
  <!-- DispatcherServlet分派器配置 -->
  <servlet>
    <servlet-name>springmvc</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <!-- init-param表示局部配置 自己的配置 -->
    <init-param>
      <param-name>contextConfigLocation</param-name>
      <!--这里要加载SpringMVC的配置文件-->
      <param-value>classpath:springmvc-config.xml</param-value>
    </init-param>
    <!--启动级别为1 表示跟服务器同时启动-->
    <load-on-startup>1</load-on-startup>
  </servlet>
  <!-- 这里就说明了哪些请求是要被DispatcherServlet分配的拦截的 -->
  <!--/ 匹配所有的请求;(不包括.jsp) 常用-->
  <!--/* 匹配所有的请求;(包括.jsp)-->
  <!--这里就表示除.jsp以外的请求都会被DispatcherServlet分配-->
  <servlet-mapping>
    <servlet-name>springmvc</servlet-name>
    <url-pattern>/</url-pattern>
  </servlet-mapping>
  <!--加载静态资源 因为Springmvc默认会拦截所有的请求包括静态资源 如果不配加载静态资源就会导致静态资源加载不出来 所以需要单独配置静态资源 需要加载哪些静态资源就配置哪些后缀-->
  <servlet-mapping>
    <servlet-name>default</servlet-name>
    <url-pattern>*.js</url-pattern>
  </servlet-mapping>
  <servlet-mapping>
    <servlet-name>default</servlet-name>
    <url-pattern>*.css</url-pattern>
  </servlet-mapping>
  <servlet-mapping>
    <servlet-name>default</servlet-name>
    <url-pattern>*.jpg</url-pattern>
  </servlet-mapping>



  <!-- 配置SpringMVC的乱码过滤器 -->
  <filter>
    <filter-name>encoding</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>encoding</filter-name>
    <!-- 这里要写/*,写/的话过滤不了jsp页面,不能解决乱码 -->
    <url-pattern>/*</url-pattern>
  </filter-mapping>
 
</web-app>

  1. 配置spring,在spring中整合mybatis
<?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:jdbc="http://www.springframework.org/schema/jdbc"
       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/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc.xsd">

    <!-- 整合mybatis 就是在spring的配置文件中配置mybatis的配置信息 通过spring IOC 的特性 将这些配置通过bean的注入方式 达到简化开发的效果 -->
    <!--
        思考:mybatis基本的配置有哪些?
            1、数据源(mybatis要操作哪个数据库所对应的数据库信息)
            2、sqlSessionFactory(通过sqlSessionFactory来获取SqlSession对象 这个对象就是mybatis操作数据库的核心对象)
    -->
    <!--绑定数据库配置文件-->
    <context:property-placeholder location="classpath:MySQLdb.properties"/>
    <!-- 配置c3p0数据源(数据库连接池) -->
    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
        <property name="driverClass" value="${driver}"/>
        <property name="jdbcUrl" value="${url}"/>
        <property name="user" value="${user}"/>
        <property name="password" value="${password}"/>
        <!-- 既然是 数据库连接池就得有初始访问量和最大连接数等 这些是对数据源的初始化配置-->
        <property name="initialPoolSize" value="5"></property>
        <property name="maxPoolSize" value="10"></property>
    </bean>

    <!--这里就用到了mybatis与spring整合包下的SqlSessionFactoryBean类-->
    <bean id="SqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <!-- 数据源 -->
        <property name="dataSource" ref="dataSource"/>
        <!-- mybatis配置文件 -->
        <property name="configLocation" value="classpath:mybatis-config.xml"/>
        <!--SqlSession帮助我们自动生成mapper接口对应的代理对象,通过代理对象来实现业务,而SqlSession要想生成mapper接口对应的代理对象就必须知道mapper接口(mapper)和对应接口实现(SQL映射文件)在哪个地方-->
        <!-- 告诉SqlSessionSQL映射文件的位置 -->
        <property name="mapperLocations" value="classpath:com/ssmbuild/mapper/*.xml"/>
    </bean>

    <!--这里就用到了mybatis与spring整合包下的MapperScannerConfigurer类-->
    <!--告诉SqlSession mapper接口的位置 扫描自定义的mapper接口-->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="basePackage" value="com.ssmbuild.mapper"></property>
    </bean>



</beans>

MySQLdb.properties

driver=com.mysql.cj.jdbc.Driver
url=jdbc:mysql://localhost:8080/studentsdb?serverTimezone=GMT&useSSL=true&useUnicode=true&characterEncoding=UTF-8
user=root
password=2524173091
  1. 在mybatis配置文件中配置辅助信息,因为核心的mybatis配置信息已经在上面的整合中配好了
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
        PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-config.dtd">

<configuration>
    <!--使用mybatis默认日志-->
    <settings>
        <setting name="logImpl" value="STDOUT_LOGGING"/>
    </settings>

    <!--起别名 指定包名 mybatis就会自动获取该包下的bean对象-->
    <typeAliases>
        <typeAlias type="com.ssmbuild.entity.Student" alias="Student"/>
    </typeAliases>
</configuration>
  1. 配置Springmvc
<?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/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/mvc
       https://www.springframework.org/schema/mvc/spring-mvc.xsd">

    <!-- 启动mvc注解驱动 只有配置这个后面才能用@RequestMapping()等关于MVC注解-->
    <mvc:annotation-driven></mvc:annotation-driven>

    <!-- 扫描业务代码 告诉springmvc在下面这些地方使用的注解才生效 扫描范围宁可写大一点也不要写小了 这里写的是整个项目包下的 -->
    <context:component-scan base-package="com.ssmbuild"></context:component-scan>

    <!-- 配置DispatcherServlet视图解析器 -->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/jsp/"/>
        <property name="suffix" value=".jsp"/>
    </bean>

</beans>
  1. 在全局配置文件ApplicationContext.xml中导入其他配置文件
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       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">

    <import resource="spring-config.xml"/>
    <import resource="springmvc-config.xml"/>

</beans>
  1. 编写实体类
package com.ssmbuild.entity;

public class Student
{
    private String sno;
    private String sname;
    private String ssex;
    private String birthday;
    private String address;

    public Student()
    {

    }

    public Student(String sno, String sname, String ssex, String birthday, String address)
    {
        this.sno = sno;
        this.sname = sname;
        this.ssex = ssex;
        this.birthday = birthday;
        this.address = address;
    }

    public String getSno()
    {
        return sno;
    }

    public void setSno(String sno)
    {
        this.sno = sno;
    }

    public String getSname()
    {
        return sname;
    }

    public void setSname(String sname)
    {
        this.sname = sname;
    }

    public String getSsex()
    {
        return ssex;
    }

    public void setSsex(String ssex)
    {
        this.ssex = ssex;
    }

    public String getBirthday()
    {
        return birthday;
    }

    public void setBirthday(String birthday)
    {
        this.birthday = birthday;
    }

    public String getAddress()
    {
        return address;
    }

    public void setAddress(String address)
    {
        this.address = address;
    }

    @Override
    public String toString()
    {
        return "Student{" + "sno='" + sno + '\'' + ", sname='" + sname + '\'' + ", ssex='" + ssex + '\'' + ", birthday='" + birthday + '\'' + ", address='" + address + '\'' + '}';
    }
}

  1. 编写实体类对应的mapper层接口
package com.ssmbuild.mapper;

import com.ssmbuild.entity.Student;
import org.springframework.stereotype.Repository;

import java.util.List;
@Repository
public interface StudentMapper
{
    //查询所有学生信息
    List<Student> findAll();
}

  1. 编写对应mapper的SQL映射文件
<?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.ssmbuild.mapper.StudentMapper">
    <select id="findAll" resultType="Student">
        select sno,Sname,Ssex,Birthday,Address from student_info
    </select>
</mapper>
  1. 编写实体类对应的service层接口
package com.ssmbuild.service;

import com.ssmbuild.entity.Student;

import org.springframework.stereotype.Service;

import java.util.List;

@Service
public interface StudentService
{
    //查询所有学生信息
    List<Student> findAll();
}

  1. 编写对应service层接口的实现类
package com.ssmbuild.service;

import com.ssmbuild.entity.Student;
import com.ssmbuild.mapper.StudentMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class StudentServiceImpl implements StudentService
{
    /**
     * 虽然service中的StudentService与mapper中的StudentMapper内容相同,但这并不是代码重复
     * 而是更好的反映三层架构之间的关系 service调用mapper层 controller层调用service层
     */
    //通过spring自动装配StudentMapper
    @Autowired
    private StudentMapper studentMapper;

    @Override
    public List<Student> findAll()
    {
        return studentMapper.findAll();
    }
}

  1. 编写实体类对应的controller
package com.ssmbuild.controller;

import com.ssmbuild.entity.Student;
import com.ssmbuild.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

@Controller
@RequestMapping("/student")
public class StudentController
{
    @Autowired
    private StudentService studentService;

    @RequestMapping("/findAll")
    public String findAll(Model model)
    {
        List<Student> studentList = studentService.findAll();
        model.addAttribute("students",studentList);

        return "student";

    }

}

  1. 编写前端jsp交互页面student.jsp
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<!--
    这里非常重要 不写这个下面就是普通的表达式 就不是对象调方法了
    在page directive中的idELIgnored属性用来指定是否忽略EL表达式 如果忽略即isELIgnored="true" 那么那么JSP中的EL表达式被当成字符串处理
    如果不忽略 那么就是正常的对象调方法了
-->
<%@ page isELIgnored="false" %>

<html>
<body>
<c:forEach items="${students}" var="student">
    <!--student对象中的字段都是私有的 但是这里也是能够直接.字段名获取的-->
    ${student.sno}--${student.sname}--${student.ssex}--${student.birthday}--${student.address}
</c:forEach>
</body>
</html>

到此为止,一个简单基本的ssm框架整合就完成了

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值