第二章 Maven聚合工程创建微服务项目

2023-10-30

1.创建聚合工程 xdclass-cloud,修改pom文件
注意:记得删除聚合工程src目录


    <!-- 一般来说父级项目的packaging都为pom,packaging默认类型jar类型-->
    <packaging>pom</packaging>

    <properties>
        <java.version>1.8</java.version>
    </properties>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-dependencies</artifactId>
                <version>2.3.3.RELEASE</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>Hoxton.SR8</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
            <dependency>
                <groupId>com.alibaba.cloud</groupId>
                <artifactId>spring-cloud-alibaba-dependencies</artifactId>
                <version>2.2.1.RELEASE</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <fork>true</fork>
                    <addResources>true</addResources>
                </configuration>
            </plugin>
        </plugins>
    </build>

2.创建4个子项目 xdclass-common、xdclass-video-service、xdclass-user-service、xdclass-order-service
3.添加子项目依赖
修改子项目xdclass-video-service、xdclass-user-service、xdclass-order-service下的pom文件

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>net.xdclass</groupId>
            <artifactId>xdclass-common</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>

4.Mybatis依赖导入+数据库配,创建common包实体类

public class User {
        private Integer id;
        private String name;
        private String pwd;
        private String headImg;
        private String phone;
        private Date createTime;
        private String wechat;
}
public class Video {
    private Integer id;
    private String title;
    private String summary;
    private String coverImg;
    private Integer  price;
    private Date createTime;
    private Double point;
}
public class VideoOrder {
    private Integer id;
    private String outTradeNo;
    private Integer state;
    private Date createTime;
    private  Integer totalFee;
    private Integer videoId;
    private String videoTitle;
    private String videoImg;
    private Integer userId;
}

5.聚合工程pom.xml修改

    <properties>
        <java.version>1.8</java.version>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
    </properties>

  	<dependency>
    	<groupId>org.mybatis.spring.boot</groupId>
    	<artifactId>mybatis-spring-boot-starter</artifactId>
    	<version>2.1.2</version>
    	<type>pom</type>
    	<scope>import</scope>
    </dependency>

6.添加mybatis依赖和数据库驱动

        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
        </dependency>

7.在xdclass-cloud\xdclass-video-service\src\main\resources创建application.yml
xdclass-user-service、xdclass-order-service项目下的端口及数据库名称要改成对应的

server:
  port: 9000

spring:
  application:
    name: xdclass-video-service
  datasource:
    driver-class-name: com.mysql.cj.jdbc.Driver
    url: jdbc:mysql://127.0.0.1:3306/cloud_video?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=UTC
    username: root
    password: 123456

# 控制台输出sql、下划线转驼峰
mybatis:
  configuration:
    log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
    map-underscore-to-camel-case: true

8.controller->service->mapper 开发


@RestController
@RequestMapping("api/v1/video")
public class VideoController {

    @Autowired
    private VideoService videoService;

    @RequestMapping("find_by_id")
    public Object findVideoById(int videoId){
        return videoService.findById(videoId);
    }
}

@Service
public class VideoServiceImpl implements VideoService {
    @Autowired
    private VideoMapper videoMapper;
    @Override
    public Video findById(int videoId) {
        return videoMapper.findById(videoId);
    }
}


@Repository
public interface VideoMapper {

    @Select("select * from video where id=#{videoId}")
    Video findById(@Param("videoId")int videoId);
}

9.启动类配置

@SpringBootApplication
@MapperScan("net.xdclass.dao")
public class VideoApplication {
    public static void main(String[] args) {
        SpringApplication.run(VideoApplication.class,args);
    }
}

10.启动服务,验证接口
在这里插入图片描述

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

第二章 Maven聚合工程创建微服务项目 的相关文章

  • 如何在 Grails 2.4 中外部化 Maven 凭证

    我正在尝试在 Grails 2 4 项目中从使用 Ivy 迁移到使用 Aether 解析器 我遇到的问题与外部化凭证有关 与此相关的信息可以在 Grails 手册中找到 http grails org doc latest guide co
  • Java如何删除设置了IMMUTABLE位的文件

    正在开发一个 Java 8 项目 我从外部源复制文件 在这些源之一中 文件设置了不可变位标志 In OSX这是这样设置的 sudo chflags schg path to file In Linux chattr i path to fi
  • 在 Java 8 中将对象追加到列表并返回结果?

    有没有一种方法可以将对象附加到列表并以功能性非命令方式在一行中返回结果 如果原始列表也不应该被改变 你会怎么做 Java 8 是允许的 我已经知道如何将两个列表连接到一行中 Source https stackoverflow com a
  • 使用Java获取CSS文件中图像的URL?

    我正在尝试使用 Java 获取远程 CSS 文件中图像 所有 MIME 类型 的 URL 我正在使用 jsoup 来获取 css 的 URL 经过无数个小时的观看CSS解析器 http cssparser sourceforge net 由
  • 为什么 hibernate 在一张表中保存两个 @OneToMany 列表?

    想象一下使用 Hibernate 和 JPA 的简化代码如下 Entity class C Id GeneratedValue public long id MappedSuperclass abstract class A Id Gene
  • maven 3:访问​​“root”企业 POM 版本

    使用 Maven 3 0 4 我的任务是为我们的组织提供企业父级 POM 我的团队将为开发人员在使用此 POM 时遇到的疑问或问题提供支持 他们通常会将构建日志附加到支持票证中 因此 我希望我的公司 POM 将公司父级的版本回显到任何构建的
  • 使用 google-api-java-client 的 2 足 OAuth

    有谁知道如何将 2 legged OAuth 与 google api java client 一起使用 我正在尝试访问 Google Apps 配置 API 以获取特定域的用户列表 以下不起作用 HttpTransport transpo
  • 具有最小刻度的图表的漂亮标签算法

    我需要手动计算图表的刻度标签和刻度范围 我知道漂亮刻度的 标准 算法 参见 我也知道这个Java实现 http erison blogspot nl 2011 07 algorithm for optimal scaling on char
  • 如何让 HttpClient 返回状态码和响应正文?

    我试图让 Apache HttpClient 触发 HTTP 请求 然后显示 HTTP 响应代码 200 404 500 等 以及 HTTP 响应正文 文本字符串 重要的是要注意我正在使用v4 2 2因为大多数 HttpClient 示例都
  • Tomcat - 多个 webapps 文件夹

    是否可以有多个文件夹来放置要部署的应用程序 这些是如何定义的 是否可以将一个文件夹限制为仅是 domain com 的应用程序 而不是其他域 Thanks 看一眼conf server xml
  • 处理 ANTLR 4 中的错误

    遵循后接受的答案 https stackoverflow com a 18137301 2279200的指示处理 ANTLR4 中的错误 https stackoverflow com q 18132078 2279200问题 我遇到了以下
  • 属性文件中的字符串主机名:Java

    这听起来可能是一个非常简单的问题 但我无法找到解决方法 我有一个 config properties 文件 其中包含两个键值 IP 地址和端口号 我读取此配置文件以提取字符串格式的键值 但是 当我尝试使用这些值时 我无法连接到从配置文件中检
  • java:如何设置全局线程ID?

    是否有可能为线程设置唯一ID 在分布式系统中 线程是在许多不同的机器上创建的 例如通过 RMI 我需要它来创建日志消息 根据我的研究 我知道可以使用 log4j mdc ndc 来完成 但只能在单线程中完成 我的问题是 在创建线程时必须设置
  • java.lang.ClassCastException: [B 无法转换为 java.lang.String

    我编写了一个带有字段 LoginId 和密码的实体类 我使用 AES ENCRYPT 加密密码并将其存储在数据库中 我只想检索已解密的密码 所以 我使用 AES DECRYPT 使用本机查询是在 OPen JPA 2 0 中 我写的查询是
  • 如何在Java中通过反射调用代理(Spring AOP)上的方法?

    一个接口 public interface Manager Object read Long id 实现该接口的类 Transactional Public class ManagerImpl implements Manager Over
  • Wildfly 10.1 消耗所有核心

    我们最近将银行应用程序从 java 1 6 升级到 1 8 将 jboss 4 x 升级到 wildfly 10 1 我们观察到 java 消耗了机器上可用的所有核心 10 有人可以告诉是什么原因吗 通常情况下 jboss 4 x 的最大
  • 找出该月第一个星期日/星期一等的日期

    我想在java中检测每个月第一周 第二周的第一个星期日 星期一的日期 我怎样才能实现它 我已经检查了 java 中的 Calendar 类和 Date 类 但无法找到解决方案 所以请帮助我解决这个问题 Calendar calendar C
  • Java“非法访问操作”方法将被弃用? [复制]

    这个问题在这里已经有答案了 JDK 9 JVM 发出非法访问操作警告后 如果您使用一些非法访问 例如setAccessible 我的问题 Is setAccessible 以后会被封吗 此功能的官方参考 如果将被弃用 在哪里 我在任何地方都
  • Java 中 .NET 的 Lambda 表达式

    我最近 再次 从 C 迁移到 Java 但我非常怀念 lambda 表达式和 C 的 IEnumerable Foreach 之类的东西 所以我正在寻找Java中的lambda表达式库 有比这更好的图书馆吗LambdaJ http code
  • 安装 JDK 时出错:keytool 命令需要已安装的 proc fs (/proc)。 Linux 的 Windows 子系统

    我尝试在 Linux 的 Windows 子系统 Ubuntu 14 04 上安装 Oracle JDK 1 7 但出现以下错误 the keytool command requires a mounted proc fs proc Jav

随机推荐