日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > java >内容正文

java

Java 捕获 mybatis异常_3 springboot集成mybatis和全局异常捕获

發布時間:2024/9/27 java 29 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Java 捕获 mybatis异常_3 springboot集成mybatis和全局异常捕获 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

mybatis有兩種方式,一種是基于XML,一種是基于注解

springboot集成mybatis

首先先創建表,這里都簡化了

DROP TABLE IF EXISTS `user`;

CREATE TABLE `user` (

`id` int(11) NOT NULL auto_increment,

`username` varchar(255) default NULL,

PRIMARY KEY (`id`)

) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8;

INSERT INTO `user` VALUES ('1', 'lijia');

然后創建maven項目,在pom.xml中添加

org.springframework.boot

spring-boot-parent

1.5.4.RELEASE

1.8

UTF-8

UTF-8

org.springframework.boot

spring-boot-starter-web

org.mybatis.spring.boot

mybatis-spring-boot-starter

1.3.0

mysql

mysql-connector-java

runtime

org.springframework.boot

spring-boot-devtools

true

com.alibaba

fastjson

1.2.31

org.projectlombok

lombok

provided

org.springframework.boot

spring-boot-maven-plugin

true

XML形式

目錄結構是

application.properties

# jdbc_config

spring.datasource.driver-class-name=com.mysql.jdbc.Driver

spring.datasource.url=jdbc:mysql://172.16.255.69:3306/test?characterEncoding=utf8

spring.datasource.username=root

spring.datasource.password=root

# Mybatis

#mybatis.typeAliasesPackage對應的是實體類位置

mybatis.typeAliasesPackage=org.lijia.bean

#mybatis.mapperLocations查找mybatis配置文件位置

mybatis.mapperLocations=classpath:mybatis/*.xml

實體類User

public class User {

private String id; //用戶id

private String username; //用戶名

public String getId() {

return id;

}

public void setId(String id) {

this.id = id;

}

public String getUsername() {

return username;

}

public void setUsername(String username) {

this.username = username;

}

}

HelloController :

import com.alibaba.fastjson.JSON;

import com.lijia.bean.User;

import com.lijia.service.HelloService;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Controller;

import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RequestParam;

import org.springframework.web.bind.annotation.ResponseBody;

@Controller

@RequestMapping("/hello")

public class HelloController {

@Autowired

private HelloService helloService;

@RequestMapping("/get")

@ResponseBody

public String get(@RequestParam String username){

User user = helloService.get(username);

if(user!=null){

System.out.println("user.getName():"+user.getUsername());

}

return JSON.toJSONString(user);

}

@RequestMapping("/exception")

@ResponseBody

public String exception(){

throw new RuntimeException();

}

}

HelloService :

import com.lijia.bean.User;

import com.lijia.dao.HelloMapper;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Service;

@Service

//@Transactional

public class HelloService {

@Autowired

private HelloMapper helloMapper;

public User get(String username) {

return helloMapper.findUserByName(username);

}

}

接口HelloMapper :

import com.lijia.bean.User;

public interface HelloMapper {

public User findUserByName(String username);

}

mybatis配置文件:

select * from user where username = #{username}

最后啟動類Application:

import org.mybatis.spring.annotation.MapperScan;

import org.springframework.boot.SpringApplication;

import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication

@MapperScan("com.lijia.dao") //掃描接口

public class Application {

public static void main(String[] args) {

SpringApplication.run(Application.class,args);

}

}

啟動Application,輸入成功獲取數據

mybatis注解方式

注解方式我感覺太簡單了,一般我也是用這個

沒有了mybatis那些配置文件

application.properties:去除了mybatis的東西

spring.datasource.driver-class-name=com.mysql.jdbc.Driver

spring.datasource.url=jdbc:mysql://172.16.255.69:3306/test?characterEncoding=utf8

spring.datasource.username=root

spring.datasource.password=root

啟動類Application:也沒有了掃描

import org.springframework.boot.SpringApplication;

import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication

public class Application {

public static void main(String[] args) {

SpringApplication.run(Application.class,args);

}

}

最主要的是:HelloMapper

import com.lijia.bean.User;

import org.apache.ibatis.annotations.Mapper;

import org.apache.ibatis.annotations.Select;

@Mapper

public interface HelloMapper {

@Select("select * from user where username = #{username}")

public User findUserByName(String username);

}

在接口上加上@Mapper

在方法上面加上@Select里面跟著sql

然后啟動Application,結果一樣

全局異常捕獲

只要在項目中加上這個類

import com.lijia.common.result.Result;

import com.lijia.common.result.ResultEnum;

import org.springframework.web.bind.annotation.ControllerAdvice;

import org.springframework.web.bind.annotation.ExceptionHandler;

import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

/**

* Desc:全局異常捕獲并返回

*

*/

@RestController

@ControllerAdvice

public class ExceptionHandlerController {

@ExceptionHandler(Exception.class)

public Result defaultExceptionHandler(HttpServletRequest request, Exception e)

throws Exception {

return new Result<>(ResultEnum.SERVER_ERROR, e.getMessage());

}

}

這里面的Result是自定義。

import com.fasterxml.jackson.annotation.JsonInclude;

import lombok.Data;

import lombok.NoArgsConstructor;

@Data

@JsonInclude(JsonInclude.Include.NON_NULL)

@NoArgsConstructor

public class Result {

private int code;

private String desc;

private T data;

public Result(ResultEnum resultEnum) {

this.code = resultEnum.getCode();

this.desc = resultEnum.getDesc();

}

public Result(ResultEnum resultEnum, T data) {

this.code = resultEnum.getCode();

this.desc = resultEnum.getDesc();

this.data = data;

}

}

還有個枚舉

public enum ResultEnum {

SUCCESS(200, "success"), SERVER_ERROR(500, "server error");

private int code;

private String desc;

ResultEnum(int code, String desc) {

this.code = code;

this.desc = desc;

}

public int getCode() {

return code;

}

public String getDesc() {

return desc;

}

}

在上面的Controller中已經有異常的方法,啟動Application

總結

以上是生活随笔為你收集整理的Java 捕获 mybatis异常_3 springboot集成mybatis和全局异常捕获的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。