存放实体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
存放实体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();
}
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>
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
它是一个SpringBoot框架web项目,集成 JSP,集成Dubbo
<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>
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>