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核心配置文件
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> <classPathEntry location="D:\mysql-connector-java-5.1.38.jar"/> <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>
<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>
<sqlMapGenerator targetPackage="com.bjpowernode.springboot.mapper" targetProject="src/main/java"> <property name="enableSubPackages" value="false"/> </sqlMapGenerator>
<javaClientGenerator type="XMLMAPPER" targetPackage="com.bjpowernode.springboot.mapper" targetProject="src/main/java"> <property name="enableSubPackages" value="false"/> </javaClientGenerator>
<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
|
<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> <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
| <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>
<dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>2.0.0</version> </dependency>
<dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> </dependency>
<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
| 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
spring.application.name=024-springboot-dubbo-ssm-provider
spring.dubbo.server=true
spring.dubbo.registry=zookeeper://localhost:2181
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;
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
| <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>
<dependency> <groupId>com.bjpowernode.springboot</groupId> <artifactId>023-springboot-dubbo-ssm-interface</artifactId> <version>1.0.0</version> </dependency>
<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
| server.port=8080 server.servlet.context-path=/
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;
@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 {
Student queryStudentById(Integer id);
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() {
Integer allStudentCount = (Integer) redisTemplate.opsForValue().get("allStudentCount");
if (null == allStudentCount) {
allStudentCount = studentMapper.selectAllStudentCount();
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框架
同时构建,并启动三个项目: