Spring Boot 学习笔记 11

0x01 Dubbo-SSM框架

接口工程:
存放实体Bean和业务接口
_服务提供者:_它是一个Springboot框架web项目,集成MyBatis,Redis

  • 添加依赖:Mybatis依赖,Mysql驱动依赖,Dubbo依赖,zookeeper依赖,Redis依赖
  • 配置Springboot核心配置文件
    • 配置连接数据库
    • 配置连接redis数据库dubbo
      _服务消费者:_它是一个Springboot框架web项目,集成JSP,dubbo
  • 添加依赖:Dubbo,zookeeper依赖,解析JSP页面的依赖,接口工程
  • 配置Springboot核心配置文件
    • 配置试图解析器
    • 配置dubbo

0x02 构建Dubbo-SSM框架

First and formost(首先),我们依次创建Maven接口项目工程、Springboot+Maven服务提供者(Provider)项目工程、Springboot+Maven服务消费者(Consumer)项目工程。

In addition(其次),我们在提供者(Provider)项目工程文件下添加一个Mybatis逆向工程的配置文件GeneratorMapper.xml:

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
<?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.38.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.154.128:3306/springboot"
userId="root"
password="123">
</jdbcConnection>
<!-- 生成 model 类,targetPackage 指定 model 类的包名, targetProject 指定
生成的 model 放在 eclipse 的哪个工程下面-->
<javaModelGenerator targetPackage="com.bjpowernode.springboot.model"
targetProject="C:\Users\Administrator\Desktop\04-SpringBoot\code\IDEA-springboot-projectes\023-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.bjpowernode.springboot.mapper"
targetProject="src/main/java">
<property name="enableSubPackages" value="false"/>
</sqlMapGenerator>
<!-- 生成 MyBatis 的 Mapper 接口类文件,targetPackage 指定 Mapper 接口类的包
名, targetProject 指定生成的 Mapper 接口放在 eclipse 的哪个工程下面 -->
<javaClientGenerator type="XMLMAPPER"
targetPackage="com.bjpowernode.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>

注意,这里要改成绝对路径!而且不能出现中文,否则构建项目会出现问题。

1
2
3
4
5
6
7
 <!-- 生成 model 类,targetPackage 指定 model 类的包名, targetProject 指定
生成的 model 放在 eclipse 的哪个工程下面-->
<javaModelGenerator targetPackage="com.bjpowernode.springboot.model"
targetProject="C:\Users\Administrator\Desktop\04-SpringBoot\code\IDEA-springboot-projectes\023-springboot-dubbo-ssm-interface\src\main\java">
<property name="enableSubPackages" value="false"/>
<property name="trimStrings" value="false"/>
</javaModelGenerator>

然后在POM.xml中添加生成插件:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
<plugins>
<!--mybatis 代码自动生成插件-->
<plugin>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-maven-plugin</artifactId>
<version>1.3.6</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>

再添加Redis依赖,接口工程依赖,zookeeper注册中心依赖,Dubbo依赖,Mybatis依赖,Mysql驱动依赖到POM.xml中:

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

<!--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>

<!--MyBatis集成Springboot起步依赖-->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.0.0</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.bjpowernode.springboot</groupId>
<artifactId>023-springboot-dubbo-ssm-interface</artifactId>
<version>1.0.0</version>
</dependency>

配置Provider项目中的核心配置文件:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#配置内嵌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/springboot?useUnicode=true&characterEncoding=UTF-8&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=GMT%2B8
spring.datasource.username=username
spring.datasource.password=password

#设置dubbo配置
spring.application.name=024-springboot-dubbo-ssm-provider
#声明当前工程为服务提供者
spring.dubbo.server=true
#设置注册中心
spring.dubbo.registry=zookeeper://localhost:2181

#设置redis配置
spring.redis.host=127.0.0.1
spring.redis.port=6379
spring.redis.password=


注意:Mybatis生成的逆向文件Model中的java构造类一定要序列化

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
package com.bjpowernode.springboot.model;

import java.io.Serializable;
// implements Serializable序列化
public class Student implements Serializable {
private Integer id;

private String name;

private Integer age;

public Integer getId() {
return id;
}

public void setId(Integer id) {
this.id = id;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public Integer getAge() {
return age;
}

public void setAge(Integer age) {
this.age = age;
}
}

In the second place,在Consumer项目工程文件中添加POM.xml依赖:

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
<!--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.bjpowernode.springboot</groupId>
<artifactId>023-springboot-dubbo-ssm-interface</artifactId>
<version>1.0.0</version>
</dependency>

<!--SpringBoot集成JSP,仅仅只是展示JSP页面需要添加解析jsp页面的依赖-->
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
</dependency>

并且添加JSP文件到编译资源文件中:

1
2
3
4
5
6
7
8
9
<resources>
<resource>
<directory>src/main/webapp</directory>
<targetPath>META-INF/resources</targetPath>
<includes>
<include>*.*</include>
</includes>
</resource>
</resources>

配置Consumer项目工程的核心配置文件:

1
2
3
4
5
6
7
8
9
10
11
12
#设置内嵌Tomcat端口号
server.port=8080
server.servlet.context-path=/

#设置dubbo配置
spring.application.name=025-springboot-dubbo-ssm-consumer
spring.dubbo.registry=zookeeper://127.0.0.1:2181

#配置视图解析器
spring.mvc.view.prefix=/
spring.mvc.view.suffix=.jsp

接下来在Application.java的同级目录或次级目录添加控制层包web,包内创建StudentController.java

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.bjpowernode.springboot.web;

import com.alibaba.dubbo.config.annotation.Reference;
import com.bjpowernode.springboot.model.Student;
import com.bjpowernode.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.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;


@Controller
public class StudentController {

@Reference(interfaceName = "com.bjpowernode.springboot.service.StudentService", version = "1.0.0", check = false)
private StudentService studentService;

//RESTFul风格
@RequestMapping(value = "/student/detail/{id}")
public String studentDetail(Model model,
@PathVariable("id") Integer id) {

Student student = studentService.queryStudentById(id);

model.addAttribute("student", student);

return "studentDetail";
}

@GetMapping(value = "/student/all/count")
public @ResponseBody
Object allStudentCount() {

Integer allStudentCount = studentService.queryAllStudentCount();

return "学生总人数为:" + allStudentCount;
}


}

为了实现这一控制层,我们需要先在接口工程项目文件(Interface)中创建所需要的接口StudentService.java:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
package com.bjpowernode.springboot.service;

import com.bjpowernode.springboot.model.Student;

public interface StudentService {

/**
* 根据学生ID查询详情
* @param id
* @return
*/
Student queryStudentById(Integer id);

/**
* 获取学生总人数
* @return
*/
Integer queryAllStudentCount();

}

然后在服务提供者(Provide)中创建该接口的实现类StudentServiceImpl.java:

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
package com.bjpowernode.springboot.service;

import com.alibaba.dubbo.config.annotation.Service;
import com.bjpowernode.springboot.mapper.StudentMapper;
import com.bjpowernode.springboot.model.Student;
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(interfaceName = "com.bjpowernode.springboot.service.StudentService",version = "1.0.0",timeout = 15000)
public class StudentServiceImpl implements StudentService {

@Autowired
private StudentMapper studentMapper;

@Autowired
private RedisTemplate<Object,Object> redisTemplate;

@Override
public Student queryStudentById(Integer id) {
return studentMapper.selectByPrimaryKey(id);
}

@Override
public Integer queryAllStudentCount() {

//提升系统性能,用户体验提升
//首先去redis缓存中查询,如果有:直接使用,如果没有:去数据库查询并存放到redis缓存中
Integer allStudentCount = (Integer) redisTemplate.opsForValue().get("allStudentCount");

//判断是否有值
if (null == allStudentCount) {

//去数据库查询
allStudentCount = studentMapper.selectAllStudentCount();

//并存放到redis缓存中
redisTemplate.opsForValue().set("allStudentCount",allStudentCount,30, TimeUnit.SECONDS);
}

return allStudentCount;
}
}

Last but not least,在Consumer项目工程文件中,在src/main/中创建webapp包,来存放jsp文件webapp/studentDetail.jsp:

1
2
3
4
5
6
7
8
9
10
11
12
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>学生详情</title>
</head>
<body>
<h3>学生编号:${student.id}</h3>
<h3>学生姓名:${student.name}</h3>
<h3>学生年龄:${student.age}</h3>
</body>
</html>

0x03 运行Dubbo-SSM框架

同时构建,并启动三个项目:

  • Maven接口项目工程
  • Springboot+Maven服务提供者(Provider)项目工程
  • Springboot+Maven服务消费者(Consumer)项目工程
    打开浏览器,输入http://localhost:8080/student/detail/1
    浏览器输出:
    1
    2
    3
    学生编号:1
    学生姓名:222
    学生年龄:25
    Dubbo-SSM框架搭建成功!congratulations!