Explorar el Código

feat:新增用户crud接口,用于测试前端

liuchuanwei hace 2 meses
padre
commit
b62763cec0

+ 65 - 0
src/main/java/com/anyway/favor/controller/UserController.java

@@ -0,0 +1,65 @@
+package com.anyway.favor.controller;
+
+import com.anyway.favor.model.User;
+import com.anyway.favor.service.UserService;
+import com.anyway.util.PageQuery;
+import com.anyway.util.R;
+import lombok.extern.slf4j.Slf4j;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.web.bind.annotation.*;
+
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
+/**
+ * 用户接口
+ *
+ * @author liuchuanwei
+ * @date 2024-08-15
+ */
+@Slf4j
+@RestController
+@RequestMapping("/user")
+public class UserController {
+
+    @Autowired
+    private UserService userService;
+
+
+    @PostMapping("/login")
+    public R<Void> login(@RequestBody User user) {
+        log.info("登录表单:{}", user);
+        User loginUser = userService.findByUserName(user.getUserName());
+        log.info("登录用户:{}", loginUser);
+        return R.ok();
+    }
+
+
+    @PostMapping("/list")
+    public R<List<User>> list(@RequestBody PageQuery<Map<String, Object>> pageQuery) {
+        Map<String, Object> terms = pageQuery.getTerms(new HashMap<>());
+        List<User> userList = userService.findPage(pageQuery);
+        return R.page(userList, pageQuery.getPage());
+    }
+
+    @PostMapping("/save")
+    public R<Void> save(@RequestBody User user) {
+        log.info("用户:{}", user);
+        userService.save(user);
+        return R.ok();
+    }
+
+    @GetMapping("/delete")
+    public R<Void> delete(@RequestParam Long id) {
+        log.info("用户:{}", id);
+        userService.deleteById(id);
+        return R.ok();
+    }
+
+    @GetMapping("/detail")
+    public R<User> detail(@RequestParam Long id) {
+        log.info("用户:{}", id);
+        return R.data(userService.findById(id));
+    }
+}

+ 7 - 0
src/main/java/com/anyway/favor/dao/UserDao.java

@@ -59,4 +59,11 @@ public interface UserDao {
      */
     int deleteById(Long id);
 
+    /**
+     * 根据用户名查找
+     *
+     * @param userName
+     * @return
+     */
+    User findByUserName(String userName);
 }

+ 37 - 4
src/main/java/com/anyway/favor/service/UserService.java

@@ -1,6 +1,7 @@
 package com.anyway.favor.service;
 
 import com.anyway.favor.model.User;
+import com.anyway.util.PageQuery;
 
 import java.util.List;
 import java.util.Map;
@@ -21,6 +22,14 @@ public interface UserService {
     List<User> findAll();
 
     /**
+     * 分页查询
+     *
+     * @param pageQuery
+     * @return
+     */
+    List<User> findPage(PageQuery<Map<String, Object>> pageQuery);
+
+    /**
      * 根据条件查询列表
      *
      * @param map
@@ -29,20 +38,44 @@ public interface UserService {
     List<User> findByCondition(Map<String, Object> map);
 
     /**
+     * 根据用户名称查找
+     *
+     * @param userName
+     * @return
+     */
+    User findByUserName(String userName);
+
+    /**
+     * 添加
+     *
+     * @param user
+     * @return
+     */
+    boolean save(User user);
+
+    /**
      * 添加
      *
-     * @param favor
+     * @param user
      * @return
      */
-    boolean add(User favor);
+    boolean add(User user);
 
     /**
      * 更新
      *
-     * @param favor
+     * @param user
+     * @return
+     */
+    boolean update(User user);
+
+    /**
+     * 根据ID删除
+     *
+     * @param id
      * @return
      */
-    boolean update(User favor);
+    boolean deleteById(Long id);
 
     /**
      * 根据ID查询

+ 37 - 6
src/main/java/com/anyway/favor/service/impl/UserServiceImpl.java

@@ -3,6 +3,9 @@ package com.anyway.favor.service.impl;
 import com.anyway.favor.dao.UserDao;
 import com.anyway.favor.model.User;
 import com.anyway.favor.service.UserService;
+import com.anyway.util.PageQuery;
+import com.anyway.util.PageUtils;
+import com.github.pagehelper.PageHelper;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Service;
 
@@ -18,7 +21,7 @@ import java.util.Map;
 @Service
 public class UserServiceImpl implements UserService {
     @Autowired
-    private UserDao userMapper;
+    private UserDao userDao;
 
     @Override
     public List<User> findAll() {
@@ -26,22 +29,50 @@ public class UserServiceImpl implements UserService {
     }
 
     @Override
+    public List<User> findPage(PageQuery<Map<String, Object>> pageQuery) {
+        PageUtils.startPage(pageQuery.getPage());
+        List<User> userList = userDao.findByCondition(pageQuery.getTerms());
+        PageUtils.setPageTotal(userList, pageQuery.getPage());
+        return userList;
+    }
+
+    @Override
+    public User findByUserName(String userName) {
+        return userDao.findByUserName(userName);
+    }
+
+    @Override
     public List<User> findByCondition(Map<String, Object> map) {
         return null;
     }
 
     @Override
-    public boolean add(User favor) {
-        return false;
+    public boolean save(User user) {
+        if (user.getId() != null) {
+            return this.update(user);
+        } else {
+            return this.add(user);
+        }
     }
 
     @Override
-    public boolean update(User favor) {
-        return false;
+    public boolean add(User user) {
+        return userDao.add(user) > 0;
+    }
+
+    @Override
+    public boolean update(User user) {
+        return userDao.update(user) > 0;
+    }
+
+
+    @Override
+    public boolean deleteById(Long id) {
+        return userDao.deleteById(id) > 0;
     }
 
     @Override
     public User findById(Long id) {
-        return userMapper.findById(id);
+        return userDao.findById(id);
     }
 }

+ 1 - 0
src/main/resources/init.sql

@@ -31,6 +31,7 @@ create table t_person(
     tags varchar(255) comment '标签,json格式',
     address varchar(100) comment '详细地址',
     remark varchar(255) comment '备注',
+    weight int(4) comment '权重,标识亲疏远近',
     create_time datetime default CURRENT_TIMESTAMP comment '创建时间',
     create_by int(11) comment '创建者',
     modify_time datetime DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP comment '修改时间',

+ 9 - 2
src/main/resources/mapper/UserMapper.xml

@@ -22,7 +22,7 @@
         SELECT
             <include refid="allColumns" />
         FROM t_user u WHERE 1 = 1
-        <if test="user_name != null and user_name != ''">
+        <if test="@Ognl@isNotEmpty(userName)">
             AND u.user_name LIKE CONCAT('%', #{userName}, '%')
         </if>
         <if test="password != null and password != ''">
@@ -43,6 +43,13 @@
     </select>
 
     <!-- 根据主键查询用户表信息 -->
+    <select id="findByUserName" resultMap="userMap">
+        SELECT
+        <include refid="allColumns" />
+        FROM t_user u WHERE u.user_name = #{userName}
+    </select>
+
+    <!-- 根据主键查询用户表信息 -->
     <select id="findById" resultMap="userMap">
         SELECT
             <include refid="allColumns" />
@@ -77,4 +84,4 @@
         DELETE FROM t_user WHERE id = #{id}
     </delete>
 
-</mapper>
+</mapper>

+ 35 - 0
用户.http

@@ -0,0 +1,35 @@
+### 用户登录
+POST {{api_url}}/user/login
+Content-Type: application/json
+
+{
+  "userName": "liuchuanwei",
+  "password": "123456"
+}
+
+### 新增用户
+POST {{api_url}}/user/save
+Content-Type: application/json
+
+{
+  "userName": "张安9",
+  "password": "123456"
+}
+
+### 查询用户详情
+GET {{api_url}}/user/detail?id=1
+
+
+### 删除用户
+DELETE {{api_url}}/user/delete?id=77
+
+### 分页查询
+POST {{api_url}}/user/list
+Content-Type: application/json
+
+{
+  "page": {
+    "pageNo": 2,
+    "pageSize": 10
+  }
+}