SpringBoot集成Dubbo、Redis、MyBatis、Spring、SpringMVC、JSP
思路
- 接口工程:
存放实体Bean(序列化)和业务接口
- 服务提供者:
它是一个SpringBoot框架web项目,集成MyBatis、Redis
2.1 添加依赖:Mybatis依赖,Dubbo依赖,zookeeper依赖,Redis依赖,接口工程
2.2配置SpringBoot核心配置文件:2.2.1 配置连接数据库
2.2.2 配置连接Redis
2.2.3 配置Dubbo
- 服务消费者:
它是一个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;
}
}
业务接口
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();
}
2. 服务提供者
2.1 添加依赖:Mybatis依赖,Dubbo依赖,zookeeper依赖,Redis依赖,接口工程
pom.xml
<?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>
<groupId>com.guo.springboot</groupId>
<artifactId>springboot-dubbo-ssm-provider</artifactId>
<version>0.0.1-SNAPSHOT</version>
<properties>
<java.version>1.8</java.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<spring-boot.version>2.3.7.RELEASE</spring-boot.version>
</properties>
<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>
<version>2.0.0</version>
</dependency>
<dependency>
<groupId>com.101tec</groupId>
<artifactId>zkclient</artifactId>
<version>0.10</version>
</dependency>
<!--MybatisPuls集成SpringBoot起步依赖-->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.5.1</version>
</dependency>
<!--MySQL驱动-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<!--SpringBoot集成Redis起步依赖-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<!--接口工程-->
<dependency>
<groupId>com.guo</groupId>
<artifactId>01-springboot-dubbo-ssm-interface</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>${spring-boot.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<resources>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.xml</include>
</includes>
</resource>
</resources>
<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>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>2.3.7.RELEASE</version>
<configuration>
<mainClass>com.guo.springboot.Application</mainClass>
</configuration>
<executions>
<execution>
<id>repackage</id>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
2.2配置SpringBoot核心配置文件:
2.2.1 配置连接数据库
2.2.2 配置连接Redis
2.2.3 配置Dubbo
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没有配置密码这里就不需要配置
数据库表映射文件
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> {
}
业务接口实现类
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;
}
}
服务提供者的核心启动类
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);
}
}
使用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页面的依赖,接口工程
<?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>
<groupId>com.guo.springboot</groupId>
<artifactId>springboot-dubbo-ssm-consumer</artifactId>
<version>0.0.1-SNAPSHOT</version>
<properties>
<java.version>1.8</java.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<spring-boot.version>2.3.7.RELEASE</spring-boot.version>
</properties>
<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>
<version>2.0.0</version>
</dependency>
<!--zookeeper注册中心-->
<dependency>
<groupId>com.101tec</groupId>
<artifactId>zkclient</artifactId>
<version>0.10</version>
</dependency>
<!--接口工程-->
<dependency>
<groupId>com.guo</groupId>
<artifactId>01-springboot-dubbo-ssm-interface</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<!--SpringBoot集成JSP,添加解析JSP页面的依赖-->
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>${spring-boot.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<resources>
<resource>
<directory>src/main/webapp</directory>
<targetPath>META-INF/resources</targetPath>
<includes>
<include>*.*</include>
</includes>
</resource>
</resources>
<plugins>
<!--SpringBoot项目编译打包插件-->
<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>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>2.3.7.RELEASE</version>
<configuration>
<mainClass>com.guo.springboot.Application</mainClass>
</configuration>
<executions>
<execution>
<id>repackage</id>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
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
服务消费者控制层
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;
}
}
服务消费者核心启动类
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);
}
}
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>
4.结果展示