• 手把手教你SSM整合(包教包会)


    SSM整合

    步骤(七步)

    1. 新建maven项目,添加依赖

    可以选择根据模版创建maven项目
    在这里插入图片描述

    <dependencies>
        
        <dependency>
          <groupId>org.springframeworkgroupId>
          <artifactId>spring-webmvcartifactId>
          <version>5.2.15.RELEASEversion>
        dependency>
     
        
        <dependency>
          <groupId>org.mybatisgroupId>
          <artifactId>mybatisartifactId>
          <version>3.5.6version>
        dependency>
     
        
        <dependency>
          <groupId>org.mybatisgroupId>
          <artifactId>mybatis-springartifactId>
          <version>2.0.6version>
        dependency>
     
        
        <dependency>
          <groupId>mysqlgroupId>
          <artifactId>mysql-connector-javaartifactId>
          <version>8.0.28version>
        dependency>
     
        
        <dependency>
          <groupId>com.alibabagroupId>
          <artifactId>druidartifactId>
          <version>1.2.1version>
        dependency>
     
        
        <dependency>
          <groupId>org.projectlombokgroupId>
          <artifactId>lombokartifactId>
          <version>1.18.24version>
        dependency>
        
        <dependency>
          <groupId>com.fasterxml.jackson.coregroupId>
          <artifactId>jackson-databindartifactId>
          <version>2.13.2.2version>
        dependency>
        
        <dependency>
          <groupId>javax.servletgroupId>
          <artifactId>javax.servlet-apiartifactId>
          <version>4.0.1version>
        dependency>
     
        <dependency>
          <groupId>org.springframeworkgroupId>
          <artifactId>spring-jdbcartifactId>
          <version>5.2.15.RELEASEversion>
        dependency>
        
        <dependency>
          <groupId>org.mybatis.generatorgroupId>
          <artifactId>mybatis-generator-coreartifactId>
          <version>1.4.0version>
        dependency>
          
        <dependency>
          <groupId>log4jgroupId>
          <artifactId>log4jartifactId>
          <version>1.2.17version>
        dependency>
        <dependency>
          <groupId>org.slf4jgroupId>
          <artifactId>slf4j-log4j12artifactId>
          <version>1.7.30version>
        dependency>
      dependencies>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    1. 将web.xml文件进行替换
      因为根据模版生成的web.xml文件版本过低,所以需要进行替换,可以复制张贴高版本的web.xml文件代码,也可以在项目管理中移除原来的web文件,生成新的web文件(但是一定要注意,如果选择生成的话,需要修改名称,等生成之后,再把名字修改回来

    高版本的web文件代码

    
    <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">
     
     
    web-app>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    1. 配置spring.xml文件
    
    <beans xmlns="http://www.springframework.org/schema/beans"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
        xmlns:aop="http://www.springframework.org/schema/aop"
        xmlns:p="http://www.springframework.org/schema/p" 
        xmlns:tx="http://www.springframework.org/schema/tx"
        xmlns:context="http://www.springframework.org/schema/context"
        xsi:schemaLocation="  
                http://www.springframework.org/schema/beans 
                http://www.springframework.org/schema/beans/spring-beans-2.5.xsd  
                http://www.springframework.org/schema/aop 
                http://www.springframework.org/schema/aop/spring-aop-2.5.xsd  
                http://www.springframework.org/schema/tx 
                http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
                http://www.springframework.org/schema/context
                http://www.springframework.org/schema/context/spring-context.xsd">
        
        <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
            destroy-method="close" scope="singleton">
            <property name="driverClassName" value="com.mysql.cj.jdbc.Driver" />
            <property name="url"
                value="jdbc:mysql://localhost:3306/ssm?characterEncoding=utf8" />
            <property name="username" value="root" />
            <property name="password" value="1128" />
            <property name="maxTotal" value="30" />
            <property name="maxIdle" value="10" />
            <property name="initialSize" value="5" />
        bean>
    
        
        <bean id="txManager"class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
            <property name="dataSource" ref="dataSource" />
        bean>
        
        <tx:annotation-driven transaction-manager="txManager" />
        
        <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
            <property name="dataSource" ref="dataSource" />
            
            <property name="configLocation" value="classpath:mybatis-config.xml" />
        bean>
        
        <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
            
            <property name="basePackage" value="com.dao" />
            <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" />
        bean>
        
        <context:component-scan base-package="com.ssm.service" />
    beans>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    1. 配置springmvc.xml文件
    
    <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:p="http://www.springframework.org/schema/p" 
        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
            http://www.springframework.org/schema/mvc
            http://www.springframework.org/schema/mvc/spring-mvc.xsd">
        
        <context:component-scan base-package="com.ssm.controller" />
        
        
        <bean
            class="org.springframework.web.servlet.view.InternalResourceViewResolver">
            <property name="prefix" value="/WEB-INF/jsp/" />
            <property name="suffix" value=".jsp" />
        bean>
    beans>   
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    1. 配置mybatis核心配置文件
     
    DOCTYPE configuration  PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
            "http://mybatis.org/dtd/mybatis-3-config.dtd">
    <configuration>
        <mappers>
            <mapper resource="com/ssm/dao/UserMapper.xml" />
        mappers>
    configuration> 
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    1. 配置web.xml文件
    
    <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xmlns="http://java.sun.com/xml/ns/javaee"
        xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
        xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
        version="3.0">
        <display-name>springMVCdisplay-name>
        <welcome-file-list>
            <welcome-file>index.jspwelcome-file>
        welcome-file-list>
        
        <context-param>
            
            <param-name>contextConfigLocationparam-name>
            <param-value>classpath:applicationContext-*.xmlparam-value>
        context-param>
        <filter>
            <filter-name>encodingFilterfilter-name>
            <filter-class>
                org.springframework.web.filter.CharacterEncodingFilter
            filter-class>
            <init-param>
                <param-name>encodingparam-name>
                <param-value>UTF-8param-value>
            init-param>
            <init-param>
                <param-name>forceEncodingparam-name>
                <param-value>trueparam-value>
            init-param>
        filter>
        <filter-mapping>
            <filter-name>encodingFilterfilter-name>
            <url-pattern>/*url-pattern>
        filter-mapping>
        <servlet>
            <servlet-name>springservlet-name>
            <servlet-class>org.springframework.web.servlet.DispatcherServletservlet-class>
            <init-param>
                <param-name>contextConfigLocationparam-name>
                <param-value>classpath:springmvc.xmlparam-value>
            init-param>
            <load-on-startup>1load-on-startup>
        servlet>
        <servlet-mapping>
            <servlet-name>springservlet-name>
            <url-pattern>/url-pattern>
        servlet-mapping>
        <listener>
            <listener-class>
                org.springframework.web.context.ContextLoaderListener
            listener-class>
        listener>
        <listener>
            <listener-class>
                org.springframework.web.util.Log4jConfigListener
            listener-class>
        listener>
    web-app>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58

    以上就是SSM配置的整合,配置完成后只需要进行相应的接口以及类的开发即可。

    1. 创建index.jsp页面
    <%@ page language="java" contentType="text/html; charset=UTF-8"
        pageEncoding="UTF-8"%>
    DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>Insert title heretitle>
    head>
    <body>
        <form action="${pageContext.request.contextPath }/select" method="post">
            输入用户名:<input type="text" name="uname"/><br>
            <input type="submit" value="提交"/>
        form>
    body>
    html>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    1. 创建实体类
    package com.ssm.pojo;
    public class MyUser {
        private Integer uid;
        private String uname;
        private String usex;
    
        public Integer getUid() {
            return uid;
        }
    
        public void setUid(Integer uid) {
            this.uid = uid;
        }
    
        public String getUname() {
            return uname;
        }
    
        public void setUname(String uname) {
            this.uname = uname;
        }
    
        public String getUsex() {
            return usex;
        }
    
        public void setUsex(String usex) {
            this.usex = usex;
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    1. 创建dao层

    dao的接口

    package com.ssm.dao;
    import java.util.List;
    import org.springframework.stereotype.Repository;
    import com.ssm.pojo.MyUser;
    
    @Repository("userDao")
    /*
    * 使用Spring自动扫描MyBatis的接口并装配 (Spring将指定包中所有被@Mapper注解标注的接口自动装配为Mybatis的映射接口
    */
    public interface UserDao {
        /**
         * 接口方法对应SQL映射文件UserMapper.xml中的id
         */
        public List<MyUser> selectUserByUname(MyUser user);
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    1. 创建service层
      service层接口
    package com.ssm.service;
    import java.util.List;
    import com.ssm.pojo.MyUser;
    public interface UserService {
        public List<MyUser> selectUserByUname(MyUser user);
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    接口实现类

    package com.ssm.service;
    
    import java.util.List;
    
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Service;
    import org.springframework.transaction.annotation.Transactional;
    
    import com.ssm.dao.UserDao;
    import com.ssm.pojo.MyUser;
    
    @Service("userService")
    @Transactional
    /**
    * 加上注解@Transactional可以指定这个类需要受Spring的事务管理,
    *注意@Transactional只能针对public属性范围内的方法添加,
    *本案例并不需要处理事务,在这里只是告诉读者如何使用事务
    */
    public class UserServiceImpl implements UserService {
        @Autowired
        private UserDao userDao;
    
        @Override
        public List<MyUser> selectUserByUname(MyUser user) {
            return userDao.selectUserByUname(user);
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    1. 创建controller层

    Controller类

    package com.ssm.controller;
    import java.util.List;
    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 com.ssm.dao.UserDao;
    import com.ssm.pojo.MyUser;
    
    @Controller
    public class UserController {
        @Autowired
        private UserDao userDao;
    
        @RequestMapping("/select")
        public String select(MyUser user, Model model) {
            List<MyUser> list = userDao.selectUserByUname(user);
            model.addAttribute("userList", list);
            return "userList";
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    1. 数据显示层 userList.jsp
    <%@ page language="java" contentType="text/html; charset=UTF-8"
        pageEncoding="UTF-8"%>
    <%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
    DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>Insert title heretitle>
    head>
    <body>
        用户信息
        <br>
        <c:forEach items="${userList }" var="user">
        ${user.uid }    
        ${user.uname }    
        ${user.usex }<br>
        c:forEach>
    body>
    html>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    1. 配置tomcat
    2. 发布并运行
  • 相关阅读:
    报表常用功能—插入公式以及父子格
    来自云仓酒庄品牌雷盛红酒分享为什么高海拔的酒价格更高?
    python使用django的详细过程
    时隔4个月我面试字节又挂了|总结与展望
    大数据时代,数据治理
    蓝桥云课--1024 第 2 场算法双周赛
    【开源】基于SpringBoot的农村物流配送系统的设计和实现
    Linux—磁盘管理
    java计算机毕业设计共享汽车管理系统源码+mysql数据库+系统+LW文档+部署
    AI教程 | 用Midjourney制作AI模特和换装的保姆级教程
  • 原文地址:https://blog.csdn.net/CXYCMY/article/details/128180596