From f3c2e916db44bb66b4a1cff5a7da9bb4f4806297 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?=E7=A7=A6=E8=8B=B1=E6=9D=B0?= <327782001@qq.com>
Date: Fri, 10 Feb 2023 23:06:27 +0800
Subject: [PATCH] =?UTF-8?q?fix:=E6=9C=8D=E5=8A=A1=E6=8F=90=E4=BE=9B?=
=?UTF-8?q?=E8=80=85?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
nacos-server-producer/pom.xml | 34 ++++++---
.../NacosProducerApplication.java | 4 +-
.../config/MyBatisPlusConfig.java | 31 +++++++++
.../controller/NacosProducerController.java | 6 +-
.../controller/UserController.java | 44 ++++++++++++
.../kwan/springcloudalibaba/entity/User.java | 24 +++++++
.../springcloudalibaba/mapper/UserMapper.java | 20 ++++++
.../service/IUserService.java | 32 +++++++++
.../service/impl/UserServiceImpl.java | 37 ++++++++++
.../src/main/resources/bootstrap.yml | 16 +----
.../src/main/resources/logback-spring.xml | 69 +++++++++++++++++++
.../src/main/resources/mapper/UserMapper.xml | 9 +++
12 files changed, 298 insertions(+), 28 deletions(-)
create mode 100644 nacos-server-producer/src/main/java/com/kwan/springcloudalibaba/config/MyBatisPlusConfig.java
create mode 100644 nacos-server-producer/src/main/java/com/kwan/springcloudalibaba/controller/UserController.java
create mode 100644 nacos-server-producer/src/main/java/com/kwan/springcloudalibaba/entity/User.java
create mode 100644 nacos-server-producer/src/main/java/com/kwan/springcloudalibaba/mapper/UserMapper.java
create mode 100644 nacos-server-producer/src/main/java/com/kwan/springcloudalibaba/service/IUserService.java
create mode 100644 nacos-server-producer/src/main/java/com/kwan/springcloudalibaba/service/impl/UserServiceImpl.java
create mode 100644 nacos-server-producer/src/main/resources/logback-spring.xml
create mode 100644 nacos-server-producer/src/main/resources/mapper/UserMapper.xml
diff --git a/nacos-server-producer/pom.xml b/nacos-server-producer/pom.xml
index 1e8f0eb..dce3eda 100644
--- a/nacos-server-producer/pom.xml
+++ b/nacos-server-producer/pom.xml
@@ -2,18 +2,14 @@
4.0.0
-
com.kwan.springcloudalibaba
nacos-server-parent
1.0.0-SNAPSHOT
-
-
nacos-server-producer
nacos-server-producer
nacos-server-producer
-
1.8
UTF-8
@@ -27,12 +23,10 @@
nacos-server-common
1.0.0
-
org.springframework.boot
spring-boot-starter-web
-
org.springframework.boot
spring-boot-starter-test
@@ -54,6 +48,30 @@
org.projectlombok
lombok
+
+ com.baomidou
+ mybatis-plus-boot-starter
+ 3.4.3
+
+
+ com.alibaba
+ druid
+ 1.2.3
+
+
+ org.apache.commons
+ commons-lang3
+ 3.9
+
+
+ mysql
+ mysql-connector-java
+ 8.0.11
+
+
+ com.alibaba
+ druid-spring-boot-starter
+ 1.1.10
+
-
-
+
\ No newline at end of file
diff --git a/nacos-server-producer/src/main/java/com/kwan/springcloudalibaba/NacosProducerApplication.java b/nacos-server-producer/src/main/java/com/kwan/springcloudalibaba/NacosProducerApplication.java
index b3541f1..8ade027 100644
--- a/nacos-server-producer/src/main/java/com/kwan/springcloudalibaba/NacosProducerApplication.java
+++ b/nacos-server-producer/src/main/java/com/kwan/springcloudalibaba/NacosProducerApplication.java
@@ -2,6 +2,7 @@ package com.kwan.springcloudalibaba;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
+import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
/**
@@ -11,10 +12,11 @@ import org.springframework.boot.autoconfigure.SpringBootApplication;
* @version : 2.2.0
* @date : 2023/1/17 12:37
*/
-@SpringBootApplication
+@SpringBootApplication(exclude = {DataSourceAutoConfiguration.class})
public class NacosProducerApplication {
public static void main(String[] args) {
SpringApplication.run(NacosProducerApplication.class, args);
+ System.out.println("-----------------------nacos-server-producer启动成功-----------------------");
}
}
diff --git a/nacos-server-producer/src/main/java/com/kwan/springcloudalibaba/config/MyBatisPlusConfig.java b/nacos-server-producer/src/main/java/com/kwan/springcloudalibaba/config/MyBatisPlusConfig.java
new file mode 100644
index 0000000..9782029
--- /dev/null
+++ b/nacos-server-producer/src/main/java/com/kwan/springcloudalibaba/config/MyBatisPlusConfig.java
@@ -0,0 +1,31 @@
+package com.kwan.springcloudalibaba.config;
+
+import com.baomidou.mybatisplus.annotation.DbType;
+import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
+import com.baomidou.mybatisplus.extension.plugins.inner.OptimisticLockerInnerInterceptor;
+import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
+import org.mybatis.spring.annotation.MapperScan;
+import org.springframework.context.annotation.Bean;
+import org.springframework.context.annotation.Configuration;
+
+/**
+ * mybatisplus配置
+ *
+ * @author : qinyingjie
+ * @version : 2.2.0
+ * @date : 2022/12/19 16:11
+ */
+@MapperScan("com.kwan.springcloudalibaba.mapper")//扫描我们的mapper文件夹 这里添加了@MapperScan注解就不需要Springboot启动类上加入@MapperScan注解了
+@Configuration //配置类
+public class MyBatisPlusConfig {
+
+ @Bean
+ public MybatisPlusInterceptor mybatisPlusInterceptor() {
+ MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
+ //注册乐观锁插件
+ interceptor.addInnerInterceptor(new OptimisticLockerInnerInterceptor());
+ //分页插件
+ interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL));
+ return interceptor;
+ }
+}
diff --git a/nacos-server-producer/src/main/java/com/kwan/springcloudalibaba/controller/NacosProducerController.java b/nacos-server-producer/src/main/java/com/kwan/springcloudalibaba/controller/NacosProducerController.java
index dc8989c..adde51a 100644
--- a/nacos-server-producer/src/main/java/com/kwan/springcloudalibaba/controller/NacosProducerController.java
+++ b/nacos-server-producer/src/main/java/com/kwan/springcloudalibaba/controller/NacosProducerController.java
@@ -1,7 +1,6 @@
package com.kwan.springcloudalibaba.controller;
import com.kwan.springcloudalibaba.common.Result;
-import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.GetMapping;
@@ -14,16 +13,13 @@ import org.springframework.web.bind.annotation.RestController;
@RequestMapping("/nacos/producer")
public class NacosProducerController {
- @Value("${user.name}")
- private String nameInfo;
-
/**
* 获取配置的变量
*/
@GetMapping(value = "/nameInfo", produces = MediaType.APPLICATION_PROBLEM_JSON_VALUE)
public Result nameInfo() {
- return Result.ok(nameInfo);
+ return Result.ok();
}
}
diff --git a/nacos-server-producer/src/main/java/com/kwan/springcloudalibaba/controller/UserController.java b/nacos-server-producer/src/main/java/com/kwan/springcloudalibaba/controller/UserController.java
new file mode 100644
index 0000000..f43a54c
--- /dev/null
+++ b/nacos-server-producer/src/main/java/com/kwan/springcloudalibaba/controller/UserController.java
@@ -0,0 +1,44 @@
+package com.kwan.springcloudalibaba.controller;
+
+
+import com.kwan.springcloudalibaba.entity.User;
+import com.kwan.springcloudalibaba.service.IUserService;
+import lombok.extern.slf4j.Slf4j;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.web.bind.annotation.*;
+
+import java.util.List;
+
+/**
+ * 用户相关
+ *
+ * @author : qinyingjie
+ * @version : 2.2.0
+ * @date : 2022/12/19 16:08
+ */
+@Slf4j
+@RestController
+@RequestMapping("/user")
+public class UserController {
+
+ @Autowired
+ private IUserService userService;
+
+ @RequestMapping(value = "/all", method = RequestMethod.GET)
+ public List addAdvertise() {
+ log.info("测试日志={}", "success");
+ return userService.getUsers();
+ }
+
+
+ @RequestMapping(value = "/getUserById/{id}", method = RequestMethod.GET)
+ public User getUserById(@PathVariable Integer id) {
+ return userService.getUserById(id);
+ }
+
+
+ @RequestMapping(value = "/getUserByName", method = RequestMethod.GET)
+ public User getUserByName(@RequestParam String sex) {
+ return userService.getUserByName(sex);
+ }
+}
\ No newline at end of file
diff --git a/nacos-server-producer/src/main/java/com/kwan/springcloudalibaba/entity/User.java b/nacos-server-producer/src/main/java/com/kwan/springcloudalibaba/entity/User.java
new file mode 100644
index 0000000..1ba8dda
--- /dev/null
+++ b/nacos-server-producer/src/main/java/com/kwan/springcloudalibaba/entity/User.java
@@ -0,0 +1,24 @@
+package com.kwan.springcloudalibaba.entity;
+
+import com.baomidou.mybatisplus.annotation.IdType;
+import com.baomidou.mybatisplus.annotation.TableId;
+import com.baomidou.mybatisplus.annotation.TableName;
+import lombok.AllArgsConstructor;
+import lombok.Builder;
+import lombok.Data;
+import lombok.NoArgsConstructor;
+
+@Data
+@Builder
+@TableName(value = "user")
+@AllArgsConstructor
+@NoArgsConstructor
+public class User {
+
+ @TableId(value = "id", type = IdType.AUTO)
+ private Integer id;
+
+ private String name;
+
+ private String sex;
+}
diff --git a/nacos-server-producer/src/main/java/com/kwan/springcloudalibaba/mapper/UserMapper.java b/nacos-server-producer/src/main/java/com/kwan/springcloudalibaba/mapper/UserMapper.java
new file mode 100644
index 0000000..16e7053
--- /dev/null
+++ b/nacos-server-producer/src/main/java/com/kwan/springcloudalibaba/mapper/UserMapper.java
@@ -0,0 +1,20 @@
+package com.kwan.springcloudalibaba.mapper;
+
+import com.baomidou.mybatisplus.core.mapper.BaseMapper;
+import com.kwan.springcloudalibaba.entity.User;
+import org.apache.ibatis.annotations.Mapper;
+
+
+/**
+ * mapper文件
+ *
+ * @author : qinyingjie
+ * @version : 2.2.0
+ * @date : 2022/12/19 16:12
+ */
+@Mapper
+public interface UserMapper extends BaseMapper {
+
+ User getUserByName(String sex);
+}
+
diff --git a/nacos-server-producer/src/main/java/com/kwan/springcloudalibaba/service/IUserService.java b/nacos-server-producer/src/main/java/com/kwan/springcloudalibaba/service/IUserService.java
new file mode 100644
index 0000000..da7a0d7
--- /dev/null
+++ b/nacos-server-producer/src/main/java/com/kwan/springcloudalibaba/service/IUserService.java
@@ -0,0 +1,32 @@
+package com.kwan.springcloudalibaba.service;
+
+
+import com.baomidou.mybatisplus.extension.service.IService;
+import com.kwan.springcloudalibaba.entity.User;
+
+import java.util.List;
+
+public interface IUserService extends IService {
+
+ /**
+ * 获取所有用户
+ *
+ * @return
+ */
+ List getUsers();
+
+ /**
+ * 获取一个用户
+ *
+ * @return
+ */
+ User getUserById(Integer id);
+
+ /**
+ * 根据姓名查询
+ *
+ * @param sex
+ * @return
+ */
+ User getUserByName(String sex);
+}
diff --git a/nacos-server-producer/src/main/java/com/kwan/springcloudalibaba/service/impl/UserServiceImpl.java b/nacos-server-producer/src/main/java/com/kwan/springcloudalibaba/service/impl/UserServiceImpl.java
new file mode 100644
index 0000000..37b9862
--- /dev/null
+++ b/nacos-server-producer/src/main/java/com/kwan/springcloudalibaba/service/impl/UserServiceImpl.java
@@ -0,0 +1,37 @@
+package com.kwan.springcloudalibaba.service.impl;
+
+import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
+import com.kwan.springcloudalibaba.entity.User;
+import com.kwan.springcloudalibaba.mapper.UserMapper;
+import com.kwan.springcloudalibaba.service.IUserService;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.stereotype.Service;
+
+import java.util.List;
+import java.util.Objects;
+
+@Service
+public class UserServiceImpl extends ServiceImpl implements IUserService {
+
+ @Autowired
+ private UserMapper userMapper;
+
+ @Override
+ public List getUsers() {
+ return userMapper.selectList(null);
+ }
+
+ @Override
+ public User getUserById(Integer id) {
+ return userMapper.selectById(id);
+ }
+
+ @Override
+ public User getUserByName(String sex) {
+ User userByName = userMapper.getUserByName(sex);
+ if (Objects.isNull(userByName)) {
+ userByName = new User();
+ }
+ return userByName;
+ }
+}
\ No newline at end of file
diff --git a/nacos-server-producer/src/main/resources/bootstrap.yml b/nacos-server-producer/src/main/resources/bootstrap.yml
index 726b8ec..ef15f6c 100644
--- a/nacos-server-producer/src/main/resources/bootstrap.yml
+++ b/nacos-server-producer/src/main/resources/bootstrap.yml
@@ -1,19 +1,7 @@
#端口号
server:
port: 9091
-
-#断电打开
-management:
- endpoint:
- health:
- show-details: always
- endpoints:
- jmx:
- exposure:
- include: '*'
- web:
- exposure:
- include: '*'
+
#spring配置
spring:
application:
@@ -28,5 +16,5 @@ spring:
server-addr: http://120.79.36.53:8848 #配置中心地址
file-extension: yaml #文件类型
group: DEV_GROUP #组别
- namespace: e750dcd5-657b-489b-8d15-b7b71aa3e984 #命名空间
+ namespace: 4cd9bd32-8f25-45cd-b919-df1a0df146e0 #命名空间
refresh-enabled: true #默认自动刷新
\ No newline at end of file
diff --git a/nacos-server-producer/src/main/resources/logback-spring.xml b/nacos-server-producer/src/main/resources/logback-spring.xml
new file mode 100644
index 0000000..86327aa
--- /dev/null
+++ b/nacos-server-producer/src/main/resources/logback-spring.xml
@@ -0,0 +1,69 @@
+
+
+
+
+
+
+
+
+
+
+
+
+
+ ../logs/springboot-mybatis.log
+
+
+ %date [%level] [%thread] %logger{60} [%file : %line] %msg%n
+
+ UTF-8
+
+
+
+
+ ../logs/springboot-mybatis.log.%d{yyyy-MM-dd}.log
+
+ 30
+
+ 10GB
+
+
+
+
+
+
+ ../logs/error.log
+
+ %date [%level] [%thread] %logger{60} [%file : %line] %msg%n
+
+ UTF-8
+
+
+
+
+ ../logs/error.%d{yyyy-MM-dd}.log
+
+ 30
+
+ 10GB
+
+
+
+ ERROR
+
+ ACCEPT
+
+ DENY
+
+
+
+
+
+
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/nacos-server-producer/src/main/resources/mapper/UserMapper.xml b/nacos-server-producer/src/main/resources/mapper/UserMapper.xml
new file mode 100644
index 0000000..af5d2cf
--- /dev/null
+++ b/nacos-server-producer/src/main/resources/mapper/UserMapper.xml
@@ -0,0 +1,9 @@
+
+
+
+
+
--
GitLab