一、SpringBoot集成SSM框架和Dubbo分布式框架思路
1、介面工程:存放物體類bean和業務介面
2、服務提供者:它是一個SpringBoot框架web專案,集成Mybatis
(1)添加依賴:Mybatis依賴,Mysql驅動依賴,Dubbo依賴,zookeeper依賴,介面工程
(2)配置Springboot核心組態檔
- a、配置連接資料庫
- b、配置dubbo
2、服務消費者:他是一個SpringBoot框架web專案,集成JSP,Dubbo
(1)添加依賴:Dubbo依賴,zookeeper依賴,決議JSP頁面的依賴,介面工程
(2)配置Springboot核心組態檔
- a、配置視圖決議器
- b、配置dubbo
二、集成SSM框架的基本步驟:
1、創建好三個工程專案
-
介面工程(Maven工程)

-
服務提供者工程(SpringBoot Web工程)

-
服務消費者工程(SpringBoot Web工程)

2、在服務提供者專案中創建Mybatis的逆向工程

注意:
這里通過逆向工程創建的物體類物件應該放置在介面工程專案中,這時我們需要修改GeneratorMapper.xml檔案里的配置

怎么樣創建Mybatis的逆向工程和哪些需要注意的地方,可以到個人博客Mybatis章節中查看
- 這里通過逆向工程生成的model包下的物體類Student物件需要進行序列化
public class Student implements Serializable {
private Integer id;
private String name;
private Integer age;
3、整理和修改服務提供者的pom.xml檔案的依賴
添加Mybatis依賴,Mysql驅動依賴,Dubbo依賴,注冊中心zookeeper的依賴,介面工程的依賴
<dependencies>
<!--解決使用@ConfigurationProperties 注解出現警告問題-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
<!--SpringBoot框架web專案起步依賴-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</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>
<!--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>
<!--介面工程-->
<dependency>
<groupId>com.hcz</groupId>
<artifactId>014-springboot-ssm-interface</artifactId>
<version>1.0.0</version>
<scope>compile</scope>
</dependency>
</dependencies>
4、配置服務提供者核心組態檔application.properties
server.port=8081
server.servlet.context-path=/
#設定連接資料庫的配置
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3366/springboot
spring.datasource.username=root
spring.datasource.password=123456
#設定Dubbo的配置
spring.application.name=015-springboot-dubbo-provider
#當前工程是一個服務提供者
spring.dubbo.server=true
#設定注冊中心
spring.dubbo.registry=zookeeper://localhost:2181
5、整理和修改服務消費者的pom.xml檔案的依賴
添加Dubbo依賴,注冊中心zookeeper的依賴,介面工程的依賴,決議JSP頁面的依賴
<dependencies>
<!--解決使用@ConfigurationProperties 注解出現警告問題-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
<!--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>
<!--介面工程-->
<dependency>
<groupId>com.hcz</groupId>
<artifactId>014-springboot-ssm-interface</artifactId>
<version>1.0.0</version>
</dependency>
<!--SpringBoot集成JSP,僅僅只是決議JSP頁面的依賴-->
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
</dependency>
</dependencies>
6、配置服務消費者核心組態檔application.properties
server.port=8080
server.servlet.context-path=/
#設定Dubbo的配置
spring.application.name=016-springboot-dubbo-consumer
#指定注冊中心
spring.dubbo.registry=zookeeper://localhost:2181
#配置視圖決議器
spring.mvc.view.prefix=/
spring.mvc.view.suffix=.jsp
7、在服務消費者專案中創建控制類方法

@Controller
public class StudentController {
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";
}
}
這里引入了業務層介面StudentService,并在介面工程中創建這個介面類
8、在介面工程中創建介面類

public interface StudentService {
//根據學生id來查詢
Student queryStudentById(Integer id);
}
9、在服務提供者專案中實作該介面的實作類

@Component
@Service(interfaceClass = StudentService.class,version = "1.0.0")
public class StudentServiceImpl implements StudentService {
@Autowired
private StudentMapper studentMapper;
@Override
public Student queryStudentById(Integer id) {
return studentMapper.selectByPrimaryKey(id);
}
}
1、將該實作類注入到Spring容器,使用注解@Component而不是@Service注解
2、使用注解@Service將該介面實作類暴露出去(注意區別和以前的注入注解,這里的為@Service(com.alibaba.dubbo.config.annotation))
3、這里使用注解@Autowired通過Dao層呼叫了資料庫里面的資料,所以需要配置注解掃描器,而這里是通過在Application類中配置

10、服務消費者參考暴露出來的介面實作類
使用注解@Reference參考暴露出來的介面實作類

11、在Application類中添加注解來開啟Dubbo配置

12、創建jsp回應頁面

13、在服務消費者專案pom.xml檔案中指定JSP編譯目錄

14、在服務提供者專案pom.xml檔案中配置映射檔案的位置

15、開啟zookeeper服務和注冊中心服務

16、啟動服務提供專案,然后再啟動服務消費者專案

轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/272210.html
標籤:其他
