提交 71978ea6 编写于 作者: 一缕82年的清风's avatar 一缕82年的清风

[A] JDbcTemplate代码提交

上级 88c6e543
......@@ -39,5 +39,19 @@
<version>1.18.22</version>
<scope>provided</scope>
</dependency>
<!-- jdbcTemplate -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<!-- MySQL连接 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
</dependencies>
</project>
\ No newline at end of file
package com.lsqingfeng.springboot.vo.result;
package com.lsqingfeng.springboot.base;
import lombok.Data;
......
package com.lsqingfeng.springboot.vo.result;
package com.lsqingfeng.springboot.base;
import lombok.Builder;
import lombok.Data;
......
package com.lsqingfeng.springboot.vo.result;
package com.lsqingfeng.springboot.base;
/**
* @enumName: ResultCodeEnum
......
package com.lsqingfeng.springboot.controller;
import com.lsqingfeng.springboot.base.ResultCode;
import com.lsqingfeng.springboot.exception.BizException;
import com.lsqingfeng.springboot.vo.User;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* @className: ThirdException
* @description:
* @author: sh.Liu
* @date: 2022-01-14 11:09
*/
@RestController
public class ThirdException {
@GetMapping("exception")
public User second(){
System.out.println(1);
throw new BizException(ResultCode.BIZ_ERROR.getCode(), "用户名密码错误");
}
}
package com.lsqingfeng.springboot.dao.jdbcTemplate;
import com.lsqingfeng.springboot.entity.User;
import java.util.List;
/**
* @interface: UserDao
* @description:
* @author: sh.Liu
* @date: 2022-01-14 16:37
*/
public interface UserDao {
/**
* 根据id查询
* @param id
* @return
*/
User getUserById(Integer id);
/**
* 查询所有用户
* @return
*/
List<User> listUser();
/**
* 保存用户
* @param user
* @return
*/
int save(User user);
/**
* 更新用户
* @param id
* @param user
* @return
*/
int update(Integer id, User user);
/**
* 删除用户
* @param id
* @return
*/
int delete(Integer id);
}
package com.lsqingfeng.springboot.dao.jdbcTemplate.impl;
import com.lsqingfeng.springboot.dao.jdbcTemplate.UserDao;
import com.lsqingfeng.springboot.entity.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;
import java.util.Date;
import java.util.List;
/**
* @className: UserDaoImpl
* @description:
* @author: sh.Liu
* @date: 2022-01-14 16:40
*/
@Repository
public class UserDaoImpl implements UserDao {
@Autowired
private JdbcTemplate jdbcTemplate;
@Override
public User getUserById(Integer id) {
User user = jdbcTemplate.queryForObject("select * from t_user where id = ?",User.class, id);
return user;
}
@Override
public List<User> listUser() {
List<User> users = jdbcTemplate.queryForList("select * from t_user", User.class);
return users;
}
@Override
public int save(User user) {
return jdbcTemplate.update("insert into t_user(name, age, address, create_time, update_time) values(?, ?, ?)",
user.getName(),user.getAge(), user.getAddress(),new Date(),new Date());
}
@Override
public int update(Integer id, User user) {
return jdbcTemplate.update("UPDATE t_user SET name = ? , age = ? ,address = ? ,update_time = ? WHERE id=?",
user.getName(), user.getAge(), user.getAddress(), new Date(), id);
}
@Override
public int delete(Integer id) {
return jdbcTemplate.update("DELETE from tb_user where id = ? ",id);
}
}
package com.lsqingfeng.springboot.entity;
import lombok.Data;
import java.util.Date;
/**
* @className: User
* @description:
* @author: sh.Liu
* @date: 2022-01-14 16:35
*/
@Data
public class User {
private Integer id;
private String name;
private Integer age;
private String address;
private Date createTime;
private Date updateTime;
}
package com.lsqingfeng.springboot.exception;
import com.lsqingfeng.springboot.base.ResultCode;
/**
* @className: BizException
* @description:
* @author: sh.Liu
* @date: 2022-01-14 11:00
*/
public class BizException extends RuntimeException {
private Integer code;
public BizException() {
}
public BizException(String message) {
super(message);
}
public BizException(Integer code, String message) {
super(message);
this.code = code;
}
public BizException(ResultCode resultCode) {
super(resultCode.getMsg());
this.code = resultCode.getCode();
}
public BizException(String message, Throwable cause) {
super(message, cause);
}
public BizException(int code, String message, Throwable cause) {
super(message, cause);
this.code = code;
}
public BizException(ResultCode resultCode, Throwable cause) {
super(resultCode.getMsg(), cause);
this.code = resultCode.getCode();
}
public Integer getCode() {
return this.code;
}
public void setCode(Integer code) {
this.code = code;
}
}
package com.lsqingfeng.springboot.exception;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.lsqingfeng.springboot.base.Result;
import com.lsqingfeng.springboot.base.ResultCode;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.core.annotation.Order;
import org.springframework.http.HttpStatus;
import org.springframework.util.StringUtils;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.web.HttpMediaTypeNotSupportedException;
import org.springframework.web.HttpRequestMethodNotSupportedException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import javax.servlet.http.HttpServletRequest;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.util.List;
/**
* @className: GlobalExceptionHandler
* @description:
* @author: sh.Liu
* @date: 2022-01-14 11:03
*/
@RestControllerAdvice
@Order(1)
public class GlobalExceptionHandler {
private static final Logger log = LoggerFactory.getLogger(GlobalExceptionHandler.class);
public GlobalExceptionHandler() {
}
private String handlerErrors(BindingResult bindingResult) {
List<FieldError> errors = bindingResult.getFieldErrors();
FieldError error = (FieldError)errors.get(0);
return error.getDefaultMessage();
}
@ExceptionHandler({BizException.class})
public Result<?> bizExceptionHandler(HttpServletRequest request, BizException e) {
Result<?> result = Result.error(e.getCode() == null ? ResultCode.BIZ_ERROR.getCode() : e.getCode(), e.getMessage());
return this.printLogAndReturn(request, result, e);
}
@ExceptionHandler({HttpRequestMethodNotSupportedException.class, HttpMediaTypeNotSupportedException.class})
public Result<?> httpRequestMethodNotSupportedExceptionHandler(HttpServletRequest request, Exception e) {
Result<?> result = Result.error(ResultCode.REQ_MODE_NOT_SUPPORTED);
return this.printLogAndReturn(request, result, e);
}
@ExceptionHandler({Exception.class})
@ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
public Result<?> exceptionHandler(HttpServletRequest request, Exception e) {
Result<?> result = Result.error(ResultCode.SYS_ERROR);
return this.printLogAndReturn(request, result, e);
}
private Result<?> printLogAndReturn(HttpServletRequest request, Result<?> result, Exception e) {
ObjectMapper mapper = new ObjectMapper();
String requestUrl = request.getRequestURL().toString() + (!StringUtils.hasLength(request.getQueryString()) ? "" : "?" + request.getQueryString());
try {
log.error("<-异常返回-> 请求接口:{} | 异常时间:{} | 异常结果:{}", new Object[]{requestUrl, System.currentTimeMillis(), mapper.writeValueAsString(result)});
} catch (JsonProcessingException jsonProcessingException) {
jsonProcessingException.printStackTrace();
}
log.error("<--异常堆栈信息-->");
StringWriter stringWriter = new StringWriter();
e.printStackTrace(new PrintWriter(stringWriter));
log.error(stringWriter.toString());
return result;
}
}
server:
port: 9090
port: 9092
spring:
datasource:
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql://localhost:3306/springboot_learning
username: root
password: root
third:
weather:
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册