Commit 989a357e authored by zengtianlai3's avatar zengtianlai3

添加全局异常处理

parent c674d9bf
package iot.sixiang.license.handler;
import iot.sixiang.license.model.BaseResult;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
/**
* Created by m33 on 2022/6/9 11:01
*/
@ControllerAdvice
public class GlobalExceptionHandler {
//指定出现什么异常执行这个方法
@ExceptionHandler(Exception.class)
@ResponseBody //为了返回数据
public BaseResult error(Exception e){
e.printStackTrace();
return BaseResult.fail().msg("执行了全局的异常类...");
}
//自定义异常
@ExceptionHandler(IotLicenseException.class)
@ResponseBody//为了返回数据
public BaseResult error(IotLicenseException e){
e.printStackTrace();
return BaseResult.fail().msg(e.getMsg()).code(e.getCode());
}
}
package iot.sixiang.license.handler;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
/**
* Created by m33 on 2022/6/9 11:13
*/
@Data
@AllArgsConstructor //生成有参构造
@NoArgsConstructor //生成无参构造
public class IotLicenseException extends RuntimeException {
private Integer code;
private String msg;//异常信息
}
...@@ -24,4 +24,6 @@ public interface UserMapper extends BaseMapper<User> { ...@@ -24,4 +24,6 @@ public interface UserMapper extends BaseMapper<User> {
boolean updateUser(int userId, String password); boolean updateUser(int userId, String password);
List<UserVo> getUserList( String userName, String company); List<UserVo> getUserList( String userName, String company);
User getUserByUserName(String userName);
} }
...@@ -30,6 +30,12 @@ public class BaseResult { ...@@ -30,6 +30,12 @@ public class BaseResult {
return respResult; return respResult;
} }
public BaseResult code(int code) {
this.code = code;
return this;
}
public BaseResult msg(String message) { public BaseResult msg(String message) {
this.msg = message; this.msg = message;
return this; return this;
......
...@@ -8,17 +8,17 @@ public class PageResult { ...@@ -8,17 +8,17 @@ public class PageResult {
private int code; private int code;
private String msg; private String msg;
private int page_no; private int pageNo;
private int pages; private int pages;
private int total; private int total;
private Object record; private Object record;
public PageResult(int code, String msg, int page_no,int pages, int total, Object record) { public PageResult(int code, String msg, int pageNo,int pages, int total, Object record) {
super(); super();
this.code = code; this.code = code;
this.msg = msg; this.msg = msg;
this.page_no = page_no; this.pageNo = pageNo;
this.pages = pages; this.pages = pages;
this.total = total; this.total = total;
this.record = record; this.record = record;
......
...@@ -30,6 +30,11 @@ public class ResResult { ...@@ -30,6 +30,11 @@ public class ResResult {
return respResult; return respResult;
} }
public ResResult code(int code) {
this.code = code;
return this;
}
public ResResult msg(String message) { public ResResult msg(String message) {
this.msg = message; this.msg = message;
return this; return this;
......
...@@ -8,5 +8,5 @@ import lombok.Data; ...@@ -8,5 +8,5 @@ import lombok.Data;
*/ */
@Data @Data
public class UserVo extends User { public class UserVo extends User {
public int count; public int deviceCount;
} }
...@@ -3,6 +3,7 @@ package iot.sixiang.license.service.impl; ...@@ -3,6 +3,7 @@ package iot.sixiang.license.service.impl;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import iot.sixiang.license.entity.User; import iot.sixiang.license.entity.User;
import iot.sixiang.license.handler.IotLicenseException;
import iot.sixiang.license.mapper.UserMapper; import iot.sixiang.license.mapper.UserMapper;
import iot.sixiang.license.model.PageInfoModel; import iot.sixiang.license.model.PageInfoModel;
import iot.sixiang.license.model.vo.UserVo; import iot.sixiang.license.model.vo.UserVo;
...@@ -58,6 +59,10 @@ public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements Us ...@@ -58,6 +59,10 @@ public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements Us
if (JsonUtil.isNull(userName) || JsonUtil.isNull(company)) { if (JsonUtil.isNull(userName) || JsonUtil.isNull(company)) {
return false; return false;
} }
User res = userMapper.getUserByUserName(userName);
if (res != null) {
throw new IotLicenseException(400,"用户名已存在");
}
return userMapper.addUser(userName,company,password); return userMapper.addUser(userName,company,password);
} }
......
...@@ -15,7 +15,7 @@ ...@@ -15,7 +15,7 @@
</update> </update>
<select id="getUserList" resultType="iot.sixiang.license.model.vo.UserVo"> <select id="getUserList" resultType="iot.sixiang.license.model.vo.UserVo">
SELECT user.user_id, user_name, password, company, COUNT(sn) AS count SELECT user.user_id, user_name, password, company, COUNT(sn) AS deviceCount
FROM USER LEFT JOIN device ON user.user_id = device.user_id FROM USER LEFT JOIN device ON user.user_id = device.user_id
where 1=1 where 1=1
<if test="null != userName and '' != userName"> <if test="null != userName and '' != userName">
...@@ -26,4 +26,9 @@ ...@@ -26,4 +26,9 @@
</if> </if>
GROUP BY user.user_id GROUP BY user.user_id
</select> </select>
<select id="getUserByUserName" resultType="iot.sixiang.license.entity.User">
select user_id from user where user_name = #{userName}
</select>
</mapper> </mapper>
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment