fix:服务提供者

上级 43322a7e
...@@ -2,18 +2,14 @@ ...@@ -2,18 +2,14 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" <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 https://maven.apache.org/xsd/maven-4.0.0.xsd"> xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion> <modelVersion>4.0.0</modelVersion>
<parent> <parent>
<groupId>com.kwan.springcloudalibaba</groupId> <groupId>com.kwan.springcloudalibaba</groupId>
<artifactId>nacos-server-parent</artifactId> <artifactId>nacos-server-parent</artifactId>
<version>1.0.0-SNAPSHOT</version> <version>1.0.0-SNAPSHOT</version>
</parent> </parent>
<artifactId>nacos-server-producer</artifactId> <artifactId>nacos-server-producer</artifactId>
<name>nacos-server-producer</name> <name>nacos-server-producer</name>
<description>nacos-server-producer</description> <description>nacos-server-producer</description>
<properties> <properties>
<java.version>1.8</java.version> <java.version>1.8</java.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
...@@ -27,12 +23,10 @@ ...@@ -27,12 +23,10 @@
<artifactId>nacos-server-common</artifactId> <artifactId>nacos-server-common</artifactId>
<version>1.0.0</version> <version>1.0.0</version>
</dependency> </dependency>
<dependency> <dependency>
<groupId>org.springframework.boot</groupId> <groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId> <artifactId>spring-boot-starter-web</artifactId>
</dependency> </dependency>
<dependency> <dependency>
<groupId>org.springframework.boot</groupId> <groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId> <artifactId>spring-boot-starter-test</artifactId>
...@@ -54,6 +48,30 @@ ...@@ -54,6 +48,30 @@
<groupId>org.projectlombok</groupId> <groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId> <artifactId>lombok</artifactId>
</dependency> </dependency>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.4.3</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.2.3</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.9</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.11</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>1.1.10</version>
</dependency>
</dependencies> </dependencies>
</project>
</project> \ No newline at end of file
...@@ -2,6 +2,7 @@ package com.kwan.springcloudalibaba; ...@@ -2,6 +2,7 @@ package com.kwan.springcloudalibaba;
import org.springframework.boot.SpringApplication; import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
/** /**
...@@ -11,10 +12,11 @@ import org.springframework.boot.autoconfigure.SpringBootApplication; ...@@ -11,10 +12,11 @@ import org.springframework.boot.autoconfigure.SpringBootApplication;
* @version : 2.2.0 * @version : 2.2.0
* @date : 2023/1/17 12:37 * @date : 2023/1/17 12:37
*/ */
@SpringBootApplication @SpringBootApplication(exclude = {DataSourceAutoConfiguration.class})
public class NacosProducerApplication { public class NacosProducerApplication {
public static void main(String[] args) { public static void main(String[] args) {
SpringApplication.run(NacosProducerApplication.class, args); SpringApplication.run(NacosProducerApplication.class, args);
System.out.println("-----------------------nacos-server-producer启动成功-----------------------");
} }
} }
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;
}
}
package com.kwan.springcloudalibaba.controller; package com.kwan.springcloudalibaba.controller;
import com.kwan.springcloudalibaba.common.Result; import com.kwan.springcloudalibaba.common.Result;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope; import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.http.MediaType; import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.GetMapping;
...@@ -14,16 +13,13 @@ import org.springframework.web.bind.annotation.RestController; ...@@ -14,16 +13,13 @@ import org.springframework.web.bind.annotation.RestController;
@RequestMapping("/nacos/producer") @RequestMapping("/nacos/producer")
public class NacosProducerController { public class NacosProducerController {
@Value("${user.name}")
private String nameInfo;
/** /**
* 获取配置的变量 * 获取配置的变量
*/ */
@GetMapping(value = "/nameInfo", produces = MediaType.APPLICATION_PROBLEM_JSON_VALUE) @GetMapping(value = "/nameInfo", produces = MediaType.APPLICATION_PROBLEM_JSON_VALUE)
public Result nameInfo() { public Result nameInfo() {
return Result.ok(nameInfo); return Result.ok();
} }
} }
......
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<User> 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
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;
}
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> {
User getUserByName(String sex);
}
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<User> {
/**
* 获取所有用户
*
* @return
*/
List<User> getUsers();
/**
* 获取一个用户
*
* @return
*/
User getUserById(Integer id);
/**
* 根据姓名查询
*
* @param sex
* @return
*/
User getUserByName(String sex);
}
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<UserMapper, User> implements IUserService {
@Autowired
private UserMapper userMapper;
@Override
public List<User> 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
#端口号 #端口号
server: server:
port: 9091 port: 9091
#断电打开
management:
endpoint:
health:
show-details: always
endpoints:
jmx:
exposure:
include: '*'
web:
exposure:
include: '*'
#spring配置 #spring配置
spring: spring:
application: application:
...@@ -28,5 +16,5 @@ spring: ...@@ -28,5 +16,5 @@ spring:
server-addr: http://120.79.36.53:8848 #配置中心地址 server-addr: http://120.79.36.53:8848 #配置中心地址
file-extension: yaml #文件类型 file-extension: yaml #文件类型
group: DEV_GROUP #组别 group: DEV_GROUP #组别
namespace: e750dcd5-657b-489b-8d15-b7b71aa3e984 #命名空间 namespace: 4cd9bd32-8f25-45cd-b919-df1a0df146e0 #命名空间
refresh-enabled: true #默认自动刷新 refresh-enabled: true #默认自动刷新
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<configuration debug="true">
<!-- 此xml在spring-boot-1.5.3.RELEASE.jar里 -->
<include resource="org/springframework/boot/logging/logback/defaults.xml" />
<!--控制台打印日志,不需要配置appender了-->
<include resource="org/springframework/boot/logging/logback/console-appender.xml" />
<!-- 开启后可以通过jmx动态控制日志级别(springboot Admin的功能) -->
<!--<jmxConfigurator/>-->
<!--总日志-->
<appender name="ALL-FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
<!--日志存放路径-->
<file>../logs/springboot-mybatis.log</file>
<encoder>
<!-- pattern节点,用来设置日志的输入格式 -->
<pattern>%date [%level] [%thread] %logger{60} [%file : %line] %msg%n</pattern>
<!--文件编码格式-->
<charset>UTF-8</charset>
</encoder>
<!-- 滚动策略 -->
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<!-- 添加.gz 历史日志会启用压缩 大大缩小日志文件所占空间 -->
<fileNamePattern>../logs/springboot-mybatis.log.%d{yyyy-MM-dd}.log</fileNamePattern>
<!-- 保留30天日志 -->
<maxHistory>30</maxHistory>
<!--文件最大存储-->
<totalSizeCap>10GB</totalSizeCap>
</rollingPolicy>
</appender>
<!--错误日志-->
<appender name="ERROR-FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
<!--日志存放路径-->
<file>../logs/error.log</file>
<encoder>
<pattern>%date [%level] [%thread] %logger{60} [%file : %line] %msg%n</pattern>
<!--文件编码格式-->
<charset>UTF-8</charset>
</encoder>
<!-- 滚动策略 -->
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<!-- 添加.gz 历史日志会启用压缩 大大缩小日志文件所占空间 -->
<fileNamePattern>../logs/error.%d{yyyy-MM-dd}.log</fileNamePattern>
<!-- 保留30天日志 -->
<maxHistory>30</maxHistory>
<!--文件最大存储-->
<totalSizeCap>10GB</totalSizeCap>
</rollingPolicy>
<!-- 过滤日志 -->
<filter class="ch.qos.logback.classic.filter.LevelFilter">
<level>ERROR</level>
<!-- 用于配置符合过滤条件的操作 -->
<onMatch>ACCEPT</onMatch>
<!-- 用于配置不符合过滤条件的操作 -->
<onMismatch>DENY</onMismatch>
</filter>
</appender>
<logger name="com.kwan.springbootkwan" level="INFO"/>
<root level="INFO">
<!--控制台打印日志-->
<appender-ref ref="CONSOLE"/>
<appender-ref ref="ALL-FILE"/>
<appender-ref ref="ERROR-FILE"/>
</root>
</configuration>
\ No newline at end of file
<?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.kwan.springcloudalibaba.mapper.UserMapper">
<select id="getUserByName" resultType="com.kwan.springcloudalibaba.entity.User">
SELECT *
FROM user
WHERE sex = #{sex}
</select>
</mapper>
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册