SpringBoot综合案例——Dubbo-Zookeeper-MyBatis-MySQL-Redis

SpringBoot综合案例

  • SpringBoot综合案例——Dubbo-Zookeeper-MyBatis-MySQL-Redis
    • 1. 父工程(maven java)
      • 1.1 packaging 设置为pom
      • 1.2 删除src文件夹
      • 1.3 指定父工程为SpringBoot工程
    • 2. 接口工程(Maven Java)
      • 2.1 指定父工程
      • 2.1 创建实体(并序列化)和接口
    • 3. 服务提供者 (SpringBoot工程)
      • 3.1 引入依赖
      • 3.2 核心配置文件
      • 3.3 接口实现类
    • 4. 服务消费者 (SpringBoot工程)
      • 4.1 引入依赖
      • 4.2 核心配置文件
      • 4.3 控制层
      • 4.4 studentDetail页面
    • 5. 启动程序
      • 5.1 Linux上启动Zookeeper、Redis服务截图
      • 5.2 通过浏览器访问结果截图
    • 6. 附录—Zookeeper、Redis服务在Linux上启动关闭命令

SpringBoot综合案例——Dubbo-Zookeeper-MyBatis-MySQL-Redis

多模块管理(父工程),接口工程,生产者,消费者

父工程管理,接口工程、生产者、消费者三个工程

使用MyBatis的逆向生成插件生成数据库表对应的实体类、映射文件、DAO接口

1. 父工程(maven java)

1.1 packaging 设置为pom

<packaging>pom</packaging>

1.2 删除src文件夹

SpringBoot综合案例——Dubbo-Zookeeper-MyBatis-MySQL-Redis_第1张图片

1.3 指定父工程为SpringBoot工程

<?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.guo</groupId>
    <artifactId>01-springboot-parent</artifactId>
    <version>1.0-SNAPSHOT</version>
    <modules>
        <module>../02-springboot-dubbo-ssm-interfacce</module>
    </modules>
    <parent>
        <artifactId>spring-boot-starter-parent</artifactId>
        <groupId>org.springframework.boot</groupId>
        <version>2.7.0</version>
        <relativePath/>
    </parent>

    <packaging>pom</packaging>
    <properties>
        <java.version>1.8</java.version>
        <dubbo-spring-boot-starter-version>2.0.0</dubbo-spring-boot-starter-version>
        <zkclient-version>0.10</zkclient-version>
        <mybatis-spring-boot-starter-version>2.2.1</mybatis-spring-boot-starter-version>
    </properties>
    <!--管理SpringBoot父工程没有管理的依赖版本-->

    <dependencyManagement>
        <dependencies>
            <!--Dubbo集成SpringBoot框架起步依赖-->
            <dependency>
                <groupId>com.alibaba.spring.boot</groupId>
                <artifactId>dubbo-spring-boot-starter</artifactId>
                <version>${dubbo-spring-boot-starter-version}</version>
            </dependency>
            <!--zookeeper注册中心-->
            <dependency>
                <groupId>com.101tec</groupId>
                <artifactId>zkclient</artifactId>
                <version>${zkclient-version}</version>
            </dependency>
            <!--MyBatis集成SpringBoot框架起步依赖-->
            <dependency>
                <groupId>org.mybatis.spring.boot</groupId>
                <artifactId>mybatis-spring-boot-starter</artifactId>
                <version>${mybatis-spring-boot-starter-version}</version>
            </dependency>
        </dependencies>
    </dependencyManagement>
</project>

2. 接口工程(Maven Java)

SpringBoot综合案例——Dubbo-Zookeeper-MyBatis-MySQL-Redis_第2张图片

2.1 指定父工程

<?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">
    <parent>
        <artifactId>01-springboot-parent</artifactId>
        <groupId>com.guo</groupId>
        <version>1.0-SNAPSHOT</version>
        <relativePath>../01-springboot-parent/pom.xml</relativePath>
    </parent>
    <modelVersion>4.0.0</modelVersion>
    <artifactId>02-springboot-dubbo-ssm-interfacce</artifactId>
</project>

2.1 创建实体(并序列化)和接口

MyBatis逆向工程 —【解决生成多个实体——解决映射文件重复出现BaseResultMap】

  • 实体通过MyBatis逆向工程插件自动生成。
  • 创建接口

StudentService.java

package com.guo.springboot.service;
import com.guo.springboot.model.Student;
public interface StudentService {
    /**
     * 根据学生ID查看学生详情
     * @param id
     * @return
     */
    Student queryStudentById(Integer id);
}

3. 服务提供者 (SpringBoot工程)

SpringBoot综合案例——Dubbo-Zookeeper-MyBatis-MySQL-Redis_第3张图片

3.1 引入依赖

Dubbo Zookeeper MyBatis MySQL Redis 接口工程

<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <artifactId>01-springboot-parent</artifactId>
        <groupId>com.guo</groupId>
        <version>1.0-SNAPSHOT</version>
        <relativePath>../01-springboot-parent/pom.xml</relativePath>
    </parent>
    <artifactId>springboot-dubbo-ssm-provider</artifactId>

    <!--
        Dubbo Zookeeper MyBatis MySQL Redis 接口工程
    -->
    <dependencies>
        <!--Springboot框架web项目起步依赖-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <!--Dubbo集成SpringBoot框架起步依赖-->
        <dependency>
            <groupId>com.alibaba.spring.boot</groupId>
            <artifactId>dubbo-spring-boot-starter</artifactId>
        </dependency>
        <!--zookeeper注册中心-->
        <dependency>
            <groupId>com.101tec</groupId>
            <artifactId>zkclient</artifactId>
        </dependency>
        <!--MySQL驱动-->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
        </dependency>
        <!--MyBatis集成SpringBoot框架起步依赖-->
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
        </dependency>
        <!--SpringBoot集成Redis起步依赖-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-redis</artifactId>
        </dependency>
        <!--接口工程 , 自己创建的Java工程的依赖版本号不需要由父工程再次管理-->
        <dependency>
            <groupId>com.guo</groupId>
            <artifactId>02-springboot-dubbo-ssm-interfacce</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>
    </dependencies>
    <build>
        <resources>
            <resource>
                <directory>src/main/java</directory>
                <includes>
                    <include>**/*.xml
                
            
            
                src/main/resources
                
                    **/*.*</include>
                </includes>
            </resource>
        </resources>
        <plugins>
            <!--Mybatis代码自动生成插件-->
            <plugin>
                <groupId>org.mybatis.generator</groupId>
                <artifactId>mybatis-generator-maven-plugin</artifactId>
                <version>1.3.3</version>
                <configuration>
                    <!--配置: 文件的位置 和 文件名 !!! 当前的mybatis-generator.xml在根目录下【src同级】!!!-->
                    <configurationFile>mybatis-generator.xml</configurationFile>
                    <verbose>true</verbose>
                    <overwrite>true</overwrite>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.8.1</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                    <encoding>UTF-8</encoding>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

3.2 核心配置文件

application-dev.properties

# 应用服务 WEB 访问端口
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&useSSL=false&serverTimezone=Asia/Shanghai
spring.datasource.username=root
spring.datasource.password=123456

#设置Dubbo配置
spring.application.name=03-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

application.properties

spring.profiles.active=dev

3.3 接口实现类

实现接口工程中接口的方法

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

package com.guo.springboot.service;

import com.alibaba.dubbo.config.annotation.Service;
import com.guo.springboot.mapper.StudentMapper;
import com.guo.springboot.model.Student;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
@Service(interfaceClass = StudentService.class,version = "1.0.0",timeout = 15000)
public class StudentServiceImpl implements StudentService {
    @Autowired
    private StudentMapper studentMapper;
    @Override
    public Student queryStudentById(Integer id) {
        return studentMapper.selectByPrimaryKey(id);
    }
}

4. 服务消费者 (SpringBoot工程)

4.1 引入依赖

<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <artifactId>01-springboot-parent</artifactId>
        <groupId>com.guo</groupId>
        <version>1.0-SNAPSHOT</version>
        <relativePath>../01-springboot-parent/pom.xml</relativePath>
    </parent>
    <artifactId>springboot-dubbo-ssm-consumer</artifactId>

    <!--
        Dubbo Zookeeper Thymeleaf  Redis 接口工程
    -->
    <dependencies>
        <!--SpringBoot框架集成Thymeleaf前端模板引擎起步依赖-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>
        <!--SpringBoot框架web项目起步依赖-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <!--Dubbo集成SpringBoot框架起步依赖-->
        <dependency>
            <groupId>com.alibaba.spring.boot</groupId>
            <artifactId>dubbo-spring-boot-starter</artifactId>
        </dependency>
        <!--zookeeper注册中心-->
        <dependency>
            <groupId>com.101tec</groupId>
            <artifactId>zkclient</artifactId>
        </dependency>
        <!--接口工程-->
        <dependency>
            <groupId>com.guo</groupId>
            <artifactId>02-springboot-dubbo-ssm-interfacce</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>
    </dependencies>
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.8.1</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                    <encoding>UTF-8</encoding>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

4.2 核心配置文件

配置多环境的配置文件通过核心配置文件application.properties指定使用哪个环境配置文件

application-dev.properties

# 设置内嵌tomcat端口号
server.port=8080
#设置上下文根
server.servlet.context-path=/

#设置Dubbo配置
spring.application.name=04-springboot-dubbo-ssm-consumer
#指定注册中心
spring.dubbo.registry=zookeeper://192.168.0.128:2181

#关闭页面缓存
spring.thymeleaf.cache=false
#设置Thymeleaf/后缀
spring.thymeleaf.prefix=classpath:/templates/
spring.thymeleaf.suffix=.html

#设置字符编码
server.servlet.encoding.enabled=true
server.servlet.encoding.force=true
server.servlet.encoding.charset=UTF-8

application.properties

spring.profiles.active=dev

4.3 控制层

URL请求采用RESTful风格

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

package com.guo.springboot.web;
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.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
public class StudentController {
    @Reference(interfaceClass = StudentService.class,version = "1.0.0",check = false    )
    private StudentService studentService;
    @RequestMapping("/student/detail/{id}")
    public String studentDetail(Model model, @PathVariable("id") Integer id){

        //根据学生id查询详情
        Student student = studentService.queryStudentById(id);
        model.addAttribute("student",student);
        return "studentDetail";
    }
    @RequestMapping("/hello")
    public @ResponseBody String Hello(){
        return "Hello";
    }
}

4.4 studentDetail页面

DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>学生详情title>
head>
<body>
学生编号:<span th:text="${student.id}">span><br/>
学生姓名:<span th:text="${student.name}">span><br/>
学生性别:<span th:text="${student.sex}">span><br/>
学生年龄:<span th:text="${student.age}">span><br/>
body>
html>

5. 启动程序

  • 先启动Zookeeper、Redis服务

  • 启动服务提供者和服务消费者

  • 通过浏览器URL访问

5.1 Linux上启动Zookeeper、Redis服务截图

1. Zookeeper服务启动
SpringBoot综合案例——Dubbo-Zookeeper-MyBatis-MySQL-Redis_第4张图片

2. Redis服务启动
SpringBoot综合案例——Dubbo-Zookeeper-MyBatis-MySQL-Redis_第5张图片通过查看进程确认服务正常运行
在这里插入图片描述

5.2 通过浏览器访问结果截图

SpringBoot综合案例——Dubbo-Zookeeper-MyBatis-MySQL-Redis_第6张图片

6. 附录—Zookeeper、Redis服务在Linux上启动关闭命令

注册中心-Zookeeper~Linux下载安装及配置 :https://blog.csdn.net/qq_45896330/article/details/125752574
启动命令:./zkServer.sh start
关闭命令:./zkServer.sh stop

Redis简介-下载安装-基本使用-1:https://blog.csdn.net/qq_45896330/article/details/124649718

启动redis服务时,指定配置文件:redis-server redis.conf &
通过redis- cli命令关闭 redis-cli shutdown

你可能感兴趣的:(Spring,Boot,spring,boot,mybatis,java)