ADD: add users
This commit is contained in:
30
docker/sql/db.sql
Normal file
30
docker/sql/db.sql
Normal file
@@ -0,0 +1,30 @@
|
|||||||
|
-- 确保启用 uuid-ossp 扩展(用于生成 UUID)
|
||||||
|
CREATE EXTENSION IF NOT EXISTS "uuid-ossp";
|
||||||
|
|
||||||
|
-- 创建 users 表
|
||||||
|
CREATE TABLE users
|
||||||
|
(
|
||||||
|
id CHAR(36) PRIMARY KEY DEFAULT uuid_generate_v4()::varchar,
|
||||||
|
username VARCHAR(255) NOT NULL UNIQUE,
|
||||||
|
password VARCHAR(255) NOT NULL,
|
||||||
|
nickname VARCHAR(255),
|
||||||
|
created_at TIMESTAMP WITH TIME ZONE DEFAULT NOW(),
|
||||||
|
updated_at TIMESTAMP WITH TIME ZONE DEFAULT NOW()
|
||||||
|
);
|
||||||
|
|
||||||
|
-- 创建触发器函数:自动更新 updated_at
|
||||||
|
CREATE OR REPLACE FUNCTION update_updated_at_column()
|
||||||
|
RETURNS TRIGGER AS
|
||||||
|
$$
|
||||||
|
BEGIN
|
||||||
|
NEW.updated_at = NOW();
|
||||||
|
RETURN NEW;
|
||||||
|
END;
|
||||||
|
$$ LANGUAGE plpgsql;
|
||||||
|
|
||||||
|
-- 创建触发器:在 users 表 UPDATE 时自动调用
|
||||||
|
CREATE TRIGGER trigger_update_users_updated_at
|
||||||
|
BEFORE UPDATE
|
||||||
|
ON users
|
||||||
|
FOR EACH ROW
|
||||||
|
EXECUTE FUNCTION update_updated_at_column();
|
||||||
@@ -1,5 +1,7 @@
|
|||||||
package com.xapg.energystoragesafety;
|
package com.xapg.energystoragesafety;
|
||||||
|
|
||||||
|
import com.mybatisflex.core.keygen.KeyGeneratorFactory;
|
||||||
|
import com.xapg.energystoragesafety.config.UUIDKeyGenerator;
|
||||||
import org.mybatis.spring.annotation.MapperScan;
|
import org.mybatis.spring.annotation.MapperScan;
|
||||||
import org.springframework.boot.SpringApplication;
|
import org.springframework.boot.SpringApplication;
|
||||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||||
@@ -7,6 +9,10 @@ import org.springframework.boot.autoconfigure.SpringBootApplication;
|
|||||||
@SpringBootApplication
|
@SpringBootApplication
|
||||||
@MapperScan("com.xapg.energystoragesafety.mapper")
|
@MapperScan("com.xapg.energystoragesafety.mapper")
|
||||||
public class EnergyStorageSafetyApplication {
|
public class EnergyStorageSafetyApplication {
|
||||||
|
static {
|
||||||
|
KeyGeneratorFactory.register("myUUID", new UUIDKeyGenerator());
|
||||||
|
}
|
||||||
|
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
SpringApplication.run(EnergyStorageSafetyApplication.class, args);
|
SpringApplication.run(EnergyStorageSafetyApplication.class, args);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,14 @@
|
|||||||
|
package com.xapg.energystoragesafety.config;
|
||||||
|
|
||||||
|
import com.mybatisflex.core.keygen.IKeyGenerator;
|
||||||
|
|
||||||
|
import java.util.UUID;
|
||||||
|
|
||||||
|
public class UUIDKeyGenerator implements IKeyGenerator {
|
||||||
|
@Override
|
||||||
|
public Object generate(Object entity, String keyColumn) {
|
||||||
|
String id = UUID.randomUUID().toString();
|
||||||
|
System.out.println("asedasd: " + id);
|
||||||
|
return id;
|
||||||
|
}
|
||||||
|
}
|
||||||
22
src/main/java/com/xapg/energystoragesafety/entity/Users.java
Normal file
22
src/main/java/com/xapg/energystoragesafety/entity/Users.java
Normal file
@@ -0,0 +1,22 @@
|
|||||||
|
package com.xapg.energystoragesafety.entity;
|
||||||
|
|
||||||
|
import com.mybatisflex.annotation.*;
|
||||||
|
import com.mybatisflex.core.mask.Masks;
|
||||||
|
import lombok.Data;
|
||||||
|
|
||||||
|
import java.sql.Timestamp;
|
||||||
|
|
||||||
|
@Data
|
||||||
|
@Table("users")
|
||||||
|
public class Users {
|
||||||
|
@Id(keyType = KeyType.Generator, value = "myUUID")
|
||||||
|
private String id;
|
||||||
|
private String username;
|
||||||
|
@ColumnMask(Masks.PASSWORD)
|
||||||
|
private String password;
|
||||||
|
private String nickname;
|
||||||
|
@Column(onInsertValue = "CURRENT_TIMESTAMP")
|
||||||
|
private Timestamp createdAt;
|
||||||
|
@Column(onInsertValue = "CURRENT_TIMESTAMP", onUpdateValue = "CURRENT_TIMESTAMP")
|
||||||
|
private Timestamp updatedAt;
|
||||||
|
}
|
||||||
@@ -0,0 +1,7 @@
|
|||||||
|
package com.xapg.energystoragesafety.mapper;
|
||||||
|
|
||||||
|
import com.mybatisflex.core.BaseMapper;
|
||||||
|
import com.xapg.energystoragesafety.entity.Users;
|
||||||
|
|
||||||
|
public interface UsersMapper extends BaseMapper<Users> {
|
||||||
|
}
|
||||||
@@ -0,0 +1,81 @@
|
|||||||
|
package com.xapg.energystoragesafety.services;
|
||||||
|
|
||||||
|
import cn.dev33.satoken.secure.BCrypt;
|
||||||
|
import com.mybatisflex.core.mask.MaskManager;
|
||||||
|
import com.mybatisflex.core.query.QueryWrapper;
|
||||||
|
import com.xapg.energystoragesafety.entity.Users;
|
||||||
|
import com.xapg.energystoragesafety.mapper.UsersMapper;
|
||||||
|
import org.springframework.stereotype.Service;
|
||||||
|
|
||||||
|
import java.util.concurrent.atomic.AtomicReference;
|
||||||
|
|
||||||
|
import static com.xapg.energystoragesafety.entity.table.UsersTableDef.USERS;
|
||||||
|
|
||||||
|
@Service
|
||||||
|
public class UsersService {
|
||||||
|
private final UsersMapper usersMapper;
|
||||||
|
|
||||||
|
public UsersService(UsersMapper usersMapper) {
|
||||||
|
this.usersMapper = usersMapper;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 根据用户名查询用户
|
||||||
|
public Users selectByUsername(String username) {
|
||||||
|
if (username == null || username.trim().isEmpty()) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
QueryWrapper query = QueryWrapper.create()
|
||||||
|
.select()
|
||||||
|
.from(USERS)
|
||||||
|
.where(USERS.USERNAME.eq(username));
|
||||||
|
return usersMapper.selectOneByQuery(query);
|
||||||
|
}
|
||||||
|
|
||||||
|
// 新增用户
|
||||||
|
public int insertUser(Users user) {
|
||||||
|
if (user == null || user.getUsername() == null || user.getUsername().trim().isEmpty()) {
|
||||||
|
throw new IllegalArgumentException("用户名不能为空");
|
||||||
|
}
|
||||||
|
Users exist = this.selectByUsername(user.getUsername());
|
||||||
|
if (exist != null) {
|
||||||
|
throw new RuntimeException("用户名已存在");
|
||||||
|
}
|
||||||
|
// 默认密码加密
|
||||||
|
user.setPassword(BCrypt.hashpw(user.getPassword(), BCrypt.gensalt()));
|
||||||
|
return usersMapper.insert(user);
|
||||||
|
}
|
||||||
|
|
||||||
|
// 更新用户信息(不允许修改用户名,密码)
|
||||||
|
public int updateByUserName(Users user) {
|
||||||
|
if (user == null || user.getUsername() == null || user.getUsername().trim().isEmpty()) {
|
||||||
|
throw new IllegalArgumentException("用户名不能为空");
|
||||||
|
}
|
||||||
|
Users old = this.selectByUsername(user.getUsername());
|
||||||
|
if (old == null) {
|
||||||
|
throw new RuntimeException("用户不存在");
|
||||||
|
}
|
||||||
|
// 保持原用户名
|
||||||
|
user.setUsername(null);
|
||||||
|
user.setPassword(null);
|
||||||
|
return usersMapper.update(user);
|
||||||
|
}
|
||||||
|
|
||||||
|
// 用户登录校验
|
||||||
|
public Boolean login(String username, String password) {
|
||||||
|
AtomicReference<Boolean> isLogin = new AtomicReference<>(Boolean.FALSE);
|
||||||
|
// 密码校验,取消脱敏处理
|
||||||
|
MaskManager.execWithoutMask(() -> {
|
||||||
|
Users user = this.selectByUsername(username);
|
||||||
|
// 找不到用户,用户名错误
|
||||||
|
if (user == null) {
|
||||||
|
throw new RuntimeException("用户名或密码错误");
|
||||||
|
}
|
||||||
|
// 密码错误
|
||||||
|
if (!BCrypt.checkpw(password, user.getPassword())) {
|
||||||
|
throw new RuntimeException("用户名或密码错误");
|
||||||
|
}
|
||||||
|
isLogin.set(Boolean.TRUE);
|
||||||
|
});
|
||||||
|
return isLogin.get();
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -6,7 +6,7 @@ import org.junit.jupiter.api.Test;
|
|||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.boot.test.context.SpringBootTest;
|
import org.springframework.boot.test.context.SpringBootTest;
|
||||||
|
|
||||||
import static com.xapg.energystoragesafety.Tables.METERS;
|
import static com.xapg.energystoragesafety.entity.table.MetersTableDef.METERS;
|
||||||
|
|
||||||
@SpringBootTest
|
@SpringBootTest
|
||||||
public class TDengineTest {
|
public class TDengineTest {
|
||||||
|
|||||||
50
src/test/java/com/xapg/energystoragesafety/UsersTest.java
Normal file
50
src/test/java/com/xapg/energystoragesafety/UsersTest.java
Normal file
@@ -0,0 +1,50 @@
|
|||||||
|
package com.xapg.energystoragesafety;
|
||||||
|
|
||||||
|
import com.mybatisflex.core.query.QueryWrapper;
|
||||||
|
import com.xapg.energystoragesafety.entity.Users;
|
||||||
|
import com.xapg.energystoragesafety.mapper.UsersMapper;
|
||||||
|
import com.xapg.energystoragesafety.services.UsersService;
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.boot.test.context.SpringBootTest;
|
||||||
|
|
||||||
|
import static com.xapg.energystoragesafety.entity.table.UsersTableDef.USERS;
|
||||||
|
|
||||||
|
@SpringBootTest
|
||||||
|
public class UsersTest {
|
||||||
|
@Autowired
|
||||||
|
private UsersMapper usersMapper;
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private UsersService usersService;
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void insertTest() {
|
||||||
|
QueryWrapper query = QueryWrapper.create().where("username = 'focus1'").from(USERS);
|
||||||
|
usersMapper.deleteByQuery(query);
|
||||||
|
|
||||||
|
Users users = new Users();
|
||||||
|
users.setUsername("focus1");
|
||||||
|
users.setPassword("plain_password");
|
||||||
|
users.setNickname("focus1");
|
||||||
|
usersService.insertUser(users);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void selectTest() {
|
||||||
|
// 根据用户名搜索
|
||||||
|
System.out.println(usersService.selectByUsername("focus1"));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void updateUser() {
|
||||||
|
Users users = usersService.selectByUsername("focus1");
|
||||||
|
users.setNickname("北溪入江流123");
|
||||||
|
usersService.updateByUserName(users);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void loginTest() {
|
||||||
|
System.out.println(usersService.login("focus1", "plain_password"));
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user