CRM项目使用Query映射类封装前端提交的数据用BeanUtils工具类转为Model对象保存到数据库中------CRM项目

CRM项目使用Query映射类封装前端提交的数据用BeanUtils工具类转为Model对象保存到数据库中------CRM项目

java">package ***.alatus.query;

import lombok.Data;

@Data
public class UserQuery {
    /**
     * 主键,自动增长,用户ID
     */
    private Integer id;

    /**
     * 登录账号
     */
    private String loginAct;

    /**
     * 登录密码
     */
    private String loginPwd;

    /**
     * 用户姓名
     */
    private String name;

    /**
     * 用户手机
     */
    private String phone;

    /**
     * 用户邮箱
     */
    private String email;

    /**
     * 账户是否没有过期,0已过期 1正常
     */
    private Integer a***ountNoExpired;

    /**
     * 密码是否没有过期,0已过期 1正常
     */
    private Integer credentialsNoExpired;

    /**
     * 账号是否没有锁定,0已锁定 1正常
     */
    private Integer a***ountNoLocked;

    /**
     * 账号是否启用,0禁用 1启用
     */
    private Integer a***ountEnabled;
}
package ***.alatus.query;

import lombok.Data;

@Data
public class UserQuery {
    /**
     * 主键,自动增长,用户ID
     */
    private Integer id;

    /**
     * 登录账号
     */
    private String loginAct;

    /**
     * 登录密码
     */
    private String loginPwd;

    /**
     * 用户姓名
     */
    private String name;

    /**
     * 用户手机
     */
    private String phone;

    /**
     * 用户邮箱
     */
    private String email;

    /**
     * 账户是否没有过期,0已过期 1正常
     */
    private Integer a***ountNoExpired;

    /**
     * 密码是否没有过期,0已过期 1正常
     */
    private Integer credentialsNoExpired;

    /**
     * 账号是否没有锁定,0已锁定 1正常
     */
    private Integer a***ountNoLocked;

    /**
     * 账号是否启用,0禁用 1启用
     */
    private Integer a***ountEnabled;
}
package ***.alatus.service.impl;

import ***.alatus.constant.Constants;
import ***.alatus.mapper.TUserMapper;
import ***.alatus.model.TUser;
import ***.alatus.query.UserQuery;
import ***.fasterxml.jackson.databind.util.BeanUtil;
import ***.github.pagehelper.PageHelper;
import ***.github.pagehelper.PageInfo;
import jakarta.annotation.Resource;
import org.springframework.beans.BeanUtils;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import java.util.ArrayList;

@Service
public class UserServiceImpl implements ***.alatus.service.UserService {
    @Resource
    private TUserMapper tUserMapper;
    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        TUser tUser = tUserMapper.selectByLoginAct(username);
        if(tUser == null){
            throw new UsernameNotFoundException("登陆账号不存在");
        }
        return tUser;
    }

    @Override
    public PageInfo<TUser> getUserByPage(Integer current) {
//        设置PageHelper和分页情况
        PageHelper.startPage(current, Constants.PAGE_SIZE);
//        查询
        ArrayList<TUser> list = tUserMapper.selectUserByPage();
//        封装分页到PageInfo中
        PageInfo<TUser> info = new PageInfo<>(list);
        return info;
    }

    @Override
    public TUser getUserById(Integer id) {
        return tUserMapper.selectDetailById(id);
    }

    @Override
    public int saveUser(UserQuery userQuery) {
        TUser tUser = new TUser();
//        把query对象的数据复制到user对象里面
//        这个工具类的复制要求是两个对象的属性名要相同,属性要相同
        BeanUtils.copyProperties(userQuery,tUser);
        return tUserMapper.insertSelective(tUser);
    }
}
package ***.alatus.service.impl;

import ***.alatus.constant.Constants;
import ***.alatus.mapper.TUserMapper;
import ***.alatus.model.TUser;
import ***.alatus.query.UserQuery;
import ***.fasterxml.jackson.databind.util.BeanUtil;
import ***.github.pagehelper.PageHelper;
import ***.github.pagehelper.PageInfo;
import jakarta.annotation.Resource;
import org.springframework.beans.BeanUtils;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import java.util.ArrayList;

@Service
public class UserServiceImpl implements ***.alatus.service.UserService {
    @Resource
    private TUserMapper tUserMapper;
    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        TUser tUser = tUserMapper.selectByLoginAct(username);
        if(tUser == null){
            throw new UsernameNotFoundException("登陆账号不存在");
        }
        return tUser;
    }

    @Override
    public PageInfo<TUser> getUserByPage(Integer current) {
//        设置PageHelper和分页情况
        PageHelper.startPage(current, Constants.PAGE_SIZE);
//        查询
        ArrayList<TUser> list = tUserMapper.selectUserByPage();
//        封装分页到PageInfo中
        PageInfo<TUser> info = new PageInfo<>(list);
        return info;
    }

    @Override
    public TUser getUserById(Integer id) {
        return tUserMapper.selectDetailById(id);
    }

    @Override
    public int saveUser(UserQuery userQuery) {
        TUser tUser = new TUser();
//        把query对象的数据复制到user对象里面
//        这个工具类的复制要求是两个对象的属性名要相同,属性要相同
        BeanUtils.copyProperties(userQuery,tUser);
        return tUserMapper.insertSelective(tUser);
    }
}
package ***.alatus.service;

import ***.alatus.model.TUser;
import ***.alatus.query.UserQuery;
import ***.github.pagehelper.PageInfo;
import org.springframework.security.core.userdetails.UserDetailsService;

public interface UserService extends UserDetailsService {

    PageInfo<TUser> getUserByPage(Integer current);

    TUser getUserById(Integer id);

    int saveUser(UserQuery userQuery);
}
package ***.alatus.service;

import ***.alatus.model.TUser;
import ***.alatus.query.UserQuery;
import ***.github.pagehelper.PageInfo;
import org.springframework.security.core.userdetails.UserDetailsService;

public interface UserService extends UserDetailsService {

    PageInfo<TUser> getUserByPage(Integer current);

    TUser getUserById(Integer id);

    int saveUser(UserQuery userQuery);
}
package ***.alatus.web;

import ***.alatus.model.TUser;
import ***.alatus.query.UserQuery;
import ***.alatus.result.R;
import ***.alatus.service.UserService;
import ***.github.pagehelper.PageInfo;
import jakarta.annotation.Resource;
import org.springframework.security.core.Authentication;
import org.springframework.web.bind.annotation.*;

@RestController
public class UserController {
    @Resource
    private UserService userService;
//    获取登录信息
    @GetMapping(value = "/api/login/info")
    public R loginInfo(Authentication authentication){
        TUser tUser = (TUser) authentication.getPrincipal();
        return R.OK(tUser);
    }
    //免登录验证
    //因为发送的请求过来首先会过filter那一关,能到这说明token验证都通过了,我们直接返回200即可
    @GetMapping(value = "/api/login/free")
    public R freeLogin(){
        return R.OK();
    }
//    查询用户列表
    @GetMapping(value = "/api/users")
//    传递参数current,可传可不传,
    public R userPage(@RequestParam(value = "current",required = false) Integer current){
        if(current == null){
            current = 1;
        }
//        返回结果为PageInfo
        PageInfo<TUser> userByPage = userService.getUserByPage(current);
        return R.OK(userByPage);
    }
    @GetMapping(value = "/api/user/{id}")
    public R userDetail(@PathVariable(value = "id")Integer id){
        TUser tUser = userService.getUserById(id);
        return R.OK(tUser);
    }
    @PostMapping(value = "/api/user/add")
    public R addUser(UserQuery userQuery){
        int save = userService.saveUser(userQuery);
        return save >= 1 ? R.OK() : R.FAIL();
    }
}

package ***.alatus.web;

import ***.alatus.model.TUser;
import ***.alatus.query.UserQuery;
import ***.alatus.result.R;
import ***.alatus.service.UserService;
import ***.github.pagehelper.PageInfo;
import jakarta.annotation.Resource;
import org.springframework.security.core.Authentication;
import org.springframework.web.bind.annotation.*;

@RestController
public class UserController {
    @Resource
    private UserService userService;
//    获取登录信息
    @GetMapping(value = "/api/login/info")
    public R loginInfo(Authentication authentication){
        TUser tUser = (TUser) authentication.getPrincipal();
        return R.OK(tUser);
    }
    //免登录验证
    //因为发送的请求过来首先会过filter那一关,能到这说明token验证都通过了,我们直接返回200即可
    @GetMapping(value = "/api/login/free")
    public R freeLogin(){
        return R.OK();
    }
//    查询用户列表
    @GetMapping(value = "/api/users")
//    传递参数current,可传可不传,
    public R userPage(@RequestParam(value = "current",required = false) Integer current){
        if(current == null){
            current = 1;
        }
//        返回结果为PageInfo
        PageInfo<TUser> userByPage = userService.getUserByPage(current);
        return R.OK(userByPage);
    }
    @GetMapping(value = "/api/user/{id}")
    public R userDetail(@PathVariable(value = "id")Integer id){
        TUser tUser = userService.getUserById(id);
        return R.OK(tUser);
    }
    @PostMapping(value = "/api/user/add")
    public R addUser(UserQuery userQuery){
        int save = userService.saveUser(userQuery);
        return save >= 1 ? R.OK() : R.FAIL();
    }
}
转载请说明出处内容投诉
CSS教程_站长资源网 » CRM项目使用Query映射类封装前端提交的数据用BeanUtils工具类转为Model对象保存到数据库中------CRM项目

发表评论

欢迎 访客 发表评论

一个令你着迷的主题!

查看演示 官网购买