0
点赞
收藏
分享

微信扫一扫

SpringBoot的全局异常拦截操作


@ControllerAdvice

@ControllerAdvice
public class GlobalExceptionHandler {

/**
* 拦截运行异常出现的错误~~~
*/
@ExceptionHandler(RuntimeException.class)
@ResponseBody
public Map<Object, Object> exceptionHandler() {
Map<Object, Object> map = new HashMap<>();
map.put("error", "500");
map.put("msg", "系统出现错误~");
return map;
}
}

  @ExceptionHandler(RuntimeException.class)

@SpringBootApplication

//@EnableAutoConfiguration   启动依赖jar包中的 类 比如 SpringMVC、Spring

//@ComponentScan("com.api.controller")

Mybatis 的配置信息:

 application.yml

spring:
datasource:
url: jdbc:mysql://localhost:3306/test
username: root
password: 123456
driver-class-name: com.mysql.jdbc.Driver
@SpringBootApplication
@MapperScan("com.mapper")
public class SpringMybatis {
public static void main(String[] args) {
SpringApplication.run(SpringMybatis.class);
}
}
public interface UserMapper {
// xml 注解
@Select("SELECT * FROM USERS WHERE NAME = #{name}")
UserEntity findByName(@Param("name") String name);

@Insert("INSERT INTO USERS(NAME, AGE) VALUES(#{name}, #{age})")
int insert(@Param("name") String name, @Param("age") Integer age);

}



举报

相关推荐

0 条评论