快速搭建springcloud 3.X+mybatis+nacos本地项目

环境: jdk17

idea 2019

springboot:3.2.4

spring-cloud:2023.0.1

大概记录下关键配置

1 创建父工程

创建项目

删除src目录

因为是父子结构不需要。

引入依赖

XML 复制代码
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>org.tuling</groupId>
    <artifactId>spring-cloud-alibaba-demo</artifactId>
    <packaging>pom</packaging>
    <version>${revision}</version>
    <modules>
        <module>order01</module>
    </modules>

    <name>spring-cloud-alibaba-demo</name>
    <description>demo</description>

    <!-- 项目依赖版本 -->
    <properties>
        <revision>1.0.0</revision>
        <java.version>17</java.version>
        <maven.compiler.source>17</maven.compiler.source>
        <maven.compiler.target>17</maven.compiler.target>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <skipTests>true</skipTests>

        <spring.boot.version>3.2.4</spring.boot.version>
        <spring-cloud.version>2023.0.1</spring-cloud.version>
        <spring-cloud-alibaba.version>2023.0.1.0</spring-cloud-alibaba.version>
        <spring.boot.test.version>3.2.4</spring.boot.test.version>
        <lombok.version>1.18.30</lombok.version>
        <flatten-maven-plugin.version>1.2.7</flatten-maven-plugin.version>
    </properties>

    <dependencyManagement>
        <dependencies>
            <!-- Spring Boot -->
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-parent</artifactId>
                <version>${spring.boot.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
            <!-- Spring Cloud -->
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>${spring-cloud.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
            <!-- Spring Cloud Alibaba -->
            <dependency>
                <groupId>com.alibaba.cloud</groupId>
                <artifactId>spring-cloud-alibaba-dependencies</artifactId>
                <version>${spring-cloud-alibaba.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
            <!-- spring-boot-starter-test -->
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-test</artifactId>
                <version>${spring.boot.test.version}</version>
            </dependency>
            <!-- lombok -->
            <dependency>
                <groupId>org.projectlombok</groupId>
                <artifactId>lombok</artifactId>
                <version>${lombok.version}</version>
                <optional>true</optional>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <build>
        <pluginManagement>
            <plugins>
                <plugin>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-maven-plugin</artifactId>
                    <version>${spring.boot.version}</version>
                </plugin>
            </plugins>
        </pluginManagement>
    </build>

    <!-- 阿里云的Maven镜像源 -->
    <repositories>
        <repository>
            <id>aliyunmaven</id>
            <name>aliyun</name>
            <url>https://maven.aliyun.com/repository/public</url>
        </repository>
        <repository>
            <id>central2</id>
            <name>central2</name>
            <url>https://repo1.maven.org/maven2/</url>
        </repository>
    </repositories>

</project>

2 创建子模块(服务提供者)

新建module

引入依赖

XML 复制代码
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <parent>
        <artifactId>spring-cloud-alibaba-demo</artifactId>
        <groupId>org.tuling</groupId>
        <version>1.0.0</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>order01</artifactId>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>

        <!-- Nacos注册中心 -->
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
        </dependency>

        <!-- lombok -->
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
        </dependency>
        <dependency>
            <groupId>com.mysql</groupId>
            <artifactId>mysql-connector-j</artifactId>
            <!-- <version>8.0.32</version> -->
        </dependency>
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>3.0.3</version>
        </dependency>


    </dependencies>

    <build>
        <plugins>
            <!-- 打包插件 -->
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <executions>
                    <execution>
                        <goals>
                            <goal>repackage</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
        <!-- 将源码中的xml文件打包到jar中 -->
        <resources>
            <resource>
                <directory>src/main/java</directory>
                <excludes>
                    <exclude>**/*.java</exclude>
                </excludes>
            </resource>
            <resource>
                <directory>src/main/resources</directory>
                <includes>
                    <include>**/*.*</include>
                </includes>
            </resource>
        </resources>
    </build>

</project>

3 添加配置文件

resources目录下新建 application.yml 配置文件,内容如下:

bash 复制代码
server:
  port: 8060

spring:
  application:
    name: tlmall-order01

  cloud:
    nacos:
      discovery:
        server-addr: tlmall-nacos-server:8848
        #server-addr: nacos.tlmall.com:8848
        #namespace: fox  #指定为fox开发环境的命名空间
        #namespace: dev  # 指定注册到开发环境
        #cluster-name: SH  #上海机房
        username: nacos
        password: nacos
  datasource:
    url: jdbc:mysql://XXX:3306/tlmall_order?useSSL=false&characterEncoding=utf8&allowPublicKeyRetrieval=true
    driver-class-name: com.mysql.cj.jdbc.Driver
    username: root
    password: 1234XXXXX
    main:
    allow-bean-definition-overriding: true
mybatis:
  # 配置 mybatis 的xml文件的扫描路径
  mapper-locations: classpath:mybatis/**/*.xml
  # 配置实体类的扫描路径
  type-aliases-package: org.tuling.tlmallorder01.entity
  configuration:
    # 开启驼峰命名转换
    map-underscore-to-camel-case: true
    # 开启日志
    #log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
    log-impl: org.apache.ibatis.logging.slf4j.Slf4jImpl


management:
  endpoints:
    web:
      exposure:
        include: "*"

配置注意:数据源

4 添加启动类

5 添加测试业务代码

controller

service

mapper

/resources/mybatis/ 目录下新增 XXMapper.xml 文件

sql 复制代码
CREATE TABLE `order` (
  `id` bigint unsigned NOT NULL AUTO_INCREMENT,
  `user_id` varchar(255) DEFAULT NULL,
  `commodity_code` varchar(255) DEFAULT NULL,
  `count` int DEFAULT NULL,
  `money` int DEFAULT '0',
  `create_time` datetime DEFAULT NULL,
  `update_time` datetime DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=90 DEFAULT CHARSET=utf8mb3;

6 服务的测试

启动nacos.参见:nacos 让服务之间互相认识-CSDN博客

启动应用,测试接口

如果启动了nacos.观察下服务列表

注意下EndPoint,http://localhost:8060/actuator/nacosdiscovery

至此,springBoot的项目基本架构搭建成功。可以陆续添加其它子模块。主要是pom里面的配置,版本不一致可能产生的问题。

相关推荐
新手小袁_J1 小时前
java.lang.IllegalStateException: Error processing condition on org.springframework.boot.autoconfigur
java·开发语言·spring·spring cloud·bootstrap·maven·mybatis
鹿屿二向箔10 小时前
基于SSM(Spring + Spring MVC + MyBatis)框架搭建一个病人跟踪信息管理系统
spring·mvc·mybatis
晨港飞燕10 小时前
SpringCloudAlibaba升级手册-nacos问题记录
nacos·grpc·兼容性
鹿屿二向箔13 小时前
基于SSM(Spring + Spring MVC + MyBatis)框架构建一个图书馆仓储管理系统
spring·mvc·mybatis
积极向上的Elbert14 小时前
Mybatis-Plus中的Page方法出现Records的值大于0但是total的值一直是0
java·开发语言·mybatis
荆州克莱19 小时前
centos7 免安装mysql5.7及配置(支持多个mysql)
spring boot·spring·spring cloud·css3·技术
听见~2 天前
MyBatisPlus
mybatis
向阳12182 天前
mybatis SqlSessionFactory
java·mybatis
Suwg2092 天前
《手写Mybatis渐进式源码实践》实践笔记(第七章 SQL执行器的创建和使用)
java·数据库·笔记·后端·sql·mybatis·模板方法模式