一、用户模块
1.1建库建表
1.2通过MyBatis的通用mapper生成entity和dao
1.3在新建一个HttpResult类,用来判断请求成功的内容或请求错误给个提示
1.4在service包下的UserService下写代码(业务)
1.4.1用户注册
/**
* 用户注册
* @param phone
* @param pwd
* @param nickName
* @return
*/
public HttpResult regedit(String phone,String pwd,String nickName){
HttpResult result=null;
AuthUser user=new AuthUser();
user.setPasswd(pwd);
user.setPhone(phone);
user.setNickname(nickName);
user.setIsactive(1);//1代表已激活
int cnt= authUserMapper.insert(user);
if(cnt>0){
result=new HttpResult(200,null,"注册成功");
}else {
result=new HttpResult(500,"注册失败",null);
}
return result;
}
1.4.2根据id删除用户
/**
* 根据id删除用户
* @param id
* @return
*/
public HttpResult remove(int id){
HttpResult result=null;
AuthUser user=new AuthUser();
user.setId(id);
int cnt = authUserMapper.delete(user);
if(cnt>0){
result=new HttpResult(200,null,"删除成功");
}else {
result=new HttpResult(500,"删除失败",null);
}
return result;
}
1.4.3根据id修改用户
/**
* 根据id修改用户
* @param id
* @param phone
* @param pwd
* @param nickName
* @return
*/
public HttpResult modify(int id,String phone,String pwd,String nickName){
HttpResult result=null;
//通过id查出用户
AuthUser u=new AuthUser();
u.setId(id);
AuthUser user= authUserMapper.selectOne(u);
if(!"null".equals(phone)){
user.setPhone(phone);
}
if(!"null".equals(pwd)){
user.setPasswd(pwd);
}
if(!"null".equals(nickName)){
user.setNickname(nickName);
}
//修改用户
int cnt = authUserMapper.updateByPrimaryKey(user);
if(cnt>0){
result=new HttpResult(200,null,"修改成功");
}else {
result=new HttpResult(500,"修改失败",null);
}
return result;
}
1.4.4分页查询用户
/**
* 查询用户列表,分页查询
* @param pageIndex
* @param pageSize
* @return
*/
public HttpResult getAll(int pageIndex,int pageSize){
List urs = authUserMapper.selectAllByLimit((pageIndex-1) *pageSize, pageSize);
return new HttpResult(200,null,urs);
}
1.4.5登录
/**
* 登录
* @param phone
* @param pwd
* @return
*/
public HttpResult login(String phone,String pwd){
HttpResult result=null;
AuthUser user=new AuthUser();
user.setPasswd(pwd);
user.setPhone(phone);
AuthUser u= authUserMapper.selectOne(user);
if(u!=null){
result=new HttpResult(200,null,"登录成功");
}else{
result=new HttpResult(500,"登录失败",null);
}
return result;
}
1.5在controller包下的UserController下完成和页面的传输
@RestController
@RequestMapping("/user")
public class UserController {
@Autowired
private UserService userService;
/**
* 注册用户
* @param phone
* @param pwd
* @param nickName
* @return
*/
@GetMapping("/regedit/{phone}/{pwd}/{nickName}")
@ResponseBody
public HttpResult regedit(@PathVariable String phone,@PathVariable String pwd,@PathVariable String nickName){
return userService.regedit(phone,pwd,nickName);
}
/**
* 根据id删除用户
* @param id
* @return
*/
@GetMapping("/remove/{id}")
@ResponseBody
public HttpResult remove(@PathVariable int id){
return userService.remove(id);
}
/**
* 根据id修改用户
* @param id
* @param phone
* @param pwd
* @param nickName
* @return
*/
@GetMapping("/modify/{id}/{phone}/{pwd}/{nickName}")
@ResponseBody
public HttpResult modify(@PathVariable int id,@PathVariable String phone,@PathVariable String pwd,@PathVariable String nickName){
return userService.modify(id,phone,pwd,nickName);
}
@GetMapping("/getAll/{pageIndex}/{pageSize}")
@ResponseBody
public HttpResult getAll(@PathVariable int pageIndex,@PathVariable int pageSize){
return userService.getAll(pageIndex,pageSize);
}
@GetMapping("/login/{phone}/{pwd}")
@ResponseBody
public HttpResult login(@PathVariable String phone,@PathVariable String pwd){
return userService.login(phone,pwd);
}
}
二、角色模块
2.1添加角色
/**
* 添加角色
* @param roleName
* @param roleType
* @return
*/
public HttpResult saveRole(String roleName,String roleType){
HttpResult result=null;
AuthRole role=new AuthRole();
role.setRolename(roleName);
role.setRoletype(roleType);
int cnt = authRoleMapper.insert(role);
if(cnt>0){
result=new HttpResult(200,null,"添加成功");
}else {
result=new HttpResult(500,"添加失败",null);
}
return result;
}
2.2删除角色
/**
* 删除角色
* @param id
* @return
*/
public HttpResult deleteRole(int id){
HttpResult result=null;
AuthRole role=new AuthRole();
role.setId(id);
int cnt = authRoleMapper.delete(role);
if(cnt>0){
result=new HttpResult(200,null,"删除成功");
}else {
result=new HttpResult(500,"删除失败",null);
}
return result;
}
2.3修改角色
/**
* 修改
* @param id
* @param roleName
* @param roleType
* @return
*/
public HttpResult updateRole(int id,String roleName,String roleType){
HttpResult result=null;
AuthRole role=new AuthRole();
role.setId(id);
AuthRole authRole = authRoleMapper.selectOne(role);
authRole.setRoletype(roleType);
authRole.setRolename(roleName);
int cnt = authRoleMapper.updateByPrimaryKey(authRole);
if(cnt>0){
result=new HttpResult(200,null,"修改成功");
}else {
result=new HttpResult(500,"修改失败",null);
}
2.4分页查询角色
/**
* 分页查询
* @param pageIndex
* @param pageSize
* @return
*/
public HttpResult getAll(int pageIndex,int pageSize){
List role=authRoleMapper.getAll((pageIndex-1)*pageSize,pageSize);
return new HttpResult(200,null,role);
}
2.5将controller完善
@RestController
@RequestMapping("/role")
public class RoleController {
@Autowired
private RoleService roleService;
@GetMapping("/saveRole/{roleName}/{roleType}")
@ResponseBody
public HttpResult saveRole(@PathVariable String roleName,@PathVariable String roleType){
return roleService.saveRole(roleName,roleType);
}
@GetMapping("/deleteRole/{id}")
@ResponseBody
public HttpResult deleteRole(@PathVariable int id){
return roleService.deleteRole(id);
}
@GetMapping("/updateRole/{id}/{roleName}/{roleType}")
@ResponseBody
public HttpResult updateRole(@PathVariable int id,@PathVariable String roleName,@PathVariable String roleType){
return roleService.updateRole(id,roleName,roleType);
}
@GetMapping("/getAll/{pageIndex}/{pageSize}")
public HttpResult getAll(@PathVariable int pageIndex,@PathVariable int pageSize){
return roleService.getAll(pageIndex,pageSize);
}
}
三、权限模块
3.1添加权限
/**
* 添加权限
* @param resName
* @param resType
* @return
*/
public HttpResult saveRes(String resName,String resType){
HttpResult result=null;
AuthRes res=new AuthRes();
res.setResname(resName);
res.setRestype(resType);
int cnt = authResMapper.insert(res);
if(cnt>0){
result=new HttpResult(200,null,"添加成功");
}else {
result=new HttpResult(500,"添加失败",null);
}
return result;
}
3.2删除权限
/**
* 删除权限
* @param id
* @return
*/
public HttpResult deleteRes(int id){
HttpResult result=null;
AuthRes res=new AuthRes();
res.setId(id);
int cnt = authResMapper.delete(res);
if(cnt>0){
result=new HttpResult(200,null,"删除成功");
}else {
result=new HttpResult(500,"删除失败",null);
}
return result;
}
3.3修改权限
/**
* 修改权限
* @param id
* @param resName
* @param resType
* @return
*/
public HttpResult updateRes(int id,String resName,String resType){
HttpResult result=null;
AuthRes res=new AuthRes();
res.setId(id);
AuthRes authRes = authResMapper.selectOne(res);
authRes.setRestype(resType);
authRes.setResname(resName);
int cnt = authResMapper.updateByPrimaryKey(authRes);
if(cnt>0){
result=new HttpResult(200,null,"修改成功");
}else {
result=new HttpResult(500,"修改失败",null);
}
return result;
}
3.4分页查询权限
/**
* 分页查询权限
* @param pageIndex
* @param pageSize
* @return
*/
public HttpResult getAll(int pageIndex,int pageSize){
List res=authResMapper.getAll((pageIndex-1)*pageSize,pageSize);
return new HttpResult(200,null,res);
}
3.5将controller完善
@RestController
@RequestMapping("/res")
public class ResController {
@Autowired
private ResService resService;
@GetMapping("/saveRes/{resName}/{resType}")
@ResponseBody
public HttpResult saveRes(@PathVariable String resName,@PathVariable String resType){
return resService.saveRes(resName,resType);
}
@GetMapping("/deleteRes/{id}")
@ResponseBody
public HttpResult deleteRes(@PathVariable int id){
return resService.deleteRes(id);
}
@GetMapping("/updateRes/{id}/{resName}/{resType}")
@ResponseBody
public HttpResult updateRes(@PathVariable int id,@PathVariable String resName,@PathVariable String resType){
return resService.updateRes(id,resName,resType) ;
}
@GetMapping("/getAll/{pageIndex}/{pageSize}")
@ResponseBody
public HttpResult getAll(@PathVariable int pageIndex,@PathVariable int pageSize){
return resService.getAll(pageIndex,pageSize);
}
}