小言_互联网的博客

SpringBoot——借助Maven多模块管理实现集成SSM、Dubbo、Thymeleaf的汇总案例

308人阅读  评论(0)

文章目录:

1.写在前面

1.1 maven父工程(普通Java工程):管理JDK编译级别、子工程需要用到的依赖

1.2 第一个子工程(普通Java工程):对应接口工程

1.3 第二个子工程(SpringBoot工程):对应服务提供者

1.4 第三个子工程(SpringBoot工程):对应服务消费者

2.该案例的所有源码

2.1 maven父工程(普通Java工程):管理JDK编译级别、子工程需要用到的依赖

2.2 第一个子工程(普通Java工程):对应接口工程

2.3 第二个子工程(SpringBoot工程):对应服务提供者

2.4 第三个子工程(SpringBoot工程):对应服务消费者

2.5 启动测试!!!


1.写在前面

这个大综合案例,我使用Maven多模块管理来实现,就像之前创建SpringBoot项目一样,它的pom文件中会有一个父工程,而父工程中又会有一个父工程,在这个父工程(通俗的说爷爷工程)中,就是各种各样需要用到的依赖。

也就是说,这个案例,先是有一个maven父工程,它是一个SpringBoot工程,它来管理其他模块中需要用到的依赖、以及JDK的编译级别。而这里集成了Dubbo,那么肯定就会有接口工程、服务提供者、服务消费者这三个工程,而这三个工程都会是上面提到的maven父工程的三个子工程。

OKOK,说了这么多,下面直接放上这四个maven工程的架构图。

1.1 maven父工程(普通Java工程):管理JDK编译级别、子工程需要用到的依赖

1.2 第一个子工程(普通Java工程):对应接口工程

1.3 第二个子工程(SpringBoot工程):对应服务提供者

1.4 第三个子工程(SpringBoot工程):对应服务消费者


2.该案例的所有源码

2.1 maven父工程(普通Java工程):管理JDK编译级别、子工程需要用到的依赖

在这个maven工程中,什么都没有,只有一个pom文件(maven项目的核心!!!) 。因为我写的这个案例是SpringBoot集成XXX,所有我需要代替之前创建SpringBoot工程的那些功能,在这里指定这个maven工程为一个SpringBoot工程(也就是pom文件中 <parent> 标签中的内容),这样我才可以管理JDK编译级别、子工程需要用到的依赖(也就是pom文件中 <properties>、<dependencyManagement> 标签中的内容)。

而声明该maven工程为父工程需要两个条件:①将 <packaging> 标签的内容设置为pom;②删除该工程的src目录。


  
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3. xsi:schemaLocation= "http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  4. <modelVersion>4.0.0 </modelVersion>
  5. <parent>
  6. <groupId>org.springframework.boot </groupId>
  7. <artifactId>spring-boot-starter-parent </artifactId>
  8. <version>2.5.0 </version>
  9. <relativePath/> <!-- lookup parent from repository -->
  10. </parent>
  11. <groupId>com.szh.springboot </groupId>
  12. <artifactId>017-springboot-parent </artifactId>
  13. <version>1.0.0 </version>
  14. <packaging>pom </packaging>
  15. <properties>
  16. <java.version>11 </java.version>
  17. <dubbo-spring-boot-starter-version>2.0.0 </dubbo-spring-boot-starter-version>
  18. <zkclient-version>0.4 </zkclient-version>
  19. <mybatis-spring-boot-starter-version>2.1.4 </mybatis-spring-boot-starter-version>
  20. </properties>
  21. <!-- 管理SpringBoot父工程没有管理的依赖 -->
  22. <dependencyManagement>
  23. <dependencies>
  24. <!-- Dubbo集成SpringBoot框架的起步依赖 -->
  25. <dependency>
  26. <groupId>com.alibaba.spring.boot </groupId>
  27. <artifactId>dubbo-spring-boot-starter </artifactId>
  28. <version>${dubbo-spring-boot-starter-version} </version>
  29. </dependency>
  30. <!-- zookeeper注册中心 -->
  31. <dependency>
  32. <groupId>org.apache.zookeeper </groupId>
  33. <artifactId>zookeeper </artifactId>
  34. <version>3.4.6 </version>
  35. <exclusions>
  36. <exclusion>
  37. <groupId>org.slf4j </groupId>
  38. <artifactId>slf4j-log4j12 </artifactId>
  39. </exclusion>
  40. </exclusions>
  41. </dependency>
  42. <dependency>
  43. <groupId>com.101tec </groupId>
  44. <artifactId>zkclient </artifactId>
  45. <version>${zkclient-version} </version>
  46. </dependency>
  47. <!-- MyBatis集成SpringBoot框架的起步依赖 -->
  48. <dependency>
  49. <groupId>org.mybatis.spring.boot </groupId>
  50. <artifactId>mybatis-spring-boot-starter </artifactId>
  51. <version>${mybatis-spring-boot-starter-version} </version>
  52. </dependency>
  53. </dependencies>
  54. </dependencyManagement>
  55. </project>

2.2 第一个子工程(普通Java工程):对应接口工程

首先来看它的pom文件,这里有一个 <parent> 标签,那么意思就是说它有一个父工程叫:017-springboot-parent

<relativePath>../017-springboot-parent/pom.xml</relativePath> 的意思是通过相对路径找到父工程的pom文件。

  
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3. xsi:schemaLocation= "http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  4. <modelVersion>4.0.0 </modelVersion>
  5. <parent>
  6. <artifactId>017-springboot-parent </artifactId>
  7. <groupId>com.szh.springboot </groupId>
  8. <version>1.0.0 </version>
  9. <relativePath>../017-springboot-parent/pom.xml </relativePath>
  10. </parent>
  11. <artifactId>018-springboot-dubbo-ssm-interface </artifactId>
  12. <properties>
  13. <project.build.sourceEncoding>UTF-8 </project.build.sourceEncoding>
  14. <maven.compiler.source>11 </maven.compiler.source>
  15. <maven.compiler.target>11 </maven.compiler.target>
  16. </properties>
  17. </project>

因为这是一个接口工程,所有它还需要实体bean(集成Dubbo的实体bean必须实现序列化)和servic业务接口。


  
  1. package com.szh.springboot.entity;
  2. import java.io.Serializable;
  3. public class Student implements Serializable {
  4. private Integer id;
  5. private String name;
  6. private Integer age;
  7. //getter and setter
  8. }

  
  1. package com.szh.springboot.service;
  2. import com.szh.springboot.entity.Student;
  3. /**
  4. *
  5. */
  6. public interface StudentService {
  7. Student queryStudentById(Integer id);
  8. }

2.3 第二个子工程(SpringBoot工程):对应服务提供者

首先来看它的pom文件。在 <parent> 标签中指定了它的父工程,这里面就用到了 017 工程中管理的各种依赖,此时我们就不需要再声明版本号了,因为父工程017中已经有了!!!


  
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3. xsi:schemaLocation= "http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
  4. <modelVersion>4.0.0 </modelVersion>
  5. <parent>
  6. <artifactId>017-springboot-parent </artifactId>
  7. <groupId>com.szh.springboot </groupId>
  8. <version>1.0.0 </version>
  9. <relativePath>../017-springboot-parent/pom.xml </relativePath>
  10. </parent>
  11. <artifactId>019-springboot-ssm-dubbo-provider </artifactId>
  12. <dependencies>
  13. <!-- SpringBoot框架web项目起步依赖 -->
  14. <dependency>
  15. <groupId>org.springframework.boot </groupId>
  16. <artifactId>spring-boot-starter-web </artifactId>
  17. </dependency>
  18. <!-- Dubbo集成SpringBoot框架的起步依赖 -->
  19. <dependency>
  20. <groupId>com.alibaba.spring.boot </groupId>
  21. <artifactId>dubbo-spring-boot-starter </artifactId>
  22. </dependency>
  23. <!-- zookeeper注册中心 -->
  24. <dependency>
  25. <groupId>org.apache.zookeeper </groupId>
  26. <artifactId>zookeeper </artifactId>
  27. <version>3.4.6 </version>
  28. <exclusions>
  29. <exclusion>
  30. <groupId>org.slf4j </groupId>
  31. <artifactId>slf4j-log4j12 </artifactId>
  32. </exclusion>
  33. </exclusions>
  34. </dependency>
  35. <dependency>
  36. <groupId>com.101tec </groupId>
  37. <artifactId>zkclient </artifactId>
  38. </dependency>
  39. <!-- MyBatis集成SpringBoot框架的起步依赖 -->
  40. <dependency>
  41. <groupId>org.mybatis.spring.boot </groupId>
  42. <artifactId>mybatis-spring-boot-starter </artifactId>
  43. </dependency>
  44. <!-- MySQL驱动 -->
  45. <dependency>
  46. <groupId>mysql </groupId>
  47. <artifactId>mysql-connector-java </artifactId>
  48. </dependency>
  49. <!-- 接口工程 -->
  50. <dependency>
  51. <groupId>com.szh.springboot </groupId>
  52. <artifactId>018-springboot-dubbo-ssm-interface </artifactId>
  53. <version>1.0.0 </version>
  54. </dependency>
  55. </dependencies>
  56. <build>
  57. <resources>
  58. <resource>
  59. <directory>src/main/java </directory>
  60. <includes>
  61. <include>**/*.xml </include>
  62. </includes>
  63. </resource>
  64. <resource>
  65. <directory>src/main/resources </directory>
  66. <includes>
  67. <include>**/*.* </include>
  68. </includes>
  69. </resource>
  70. </resources>
  71. <plugins>
  72. <plugin>
  73. <groupId>org.springframework.boot </groupId>
  74. <artifactId>spring-boot-maven-plugin </artifactId>
  75. </plugin>
  76. </plugins>
  77. </build>
  78. </project>

因为它是一个SpringBoot工程,是一个集成Dubbo之后的服务提供者,所以它还需要dao、mapper、对接口工程中接口方法的实现。


  
  1. package com.szh.springboot.mapper;
  2. import com.szh.springboot.entity.Student;
  3. public interface StudentMapper {
  4. Student selectByPrimaryKey(Integer id);
  5. }

  
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
  3. <mapper namespace="com.szh.springboot.mapper.StudentMapper">
  4. <resultMap id="BaseResultMap" type="com.szh.springboot.entity.Student">
  5. <id column="id" jdbcType="INTEGER" property="id" />
  6. <result column="name" jdbcType="VARCHAR" property="name" />
  7. <result column="age" jdbcType="INTEGER" property="age" />
  8. </resultMap>
  9. <sql id="Base_Column_List">
  10. id, name, age
  11. </sql>
  12. <select id="selectByPrimaryKey" parameterType="java.lang.Integer" resultMap="BaseResultMap">
  13. select
  14. <include refid="Base_Column_List" />
  15. from t_student
  16. where id = #{id,jdbcType=INTEGER}
  17. </select>
  18. </mapper>

  
  1. package com.szh.springboot.service.impl;
  2. import com.alibaba.dubbo.config.annotation.Service;
  3. import com.szh.springboot.entity.Student;
  4. import com.szh.springboot.mapper.StudentMapper;
  5. import com.szh.springboot.service.StudentService;
  6. import org.springframework.beans.factory.annotation.Autowired;
  7. import org.springframework.stereotype.Component;
  8. /**
  9. *
  10. */
  11. @Component
  12. @Service(interfaceClass = StudentService.class,version = "1.0.0",timeout = 15000)
  13. public class StudentServiceImpl implements StudentService {
  14. @Autowired
  15. private StudentMapper studentMapper;
  16. @Override
  17. public Student queryStudentById(Integer id) {
  18. return studentMapper.selectByPrimaryKey(id);
  19. }
  20. }

同时配置SpringBoot的核心配置文件。 


  
  1. # 配置内嵌tomcat端口号和上下文根
  2. server.port=8081
  3. server.servlet.context-path=/
  4. # 配置连接数据库的信息
  5. spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
  6. spring.datasource.url=jdbc:mysql://localhost:3306/springboot?useUnicode=true&characterEncoding=UTF-8&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=GMT%2B8
  7. spring.datasource.username=root
  8. spring.datasource.password=12345678
  9. # 配置Dubbo
  10. spring.application.name=019-springboot-ssm-dubbo-provider
  11. spring.dubbo.server=true
  12. spring.dubbo.registry=zookeeper://localhost:2181

最后是SpringBoot项目启动入口类。


  
  1. package com.szh.springboot;
  2. import com.alibaba.dubbo.spring.boot.annotation.EnableDubboConfiguration;
  3. import org.mybatis.spring.annotation.MapperScan;
  4. import org.springframework.boot.SpringApplication;
  5. import org.springframework.boot.autoconfigure.SpringBootApplication;
  6. @SpringBootApplication
  7. @MapperScan(basePackages = "com.szh.springboot.mapper")
  8. @EnableDubboConfiguration
  9. public class Application {
  10. public static void main(String[] args) {
  11. SpringApplication.run(Application.class, args);
  12. }
  13. }

2.4 第三个子工程(SpringBoot工程):对应服务消费者

首先来看它的pom文件。 在 <parent> 标签中指定了它的父工程。


  
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3. xsi:schemaLocation= "http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
  4. <modelVersion>4.0.0 </modelVersion>
  5. <parent>
  6. <artifactId>017-springboot-parent </artifactId>
  7. <groupId>com.szh.springboot </groupId>
  8. <version>1.0.0 </version>
  9. <relativePath>../017-springboot-parent/pom.xml </relativePath>
  10. </parent>
  11. <artifactId>020-springboot-ssm-dubbo-consumer </artifactId>
  12. <dependencies>
  13. <!-- SpringBoot框架集成Thymeleaf前端模板引擎的起步依赖 -->
  14. <dependency>
  15. <groupId>org.springframework.boot </groupId>
  16. <artifactId>spring-boot-starter-thymeleaf </artifactId>
  17. </dependency>
  18. <!-- SpringBoot框架web项目的起步依赖 -->
  19. <dependency>
  20. <groupId>org.springframework.boot </groupId>
  21. <artifactId>spring-boot-starter-web </artifactId>
  22. </dependency>
  23. <!-- Dubbo集成SpringBoot框架的起步依赖 -->
  24. <dependency>
  25. <groupId>com.alibaba.spring.boot </groupId>
  26. <artifactId>dubbo-spring-boot-starter </artifactId>
  27. </dependency>
  28. <!-- zookeeper注册中心 -->
  29. <dependency>
  30. <groupId>org.apache.zookeeper </groupId>
  31. <artifactId>zookeeper </artifactId>
  32. <version>3.4.6 </version>
  33. <exclusions>
  34. <exclusion>
  35. <groupId>org.slf4j </groupId>
  36. <artifactId>slf4j-log4j12 </artifactId>
  37. </exclusion>
  38. </exclusions>
  39. </dependency>
  40. <dependency>
  41. <groupId>com.101tec </groupId>
  42. <artifactId>zkclient </artifactId>
  43. </dependency>
  44. <!-- 接口工程 -->
  45. <dependency>
  46. <groupId>com.szh.springboot </groupId>
  47. <artifactId>018-springboot-dubbo-ssm-interface </artifactId>
  48. <version>1.0.0 </version>
  49. </dependency>
  50. </dependencies>
  51. <build>
  52. <plugins>
  53. <plugin>
  54. <groupId>org.springframework.boot </groupId>
  55. <artifactId>spring-boot-maven-plugin </artifactId>
  56. </plugin>
  57. </plugins>
  58. </build>
  59. </project>

因为它是一个SpringBoot工程,是一个集成Dubbo之后的服务消费者,所以它还需要一个控制层方法的实现,以及响应的html页面。


  
  1. package com.szh.springboot.controller;
  2. import com.alibaba.dubbo.config.annotation.Reference;
  3. import com.szh.springboot.entity.Student;
  4. import com.szh.springboot.service.StudentService;
  5. import org.springframework.stereotype.Controller;
  6. import org.springframework.ui.Model;
  7. import org.springframework.web.bind.annotation.PathVariable;
  8. import org.springframework.web.bind.annotation.RequestMapping;
  9. /**
  10. *
  11. */
  12. @Controller
  13. public class StudentController {
  14. @Reference(interfaceClass = StudentService.class,version = "1.0.0",check = false)
  15. private StudentService studentService;
  16. @RequestMapping(value = "/student/detail/{id}")
  17. public String studentDetail(@PathVariable("id") Integer id, Model model) {
  18. Student student=studentService.queryStudentById(id);
  19. model.addAttribute( "student",student);
  20. return "studentDetail";
  21. }
  22. }

  
  1. <!DOCTYPE html>
  2. <html lang="en" xmlns:th="http://www.thymeleaf.org">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>Title </title>
  6. </head>
  7. <body>
  8. <h3>学生详情 </h3>
  9. 学生编号: <span th:text="${student.id}"> </span> <br/>
  10. 学生姓名: <span th:text="${student.name}"> </span> <br/>
  11. 学生年龄: <span th:text="${student.age}"> </span> <br/>
  12. </body>
  13. </html>

最后是SpringBoot的核心配置文件,以及SpringBoot项目启动入口类。


  
  1. # 配置内嵌tomcat的端口号和上下文根
  2. server.port= 8080
  3. server.servlet.context-path=/
  4. # 关闭Thymeleaf的页面缓存开关
  5. spring.thymeleaf.cache= false
  6. # 配置Thymeleaf前后缀
  7. spring.thymeleaf.prefix=classpath:/templates/
  8. spring.thymeleaf.suffix=.html
  9. # 配置字符编码格式
  10. server.servlet.encoding.enabled= true
  11. server.servlet.encoding.force= true
  12. server.servlet.encoding.charset=UTF- 8
  13. # 配置Dubbo
  14. spring.application.name= 020-springboot-ssm-dubbo-consumer
  15. spring.dubbo.registry=zookeeper://localhost: 2181

  
  1. package com.szh.springboot;
  2. import com.alibaba.dubbo.spring.boot.annotation.EnableDubboConfiguration;
  3. import org.springframework.boot.SpringApplication;
  4. import org.springframework.boot.autoconfigure.SpringBootApplication;
  5. @SpringBootApplication
  6. @EnableDubboConfiguration
  7. public class Application {
  8. public static void main(String[] args) {
  9. SpringApplication.run(Application.class, args);
  10. }
  11. }

2.5 启动测试!!!

启动的步骤如下:👇👇👇

  1. 启动zookeeper注册中心(zkServer.cmd),我这里直接就在Windows端启动了。
  2. 启动服务提供者的Tomcat。
  3. 启动服务消费者的Tomcat。
  4. 到浏览器中输入请求方法中定义的 url 访问就可以了。

 


转载:https://blog.csdn.net/weixin_43823808/article/details/117389459
查看评论
* 以上用户言论只代表其个人观点,不代表本网站的观点或立场