• SpringBoot集成Dubbo、Redis、MyBatis、Spring、SpringMVC、JSP


    SpringBoot集成Dubbo、Redis、MyBatis、Spring、SpringMVC、JSP

    思路

    1. 接口工程:

    存放实体Bean(序列化)和业务接口

    1. 服务提供者:

    它是一个SpringBoot框架web项目,集成MyBatis、Redis
    2.1 添加依赖:Mybatis依赖,Dubbo依赖,zookeeper依赖,Redis依赖,接口工程
    2.2配置SpringBoot核心配置文件:

    2.2.1 配置连接数据库
    2.2.2 配置连接Redis
    2.2.3 配置Dubbo

    1. 服务消费者:

    它是一个SpringBoot框架web项目,集成 JSP,集成Dubbo
    3.1 添加依赖:Dubbo依赖,Zookeeper依赖,解析JSP页面的依赖,接口工程
    3.2 配置SpringBoot核心配置文件

    3.2.1 配置视图解析器
    3.2.3 配置Dubbo

    1. 接口工程

    存放实体Bean(序列化)和业务接口

    创建实体还是直接用MybatisPlus,,,拿来就用接口直接集成封装好的用什么逆向工程生成实体Bean接口,映射文件,DAO接口,,,拿来即用(Idea可以自动生成数据库表对应的实体Bean),,,码农这辈子都不可能的
    实体记得序列化 :public class Student implements Serializable

    Mybatis-Plus入门 :https://blog.csdn.net/qq_45896330/article/details/123247828
    在这里插入图片描述

    实体Bean (序列化)

    com/guo/springboot/model/Student.java

    package com.guo.springboot.model;
    import java.io.Serializable;
    public class Student implements Serializable {
      private long id;
      private String name;
      private String sex;
      private long age;
      public Student() {
      }
      public Student(long id, String name, String sex, long age) {
        this.id = id;
        this.name = name;
        this.sex = sex;
        this.age = age;
      }
      public long getId() {
        return id;
      }
      public void setId(long id) {
        this.id = id;
      }
      public String getName() {
        return name;
      }
      public void setName(String name) {
        this.name = name;
      }
      public String getSex() {
        return sex;
      }
      public void setSex(String sex) {
        this.sex = sex;
      }
      public long getAge() {
        return age;
      }
      public void setAge(long age) {
        this.age = age;
      }
    }
    
    • 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

    业务接口

    com/guo/springboot/service/StudentService.java

    package com.guo.springboot.service;
    import com.guo.springboot.model.Student;
    public interface StudentService {
        /**
         * 根据用户id查询用户信息
         */
        Student queryUserById(int id);
        /**
         * 查看学生人数
         * @return
         */
        int queryAllStudentCount();
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13

    2. 服务提供者

    2.1 添加依赖:Mybatis依赖,Dubbo依赖,zookeeper依赖,Redis依赖,接口工程

    pom.xml

    
    <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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
        <modelVersion>4.0.0modelVersion>
        <groupId>com.guo.springbootgroupId>
        <artifactId>springboot-dubbo-ssm-providerartifactId>
        <version>0.0.1-SNAPSHOTversion>
        <properties>
            <java.version>1.8java.version>
            <project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
            <project.reporting.outputEncoding>UTF-8project.reporting.outputEncoding>
            <spring-boot.version>2.3.7.RELEASEspring-boot.version>
        properties>
    
        <dependencies>
            
            <dependency>
                <groupId>org.springframework.bootgroupId>
                <artifactId>spring-boot-starter-webartifactId>
            dependency>
            
            <dependency>
                <groupId>com.alibaba.spring.bootgroupId>
                <artifactId>dubbo-spring-boot-starterartifactId>
                <version>2.0.0version>
            dependency>
            <dependency>
                <groupId>com.101tecgroupId>
                <artifactId>zkclientartifactId>
                <version>0.10version>
            dependency>
            
            <dependency>
                <groupId>com.baomidougroupId>
                <artifactId>mybatis-plus-boot-starterartifactId>
                <version>3.5.1version>
            dependency>
            
            <dependency>
                <groupId>mysqlgroupId>
                <artifactId>mysql-connector-javaartifactId>
            dependency>
            
            <dependency>
                <groupId>org.springframework.bootgroupId>
                <artifactId>spring-boot-starter-data-redisartifactId>
            dependency>
            
            <dependency>
                <groupId>com.guogroupId>
                <artifactId>01-springboot-dubbo-ssm-interfaceartifactId>
                <version>1.0-SNAPSHOTversion>
            dependency>
        dependencies>
    
        <dependencyManagement>
            <dependencies>
                <dependency>
                    <groupId>org.springframework.bootgroupId>
                    <artifactId>spring-boot-dependenciesartifactId>
                    <version>${spring-boot.version}version>
                    <type>pomtype>
                    <scope>importscope>
                dependency>
            dependencies>
        dependencyManagement>
        <build>
            <resources>
                <resource>
                    <directory>src/main/javadirectory>
                    <includes>
                        <include>**/*.xmlinclude>
                    includes>
                resource>
            resources>
            <plugins>
                <plugin>
                    <groupId>org.apache.maven.pluginsgroupId>
                    <artifactId>maven-compiler-pluginartifactId>
                    <version>3.8.1version>
                    <configuration>
                        <source>1.8source>
                        <target>1.8target>
                        <encoding>UTF-8encoding>
                    configuration>
                plugin>
                <plugin>
                    <groupId>org.springframework.bootgroupId>
                    <artifactId>spring-boot-maven-pluginartifactId>
                    <version>2.3.7.RELEASEversion>
                    <configuration>
                       <mainClass>com.guo.springboot.ApplicationmainClass>
                    configuration>
                    <executions>
                        <execution>
                            <id>repackageid>
                            <goals>
                                <goal>repackagegoal>
                            goals>
                        execution>
                    executions>
                plugin>
            plugins>
        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

    2.2配置SpringBoot核心配置文件:

    2.2.1 配置连接数据库
    2.2.2 配置连接Redis
    2.2.3 配置Dubbo
    
    • 1
    • 2
    • 3

    src/main/resources/application.properties

    #配置内嵌Tomcat端口号
    server.port=8081
    #设置上下文根
    server.servlet.context-path=/
    
    #连接数据库
    spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
    spring.datasource.url=jdbc:mysql://localhost:3306/study?useUnicode=true&characterEncoding=UTF-8&serverTimezone=GMT%2B8
    spring.datasource.username=root
    spring.datasource.password=123456
    spring.datasource.type=com.zaxxer.hikari.HikariDataSource
    
    #设置dubbo配置文件
    spring.application.name=02-springboot-dubbo-ssm-provider
    #声明当前工程为服务提供者
    spring.dubbo.server=true
    #设置注册中心
    spring.dubbo.registry=zookeeper://192.168.0.128:2181
    
    #设置Redis的配置
    spring.redis.host=192.168.0.128
    spring.redis.port=6379
    #spring.redis.password=123456  我的redis没有配置密码这里就不需要配置
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23

    数据库表映射文件

    src/main/java/com/guo/springboot/mapper/StudentMapper.java

    package com.guo.springboot.mapper;
    import com.baomidou.mybatisplus.core.mapper.BaseMapper;
    import com.guo.springboot.model.Student;
    public interface StudentMapper extends BaseMapper<Student> {
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5

    业务接口实现类

    src/main/java/com/guo/springboot/serviceImpl/StudentServiceImpl.java

    package com.guo.springboot.serviceImpl;
    import com.alibaba.dubbo.config.annotation.Service;
    import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
    import com.guo.springboot.mapper.StudentMapper;
    import com.guo.springboot.model.Student;
    import com.guo.springboot.service.StudentService;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.data.redis.core.RedisTemplate;
    import org.springframework.stereotype.Component;
    import java.util.concurrent.TimeUnit;
    
    @Component   //类交给Spring容器进行管理
    @Service(interfaceName = "com.guo.springboot.service.StudentService", version = "1.0.0",timeout = 15000)
    public class StudentServiceImpl implements StudentService {
        @Autowired
        private StudentMapper studentMapper;
        @Autowired
        private RedisTemplate<Object,Object> redisTemplate;
        /**
         * 根据Id 查询学生信息
         */
        @Override
        public Student queryUserById(int id) {
            return studentMapper.selectById(id);
        }
        /**
         * 获取学生总人数
         * @return
         */
        @Override
        public int queryAllStudentCount() {
            //提升系统性能,提升用户体验
            //先去缓存中查找,,,如果有就取缓存中的,,如果没有再去数据库中取值放到redis缓存中
            Integer num = (Integer)redisTemplate.opsForValue().get("allStudentCount");
            if (null == num){
                QueryWrapper<Student> queryWrapper=new QueryWrapper();
                Long aLong = studentMapper.selectCount(queryWrapper);
                num = Math.toIntExact(aLong);
                redisTemplate.opsForValue().set("allStudentCount",num,10, TimeUnit.MINUTES);  //过期时间10分钟
            }
            return num;
        }
    }
    
    • 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

    服务提供者的核心启动类

    package com.guo.springboot;
    import com.alibaba.dubbo.spring.boot.annotation.EnableDubboConfiguration;
    import org.mybatis.spring.annotation.MapperScan;
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    @SpringBootApplication
    @MapperScan(basePackages = "com.guo.springboot.mapper")
    @EnableDubboConfiguration  //开启Dubbo配置
    public class Application {
        public static void main(String[] args) {
            SpringApplication.run(Application.class, args);
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13

    使用Mybatis逆向工程插件:生成实体Bean、映射文件和DAO接口

    SpringBoot集成MyBatis——逆向工程生成实体Bean、映射文件、DAO接口:https://blog.csdn.net/qq_45896330/article/details/124610855

    3. 服务消费者

    它是一个SpringBoot框架web项目,集成 JSP,集成Dubbo

    3.1 添加依赖:Dubbo依赖,Zookeeper依赖,解析JSP页面的依赖,接口工程

    
    <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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
        <modelVersion>4.0.0modelVersion>
        <groupId>com.guo.springbootgroupId>
        <artifactId>springboot-dubbo-ssm-consumerartifactId>
        <version>0.0.1-SNAPSHOTversion>
        <properties>
            <java.version>1.8java.version>
            <project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
            <project.reporting.outputEncoding>UTF-8project.reporting.outputEncoding>
            <spring-boot.version>2.3.7.RELEASEspring-boot.version>
        properties>
        <dependencies>
            
            <dependency>
                <groupId>org.springframework.bootgroupId>
                <artifactId>spring-boot-starter-webartifactId>
            dependency>
            
            <dependency>
                <groupId>com.alibaba.spring.bootgroupId>
                <artifactId>dubbo-spring-boot-starterartifactId>
                <version>2.0.0version>
            dependency>
            
            <dependency>
                <groupId>com.101tecgroupId>
                <artifactId>zkclientartifactId>
                <version>0.10version>
            dependency>
            
            <dependency>
                <groupId>com.guogroupId>
                <artifactId>01-springboot-dubbo-ssm-interfaceartifactId>
                <version>1.0-SNAPSHOTversion>
            dependency>
            
            <dependency>
                <groupId>org.apache.tomcat.embedgroupId>
                <artifactId>tomcat-embed-jasperartifactId>
            dependency>
        dependencies>
        <dependencyManagement>
            <dependencies>
                <dependency>
                    <groupId>org.springframework.bootgroupId>
                    <artifactId>spring-boot-dependenciesartifactId>
                    <version>${spring-boot.version}version>
                    <type>pomtype>
                    <scope>importscope>
                dependency>
            dependencies>
        dependencyManagement>
        <build>
            <resources>
                <resource>
                    <directory>src/main/webappdirectory>
                    <targetPath>META-INF/resourcestargetPath>
                    <includes>
                        <include>*.*include>
                    includes>
                resource>
            resources>
            <plugins>
                
                <plugin>
                    <groupId>org.apache.maven.pluginsgroupId>
                    <artifactId>maven-compiler-pluginartifactId>
                    <version>3.8.1version>
                    <configuration>
                        <source>1.8source>
                        <target>1.8target>
                        <encoding>UTF-8encoding>
                    configuration>
                plugin>
                <plugin>
                    <groupId>org.springframework.bootgroupId>
                    <artifactId>spring-boot-maven-pluginartifactId>
                    <version>2.3.7.RELEASEversion>
                    <configuration>
                        <mainClass>com.guo.springboot.ApplicationmainClass>
                    configuration>
                    <executions>
                        <execution>
                            <id>repackageid>
                            <goals>
                                <goal>repackagegoal>
                            goals>
                        execution>
                    executions>
                plugin>
            plugins>
        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

    3.2 配置SpringBoot核心配置文件

    3.2.1 配置视图解析器
    3.2.3 配置Dubbo

    #设置内嵌Tomcat端口号
    server.port=8080
    #设置上下文根
    server.servlet.context-path=/
    
    #设置Dubbo配置
    spring.application.name=03-springboot-dubbo-ssm-comsumer
    #注册中心
    spring.dubbo.registry=zookeeper://192.168.0.128:2181
    
    #配置视图解析器
    spring.mvc.view.prefix=/
    spring.mvc.view.suffix=.jsp
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13

    服务消费者控制层

    src/main/java/com/guo/springboot/controller/StudentController.java

    package com.guo.springboot.controller;
    import com.alibaba.dubbo.config.annotation.Reference;
    import com.guo.springboot.model.Student;
    import com.guo.springboot.service.StudentService;
    import org.springframework.stereotype.Controller;
    import org.springframework.ui.Model;
    import org.springframework.web.bind.annotation.GetMapping;
    import org.springframework.web.bind.annotation.PathVariable;
    import org.springframework.web.bind.annotation.ResponseBody;
    @Controller
    public class StudentController {
        @Reference(interfaceName = "com.guo.springboot.service.StudentService",version = "1.0.0", check = false)
        private StudentService studentService;
    
        /**
         * 根据Id查询学生信息
         * @param model
         * @param id
         * @return
         */
        @GetMapping("/student/{id}")
        public String userDetail(Model model , @PathVariable("id") int id){
            Student student = studentService.queryUserById(id);
            model.addAttribute("student",student);
            return "userDetail";
        }
        /**
         * 查询学生总人数
         * @return
         */
        @GetMapping("/student/count")
        public @ResponseBody Object userCount(){
            int i = studentService.queryAllStudentCount();
            return "学生总人数为:" + i;
        }
    }
    
    • 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

    服务消费者核心启动类

    src/main/java/com/guo/springboot/Application.java

    package com.guo.springboot;
    import com.alibaba.dubbo.spring.boot.annotation.EnableDubboConfiguration;
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    @SpringBootApplication
    @EnableDubboConfiguration  //开启Dubbo配置
    public class Application {
        public static void main(String[] args) {
            SpringApplication.run(Application.class, args);
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    jsp页面

    src/main/webapp/userDetail.jsp

    <%--
      Created by IntelliJ IDEA.
      User: abc
      Date: 2022/7/24
      Time: 16:11
      To change this template use File | Settings | File Templates.
    --%>
    <%@ page contentType="text/html;charset=UTF-8" language="java" %>
    <html>
    <head>
        <title>用户详情title>
    head>
    <body>
        <h1>用户详情h1>
        <div>用户ID:${student.id}div>
        <div>学生姓名:${student.name}div>
        <div>学生年龄:${student.age}div>
        <div>学生性别:${student.sex}div>
    body>
    html>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20

    4.结果展示

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

  • 相关阅读:
    MySQL 通过存储过程高效插入100w条数据
    期权定价模型系列【6】:欧式期权、百慕大期权、美式期权的定价模型与对比【蒙特卡洛模拟、二叉树模型】
    kubernetes组件 Controller manager深刻认知
    2022牛客多校#4 C. Easy Counting Problem
    vue路由传参刷新丢失
    前端生态系统:构建现代Web应用的完整指南
    机器学习和数据挖掘02-Gaussian Naive Bayes
    springboot使用Mybatis中兼容多数据源的databaseId(databaseIdProvider)的简单使用方法
    游戏企业通关秘籍:华为云游戏全场景能力,开发+部署+运营“关关难过关关过”...
    怎样把英语视频字幕翻译成中文
  • 原文地址:https://blog.csdn.net/qq_45896330/article/details/125958110