一:在Project中创建四个Module分别是:
1.springboot-parent(maven的父工程)(普通maven项目,只留下pom.xml文件)
目录结构如图:
2.springboot-dubbo-ssm-interface(接口工程)(普通maven项目,创建的时候让其父工程是 springboot-parent,这样在其pom.xml文件中会显示出来)
3.springboot-dubbo-ssm-provider(提供者)(springboot-web)
4.springboot-dubbo-ssm-consumer(消费者)(springboot-web-thymeleaf)
二:分别修改对应的pom.xml文件:
1.springboot-parent工程的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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.5.3</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.zsz.springboot</groupId>
<artifactId>050-springboot-parent</artifactId>
<version>1.0.0</version>
<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.0.0</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.springboot-dubbo-ssm-interface工程的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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>050-springboot-parent</artifactId>
<groupId>com.zsz.springboot</groupId>
<version>1.0.0</version>
<relativePath>../050-springboot-parent/pom.xml</relativePath>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>051-springboot-dubbo-ssm-interface</artifactId>
</project>
3.springboot-dubbo-ssm-provider工程的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>
<parent>
<artifactId>050-springboot-parent</artifactId>
<groupId>com.zsz.springboot</groupId>
<version>1.0.0</version>
<relativePath>../050-springboot-parent/pom.xml</relativePath>
</parent>
<artifactId>052-springboot-dubbo-ssm-provider</artifactId>
<name>052-springboot-dubbo-ssm-provider</name>
<description>Demo project for Spring Boot</description>
<!--
dubbo,zookeeper,mybatis,mysql,redis,接口工程
-->
<dependencies>
<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.zsz.springboot</groupId>
<artifactId>051-springboot-dubbo-ssm-interface</artifactId>
<version>1.0.0</version>
</dependency>
</dependencies>
<build>
<!--扫描mapper.xml文件-->
<resources>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.xml</include>
</includes>
</resource>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>**/*.*</include>
</includes>
</resource>
</resources>
<plugins>
<!--mybatis 代码自动生成插件-->
<plugin>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-maven-plugin</artifactId>
<version>1.3.5</version>
<configuration>
<!--配置文件的位置-->
<configurationFile>GeneratorMapper.xml</configurationFile>
<verbose>true</verbose>
<overwrite>true</overwrite>
</configuration>
</plugin>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
4.springboot-dubbo-ssm-consumer工程的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>
<parent>
<artifactId>050-springboot-parent</artifactId>
<groupId>com.zsz.springboot</groupId>
<version>1.0.0</version>
<relativePath>../050-springboot-parent/pom.xml</relativePath>
</parent>
<artifactId>053-springboot-dubbo-ssm-consumer</artifactId>
<name>053-springboot-dubbo-ssm-consumer</name>
<description>Demo project for Spring Boot</description>
<!--
dubbo,zookeeper,thymeleaf,接口工程
-->
<dependencies>
<!--web-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--thymeleaf-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<!--dubbo-->
<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.zsz.springboot</groupId>
<artifactId>051-springboot-dubbo-ssm-interface</artifactId>
<version>1.0.0</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
三.provider和consumer的核心配置文件的修改
1.springboot-dubbo-ssm-provider工程的核心配置文件application.properties。
#设置内嵌Tomcat端口号
server.port=8081
#设置上下文根
server.servlet.context-path=/
#设置连接mysql数据库的信息
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://192.168.1.184:3306/springboot?useUnicode=true&characterEncoding=UTF-8&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=GMT%2B8
spring.datasource.username=root
spring.datasource.password=123
#设置dubbo的配置
spring.application.name=052-springboot-dubbo-ssm-provider
#设置当前工程为服务提供者
spring.dubbo.server=true
#指定注册中心
spring.dubbo.registry=zookeeper://192.168.1.184:2181
#设置redis服务
spring.redis.host=192.168.1.184
spring.redis.port=6379
#spring.redis.password=这里我们本就没设置redis的密码,所以这里不需要写
2.springboot-dubbo-ssm-consumer工程的核心配置文件application.properties。
#设置内嵌tomcat的端口号
server.port=8080
#设置上下文根
server.servlet.context-path=/
#设置dubbo
spring.application.name=053-springboot-dubbo-ssm-consumer
spring.dubbo.registry=zookeeper://192.168.1.184: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
四:mybatis(generator):GeneratorMapper.xml
(文件放在provider项目下)这个文件会改就行。
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE generatorConfiguration
PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN"
"http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd">
<generatorConfiguration>
<!-- 指定连接数据库的 JDBC 驱动包所在位置,指定到你本机的完整路径 -->
<classPathEntry location="D:\mysql-connector-java-5.1.23.jar"/>
<!-- 配置 table 表信息内容体,targetRuntime 指定采用 MyBatis3 的版本 -->
<context id="tables" targetRuntime="MyBatis3">
<!-- 抑制生成注释,由于生成的注释都是英文的,可以不让它生成 -->
<commentGenerator>
<property name="suppressAllComments" value="true"/>
</commentGenerator>
<!-- 配置数据库连接信息 -->
<jdbcConnection driverClass="com.mysql.jdbc.Driver"
connectionURL="jdbc:mysql://192.168.1.184:3306/springboot"
userId="root"
password="123">
</jdbcConnection>
<!-- 生成 model 类,targetPackage 指定 model 类的包名, targetProject 指定
生成的 model 放在 eclipse 的哪个工程下面-->
<javaModelGenerator targetPackage="com.zsz.springboot.model"
targetProject="G:\springboot-course\051-springboot-dubbo-ssm-interface\src\main\java">
<property name="enableSubPackages" value="false"/>
<property name="trimStrings" value="false"/>
</javaModelGenerator>
<!-- 生成 MyBatis 的 Mapper.xml 文件,targetPackage 指定 mapper.xml 文件的
包名, targetProject 指定生成的 mapper.xml 放在 eclipse 的哪个工程下面 -->
<sqlMapGenerator targetPackage="com.zsz.springboot.mapper"
targetProject="src/main/java">
<property name="enableSubPackages" value="false"/>
</sqlMapGenerator>
<!-- 生成 MyBatis 的 Mapper 接口类文件,targetPackage 指定 Mapper 接口类的包
名, targetProject 指定生成的 Mapper 接口放在 eclipse 的哪个工程下面 -->
<javaClientGenerator type="XMLMAPPER"
targetPackage="com.zsz.springboot.mapper"
targetProject="src/main/java">
<property name="enableSubPackages" value="false"/>
</javaClientGenerator>
<!-- 数据库表名及对应的 Java 模型类名 -->
<table tableName="t_student" domainObjectName="Student"
enableCountByExample="false"
enableUpdateByExample="false"
enableDeleteByExample="false"
enableSelectByExample="false"
selectByExampleQueryId="false"/>
</context>
</generatorConfiguration>
五.项目结构及代码(generator生成的model类和mapper(mapper.xml)包中的代码就不粘了)
1.parent
2.interface工程
service包中StudentService类:
package com.zsz.springboot.service;
import com.zsz.springboot.model.Student;
public interface StudentService {
//根据学生id查询详细信息
Student queryStudentById(Integer id);
Integer queryStudentCount();
}
3.provider工程
service.impl包中的StudentServiceImpl类
package com.zsz.springboot.service.impl;
import com.alibaba.dubbo.config.annotation.Service;
import com.zsz.springboot.mapper.StudentMapper;
import com.zsz.springboot.model.Student;
import com.zsz.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
@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) {
Student student = studentMapper.selectByPrimaryKey(id);
return student;
}
@Autowired
private RedisTemplate<Object, Object> redisTemplate;
@Override
public Integer queryStudentCount() {
//提升系统性能,用户体验提升
//(这时为了提高系统的效率)首先去redis缓存中查找,如果有:直接使用,如果没有:去数据库查询并存放到redis缓存中
Integer studentCount = (Integer) redisTemplate.opsForValue().get("studentCount");
//判断redis中是否有值
if (null == studentCount) {
//去数据库查询
studentCount = studentMapper.queryStudentCount();
//并存放在redis缓存中
redisTemplate.opsForValue().set("studentCount",studentCount,30, TimeUnit.SECONDS);
}
return studentCount;
}
}
Application类
package com.zsz.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.zsz.springboot.mapper") //扫描mapper包
@EnableDubboConfiguration //开启dubbo配置
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
4.consumer工程
web包中StudentController类
package com.zsz.springboot.web;
import com.alibaba.dubbo.config.annotation.Reference;
import com.zsz.springboot.model.Student;
import com.zsz.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(value = "/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(value = "/count/detail/count")
@ResponseBody
public Object studentCount() {
//查询所有的学生人数
Integer count = studentService.queryStudentCount();
return "学生总人数为:"+count;
}
}
Application类
package com.zsz.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);
}
}
templates包中studentDetail.html文件
<!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.age}"></span><br/>
<!--学生总人数:<sapn th:text="${conut}"></sapn>-->
</body>
</html>