package com.alatus.query;import lombok.Data;@Datapublic 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 accountNoExpired;/** * 密码是否没有过期,0已过期 1正常 */private Integer credentialsNoExpired;/** * 账号是否没有锁定,0已锁定 1正常 */private Integer accountNoLocked;/** * 账号是否启用,0禁用 1启用 */private Integer accountEnabled;}
package com.alatus.query;import lombok.Data;@Datapublic 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 accountNoExpired;/** * 密码是否没有过期,0已过期 1正常 */private Integer credentialsNoExpired;/** * 账号是否没有锁定,0已锁定 1正常 */private Integer accountNoLocked;/** * 账号是否启用,0禁用 1启用 */private Integer accountEnabled;}
package com.alatus.service.impl;import com.alatus.constant.Constants;import com.alatus.mapper.TUserMapper;import com.alatus.model.TUser;import com.alatus.query.UserQuery;import com.fasterxml.jackson.databind.util.BeanUtil;import com.github.pagehelper.PageHelper;import com.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;@Servicepublic class UserServiceImpl implements com.alatus.service.UserService {@Resourceprivate TUserMapper tUserMapper;@Overridepublic UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {TUser tUser = tUserMapper.selectByLoginAct(username);if(tUser == null){throw new UsernameNotFoundException("登陆账号不存在");}return tUser;}@Overridepublic PageInfo getUserByPage(Integer current) {//设置PageHelper和分页情况PageHelper.startPage(current, Constants.PAGE_SIZE);//查询ArrayList list = tUserMapper.selectUserByPage();//封装分页到PageInfo中PageInfo info = new PageInfo(list);return info;}@Overridepublic TUser getUserById(Integer id) {return tUserMapper.selectDetailById(id);}@Overridepublic int saveUser(UserQuery userQuery) {TUser tUser = new TUser();//把query对象的数据复制到user对象里面//这个工具类的复制要求是两个对象的属性名要相同,属性要相同BeanUtils.copyProperties(userQuery,tUser);return tUserMapper.insertSelective(tUser);}}
package com.alatus.service.impl;import com.alatus.constant.Constants;import com.alatus.mapper.TUserMapper;import com.alatus.model.TUser;import com.alatus.query.UserQuery;import com.fasterxml.jackson.databind.util.BeanUtil;import com.github.pagehelper.PageHelper;import com.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;@Servicepublic class UserServiceImpl implements com.alatus.service.UserService {@Resourceprivate TUserMapper tUserMapper;@Overridepublic UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {TUser tUser = tUserMapper.selectByLoginAct(username);if(tUser == null){throw new UsernameNotFoundException("登陆账号不存在");}return tUser;}@Overridepublic PageInfo getUserByPage(Integer current) {//设置PageHelper和分页情况PageHelper.startPage(current, Constants.PAGE_SIZE);//查询ArrayList list = tUserMapper.selectUserByPage();//封装分页到PageInfo中PageInfo info = new PageInfo(list);return info;}@Overridepublic TUser getUserById(Integer id) {return tUserMapper.selectDetailById(id);}@Overridepublic int saveUser(UserQuery userQuery) {TUser tUser = new TUser();//把query对象的数据复制到user对象里面//这个工具类的复制要求是两个对象的属性名要相同,属性要相同BeanUtils.copyProperties(userQuery,tUser);return tUserMapper.insertSelective(tUser);}}
package com.alatus.service;import com.alatus.model.TUser;import com.alatus.query.UserQuery;import com.github.pagehelper.PageInfo;import org.springframework.security.core.userdetails.UserDetailsService;public interface UserService extends UserDetailsService {PageInfo getUserByPage(Integer current);TUser getUserById(Integer id);int saveUser(UserQuery userQuery);}
package com.alatus.service;import com.alatus.model.TUser;import com.alatus.query.UserQuery;import com.github.pagehelper.PageInfo;import org.springframework.security.core.userdetails.UserDetailsService;public interface UserService extends UserDetailsService {PageInfo getUserByPage(Integer current);TUser getUserById(Integer id);int saveUser(UserQuery userQuery);}
package com.alatus.web;import com.alatus.model.TUser;import com.alatus.query.UserQuery;import com.alatus.result.R;import com.alatus.service.UserService;import com.github.pagehelper.PageInfo;import jakarta.annotation.Resource;import org.springframework.security.core.Authentication;import org.springframework.web.bind.annotation.*;@RestControllerpublic class UserController {@Resourceprivate 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;}//返回结果为PageInfoPageInfo 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 com.alatus.web;import com.alatus.model.TUser;import com.alatus.query.UserQuery;import com.alatus.result.R;import com.alatus.service.UserService;import com.github.pagehelper.PageInfo;import jakarta.annotation.Resource;import org.springframework.security.core.Authentication;import org.springframework.web.bind.annotation.*;@RestControllerpublic class UserController {@Resourceprivate 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;}//返回结果为PageInfoPageInfo 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();}}