目录
二、创建子工程user-service/order-service
一、创建主工程cloud-demo并删除src目录
二、创建子工程user-service/order-service
三、更改父工程build.gradle文件
-
ext内容需配合设置云效仓库参数到环境变量及gradle使用-优快云博客使用
plugins {
id 'org.springframework.boot' version '3.2.4'
id 'io.spring.dependency-management' version '1.1.4'
}
group = 'com.fafa'
subprojects {
apply plugin: 'org.springframework.boot'
apply plugin: 'io.spring.dependency-management'
apply plugin: 'java'
sourceCompatibility = 17
targetCompatibility = 17
ext {
MAVEN_REPO_RELEASE_URL = System.getenv('MAVEN_REPO_RELEASE_URL')
MAVEN_REPO_SNAPSHOT_URL = System.getenv('MAVEN_REPO_SNAPSHOT_URL')
MAVEN_DEPLOY_USER = System.getenv('MAVEN_DEPLOY_USER')
MAVEN_DEPLOY_PASSWORD = System.getenv('MAVEN_DEPLOY_PASSWORD')
}
repositories {
maven { url "https://maven.aliyun.com/nexus/content/groups/public/" }
maven {
url 'https://maven.aliyun.com/repository/public'
}
maven { url "https://repo.spring.io/milestone" }
maven { url "https://plugins.gradle.org/m2/" }
maven {
credentials {
username MAVEN_DEPLOY_USER
password MAVEN_DEPLOY_PASSWORD
}
url MAVEN_REPO_RELEASE_URL
}
maven {
credentials {
username MAVEN_DEPLOY_USER
password MAVEN_DEPLOY_PASSWORD
}
url MAVEN_REPO_SNAPSHOT_URL
}
mavenCentral()
}
dependencies {
//Spring Boot Web 启动器
implementation 'org.springframework.boot:spring-boot-starter-web'
compileOnly 'org.projectlombok:lombok'
runtimeOnly 'com.mysql:mysql-connector-j'
annotationProcessor 'org.projectlombok:lombok'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
}
test {
useJUnitPlatform()
}
sourceSets {
main {
resources {
srcDirs = ['src/main/resources']
}
}
}
tasks.withType(JavaCompile) {
options.encoding = "UTF-8"
doLast {
copy {
from sourceSets.main.resources
into "$buildDir/resources/main"
}
}
}
}
四、子工程使用mybatis框架
-
build.gradle
group = 'com.fafa.order'
version = '1.0'
description = "order-service"
apply plugin: 'application'
dependencies {
implementation 'org.mybatis.spring.boot:mybatis-spring-boot-starter:3.0.3'
}
-
application.yml
server:
port: 8084
spring:
datasource:
url: jdbc:mysql://localhost:3306/db_order?useSSL=false&useUnicode=true&characterEncoding=UTF-8&serverTimezone=UTC
username: root
password: 123456
mybatis:
mapper-locations: classpath:/mappers/**/*.xml
type-aliases-package: com.fafa.order.entity
-
OrderMapper.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.fafa.order.mapper.OrderMapper">
<resultMap type="Order" id="orderResult">
<result property="orderId" column="order_id"/>
<result property="orderName" column="order_name"/>
<result property="goodsId" column="goods_id"/>
<result property="summary" column="summary"/>
</resultMap>
<sql id="selectOrderVo">
select * from order_info
</sql>
<select id="getAll" parameterType="Order" resultMap="orderResult">
<include refid="selectOrderVo"/>
<where>
<if test="orderName != null ">and order_name like concat('%', #{orderName}, '%')</if>
</where>
order by order_id desc
</select>
</mapper>
-
Order.java
package com.fafa.order.entity;
import lombok.Data;
@Data
public class Order {
private int orderId;
private String orderName;
private int goodsId;
private int summary;
}
-
OrderMapper.java
package com.fafa.order.mapper;
import com.fafa.order.entity.Order;
import org.apache.ibatis.annotations.Mapper;
import java.util.List;
@Mapper
public interface OrderMapper {
public List<Order> getAll(Order order);
}
-
IOrderService.java
package com.fafa.order.service;
import com.fafa.order.entity.Order;
import java.util.List;
public interface IOrderService {
List<Order> getAll(Order order);
}
-
OrderService.java
package com.fafa.order.service;
import com.fafa.order.entity.Order;
import com.fafa.order.mapper.OrderMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class OrderService implements IOrderService{
@Autowired
private OrderMapper orderMapper;
@Override
public List<Order> getAll(Order order) {
return orderMapper.getAll(order);
}
}
-
OrderController.java
package com.fafa.order.controller;
import com.fafa.order.entity.Order;
import com.fafa.order.service.IOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
@RestController
@RequestMapping("/order")
public class OrderController {
@Autowired
private IOrderService orderService;
@GetMapping("/getAll")
public List<Order> getAll(Order order) {
return orderService.getAll(null);
}
}
-
OrderApplication.java
package com.fafa.order;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class OrderApplication {
public static void main(String[] args) {
SpringApplication.run(OrderApplication.class, args);
}
}
-
OrderApplicationTests.java
package com.fafa.order;
import com.fafa.order.entity.Order;
import com.fafa.order.service.IOrderService;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.util.Assert;
import java.util.List;
@SpringBootTest
public class OrderApplicationTests {
@Autowired
private IOrderService orderService;
@Test
public void getAll(){
List<Order> list=orderService.getAll(null);
Assert.isTrue(list.isEmpty(),"有数据");
list.forEach(System.out::println);
}
}
-
test.http
### getAll
GET http://localhost:8084/order/getAll
Accept: application/json
五、子工程使用mybatis-plus框架
参考文章:gradle7.6.1+springboot3.2.4+mybatis-plus搭建工程-优快云博客
六、相关数据库创建
create database db_order;
use db_order;
SET NAMES utf8mb4;
SET FOREIGN_KEY_CHECKS = 0;
-- ----------------------------
-- Table structure for order_info
-- ----------------------------
DROP TABLE IF EXISTS `order_info`;
CREATE TABLE `order_info` (
`order_id` bigint NOT NULL AUTO_INCREMENT COMMENT '订单ID',
`order_name` varchar(100) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL COMMENT '订单名称',
`goods_id` bigint NULL DEFAULT NULL COMMENT '商品ID',
`summary` bigint NULL DEFAULT NULL COMMENT '总额',
PRIMARY KEY (`order_id`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 1 CHARACTER SET = utf8mb4 COLLATE = utf8mb4_general_ci COMMENT = '订单信息' ROW_FORMAT = Dynamic;
SET FOREIGN_KEY_CHECKS = 1;
create database db_user;
use db_user;
SET NAMES utf8mb4;
SET FOREIGN_KEY_CHECKS = 0;
-- ----------------------------
-- Table structure for user_info
-- ----------------------------
DROP TABLE IF EXISTS `user_info`;
CREATE TABLE `user_info` (
`user_id` bigint NOT NULL AUTO_INCREMENT COMMENT '用户ID',
`user_name` varchar(50) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT '' COMMENT '用户名',
`sex` tinyint NULL DEFAULT 0 COMMENT '性别 0 未知 1女 2男',
PRIMARY KEY (`user_id`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 6 CHARACTER SET = utf8mb4 COLLATE = utf8mb4_general_ci COMMENT = '用户表' ROW_FORMAT = Dynamic;
SET FOREIGN_KEY_CHECKS = 1;