ssm整合详解

news/2024/7/7 16:28:38

目录

  • 创建一个 module 项目
  • 导入依赖
  • mybatis
    • mybatis-config.xml 配置文件
  • spring-mybatis 整合
    • spring-mybatis.xml配置文件
    • spring-dao.xml 配置文件
    • spring-service 配置文件
  • spring 整合 springmvc
    • 添加 web 模块
    • web.xml 配置文件
    • spring-mvc配置文件
  • jsp
  • 运行测试
    • 配置服务器
  • 例子
  • 总结

创建一个 module 项目

在这里插入图片描述


导入依赖

思考一下应该导入什么依赖,不要粘,敲一敲
  • junit 测试
  • mysql 数据库连接
  • 数据库连接池(c3p0、druid、dbcp)
  • servlet、jsp(servlet-api、jsp-api、jstl)
  • mybatis(mybatis、mybatis-spring)
  • spring(jdbc)
  • sprinmvc(spring-webmvc)
 <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
        </dependency>

        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>8.0.18</version>
        </dependency>

        <dependency>
            <groupId>com.mchange</groupId>
            <artifactId>c3p0</artifactId>
            <version>0.9.5.4</version>
        </dependency>

        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid</artifactId>
            <version>1.1.18</version>
        </dependency>

        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>servlet-api</artifactId>
            <version>2.5</version>
        </dependency>

        <dependency>
            <groupId>javax.servlet.jsp</groupId>
            <artifactId>jsp-api</artifactId>
            <version>2.2</version>
        </dependency>

        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>jstl</artifactId>
            <version>1.2</version>
        </dependency>

        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis</artifactId>
            <version>3.5.3</version>
        </dependency>

        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis-spring</artifactId>
            <version>2.0.3</version>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-jdbc</artifactId>
            <version>5.2.5.RELEASE</version>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>5.2.5.RELEASE</version>
        </dependency>

		<dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.18.10</version>
        </dependency>
    </dependencies>

mybatis

  • 创建 mybatis-config.xml
  • 创建 service、controller、dao、pojo 包

mybatis-config.xml 配置文件

  • 实体类包扫描 typeAliases
  • xml 文件映射 mappers

在这里插入图片描述

<?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>

    <typeAliases>
        <package name="com.common.pojo"/>
    </typeAliases>

    <mappers>
        <mapper resource="com/common/dao/UserMapper.xml"/>
    </mappers>

</configuration>

spring-mybatis 整合

  • 创建 applicationContext 文件,并将配置好的spring-dao.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" xmlns:context="http://www.springframework.org/schema/util"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/util https://www.springframework.org/schema/util/spring-util.xsd">
    
<!--整合 spring-mybatis、spring-springmvc 文件注册到spirng中-->
	    <import resource="spring-dao.xml"/>

</beans>

spring-mybatis.xml配置文件

  • 创建 dataSource.properties 数据源配置文件

如果你导入的mysql驱动是8.x 版本, 驱动加 cj,url要加时区

jdbc.driver=com.mysql.cj.jdbc.Driver
jdbc.url=mysql:jdbc://localhost:3306/ssm?useUnicode=true&useSSL=true&characterEncoding=utf8&serverTimezone=Asia/Shanghai
jdbc.username=root
jdbc.password=root

spring-dao.xml 配置文件

1.关联数据库配置文件
2.连接池配置
3.sqlSessionFactory对象
4.配置扫描Dao接口包,动态实现Dao接口注入到spring容器中

<?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"
       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">

<!--    1.关联数据库配置文件-->
<context:property-placeholder location="classpath:dataSource.properties"/>

<!--    2.连接池配置-->
    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
        <!--    <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">-->
        <!-- 配置连接池属性 -->
        <property name="driverClass" value="${jdbc.driver}"/>
        <property name="jdbcUrl" value="${jdbc.url}"/>
        <property name="user" value="${jdbc.username}"/>
        <property name="password" value="${jdbc.username}"/>

        <!-- c3p0连接池的私有属性 -->
        <property name="maxPoolSize" value="30" />    <!--最大连接数30-->
        <property name="initialPoolSize" value="10"/> <!--初始化连接数10-->
        <property name="minPoolSize" value="10" />    <!--最小连接数10-->

        <!-- 关闭连接后不自动commit -->
        <property name="autoCommitOnClose" value="false"/>
        <!-- 获取连接超时时间 -->
        <property name="checkoutTimeout" value="10000"/>
        <!-- 当获取连接失败重试次数 -->
        <property name="acquireRetryAttempts" value="2"/>
    </bean>

    <!-- 3.配置SqlSessionFactory对象 -->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <!-- 注入数据库连接池 -->
        <property name="dataSource" ref="dataSource"/>
        <!-- 配置MyBaties全局配置文件:mybatis-config.xml -->
        <property name="configLocation" value="classpath:mybatis-config.xml"/>
    </bean>

<!--    4.配置扫描Dao接口包,动态实现Dao接口注入到spring容器中 -->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <!-- 注入sqlSessionFactory -->
        <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
        <!-- 给出需要扫描Dao接口包 -->
        <property name="basePackage" value="com.common.dao"/>
    </bean>
</beans>

spring-service 配置文件

1.扫描service相关的bean

2.UserServiceImpl(接口实现类)注入到IOC容器中

3.配置事务管理器

配置过程中报 bean 没有注入?

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

<?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"
       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">

    <!--    Spring整合service层-->

    <!-- 扫描service相关的bean -->
    <context:component-scan base-package="com.common.service" />

    <!--UserServiceImpl注入到IOC容器中-->
    <bean id="UserServiceImpl" class="com.common.service.serviceImpl.UserServiceImpl">
        <property name="userMapper" ref="userMapper"/>
    </bean>

    <!-- 配置事务管理器 -->
    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <!-- 注入数据库连接池 -->
        <property name="dataSource" ref="dataSource" />
    </bean>

</beans>

配置完spring-service 注入到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" xmlns:context="http://www.springframework.org/schema/util"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/util https://www.springframework.org/schema/util/spring-util.xsd">

<!--整合 spring-mybatis、spring-springmvc 文件注册到spirng中-->
    <import resource="spring-dao.xml"/>
    <import resource="spring-service.xml"/>

</beans>

spring 整合 springmvc

添加 web 模块

在这里插入图片描述
在这里插入图片描述

web.xml 配置文件

  • 注册中心控制器
  • 乱码处理
  • session超时设置
<?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_4_0.xsd"
         version="4.0">

    <!--    SpringMVC层-->

    <!--DispatcherServlet-->
    <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:applicationContext.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>DispatcherServlet</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>

    <!--    乱码处理-->
    <!--encodingFilter-->
    <filter>
        <filter-name>encodingFilter</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>encodingFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

    <!--Session过期时间-->
    <session-config>
        <session-timeout>15</session-timeout>
    </session-config>

</web-app>

spring-mvc配置文件

  • 1.开启SpringMVC注解驱动
  • 2.静态资源默认servlet配置
  • 3.配置jsp 显示ViewResolver视图解析器
  • 4.扫描web相关的bean
<?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
   http://www.springframework.org/schema/context/spring-context.xsd
   http://www.springframework.org/schema/mvc
   https://www.springframework.org/schema/mvc/spring-mvc.xsd">

    <!-- 配置SpringMVC -->
    <!-- 1.开启SpringMVC注解驱动 -->
    <mvc:annotation-driven />
    <!-- 2.静态资源默认servlet配置-->
    <mvc:default-servlet-handler/>

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

    <!-- 4.扫描web相关的bean -->
    <context:component-scan base-package="com.common.controller" />

</beans>

配置完spring-mvc 注入到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" xmlns:context="http://www.springframework.org/schema/util"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/util https://www.springframework.org/schema/util/spring-util.xsd">

<!--整合 spring-mybatis、spring-springmvc 文件注册到spirng中-->
    <import resource="spring-dao.xml"/>
    <import resource="spring-service.xml"/>
    <import resource="spring-mvc.xml"/>
</beans>

jsp

在这里插入图片描述

index.jsp

<%--
  Created by IntelliJ IDEA.
  User: klz
  Date: 2020/4/20
  Time: 23:00
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
  <title>index</title>
  <style type="text/css">
    a {
      text-decoration: none;
      color: black;
      font-size: 18px;
    }
    h3 {
      width: 180px;
      height: 38px;
      margin: 100px auto;
      text-align: center;
      line-height: 38px;
      background: deepskyblue;
      border-radius: 4px;
    }
  </style>
</head>
<body>
<a href="${pageContext.request.contextPath}/user/allUser">进入用户页面</a>
</body>
</html>

allUser.jsp

<%--
  Created by IntelliJ IDEA.
  User: klz
  Date: 2020/4/20
  Time: 23:12
  To change this template use File | Settings | File Templates.
--%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>用户</title>
    <!-- 引入 Bootstrap -->
    <link href="https://cdn.bootcss.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet">
</head>
<body>
    <div class="container">

        <div class="row clearfix">
            <div class="col-md-12 column">
                <div class="page-header">
                    <h1>
                        <small>所有用户</small>
                    </h1>
                </div>
            </div>
        </div>

        <div class="row clearfix">
            <div class="col-md-12 column">
                <table class="table table-hover table-striped">
                    <thead>
                    <tr>
                        <th>id</th>
                        <th>用户名</th>
                        <th>用户名密码</th>
                        <th>地址</th>
                        <th>操作</th>
                    </tr>
                    </thead>

                    <tbody>
                    <c:forEach var="user" items="${requestScope.get('listUser')}">
                        <tr>
                            <td>${user.getId()}</td>
                            <td>${user.getUserName()}</td>
                            <td>${user.getPassword()}</td>
                            <td>${user.getAddress()}</td>
                            <td>
                                <a href="#">更改</a> |
<%--                                href="${pageContext.request.contextPath}/user/del/${user.getId()}"--%>
                                <a href="#">删除</a>
                            </td>
                        </tr>
                    </c:forEach>
                    </tbody>

                </table>
            </div>
        </div>

    </div>

</body>
</html>

运行测试

配置服务器

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
创建一个lib文件夹,存jar
在这里插入图片描述

在这里插入图片描述
在这里插入图片描述

在这里插入图片描述
运行服务器

例子

链接:https://pan.baidu.com/s/1IJFTl5NqNStedXofNaxddQ
提取码:w48c


总结

相信大家都看了,ssm 是考spring整合的,考的就是配置文件
  • spring-dao.xml 相当于 @Repository
  • spring-service.xml 相当于 @Service
  • spring-mvc.xml 相当于 @Controller
  • applicatonContext.xml 相当于 @Component(将bean注册到spring)、@ComponentScan(报扫描,并且注册到spring中)

在后面我们用 springbot 写项目都是零配置,很快


http://www.niftyadmin.cn/n/1597404.html

相关文章

[功能改进]增强的留言簿功能

留言分为公开留言与私人留言。公开留言任何都可以查看。私人留言只有Blog的所有者才能查看。若要发表私人留言&#xff0c;请在发送时选择“私人留言”。对于公开留言&#xff0c;任何人都可以发表回复&#xff0c;就像随笔那样。在“查看私人留言”页面&#xff0c;看到的是所…

看看MS内部对.NET的使用情况...

看看MS内部对.NET的使用情况 (摘自Dan Fernandezs Blog)In the comments of Scott Hanselmans blog posts on why VB developers dont switch/migrate/convert to VB.NET, someone replied with the following: So what application has Microsoft written totally in .NET th…

XSL学习笔记 转自竹笋炒肉

XSL学习笔记(一) 有人说XSL是CSS的替换者&#xff0c;真的吗&#xff1f; 1、简介  XSL是一种描述样式单的语言&#xff0c;包括XSLT、XPath和XML格式对象三部分。  与HTML不同&#xff0c;XML没有预定义的标签&#xff0c;所以浏览器不知道如何显示XML文档&#xff0c;而X…

jdk8 安装教程(windows版)

目录jdk下载地址jdk 安装jdk 环境变量jdk下载地址 https://www.oracle.com/java/technologies/javase-jdk8-downloads.html 需要注册一个oracle账号&#xff0c;信息一填&#xff0c;随便注册一个就行了!jdk 安装 注意&#xff1a;修改安装目录不要有中文 jdk 环境变量 JAVA_…

UML类图关系大全

http://www.cnblogs.com/riky/archive/2007/04/07/704298.html 1、关联 双向关联&#xff1a; C1-C2&#xff1a;指双方都知道对方的存在&#xff0c;都可以调用对方的公共属性和方法。 在GOF的设计模式书上是这样描述的&#xff1a;虽然在分析阶段这种关系是适用的&#x…

FireFox vs IE?

看到PCOnline上有一篇造反派与保皇党的对话:Firefox vs. IE谁赢&#xff1f;的文章&#xff0c;其实也就是引用一些国外用户的话&#xff0c;文章开头比较有意思&#xff1a;“前几天有报道&#xff0c;在开源浏览器Firefox 1.0刚发布后&#xff0c;微软的高层马上对IE进行辩护…

mysql8.0.19 安装(windows压缩版)

目录mysql 下载企业版和标准版和社区版的区别mysql 压缩版和应用程序版的区别mysql 安装环境变量vc_redist.x64.exemy.ini 配置mysql 初始化、启动服务、修改密码mysql 下载 mysql官网&#xff1a;https://www.mysql.com/ MySQL是一种开放源代码的关系型数据库管理系统&#xf…

SQL的主键和外键作用

SQL的主键和外键 http://www.cnblogs.com/ywb-lv/archive/2012/03/12/2391860.html SQL的主键和外键的作用&#xff1a; 外键取值规则&#xff1a;空值或参照的主键值。 (1)插入非空值时&#xff0c;如果主键表中没有这个值&#xff0c;则不能插入。 (2)更新时&#xff0c;不能…