账号列表修改为已存在的账号时报错,加个判断,在前端提示

@desc API 统一返回状态码 /* 用户错误:20001-29999*/ USER_HAS_EXISTED(20005, “用户已存在”),
没有合适的MySQL JDBC驱动 No suitable driver found for localhost
Mybatis不鼓励捕捉异常,但在某些特定场景下需要捕捉,这个时候通过try catch是捕捉不到的,mybatis有自己的处理方式,它把异常映射成了DataAccessException,那么我们需要抛出异常并捕捉。
账号列表修改为已存在的账号时报错,初步解决方案try {
managerUserService.updateByPrimaryKeySelective(managerUser);
} catch (Exception e) {
throw new BusinessException(ResultCode.DATA_IS_WRONG);
}

加个判断,在前端提示,没有合适的MySQL JDBC驱动 尚未解决
Class.forName(“com.mysql.cj.jdbc.Driver”); //加载数据库驱动
String DB_URL = “localhost:3306/background?serverTimezone=Asia/Shanghai&useUnicode=true&characterEncoding=utf8&characterSetResults=utf8&useSSL=false&verifyServerCertificate=false&autoReconnct=true&autoReconnectForPools=true&allowMultiQueries=true”;
String USER = “root”; //数据库账号
String PASS = “mxy”; //数据库密码
Connection conn = DriverManager.getConnection(DB_URL, USER, PASS); //建立连接
Statement stmt = conn.createStatement();
String sql = “SELECT user_name FROM citizen-app.manager_user where user_name = '”+updateManagerUser.userName+"’";
ResultSet rs = stmt.executeQuery(sql); //执行语句
if (updateManagerUser.userName.equals(sql)) {
throw new BusinessException(ResultCode.USER_HAS_EXISTED); //抛出异常
}

try {
managerUserService.updateByPrimaryKeySelective(managerUser);
} catch (Exception e) {
if (managerUserService.getUserName(updateManagerUser.userName).getList().get(0).getUserName().equals(updateManagerUser.userName)) {
try {
managerUserService.updateByPrimaryKeySelective(managerUser);
} catch (Exception a) {
throw new BusinessException(ResultCode.USER_HAS_EXISTED);//账号已存在
}
} else {
throw new BusinessException(ResultCode.DATA_IS_WRONG);
}
} finally {
}
}

if (!managerUserService.getUserName(updateManagerUser.userName).equals("[]")) { //判断账号已存在
throw new BusinessException(ResultCode.USER_HAS_EXISTED);//账号已存在
}
managerUserService.updateByPrimaryKeySelective(managerUser); //不重复的时候

List getUserName(String userName);

@Override
public List getUserName(String userName) {
    List list = managerUserMapper.getUserName(userName);
    return list;
}


List<ManagerUser> getUserName(@Param("userName") String userName);
  <select id="getUserName" resultMap="ManagerUserListResultMap">
        select

        mu.user_name

        from manager_user mu

        <where>
            <if test="userName!=null">
                mu.user_name = '${userName}'
            </if>
        </where>
    </select>
</mapper>
发布了19 篇原创文章 · 获赞 3 · 访问量 2099
展开阅读全文

没有更多推荐了,返回首页

©️2019 CSDN 皮肤主题: 点我我会动 设计师: 上身试试

分享到微信朋友圈

×

扫一扫,手机浏览