• Spring MVC 框架学习(八)---- SSM 框架整合


    Spring MVC 框架学习(八)---- SSM 框架整合


    一、准备工作导入依赖


    导入 spring mybatis mvc 等依赖,同时过滤 xml文件和 properties 文件


    
    <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.0modelVersion>
    
        <groupId>org.examplegroupId>
        <artifactId>2022-6-29artifactId>
        <version>1.0-SNAPSHOTversion>
    
    <dependencies>
    
        
        <dependency>
            <groupId>org.projectlombokgroupId>
            <artifactId>lombok-maven-pluginartifactId>
            <version>1.18.12.0version>
            <scope>providedscope>
        dependency>
    
    
            
            <dependency>
                <groupId>org.springframeworkgroupId>
                <artifactId>spring-webmvcartifactId>
                <version>5.3.18version>
            dependency>
    
    
            <dependency>
                <groupId>junitgroupId>
                <artifactId>junitartifactId>
                <version>4.12version>
                <scope>testscope>
            dependency>
    
            
            <dependency>
                <groupId>mysqlgroupId>
                <artifactId>mysql-connector-javaartifactId>
                <version>8.0.16version>
            dependency>
    
        
        <dependency>
            <groupId>org.aspectjgroupId>
            <artifactId>aspectjweaverartifactId>
            <version>1.9.9.1version>
        dependency>
    
    
        
        <dependency>
            <groupId>org.mybatisgroupId>
            <artifactId>mybatisartifactId>
            <version>3.5.6version>
        dependency>
    
        
        <dependency>
            <groupId>org.mybatisgroupId>
            <artifactId>mybatis-springartifactId>
            <version>2.0.7version>
        dependency>
    
        
        <dependency>
            <groupId>org.springframeworkgroupId>
            <artifactId>spring-jdbcartifactId>
            <version>5.3.21version>
        dependency>
    
        
        <dependency>
            <groupId>org.springframeworkgroupId>
            <artifactId>spring-txartifactId>
            <version>5.3.21version>
        dependency>
    
           
        <dependency>
            <groupId>javax.servletgroupId>
            <artifactId>servlet-apiartifactId>
            <version>2.5version>
        dependency>
    
    
        
        <dependency>
            <groupId>com.fasterxml.jackson.coregroupId>
            <artifactId>jackson-databindartifactId>
            <version>2.9.3version>
        dependency>
       
        <dependency>
            <groupId>com.fasterxml.jackson.coregroupId>
            <artifactId>jackson-coreartifactId>
            <version>2.9.3version>
        dependency>
     
        <dependency>
            <groupId>com.fasterxml.jackson.coregroupId>
            <artifactId>jackson-annotationsartifactId>
            <version>2.9.3version>
        dependency>
    
    
    dependencies>
    
        
          
        
        <build>
            <resources>
                <resource>
                    <directory>src/main/resourcesdirectory>
                    <includes>
                        <include>**/*.xmlinclude>
                        <include>**/*.propertiesinclude>
                    includes>
                    <filtering>truefiltering>
                resource>
    
                <resource>
                    <directory>src/main/javadirectory>
                    <includes>
                        <include>**/*.xmlinclude>
                        <include>**/*.propertiesinclude>
                    includes>
                    <filtering>truefiltering>
                resource>
            resources>
        build>
    
    project>
    
    • 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
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
    • 117
    • 118
    • 119
    • 120
    • 121
    • 122
    • 123
    • 124
    • 125
    • 126
    • 127
    • 128
    • 129
    • 130
    • 131
    • 132
    • 133
    • 134
    • 135

    总体配置文件架构

    在这里插入图片描述


    建表语句,测试 mybatis 是否成功连接

    create database if not exists ssm;
    
    use ssm;
    
    drop table if exists users;
    
    create table users(
          id int primary key auto_increment,
          username varchar(20),
          password varchar(20)
    );
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12

    总的applicationContext.xml 文件

    
    <beans xmlns="http://www.springframework.org/schema/beans"
           xmlns:context="http://www.springframework.org/schema/context"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xmlns:aop="http://www.springframework.org/schema/aop" xmlns:cache="http://www.springframework.org/schema/cache"
           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/aop
            https://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache.xsd">
    
    
    <import resource="classpath:spring-dao.xml"/>
    
    
    
    
    
        <context:component-scan base-package="com.*"/>
    
    
    
        <aop:aspectj-autoproxy/>
    
    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

    二、Spring 整合 Mybatis


    db.properties

    jdbc.driverClassName=com.mysql.cj.jdbc.Driver
    jdbc.url=jdbc:mysql://127.0.0.1:3306/ssm?useUnicode=true&characterEncoding=utf-8&serverTimezone=GMT
    jdbc.username=root
    jdbc.password=123456
    
    • 1
    • 2
    • 3
    • 4

    mybatis-config.xml,这个文件可以完全省略,但是还是留下方便做一些其他的配置

    
    DOCTYPE configuration
            PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
            "http://mybatis.org/dtd/mybatis-3-config.dtd">
    <configuration>
    
        
         
        
        <typeAliases>
            <package name="com.bit.pojo"/>
        typeAliases>
    
    configuration>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14

    spring-dao.xml

    
    <beans xmlns="http://www.springframework.org/schema/beans"
           xmlns:context="http://www.springframework.org/schema/context"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xmlns:aop="http://www.springframework.org/schema/aop"
           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/aop
            https://www.springframework.org/schema/aop/spring-aop.xsd">
    
        
    
        <context:property-placeholder location="classpath:db.properties" />
    
        
        <bean class="org.springframework.jdbc.datasource.DriverManagerDataSource" id="dataSource">
            <property name="driverClassName" value="${jdbc.driverClassName}"/>
            <property name="username" value="${jdbc.username}"/>
            <property name="url" value="${jdbc.url}"/>
            <property name="password" value="${jdbc.password}"/>
        bean>
    
        
        <bean class="org.mybatis.spring.SqlSessionFactoryBean" id="sqlSessionFactory">
            <property name="dataSource" ref="dataSource"/>
            <property name="configLocation" value="classpath:mybatis-config.xml"/>
            <property name="mapperLocations" value="classpath:com/bit/mapper/UserMapper.xml"/>
        bean>
    
    
       <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
    
           <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
           <property name="basePackage" value="com.bit.mapper"/>
    
    
       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
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42

    建一个pojo包,创建一个实体类

    package com.bit.pojo;
    
    import lombok.AllArgsConstructor;
    import lombok.Data;
    import lombok.NoArgsConstructor;
    
    @Data
    @AllArgsConstructor
    @NoArgsConstructor
    public class User {
        private int id;
        private String username;
        private String password;
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15

    建一个mapper 包,创建一个mapper接口以及 mapper.xml 文件


    UserMapper 接口

    package com.bit.mapper;
    
    import com.bit.pojo.User;
    import org.apache.ibatis.annotations.Param;
    import java.util.List;
    
    public interface UserMapper {
    
        int insert(User user);
    
        int delete(@Param("id") int id);
    
        int update(@Param("id") int id,User user);
    
        List<User>  selectAll();
    
        User selectById(int id);
    
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19

    UserMapper.xml 文件

    
    DOCTYPE mapper
            PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
            "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
    
    <mapper namespace="com.bit.mapper.UserMapper">
         <insert id="insert" parameterType="user">
             insert into users values(#{id},#{username},#{password})
         insert>
    
        <delete id="delete">
            delete from users where id=#{id}
        delete>
    
        <update id="update" parameterType="user">
            update  users set username=#{username},password=#{password} where id=#{id}
        update>
    
        <select id="selectAll" resultType="user" >
            select * from users;
        select>
    
        <select id="selectById" resultType="user" >
            select * from users where id=#{id};
        select>
    
    mapper>
    
    • 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

    在service 层创建接口,对dao层进行封装

    创建一个service包,写一个UserService接口 和 UserServiceImpl 实现类


    UserService 接口

    package com.bit.service;
    
    import com.bit.pojo.User;
    
    import java.util.List;
    
    public interface UserService {
    
        int  addUser(User user);
    
        int  delteteUser(int id);
    
        int  updateUser(int id,User user);
    
        List<User> selectAllUser();
    
        User selectOne(int id);
    
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20

    UserServiceImpl 实现类

    package com.bit.service;
    
    import com.bit.mapper.UserMapper;
    import com.bit.pojo.User;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Service;
    
    import java.util.List;
    
    @Service
    public class UserServiceImpl implements UserService{
    
        @Autowired 
        private UserMapper userMapper;
    
        public int addUser(User user) {
            return userMapper.insert(user);
        }
    
        public int delteteUser(int id) {
            return userMapper.delete(id);
        }
    
        public int updateUser(int id, User user) {
            return userMapper.update(id,user);
        }
    
        public List<User> selectAllUser() {
            return userMapper.selectAll();
        }
    
        public User selectOne(int id) {
            return userMapper.selectById(id);
        }
    
        public static void main(String[] args) {
    
        }
    }
    
    
    • 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

    测试是否整合mybatis成功的时候一定要看一下,是否spring的配置文件都加载上了

    在这里插入图片描述


    测试是否成功整合代码

    import com.bit.pojo.User;
    import com.bit.service.UserService;
    import org.junit.Test;
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    
    public class Main {
    
        @Test
        public void test(){
            ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
            UserService userService = context.getBean("userServiceImpl", UserService.class);
            User user = userService.selectOne(5);
            System.out.println(user);
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16

    测试成功

    在这里插入图片描述


    三、Spring 整合 Spring MVC


    web.xml,配置 DispatcherServlet ,同时加载application.xml 总的spring 配置文件

    
    <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">
    
        <servlet>
            <servlet-name>springmvcservlet-name>
            <servlet-class>org.springframework.web.servlet.DispatcherServletservlet-class>
            <init-param>
                <param-name>contextConfigLocationparam-name>
                <param-value>classpath:applicationContext.xmlparam-value>
            init-param>
            <load-on-startup>1load-on-startup>
    
        servlet>
    
        <servlet-mapping>
            <servlet-name>springmvcservlet-name>
            <url-pattern>/url-pattern>
        servlet-mapping>
    
    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

    spring-mvc.xml

    
    <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
            http://www.springframework.org/schema/mvc/spring-mvc.xsd">
    
    
    
        
    
    
    
    
    
        
        
    
    
    
    
    
        
        <context:component-scan base-package="com.bit.controller"/>
    
       
        <mvc:default-servlet-handler/>
    
        
        <mvc:annotation-driven/>
    
    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

    spring-service

    
    <beans xmlns="http://www.springframework.org/schema/beans"
           xmlns:context="http://www.springframework.org/schema/context"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           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.xsd
            http://www.springframework.org/schema/context
            https://www.springframework.org/schema/context/spring-context.xsd
            http://www.springframework.org/schema/aop
            https://www.springframework.org/schema/aop/spring-aop.xsd
        http://www.springframework.org/schema/tx
        https://www.springframework.org/schema/tx/spring-tx.xsd">
    
    
    
        
           <context:component-scan base-package="com.bit.service"/>
    
    
        <bean class="org.springframework.jdbc.datasource.DataSourceTransactionManager" id="transactionManager">
            <property name="dataSource" ref="dataSource"/>
        bean>
    
    
         <aop:aspectj-autoproxy/>
    
    
    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

    在总的applicationContext 导入 spring-mvc.xml 、spring-service.xml 文件

    
        <import resource="classpath:spring-mvc.xml"/>
    
        <import resource="classpath:spring-service.xml"/>
    
    • 1
    • 2
    • 3
    • 4

    测试mvc框架是否整合完成的代码


    创建controller包,创建UserController类

    package com.bit.controller;
    
    import com.bit.service.UserService;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Controller;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.ResponseBody;
    
    @Controller
    @RequestMapping("/user")
    public class UserController {
    
        @Autowired
        private UserService userService;
    
        @RequestMapping("/select")
        @ResponseBody
        public Object select(){
            return userService.selectAllUser();
        }
        
    }
    
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24

    运行项目,访问接口出现JSON格式的数据,整合mvc 成功


    在这里插入图片描述


    最后所有的整合三个框架的工作完成。


    如果有静态资源文件(css、html、js),那么放在web目录下 的 创建的static 文件夹下面。

  • 相关阅读:
    Java本地远程断点调试(实战记录)
    React环境搭建
    土豆便宜了吗?python可视化显示价格数据
    工控机通过Profinet转Modbus RTU网关连接变频器与电机通讯案例
    【GUI开发案例】用python爬百度搜索结果,并开发成exe桌面软件!
    keyboard库,keyboard.press(”a“)没有效果的可能原因
    技术分享 | Selenium多浏览器处理
    【R语言简介】
    SpringBoot JWT
    LeetCode算法心得——和可被 K 整除的子数组(前缀和+HashMap)
  • 原文地址:https://blog.csdn.net/rain67/article/details/125532409