读取所有的列表
package com.xuecheng.manage_user.dao;
import com.xuecheng.framework.domain.nuoyun.User;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import java.util.List;
import java.util.Optional;
/**
* @author Administrator
* @version 1.0
**/
@SpringBootTest
@RunWith(SpringRunner.class)
public class TestDao {
@Autowired
UserRepository userRepository;
@Test
public void testCourseBaseRepository(){
Optional<User> optional = userRepository.findById("34993");
if(optional.isPresent()){
User user = optional.get();
System.out.println(user);
}
}
@Test
public void testFindUser(){
User user = userRepository.findUserById("34993");
System.out.println(user);
}
@Test
public void testFindAll(){
List<User> all = userRepository.findAll();
System.out.println(all);
}
}
模型
package com.xuecheng.framework.domain.nuoyun;
import lombok.Data;
import lombok.ToString;
import javax.persistence.*;
import java.io.Serializable;
/**
* Created by admin on 2018/2/10.
*/
@Data
@ToString
@Entity
@Table(name="ny_user")
//@GenericGenerator(name = "jpa-uuid", strategy = "uuid")
public class User implements Serializable {
private static final long serialVersionUID = -916357110051689499L;
@Id //声明此属性为主键。该属性值可以通过应该自身创建,但是Hibernate推荐通过Hibernate生成
// @GeneratedValue(generator = "jpa-uuid")
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(length = 32)
private String id;
private String openid;
private String unionid;
@Column(name="first_name")//数据库里的字段名称
private String firstName;//项目中中的字段名称
@Column(name="invite_code")//数据库里的字段名称
private String inviteCode;//项目中中的字段名称
@Column(name="wechat_name")
private String wechatName;
private String name;
private String type;
@Column(name="reg_ip")
private String regIp;
@Column(name="reg_time")
private String regTime;
private String photo;
@Column(name="auth_status")
private String authStatus;
@Column(name="auth_time")
private String authTime;
@Column(name="waiter_id")
private String waiterId;
@Column(name="master_type")
private String masterType;
@Column(name="client_type")
private String clientType;
@Column(name="room_limit")
private String roomLimit;
@Column(name="create_area")
private String createArea;
@Column(name="salesman")
private String salesman;
@Column(name="is_webrtc")
private String isWebrtc;
@Column(name="cdn_switch")
private String cdnSwitch;
@Column(name="push_extime")
private String pushExtime;
@Column(name="concurrency_limit")
private String concurrencyLimit;
}
request
package com.xuecheng.framework.domain.nuoyun.request;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
/**
* @author Administrator
* @version 1.0
* @create 2018-09-12 14:59
**/
@Data
public class QueryUserRequest {
//接收页面查询的查询条件
//站点id
//站点id
@ApiModelProperty("邀请码")
private String inviteCode;
//页面ID
private String type;
//页面名称
private String openid;
//别名
private String unionid;
//....
}
控制器
package com.xuecheng.manage_user.controller;
import com.xuecheng.api.nuoyun.UserControllerApi;
import com.xuecheng.framework.domain.nuoyun.User;
import com.xuecheng.framework.domain.nuoyun.ext.UserNode;
import com.xuecheng.framework.domain.nuoyun.request.QueryUserRequest;
import com.xuecheng.framework.model.response.QueryResponseResult;
import com.xuecheng.manage_user.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
/**
* @author Administrator
* @version 1.0
**/
@RestController
@RequestMapping("/nuoyun")
public class UserController implements UserControllerApi {
@Autowired
UserService userService;
@Override
@GetMapping("/User/list/{openid}/{type}")
public String findUserList(@PathVariable("openid") String openid, @PathVariable("type") String type) {
return userService.findUserList(openid,type);
}
@Override
@GetMapping("/Invite/list/{page}/{size}")
public QueryResponseResult findInviteList(@PathVariable("page") int page, @PathVariable("size") int size, QueryUserRequest queryUserRequest) {
return userService.findInviteList(page,size,queryUserRequest);
}
@Override
@GetMapping("/User/findUserById/{id}")
public String findUserById(@PathVariable("id") String id) {
return userService.findUserById(id);
}
@Override
public UserNode addUser(User user) {
return null;
}
}
api
package com.xuecheng.api.nuoyun;
import com.xuecheng.framework.domain.cms.request.QueryPageRequest;
import com.xuecheng.framework.domain.nuoyun.User;
import com.xuecheng.framework.domain.nuoyun.ext.UserNode;
import com.xuecheng.framework.domain.nuoyun.request.QueryUserRequest;
import com.xuecheng.framework.model.response.QueryResponseResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
/**
* Created by Administrator.
*/
@Api(value="用户管理接口",description = "用户管理接口,提供课程的增、删、改、查")
public interface UserControllerApi {
@ApiOperation("用户查询")
public String findUserList(String openid,String type);
//页面查询
@ApiOperation("分页查询页面列表")
@ApiImplicitParams({
@ApiImplicitParam(name="page",value = "页码",required=true,paramType="path",dataType="int"),
@ApiImplicitParam(name="size",value = "每页记录数",required=true,paramType="path",dataType="int")
})
public QueryResponseResult findInviteList(int page, int size, QueryUserRequest queryUserRequest);
@GetMapping("单个用户查询")
String findUserById(@PathVariable("id") String id);
@ApiOperation("添加")
UserNode addUser(@RequestBody User user);
}
service
package com.xuecheng.manage_user.service;
import com.xuecheng.framework.domain.nuoyun.User;
import com.xuecheng.framework.domain.nuoyun.request.QueryUserRequest;
import com.xuecheng.framework.model.response.CommonCode;
import com.xuecheng.framework.model.response.QueryResponseResult;
import com.xuecheng.framework.model.response.QueryResult;
import com.xuecheng.manage_user.dao.UserRepository;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.*;
import org.springframework.stereotype.Service;
import java.util.List;
/**
* @author Administrator
* @version 1.0
**/
@Service
public class UserService {
@Autowired
UserRepository userRepository;
//查询课程计划
public String findUserList(String openid,String type){
List<User> userList = userRepository.findByOpenidAndType(openid, type);
if(userList == null || userList.size()<=0){
//新添加一个课程的根结点
User user = new User();
user.setOpenid(openid);
user.setType("1");
user.setRegIp("171.11.1.228");
user.setRegTime("20200228");
user.setPhoto("1.jpg");
user.setAuthStatus("1");
user.setAuthTime("20200228");
user.setRegTime("20200228");
user.setWaiterId("2");
user.setMasterType("2");
user.setClientType("2");
user.setRoomLimit("2");
user.setCreateArea("2");
user.setSalesman("2");
user.setIsWebrtc("2");
user.setCdnSwitch("2");
user.setPushExtime("20200228");
user.setConcurrencyLimit("1");
userRepository.save(user);
return user.getId();
}
return userList.get(0).getId();
}
public String findUserById(String id){
User user = userRepository.findUserById("34993");
return user.getWechatName();
}
public QueryResponseResult findInviteList(int page, int size, QueryUserRequest queryUserRequest){
if(queryUserRequest == null){//当queryPageRequest这个搜索为空时,queryPageRequest.getSiteId()会报错,所以加上如果为空,queryPageRequest = new QueryPageRequest();设置一个空对象
queryUserRequest = new QueryUserRequest();
}
//条件值对象
User user= new User();
//设置页面别名
//设置条件值(站点id)
if(StringUtils.isNotEmpty(queryUserRequest.getInviteCode())){//如果搜索的站点id不为空
user.setInviteCode(queryUserRequest.getInviteCode());
}
//设置模板id作为查询条件
if(StringUtils.isNotEmpty(queryUserRequest.getOpenid())){//如果搜索的模板id不为空
user.setOpenid(queryUserRequest.getOpenid());
}
//设置页面别名作为查询条件
if(StringUtils.isNotEmpty(queryUserRequest.getUnionid())){//如果搜索的页面别名不为空
user.setUnionid(queryUserRequest.getUnionid());
}
//条件匹配器
ExampleMatcher exampleMatcher = ExampleMatcher.matching()
.withMatcher("pageAliase", ExampleMatcher.GenericPropertyMatchers.contains());//因为要对pageAliase进行模糊查询,所以加这句话
//定义Example
Example<User> example = Example.of(user,exampleMatcher);
//分页参数
if(page <=0){
page = 1;
}
page = page -1;
if(size<=0){
size = 10;
}
Pageable pageable = PageRequest.of(page,size);
Page<User> all = userRepository.findAll(example, pageable);
QueryResult queryResult = new QueryResult();
queryResult.setList(all.getContent());//数据列表
queryResult.setTotal(all.getTotalElements());//数据总记录数
QueryResponseResult queryResponseResult = new QueryResponseResult(CommonCode.SUCCESS,queryResult);
return queryResponseResult;
}
}
postman测试
http://localhost:31200/nuoyun/Invite/list/1/10?inviteCode=111