SpringCloud构建工程
一、新建数据库和表,并填写测试数据
二、创建父级工程
1、创建maven工程
2、工程名字OfficeAutomation
3、pom.xml文件中添加依赖
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.encoding>UTF-8</maven.compiler.encoding>
<java.version>1.8</java.version>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<spring.cloud.alibaba.version>2021.0.4.0</spring.cloud.alibaba.version>
<spring.cloud.version>2021.0.4</spring.cloud.version>
<spring.boot.version>2.6.11</spring.boot.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>${spring.boot.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring.cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<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>
<!--mysql驱动工具-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.11</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.0.31</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.60</version>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.2.0</version>
</dependency>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus</artifactId>
<version>3.5.3.1</version>
</dependency>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.5.3.1</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.6</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>3.9</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>3.9</version>
</dependency>
<dependency>
<groupId>com.auth0</groupId>
<artifactId>java-jwt</artifactId>
<version>4.4.0</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.13.3</version>
</dependency>
</dependencies>
</dependencyManagement>
三、创建api工程
1、选择父级工程 新建Maven Module模块
2、创建包及.java类
3、举例其中一个Budgets.java类的内容
package com.oacloud.entity;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import java.sql.Date;
@AllArgsConstructor
@NoArgsConstructor
@Data
public class Budgets {
private int id;
private int departmentId;
private int budgetYear;
private double amount;
private String status;
}
4、将该工程打包等待使用
四、创建提供者工程
1、选择父级工程 新建Maven Module模块
模版名称:model-5001
2、pom.xml文件添加依赖
<dependencies>
<dependency>
<groupId>com.oacloud</groupId>
<artifactId>api</artifactId>
<version>1.0-SNAPSHOT</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.11</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.0.31</version>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.2.0</version>
</dependency>
<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>
</dependencies>
3、resources文件夹下创建application.yml
4、application.yml文件内容
server:
port: 5001 # 端口号
spring:
application:
name: model-5001 #服务名称
datasource: # 连接数据库资源
type: com.alibaba.druid.pool.DruidDataSource # 连接池管理工具 # 当前数据源操作类型,类似JDBC的注册驱动
driver-class-name: com.mysql.cj.jdbc.Driver # 驱动地址 # mysql驱动包
url: jdbc:mysql://127.0.0.1:3306/office_automation?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=GMT%2B8 # 连接数据库 # 数据库名称
username: root
password: 'wjy718'
dbcp2:
min-idle: 5 # 数据库连接池的最小维持连接数
initial-size: 5 # 初始化连接数
max-total: 5 # 最大连接数
max-wait-millis: 200 # 等待连接获取的最大超时时间
mybatis:
config-location: classpath:mybatis/mybatis.cfg.xml # mybatis配置文件所在路径
type-aliases-package: com.oacloud.entity # 实体关系映射地址 # 所有Entity别名类所在包
mapper-locations:
- classpath:mybatis/mapper/**/*.xml # 编写sql语句的文件的位置 # -表示部分加载
5、在resources文件夹下创建mybatis文件夹,并创建mybatis的配置文件及映射文件
6、mybatis.cfg.xml文件内容
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<settings>
<setting name="cacheEnabled" value="true" /><!-- 二级缓存开启 -->
</settings>
</configuration>
7、其中一个Mapper.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.oacloud.dao.BudgetsDao">
<sql id="budgets_column">
id,department_id departmentId,budget_year budgetYear,amount,status
</sql>
<select id="selectBudgetsAll" resultType="Budgets">
select <include refid="budgets_column"></include> from budgets
</select>
<select id="selectBudgetsById" parameterType="Integer" resultType="Budgets">
select <include refid="budgets_column"></include> from budgets where department_id = #{departmentId}
</select>
<update id="updateById" parameterType="Budgets">
update budgets set budget_year=#{budgetYear},amount=#{amount},status=#{status} where department_id = #{departmentId}
</update>
<insert id="insertById" parameterType="Budgets">
insert into budgets values(#{id},#{departmentId},#{budgetYear},#{amount},#{status})
</insert>
<delete id="deleteById" parameterType="Integer">
delete from budgets where department_id = #{departmentId}
</delete>
<select id="selectBudgetsStatusById" parameterType="Integer" resultType="Budgets">
select status from budgets where department_id = #{departmentId}
</select>
<update id="updateStatusById" parameterType="Budgets">
update budgets set status=#{status} where department_id = #{departmentId}
</update>
</mapper>
8、创建java文件包结构及启动类
9.启动类Model_5001_App.java类的内容
package com.oacloud;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class Model_5001_App {
public static void main(String[] args) {
SpringApplication.run(Model_5001_App.class,args);
}
}
10、创建包中对应的类、
11、其中BudgetsDao.java接口内容
package com.oacloud.dao;
import com.oacloud.entity.Budgets;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import java.util.ArrayList;
@Mapper
public interface BudgetsDao {
//查询所有
ArrayList<Budgets> selectBudgetsAll();
//根据id查询
Budgets selectBudgetsById(@Param("departmentId") Integer departmentId);
//根据id修改预算信息
void updateById(Budgets budgets);
//根据部门id增加预算信息
void insertById(Budgets budgets);
//根据部门id删除预算信息
void deleteById(Budgets budgets);
//根据部门id查询预算状态
Budgets selectBudgetsStatusById(@Param("departmentId") Integer departmentId);
//根据部门id修改预算状态
void updateStatusById(Budgets budgets);
}
12、其中BudgetsService.java接口内容
package com.oacloud.service;
import com.oacloud.entity.Budgets;
import org.apache.ibatis.annotations.Param;
import java.util.ArrayList;
public interface BudgetsService {
ArrayList<Budgets> findBudgetsAll();
// 根据id查询预算信息
Budgets findBudgetsById(Integer departmentId);
//根据id修改预算信息
void updateById(Budgets budgets);
//增加预算信息
void insertById(Budgets budgets);
//根据部门id删除预算信息
void deleteById(Budgets budgets);
//根据部门id查询预算状态
Budgets findBudgetsStatusById(@Param("departmentId") Integer departmentId);
//根据部门id修改预算状态
void updateStatusById(Budgets budgets);
}
13、其中BudgetsImpl.java类内容
package com.oacloud.service.impl;
import com.oacloud.dao.BudgetsDao;
import com.oacloud.entity.Budgets;
import com.oacloud.service.BudgetsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
@Service
public class BudgetsServiceImpl implements BudgetsService {
@Autowired
@SuppressWarnings("all")
private BudgetsDao budgetsDao;
@Override
public ArrayList<Budgets> findBudgetsAll(){
return budgetsDao.selectBudgetsAll();
}
@Override
public Budgets findBudgetsById(Integer departmentId) {
return budgetsDao.selectBudgetsById(departmentId);
}
@Override
public void updateById(Budgets budgets) {
budgetsDao.updateById(budgets);
}
@Override
public void insertById(Budgets budgets) {
budgetsDao.insertById(budgets);
}
@Override
public void deleteById(Budgets budgets) {
budgetsDao.deleteById(budgets);
}
@Override
public Budgets findBudgetsStatusById(Integer departmentId) {
return budgetsDao.selectBudgetsStatusById(departmentId);
}
@Override
public void updateStatusById(Budgets budgets) {
budgetsDao.updateStatusById(budgets);
}
}
14、其中BudgetsController.java类内容
package com.oacloud.controller;
import com.oacloud.entity.Budgets;
import com.oacloud.entity.Result;
import com.oacloud.service.BudgetsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.ArrayList;
@RestController
@RequestMapping("budgets")
@CrossOrigin
public class BudgetsController {
@SuppressWarnings("all")
@Autowired
private BudgetsService budgetsService;
@GetMapping("getall")
ArrayList<Budgets> getAll() {
return budgetsService.findBudgetsAll();
}
//http://127.0.0.1:5001/budgets/getall
@GetMapping("getbudgetsbyid/{departmentId}")//get被称为请求方式,浏览器端向服务器端发送数据的方式
public Result<Budgets> getBudgetsById(@PathVariable("departmentId") Integer departmentId) {
return Result.success(budgetsService.findBudgetsById(departmentId));
}
@RequestMapping("setbudgetsbyid")
public Result<Budgets> setBudgetsById(@RequestBody Budgets budgets) {
System.out.println(budgets);
budgetsService.updateById(budgets);
return Result.success();
}
@RequestMapping("insertbyid")
public Result<Budgets> insertById(@RequestBody Budgets budgets){
System.out.println(budgets);
budgetsService.insertById(budgets);
return Result.success();
}
@RequestMapping("deletebyid")
public Result<Budgets> deleteById(@RequestBody Budgets budgets){
System.out.println(budgets);
budgetsService.deleteById(budgets);
return Result.success();
}
@GetMapping("getbudgetsStatusbyid/{departmentId}")//get被称为请求方式,浏览器端向服务器端发送数据的方式
public Result<Budgets> getBudgetsStatusById(@PathVariable("departmentId") Integer departmentId) {
return Result.success(budgetsService.findBudgetsStatusById(departmentId));
}
@RequestMapping("setbudgetsStatusbyid")
public Result<Budgets> setBudgetsStatusById(@RequestBody Budgets budgets) {
System.out.println(budgets);
budgetsService.updateStatusById(budgets);
return Result.success();
}
}
15、测试
启动5001工程
访问地址:http://127.0.0.1:5001/budgets/getall
测试成功
五、创建消费者工程
原文地址:https://blog.csdn.net/m0_75068951/article/details/142362620
免责声明:本站文章内容转载自网络资源,如本站内容侵犯了原著者的合法权益,可联系本站删除。更多内容请关注自学内容网(zxcms.com)!