捕获全局异常是在项目运行期间如果调用的某一个方法出现了运行时异常,则会捕获,并且给出回馈。 首先需要建一个包,包里新建一个捕获异常类GlobalExceptionHandler。前提是springboot的启动类的扫描注解ComponentScan()要扫描到。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
/** * 用于捕获全局异常 */ @ControllerAdvice//控制器切面 public class GlobalExceptionHandler { @ExceptionHandler(RuntimeException.class)//捕获运行时异常 @ResponseBody public Map<String,Object> exceptionHandler(){//处理异常方法 Map<String,Object> map=new HashMap<String, Object>(); map.put("errorCode","101"); map.put("errorMsg","系统错误!"); return map; } } |
这个捕获异常类可以捕获到全局的运行时异常,例如商城购物车的控制层一个方法出现异常,黄色背景的是重点,会发生异常。
1 2 3 4 5 6 7 |
@RequestMapping("getShoppingCar") public String getShoppingCar(HttpSession session,Model model){ Map<Integer,Cart> cartMap =(Map<Integer,Cart>)session.getAttribute("cartMap"); model.addAttribute("carList",cartMap); int i=1/0; return "udai_shopcart"; } |
然后启动项目,进入主页,当点击购物车按钮时也就是调用了getShoppingCar这个方法,发生了异常,运行结果是跳转页面失败,直接给出异常信息,也就是全局捕获异常类中设置的信息。 配置多数据源# 以前是在applicationContext.xml中配置的,现在springboot通过注解来配置数据源。 首先在application.properties配置文件中加入多数据源配置。
1 2 3 4 5 6 7 8 9 |
spring.datasource.test1.url=jdbc:mysql://127.0.0.1:3306/test1?serverTimezone=UTC spring.datasource.test1.username=root spring.datasource.test1.password=1234 spring.datasource.test1.driverClassName=com.mysql.cj.jdbc.Driver spring.datasource.test2.url=jdbc:mysql://127.0.0.1:3306/test2?serverTimezone=UTC spring.datasource.test2.username=root spring.datasource.test2.password=1234 spring.datasource.test2.driverClassName=com.mysql.cj.jdbc.Driver |
在例子开始之前,首先去创建两个用于测试的数据库test1和test2,并且新建两张空表。 写两个数据源的配置类。# 在java文件夹下新建dataSource包,新建数据源配置类DataSource01和DataSource02。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
@Configuration//注解到springboot容器中 @MapperScan(basePackages="com.gyf.test1.mapper",sqlSessionFactoryRef="test1SqlSessionFactory") public class DataSource01 { /** * @return 返回test1数据库的数据源 */ @Bean(name="test1DataSource") @Primary//主数据源,一个应用只能有一个主数据源 @ConfigurationProperties(prefix="spring.datasource.test1") public DataSource dateSource(){ return DataSourceBuilder.create().build(); } /** * @return 返回test1数据库的会话工厂 */ @Bean(name = "test1SqlSessionFactory") public SqlSessionFactory sqlSessionFactory(@Qualifier("test1DataSource") DataSource ds) throws Exception{ SqlSessionFactoryBean bean = new SqlSessionFactoryBean(); bean.setDataSource(ds); return bean.getObject(); } /** * @return 返回test1数据库的事务 */ @Bean(name = "test1TransactionManager") @Primary//主事务 public DataSourceTransactionManager transactionManager(@Qualifier("test1DataSource") DataSource dataSource) { return new DataSourceTransactionManager(dataSource); } /** * @return 返回test1数据库的会话模版 */ @Bean(name = "test1SqlSessionTemplate") public SqlSessionTemplate sqlSessionTemplate( @Qualifier("test1SqlSessionFactory") SqlSessionFactory sqlSessionFactory) throws Exception { return new SqlSessionTemplate(sqlSessionFactory); } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
@Configuration//注解到springboot容器中 @MapperScan(basePackages="com.gyf.test2.mapper",sqlSessionFactoryRef="test2SqlSessionFactory") public class DataSource02 { /** * @return 返回test2数据库的数据源 */ @Bean(name="test2DataSource") @ConfigurationProperties(prefix="spring.datasource.test2") public DataSource dateSource(){ return DataSourceBuilder.create().build(); } /** * @return 返回test2数据库的会话工厂 */ @Bean(name = "test2SqlSessionFactory") public SqlSessionFactory sqlSessionFactory(@Qualifier("test2DataSource") DataSource ds) throws Exception{ SqlSessionFactoryBean bean = new SqlSessionFactoryBean(); bean.setDataSource(ds); return bean.getObject(); } /** * @return 返回test2数据库的事务 */ @Bean(name = "test2TransactionManager") public DataSourceTransactionManager transactionManager(@Qualifier("test2DataSource") DataSource dataSource) { return new DataSourceTransactionManager(dataSource); } /** * @return 返回test2数据库的会话模版 */ @Bean(name = "test2SqlSessionTemplate") public SqlSessionTemplate sqlSessionTemplate( @Qualifier("test2SqlSessionFactory") SqlSessionFactory sqlSessionFactory) throws Exception { return new SqlSessionTemplate(sqlSessionFactory); } } |
在java文件夹下分别创建两个文件夹test1和test2,分别对这两个数据库进行操作,启动类都需要先扫描到test1和test2的包
1 2 3 4 5 6 7 8 9 10 |
@EnableAutoConfiguration @ComponentScan(basePackages = {"com.gyf.datasource","com.gyf.web","com.gyf.test1.service","com.gyf.test2.service"}) public class App { public static void main( String[] args ) { //启动springboot项目 SpringApplication.run(App.class,args); } } |
Test1的userMapper
1 2 3 4 |
public interface UserMapper { @Insert("insert user (username,password) values (#{username},#{password})") public int save(@Param("username") String username, @Param("password") String password); } |
Test1的UserServiceImpl
1 2 3 4 5 6 7 8 9 |
@Service @Transactional public class UserServiceImpl{ @Autowired private UserMapper userMapper; public void register(String username, String password) { userMapper.save(username,password); } } |
Test2的CustomerMapper
1 2 3 4 5 |
public interface CustomerMapper { @Insert("insert customer (name,tel) values (#{name},#{tel})") public int save(@Param("name") String name, @Param("tel") String tel); } |
Test2的CustomerServiceImpl
1 2 3 4 5 6 7 8 9 |
@Service @Transactional public class CustomerServiceImpl { @Autowired private CustomerMapper customerMapper; public void save(String name, String tel) { customerMapper.save(name,tel); } } |
最后写一个controller类,调用这两个service
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
@RestController //声明Rest风格的控制器 //@EnableAutoConfiguration //自动配置,相当于写了spring的配置文件 @RequestMapping("user") public class UserController { @Autowired private UserServiceImpl userService; @Autowired private CustomerServiceImpl customerService; @RequestMapping("register") @ResponseBody public String register(String username,String password){ //把数据保存到test1数据库 userService.register(username,password); //把数据保存到test2数据库 customerService.save(username,"120"); return "success"; } } |
启动项目,看数据库表内容,两张表都插入了信息。 from:https://www.cnblogs.com/fantongxue/p/12443377.html
View Details具体错误: 使用mysql创建、调用存储过程,函数以及触发器的时候会有错误符号为1418错误。 [Err] 1418 – This function has none of DETERMINISTIC, NO SQL, or READS SQL DATA in its declaration and binary logging is enabled (you *might* want to use the less safe log_bin_trust_function_creators variable) 查阅相关资料,意思是说binlog启用时,创建的函数没有声明类型,因为binlog在主从复制需要知道这个函数创建语句是什么类型,否则同步数据会有不一致现象。 mysql开启了bin-log, 我们就必须指定我们的函数是否是哪种类型: 1 DETERMINISTIC 不确定的 2 NO SQL 没有SQl语句,当然也不会修改数据 3 READS SQL DATA 只是读取数据,当然也不会修改数据 4 MODIFIES SQL DATA 要修改数据 5 CONTAINS SQL 包含了SQL语句 为了解决这个问题,MySQL强制要求: 在主服务器上,除非子程序被声明为确定性的或者不更改数据,否则创建或者替换子程序将被拒绝。这意味着当创建一个子程序的时候,必须要么声明它是确定性的,要么它不改变数据。 声明方式有两种: 第一种:声明是否是确定性的 DETERMINISTIC和NOT DETERMINISTIC指出一个子程序是否对给定的输入总是产生同样的结果。 如果没有给定任一特征,默认是NOT DETERMINISTIC,所以必须明确指定DETERMINISTIC来声明一个子程序是确定性的。 这里要说明的是:使用NOW() 函数(或它的同义)或者RAND() 函数不会使一个子程序变成非确定性的。对NOW()而言,二进制日志包括时间戳并会被正确的执行。RAND()只要在一个子程序内被调用一次也可以被正确的复制。所以,可以认为时间戳和随机数种子是子程序的确定性输入,它们在主服务器和从服务器上是一样的。 第二种:声明是否会改变数据 CONTAINS SQL, NO SQL, READS SQL DATA, MODIFIES SQL用来指出子程序是读还是写数据的。 无论NO SQL还是READS SQL DATA都指出,子程序没有改变数据,但是必须明确地指定其中一个,因为如果任何指定,默认的指定是CONTAINS SQL。 默认情况下,如果允许CREATE PROCEDURE 或CREATE […]
View Details总归上述问题Rt,其实今天开发刚遇到,当时找了半天为啥前台传参后台却接收不到,原来是返回的时候返回小写,但是前台依旧大写传参。 查了很多后发现其实是json返回的时候把首字母变小写了,也就是Spring Boot中Jackson的功劳 百度后得@JsonProperty注解完美解决。但与此同时会出现两个问题 如果注解放到属性上,则返回的时候既有大写也有小写,
1 2 |
@JsonProperty("Ao") private Integer Ao; |
Result:{Ao:xxx,ao:xxx} 所以注解放在getter上完美解决,返回只有大写不再自动变为小写的问题。
1 2 3 4 |
@JsonProperty("Ao") public Integer getAo() { return Ao; } |
Result:{Ao:xxx} from:https://blog.csdn.net/github_36887863/article/details/81807088
View Details