diff --git a/pom.xml b/pom.xml index 40e3b34..88fe444 100644 --- a/pom.xml +++ b/pom.xml @@ -20,6 +20,8 @@ UTF-8 + + 22 @@ -37,5 +39,61 @@ org.springframework.boot spring-boot-starter-data-redis + + + org.springframework.boot + spring-boot-starter-jdbc + + + + com.mysql + mysql-connector-j + runtime + + + + org.springframework.boot + spring-boot-starter-test + test + + + org.junit.vintage + junit-vintage-engine + + + + + + org.mybatis.spring.boot + mybatis-spring-boot-starter + 3.0.3 + + + + + + javamemories + + + + + org.springframework.boot + spring-boot-maven-plugin + 3.2.3 + + + + + repackage + + + + + + cn.mayiming.App + + + + diff --git a/src/main/java/cn/mayiming/App.java b/src/main/java/cn/mayiming/App.java index 72f4bda..c5d051d 100644 --- a/src/main/java/cn/mayiming/App.java +++ b/src/main/java/cn/mayiming/App.java @@ -1,8 +1,10 @@ package cn.mayiming; +import org.mybatis.spring.annotation.MapperScan; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; +@MapperScan("cn.mayiming.Mapper") @SpringBootApplication public class App { diff --git a/src/main/java/cn/mayiming/Controller/Redis/Redistest.java b/src/main/java/cn/mayiming/Controller/Redis/Redistest.java index 71b1232..9ecfabe 100644 --- a/src/main/java/cn/mayiming/Controller/Redis/Redistest.java +++ b/src/main/java/cn/mayiming/Controller/Redis/Redistest.java @@ -8,7 +8,6 @@ import org.springframework.web.bind.annotation.RestController; import java.util.concurrent.TimeUnit; - @RestController public class Redistest { @Autowired diff --git a/src/main/java/cn/mayiming/Controller/User.java b/src/main/java/cn/mayiming/Controller/User.java deleted file mode 100644 index 9991bc9..0000000 --- a/src/main/java/cn/mayiming/Controller/User.java +++ /dev/null @@ -1,17 +0,0 @@ -package cn.mayiming.Controller; - -import org.springframework.web.bind.annotation.PostMapping; -import org.springframework.web.bind.annotation.RestController; - -import java.util.HashMap; -import java.util.Map; - -@RestController -public class User { - @PostMapping("/login") - public Map login() { - Map map = new HashMap<>(); - map.put("username", "admin"); - return map; - } -} diff --git a/src/main/java/cn/mayiming/Controller/UserCon/UserBody.java b/src/main/java/cn/mayiming/Controller/UserCon/UserBody.java new file mode 100644 index 0000000..1be8081 --- /dev/null +++ b/src/main/java/cn/mayiming/Controller/UserCon/UserBody.java @@ -0,0 +1,22 @@ +package cn.mayiming.Controller.UserCon; + +public class UserBody { + 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; + } + + public String username; + public String password; +} diff --git a/src/main/java/cn/mayiming/Controller/UserCon/UserController.java b/src/main/java/cn/mayiming/Controller/UserCon/UserController.java new file mode 100644 index 0000000..2db35f9 --- /dev/null +++ b/src/main/java/cn/mayiming/Controller/UserCon/UserController.java @@ -0,0 +1,46 @@ +package cn.mayiming.Controller.UserCon; + +import cn.mayiming.Mapper.UserMapper; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.jdbc.core.JdbcTemplate; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.RequestBody; +import org.springframework.web.bind.annotation.RestController; + +import java.util.HashMap; +import java.util.Map; + +@RestController +public class UserController { + + @Autowired + private UserMapper userMapper; + + @PostMapping("/login") + public Map login(@RequestBody UserBody user) { + Map result = new HashMap<>(); + try { + String dbPassword = userMapper.getPasswordByUsername(user.getUsername()); + String username = user.getUsername(); + String password = user.getPassword(); + // 3. 编写 SQL 查询数据库中的用户密码 + // 用 ? 占位符,防止 SQL 注入(必须!) + + // 4. 验证密码(实际项目要加密对比,这里简单演示) + if (password != null && password.equals(dbPassword)) { + result.put("code", 200); + result.put("msg", "登录成功"); + result.put("data", username); + } else { + result.put("code", 400); + result.put("msg", "密码错误"); + } + } catch (Exception e) { + // 捕获“用户不存在”等异常 + result.put("code", 404); + result.put("msg", "用户名不存在或登录失败"); + result.put("error", e.getMessage()); + } + return result; + } +} diff --git a/src/main/java/cn/mayiming/Mapper/UserMapper.java b/src/main/java/cn/mayiming/Mapper/UserMapper.java new file mode 100644 index 0000000..874dde2 --- /dev/null +++ b/src/main/java/cn/mayiming/Mapper/UserMapper.java @@ -0,0 +1,81 @@ +package cn.mayiming.Mapper; +import cn.mayiming.Controller.UserCon.UserBody; +import org.apache.ibatis.annotations.*; + +import org.springframework.stereotype.Repository; + +import java.util.List; + +@Repository +public interface UserMapper { + @Select("SELECT password FROM user WHERE username = #{username}") + String getPasswordByUsername(String username); + /** + * 根据ID查询用户(单条查询) + * @param id 用户ID + * @return 完整用户信息 + */ + @Select("SELECT id, username, password, nickname, create_time AS createTime FROM user WHERE id = #{id}") + UserBody getUserById(Long id); + + /** + * 根据用户名查询完整用户信息(登录/校验用) + * @param username 用户名 + * @return 完整用户信息 + */ + @Select("SELECT id, username, password, nickname, create_time AS createTime FROM user WHERE username = #{username}") + UserBody getUserByUsername(String username); + + /** + * 查询所有用户(分页建议用 MyBatis-Plus,这里先实现全量) + * @return 用户列表 + */ + @Select("SELECT id, username, password, nickname, create_time AS createTime FROM user") + List listAllUsers(); + + // ==================== 增 ==================== + /** + * 新增用户(自增ID) + * @param user 用户实体 + * @return 影响行数(1=成功,0=失败) + */ + @Insert("INSERT INTO user (username, password, nickname, create_time) VALUES (#{username}, #{password}, #{nickname}, #{createTime})") + @Options(useGeneratedKeys = true, keyProperty = "id") // 自动返回自增的ID到user对象的id字段 + int insertUser(UserBody user); + + // ==================== 改 ==================== + /** + * 根据ID修改用户昵称 + * @param id 用户ID + * @param nickname 新昵称 + * @return 影响行数 + */ + @Update("UPDATE user SET nickname = #{nickname} WHERE id = #{id}") + int updateNicknameById(@Param("id") Long id, @Param("nickname") String nickname); + + /** + * 根据用户名修改密码 + * @param username 用户名 + * @param newPassword 新密码 + * @return 影响行数 + */ + @Update("UPDATE user SET password = #{newPassword} WHERE username = #{username}") + int updatePasswordByUsername(@Param("username") String username, @Param("newPassword") String password); + + // ==================== 删 ==================== + /** + * 根据ID删除用户 + * @param id 用户ID + * @return 影响行数 + */ + @Delete("DELETE FROM user WHERE id = #{id}") + int deleteUserById(Long id); + + /** + * 根据用户名删除用户 + * @param username 用户名 + * @return 影响行数 + */ + @Delete("DELETE FROM user WHERE username = #{username}") + int deleteUserByUsername(String username); +} diff --git a/src/main/resources/application.yml b/src/main/resources/application.yml index 29d2388..ce46e84 100644 --- a/src/main/resources/application.yml +++ b/src/main/resources/application.yml @@ -6,4 +6,19 @@ spring: port: 6379 password: "" database: 0 - timeout: 10000 \ No newline at end of file + timeout: 10000 + datasource: + # 连接 URL(关键:test_db 是你要连接的数据库名,需提前创建) + url: jdbc:mysql://rm-f8z6oc5a03331500p8o.mysql.rds.aliyuncs.com:3306/test?useUnicode=true&characterEncoding=utf8&useSSL=false&serverTimezone=Asia/Shanghai&allowPublicKeyRetrieval=true + # 数据库用户名 + username: root + # 数据库密码(替换为你的 MySQL 密码) + password: Root123456 + # 驱动类名(MySQL 8.x 用这个,5.x 改为 com.mysql.jdbc.Driver) + driver-class-name: com.mysql.cj.jdbc.Driver + # 连接池配置(可选,提升性能) + hikari: + maximum-pool-size: 10 # 最大连接数 + minimum-idle: 2 # 最小空闲连接数 + idle-timeout: 60000 # 空闲连接超时时间(毫秒) + connection-timeout: 30000 # 连接超时时间(毫秒) \ No newline at end of file diff --git a/src/test/java/cn/mayiming/AppTest.java b/src/test/java/cn/mayiming/AppTest.java index 2244532..c2ca5ab 100644 --- a/src/test/java/cn/mayiming/AppTest.java +++ b/src/test/java/cn/mayiming/AppTest.java @@ -3,18 +3,27 @@ package cn.mayiming; import junit.framework.Test; import junit.framework.TestCase; import junit.framework.TestSuite; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.jdbc.core.JdbcTemplate; /** * Unit test for simple App. */ +@SpringBootTest public class AppTest extends TestCase { + @Autowired + private JdbcTemplate jdbcTemplate; + + /** * Create the test case * * @param testName name of the test case */ + public AppTest( String testName ) { super( testName ); @@ -36,3 +45,7 @@ public class AppTest assertTrue( true ); } } + + + + diff --git a/target/classes/application.yml b/target/classes/application.yml deleted file mode 100644 index 29d2388..0000000 --- a/target/classes/application.yml +++ /dev/null @@ -1,9 +0,0 @@ -server: - port: 9090 -spring: - data: - redis: - port: 6379 - password: "" - database: 0 - timeout: 10000 \ No newline at end of file