完成学习nacos请求转发
This commit is contained in:
@@ -36,6 +36,18 @@
|
||||
<groupId>com.alibaba.cloud</groupId>
|
||||
<artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
|
||||
</dependency>
|
||||
|
||||
<!-- MyBatis 版本管理 -->
|
||||
<dependency>
|
||||
<groupId>org.mybatis.spring.boot</groupId>
|
||||
<artifactId>mybatis-spring-boot-starter</artifactId>
|
||||
</dependency>
|
||||
<!-- MySQL 驱动(显式绑定版本) -->
|
||||
<dependency>
|
||||
<groupId>com.mysql</groupId>
|
||||
<artifactId>mysql-connector-j</artifactId>
|
||||
<scope>runtime</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
<!-- 构建配置:确保编译和打包正常 -->
|
||||
<build>
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package cn.mayiming;
|
||||
|
||||
|
||||
import org.mybatis.spring.annotation.MapperScan;
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
|
||||
@@ -8,7 +9,9 @@ import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
* Hello world!
|
||||
*
|
||||
*/
|
||||
|
||||
@SpringBootApplication
|
||||
@MapperScan("cn.mayiming.Mapper")
|
||||
public class App
|
||||
{
|
||||
public static void main( String[] args )
|
||||
|
||||
@@ -0,0 +1,21 @@
|
||||
package cn.mayiming.Controller;
|
||||
|
||||
import cn.mayiming.Mapper.UserMapper;
|
||||
import cn.mayiming.Service.UserService;
|
||||
import cn.mayiming.entity.User;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
import org.springframework.web.bind.annotation.RequestBody;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
@RestController
|
||||
public class UserController {
|
||||
|
||||
@Autowired
|
||||
private UserMapper userMapper;
|
||||
|
||||
@PostMapping("/user")
|
||||
public User getUser(@RequestBody User user) {
|
||||
return userMapper.selectByUsername(user.getUsername());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,65 @@
|
||||
package cn.mayiming.Mapper;
|
||||
|
||||
import cn.mayiming.entity.User;
|
||||
import org.apache.ibatis.annotations.*;
|
||||
import org.springframework.stereotype.Repository;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@Repository
|
||||
public interface UserMapper {
|
||||
/**
|
||||
* 根据ID查询用户
|
||||
* @param id 用户ID
|
||||
* @return 用户信息
|
||||
*/
|
||||
@Select("SELECT id, username, password, nickname FROM user WHERE id = #{id}")
|
||||
User selectById(@Param("id") Integer id); // 注意:id类型改为Integer(对应表的int)
|
||||
|
||||
/**
|
||||
* 根据用户名查询用户
|
||||
* @param username 用户名
|
||||
* @return 用户信息
|
||||
*/
|
||||
@Select("SELECT id, username, password, nickname FROM user WHERE username = #{username}")
|
||||
User selectByUsername(@Param("username") String username);
|
||||
|
||||
/**
|
||||
* 新增用户(自动回填自增ID)
|
||||
* @param user 用户对象
|
||||
* @return 影响行数
|
||||
*/
|
||||
@Insert("INSERT INTO user (username, password, nickname) " +
|
||||
"VALUES (#{username}, #{password}, #{nickname})")
|
||||
// 适配int类型自增主键,keyProperty对应实体类的id属性
|
||||
@Options(useGeneratedKeys = true, keyProperty = "id", keyColumn = "id")
|
||||
int insert(User user);
|
||||
|
||||
/**
|
||||
* 更新用户信息(全字段更新)
|
||||
* @param user 用户对象(含要更新的字段)
|
||||
* @return 影响行数
|
||||
*/
|
||||
@Update("UPDATE user SET " +
|
||||
"username = #{username}, " +
|
||||
"password = #{password}, " +
|
||||
"nickname = #{nickname} " +
|
||||
"WHERE id = #{id}")
|
||||
int updateById(User user);
|
||||
|
||||
/**
|
||||
* 删除用户
|
||||
* @param id 用户ID
|
||||
* @return 影响行数
|
||||
*/
|
||||
@Delete("DELETE FROM user WHERE id = #{id}")
|
||||
int deleteById(@Param("id") Integer id);
|
||||
|
||||
/**
|
||||
* 查询所有用户
|
||||
* @return 用户列表
|
||||
*/
|
||||
@Select("SELECT id, username, password, nickname FROM user")
|
||||
List<User> selectAll();
|
||||
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
package cn.mayiming.Service;
|
||||
|
||||
import cn.mayiming.Mapper.UserMapper;
|
||||
import cn.mayiming.entity.User;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@Service
|
||||
public class UserService {
|
||||
@Autowired
|
||||
private UserMapper userMapper;
|
||||
|
||||
public int addUser() {
|
||||
User user = new User();
|
||||
user.setUsername("test01");
|
||||
user.setPassword("123456");
|
||||
// 新增后id会自动回填
|
||||
int rows = userMapper.insert(user);
|
||||
return rows;
|
||||
}
|
||||
|
||||
// 根据用户名查询
|
||||
public User getUserByUsername(String username) {
|
||||
return userMapper.selectByUsername(username);
|
||||
}
|
||||
|
||||
// 查询所有用户
|
||||
public List<User> getAllUsers() {
|
||||
return userMapper.selectAll();
|
||||
}
|
||||
}
|
||||
22
user-service/src/main/java/cn/mayiming/entity/User.java
Normal file
22
user-service/src/main/java/cn/mayiming/entity/User.java
Normal file
@@ -0,0 +1,22 @@
|
||||
package cn.mayiming.entity;
|
||||
|
||||
public class User {
|
||||
public String getUsername() {
|
||||
return username;
|
||||
}
|
||||
|
||||
public void setUsername(String username) {
|
||||
this.username = username;
|
||||
}
|
||||
|
||||
public String getPassword() {
|
||||
return password;
|
||||
}
|
||||
|
||||
public void setPassword(String password) {
|
||||
this.password = password;
|
||||
}
|
||||
|
||||
String username;
|
||||
String password;
|
||||
}
|
||||
@@ -1,5 +1,5 @@
|
||||
server:
|
||||
port: 9090
|
||||
port: 9091
|
||||
spring:
|
||||
application:
|
||||
name: user-service
|
||||
@@ -14,8 +14,27 @@ spring:
|
||||
# 配置管理配置(如果引入了 config 依赖才需要)
|
||||
config:
|
||||
server-addr: localhost:8848 # 和 discovery 一致
|
||||
file-extension: yaml # 配置文件格式(yaml/yml/properties)
|
||||
file-extension: yaml
|
||||
namespace: public
|
||||
group: DEFAULT_GROUP
|
||||
config:
|
||||
import: nacos:${spring.application.name}.${spring.cloud.nacos.config.file-extension}?server-addr=${spring.cloud.nacos.config.server-addr}
|
||||
import: nacos:${spring.application.name}.${spring.cloud.nacos.config.file-extension}?server-addr=${spring.cloud.nacos.config.server-addr}
|
||||
datasource:
|
||||
# 数据库驱动类(MySQL 8.x 用 com.mysql.cj.jdbc.Driver,5.x 用 com.mysql.jdbc.Driver)
|
||||
driver-class-name: com.mysql.cj.jdbc.Driver
|
||||
# 数据库连接 URL(替换为你的数据库地址、端口、库名,如 user_db)
|
||||
url: jdbc:mysql://rm-f8z6oc5a03331500p8o.mysql.rds.aliyuncs.com:3306/test?useUnicode=true&characterEncoding=utf8&useSSL=false&serverTimezone=Asia/Shanghai&allowPublicKeyRetrieval=true
|
||||
# 数据库用户名(默认 root,根据实际情况修改)
|
||||
username: root
|
||||
# 数据库密码(替换为你的 MySQL 密码)
|
||||
password: Root123456
|
||||
# 可选:连接池配置(推荐使用 HikariCP,Spring Boot 2.x 默认)
|
||||
hikari:
|
||||
# 连接池最大连接数
|
||||
maximum-pool-size: 10
|
||||
# 连接池最小空闲连接数
|
||||
minimum-idle: 2
|
||||
# 连接超时时间(毫秒)
|
||||
connection-timeout: 30000
|
||||
# 连接最大存活时间(毫秒)
|
||||
max-lifetime: 1800000
|
||||
Reference in New Issue
Block a user