增加查询用户接口

master
pan 5 years ago
parent 55b7f36048
commit 476625d359
  1. 3
      src/main/java/com/share/help/Constants.java
  2. 29
      src/main/java/com/share/help/Util.java
  3. 2
      src/main/java/com/share/help/controller/ActivityController.java
  4. 26
      src/main/java/com/share/help/controller/UserController.java
  5. 9
      src/main/java/com/share/help/dao/ActivityMapper.java
  6. 26
      src/main/java/com/share/help/dao/UserMapper.java
  7. 49
      src/main/java/com/share/help/form/FindUserForm.java
  8. 20
      src/main/java/com/share/help/res/account/FindUserRes.java
  9. 2
      src/main/java/com/share/help/res/activity/ActivitySimple.java
  10. 8
      src/main/java/com/share/help/res/activity/FindActivityRes.java
  11. 30
      src/main/java/com/share/help/service/ActivityService.java
  12. 5
      src/main/java/com/share/help/service/ImageService.java
  13. 33
      src/main/java/com/share/help/service/UserService.java
  14. 2
      src/main/resources/mapping/ActivityMapper.xml
  15. 19
      src/test/java/com/share/help/HelpApplicationTests.java

@ -22,4 +22,7 @@ public class Constants {
//查找活动列表
public static final String ACTIVITY_INTERFACE_FIND = "/find";
//查找用户接口
public static final String USER_INTERFACE_FIND = "/find";
}

@ -0,0 +1,29 @@
package com.share.help;
import java.util.ArrayList;
import java.util.List;
public class Util {
/**
* 按分割大小切割子集合
* @param dataList 列表数据
* @param splitSize 分割大小
* @param <T> 数据类型
* @return 返回包含所有子集的集合
*/
public static <T> List<List<T>> splitList(Class<T> c,List<T> dataList, int splitSize){
List<List<T>> splitList=new ArrayList<>();
if(dataList.size()<splitSize){
splitList.add(dataList);
}else {
for (int i = splitSize, j = 0; i < dataList.size(); j = i, i += splitSize) {
splitList.add(dataList.subList(j, i));
if (i + splitSize >= dataList.size()) {
splitList.add(dataList.subList(i, dataList.size()));
}
}
}
return splitList;
}
}

@ -3,10 +3,10 @@ package com.share.help.controller;
import com.share.help.Constants;
import com.share.help.form.FindActivityForm;
import com.share.help.form.SendHelpForm;
import com.share.help.res.FindActivityRes;
import com.share.help.res.JSONResponse;
import com.share.help.res.Result;
import com.share.help.res.SendHelpRes;
import com.share.help.res.activity.FindActivityRes;
import com.share.help.service.ActivityService;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;

@ -1,20 +1,19 @@
package com.share.help.controller;
import com.share.help.Constants;
import com.share.help.form.FindUserForm;
import com.share.help.form.UserLoginForm;
import com.share.help.form.UserRegisterForm;
import com.share.help.res.JSONResponse;
import com.share.help.res.Result;
import com.share.help.res.account.FindUserRes;
import com.share.help.res.account.LoginRes;
import com.share.help.res.account.RegisterRes;
import com.share.help.service.UserService;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.*;
/**
* 用户接口
@ -45,6 +44,11 @@ public class UserController {
}
/**
* 登录
* @param userLoginForm 登陆表单
* @return 返回登陆结果
*/
@PostMapping(Constants.USER_INTERFACE_LOGIN)
public JSONResponse<LoginRes.Res,LoginRes> login(UserLoginForm userLoginForm){
if(StringUtils.isNoneEmpty(userLoginForm.getUserId(),userLoginForm.getPassword())){
@ -53,4 +57,18 @@ public class UserController {
return new JSONResponse<>(LoginRes.Res.form_error,Result.FAIL);
}
}
/**
* 查找用户
* @param findUserForm 查找表单
* @return 返回用户列表
*/
@GetMapping(Constants.USER_INTERFACE_FIND)
public JSONResponse<FindUserRes.Res,FindUserRes> find(FindUserForm findUserForm){
if(findUserForm.getCurrentPage()!=null){
return userService.find(findUserForm);
}else{
return new JSONResponse<>(FindUserRes.Res.fail,Result.FAIL);
}
}
}

@ -1,8 +1,8 @@
package com.share.help.dao;
import com.share.help.entity.ActivityEntity;
import com.share.help.res.ActivitySimple;
import com.share.help.res.FindActivityRes;
import com.share.help.res.activity.ActivitySimple;
import com.share.help.res.activity.FindActivityRes;
import org.apache.ibatis.annotations.*;
import java.util.List;
@ -47,6 +47,11 @@ public interface ActivityMapper {
@ResultMap("activitySimpleMap")
List<ActivitySimple> getPage(@Param("start") Integer start, @Param("count") Integer count);
/**
* 获取活动列表分页数
* @param pageSize 分页大小
* @return 返回分页信息
*/
@Select("select count(*) as count,ceil(count(*)/#{pageSize}) as totalPage from activity")
FindActivityRes count(@Param("pageSize") Integer pageSize);
}

@ -1,6 +1,7 @@
package com.share.help.dao;
import com.share.help.entity.UserEntity;
import com.share.help.res.account.FindUserRes;
import org.apache.ibatis.annotations.*;
import java.util.List;
@ -71,7 +72,28 @@ public interface UserMapper {
* @param count 查询个数
* @return 返回用户列表
*/
@Select("select * from user limit #{start},#{count}")
@Select( " <script>select * from user " +
"<where>"+
"<if test='userTypes.size>0'>and user_type in <foreach collection='userTypes' open='(' item='userType' separator=',' close=')'> #{userType}</foreach></if>"+
"<if test='info!=null and info!=\"\"'>and info like '%${info}%'</if>"+
"<if test='serviceAddress!=null and serviceAddress!=\"\"'>and service_address = #{serviceAddress}</if>"+
"</where></script>" )
@ResultMap("userMap")
List<UserEntity> getPage(@Param("start") long start,@Param("count") long count);
List<UserEntity> getPage(@Param("start") Integer start, @Param("count") Integer count,
@Param("userTypes") List<String> userTypes,@Param("info") String info,@Param("serviceAddress") String serviceAddress);
/**
* 获取志愿者分页数
* @param pageSize 分页大小
* @return 返回分页信息
*/
@Select( " <script>" +
"select count(*) as count,ceil(count(*)/#{pageSize}) as totalPage from user" +
"<where>"+
"<if test='userTypes.size>0'>and user_type in <foreach collection='userTypes' open='(' item='userType' separator=',' close=')'> #{userType}</foreach></if>"+
"<if test='info!=null and info!=\"\"'>and info like '%${info}%'</if>"+
"<if test='serviceAddress!=null and serviceAddress!=\"\"'>and service_address = #{serviceAddress}</if>"+
"</where></script>" )
FindUserRes count(@Param("pageSize") Integer pageSize, @Param("userTypes") List<String> userTypes,@Param("info")
String info,@Param("serviceAddress") String serviceAddress);
}

@ -0,0 +1,49 @@
package com.share.help.form;
import java.util.List;
/**
* 查找用户列表
*/
public class FindUserForm {
//查找分页
private Integer currentPage;
//查找用户类型
private List<String> userTypes;
//描述信息
private String info;
//服务地点
private String serviceAddress;
public String getInfo() {
return info;
}
public void setInfo(String info) {
this.info = info;
}
public String getServiceAddress() {
return serviceAddress;
}
public void setServiceAddress(String serviceAddress) {
this.serviceAddress = serviceAddress;
}
public Integer getCurrentPage() {
return currentPage;
}
public void setCurrentPage(Integer currentPage) {
this.currentPage = currentPage;
}
public List<String> getUserTypes() {
return userTypes;
}
public void setUserTypes(List<String> userTypes) {
this.userTypes = userTypes;
}
}

@ -0,0 +1,20 @@
package com.share.help.res.account;
import com.share.help.entity.UserEntity;
import com.share.help.res.Page;
import java.util.List;
/**
* 用户列表
*/
public class FindUserRes extends Page<List<List<UserEntity>>> {
public enum Res{
ok,
// 空数据
empty,
fail
}
}

@ -1,4 +1,4 @@
package com.share.help.res;
package com.share.help.res.activity;
/**
* 活动列表实体

@ -1,15 +1,19 @@
package com.share.help.res;
package com.share.help.res.activity;
import com.share.help.res.Page;
import java.util.List;
/**
* 查找活动列表结果
*/
public class FindActivityRes extends Page<List<List<ActivitySimple>>>{
public class FindActivityRes extends Page<List<List<ActivitySimple>>> {
public enum Res{
//成功
ok,
// 空数据
empty,
//失败
fail
}

@ -1,27 +1,34 @@
package com.share.help.service;
import com.share.help.Util;
import com.share.help.dao.ActivityMapper;
import com.share.help.entity.ActivityEntity;
import com.share.help.form.FindActivityForm;
import com.share.help.form.SendHelpForm;
import com.share.help.res.*;
import com.share.help.res.JSONResponse;
import com.share.help.res.Result;
import com.share.help.res.SendHelpRes;
import com.share.help.res.activity.ActivitySimple;
import com.share.help.res.activity.FindActivityRes;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;
/**
* 活动服务层
*/
@Service
public class ActivityService extends ImageService{
public class ActivityService{
@Autowired
private ActivityMapper activityMapper;
@Autowired
private ImageService imageService;
//活动分页配置
@Value("${activity.page-size}")
private int pageSize;
@ -45,7 +52,7 @@ public class ActivityService extends ImageService{
activityEntity.setActivityStartTime(sendHelpForm.getActivityStartTime());
activityEntity.setActivityEndTime(sendHelpForm.getActivityEndTime());
//保存活动背景图
String imageName=saveImg(sendHelpForm.getActivityImgFile());
String imageName=imageService.saveImg(sendHelpForm.getActivityImgFile());
if(imageName==null){
return new JSONResponse<>(SendHelpRes.fail, Result.FAIL);
}
@ -64,22 +71,11 @@ public class ActivityService extends ImageService{
findActivityRes.setPageSize(pageSize);
findActivityRes.setCurrentPage(findActivityForm.getCurrentPage());
List<ActivitySimple> activitySimpleList=activityMapper.getPage((findActivityForm.getCurrentPage()-1)*pageSize,pageSize);
List<List<ActivitySimple>> dataList=new ArrayList<>();
if(!activitySimpleList.isEmpty()){
if(activitySimpleList.size()<rowSize){
dataList.add(activitySimpleList);
}else {
for (int i = rowSize, j = 0; i < activitySimpleList.size(); j = i, i += rowSize) {
dataList.add(activitySimpleList.subList(j, i));
if (i + rowSize >= activitySimpleList.size()) {
dataList.add(activitySimpleList.subList(i, activitySimpleList.size()));
}
}
}
findActivityRes.setBody(dataList);
findActivityRes.setBody(Util.splitList(ActivitySimple.class,activitySimpleList,rowSize));
return new JSONResponse<FindActivityRes.Res,FindActivityRes>(FindActivityRes.Res.ok,Result.OK).setBody(findActivityRes);
}else{
return new JSONResponse<>(FindActivityRes.Res.fail, Result.FAIL);
return new JSONResponse<>(FindActivityRes.Res.empty, Result.FAIL);
}
}

@ -3,14 +3,15 @@ package com.share.help.service;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import java.io.File;
import java.io.IOException;
import java.util.UUID;
public abstract class ImageService {
@Service
public class ImageService {
private enum ImageType{
jpg,
jpeg,

@ -1,11 +1,14 @@
package com.share.help.service;
import com.share.help.Util;
import com.share.help.dao.UserMapper;
import com.share.help.entity.UserEntity;
import com.share.help.form.FindUserForm;
import com.share.help.form.UserLoginForm;
import com.share.help.form.UserRegisterForm;
import com.share.help.res.JSONResponse;
import com.share.help.res.Result;
import com.share.help.res.account.FindUserRes;
import com.share.help.res.account.LoginRes;
import com.share.help.res.account.RegisterRes;
import org.springframework.beans.factory.annotation.Autowired;
@ -13,11 +16,13 @@ import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;
import java.util.List;
/**
* 用户服务层
*/
@Service
public class UserService extends ImageService{
public class UserService{
@Autowired
private UserMapper userMapper;
@ -25,6 +30,16 @@ public class UserService extends ImageService{
@Value("${user.register.time-score}")
private int timeScore;
//用户分页配置
@Value("${activity.page-size}")
private int pageSize;
@Value("${activity.row-size}")
private int rowSize;
@Autowired
private ImageService imageService;
/**
* 注册
* @param userRegisterForm 注册表单
@ -44,7 +59,7 @@ public class UserService extends ImageService{
userEntity.setName(userRegisterForm.getName());
userEntity.setSex(userRegisterForm.getSex());
//保存头像
String imageName=saveImg(userRegisterForm.getImgFile());
String imageName=imageService.saveImg(userRegisterForm.getImgFile());
if(imageName==null){
return new JSONResponse<>(RegisterRes.fail, Result.FAIL);
}
@ -71,4 +86,18 @@ public class UserService extends ImageService{
return userEntity==null?new JSONResponse<>(LoginRes.Res.valid_error,Result.FAIL)
:new JSONResponse<LoginRes.Res,LoginRes>(LoginRes.Res.ok,Result.OK).setBody(new LoginRes(userEntity));
}
public JSONResponse<FindUserRes.Res,FindUserRes> find(FindUserForm findUserForm){
FindUserRes findUserRes=userMapper.count(pageSize,findUserForm.getUserTypes(),findUserForm.getInfo(),findUserForm.getServiceAddress());
findUserRes.setPageSize(pageSize);
findUserRes.setCurrentPage(findUserForm.getCurrentPage());
List<UserEntity> userEntityList=userMapper.getPage((findUserForm.getCurrentPage()-1)*pageSize,pageSize,findUserForm.getUserTypes(),
findUserForm.getInfo(),findUserForm.getServiceAddress());
if(!userEntityList.isEmpty()){
findUserRes.setBody(Util.splitList(UserEntity.class,userEntityList,rowSize));
return new JSONResponse<FindUserRes.Res,FindUserRes>(FindUserRes.Res.ok,Result.OK).setBody(findUserRes);
}else{
return new JSONResponse<>(FindUserRes.Res.empty, Result.FAIL);
}
}
}

@ -14,7 +14,7 @@
<result property="serviceAddress" column="service_address"/>
</resultMap>
<resultMap id="activitySimpleMap" type="com.share.help.res.ActivitySimple">
<resultMap id="activitySimpleMap" type="com.share.help.res.activity.ActivitySimple">
<id property="activityId" column="activity_id"/>
<result property="title" column="title"/>
<result property="content" column="content"/>

@ -4,6 +4,7 @@ import com.share.help.dao.ActivityMapper;
import com.share.help.dao.UserMapper;
import com.share.help.entity.ActivityEntity;
import com.share.help.entity.UserEntity;
import com.share.help.res.account.FindUserRes;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
@ -18,13 +19,16 @@ import org.springframework.test.web.servlet.request.MockHttpServletRequestBuilde
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
import org.springframework.test.web.servlet.result.MockMvcResultHandlers;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import org.springframework.web.context.WebApplicationContext;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
@SpringBootTest
@MapperScans(@MapperScan("com.share.help.dao"))
@EnableTransactionManagement
class HelpApplicationTests {
@Autowired
@ -107,6 +111,21 @@ class HelpApplicationTests {
Assertions.assertTrue(userMapper.delete(userId),"删除成功");
}
/**
* 测试用户分页查询
*/
@Test
public void testFindUser(){
List<String> list=new ArrayList<String>(){{
add("help");
}};
FindUserRes findUserRes=userMapper.count(10,list,null,null);
List<UserEntity> userEntities=userMapper.getPage(0,10,list,null,null);
Assertions.assertFalse(userEntities.isEmpty());
}
/**
* 测试用户接口
*/

Loading…
Cancel
Save