1、创建新项目
2、引入Spring Boot模块
web
Thymeleaf
MySQL
3、配置文件
jpa和mybatis的区别啊,搜了一下,好像大型项目使用mybatis,小型用jpa更好吧
问题:driver-class-name: com.mysql.jdbc.Driver 爆红
解决办法:setting->libraries->+号->导入本地的mysql包即可
application.yml 配置
spring:
thymeleaf:
mode: HTML
# 选择使用开发还是生产环境
profiles:
active: dev
messages:
basename: i18n/messages
comment.avatar: /images/avatar.png
application-dev.yml 生产环境
spring:
datasource:
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql://localhost:3306/blog?useUnicode=true&characterEncoding=utf-8
username: root
password: root
jpa:
hibernate:
# 自动更新
ddl-auto: update
# 日志中显示SQL语句
show-sql: true
#配置日志级别
logging:
level:
root: info
com.lrm: debug
file:
name: log/blog-dev.log
application-pro.yml 开发环境
#生产环境
spring:
datasource:
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql://localhost:3306/blog?useUnicode=true&characterEncoding=utf-8
username: root
password: root
jpa:
hibernate:
ddl-auto: none
show-sql: true
logging:
level:
root: warn
com.lrm: info
file:
name: log/blog-pro.log
#默认启动为8080
server:
port: 8080
启动BlogApplication.class,输入127.0.0.1:8080进行查看
4、异常处理
(1)定义一个web控制器
@Controller
public class IndexController {
@GetMapping("/")
public String index(){
return "index";
}
}
(2)自定义控制异常拦截器 Handler
package com.demo.blog.handler;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.servlet.ModelAndView;
import javax.servlet.http.HttpServletRequest;
/**
*
* @author demo
* @date 2021/12/20 13:27
* @param null
* @return null
*/
@ControllerAdvice
public class ControllerExceptionHandler {
// 获取日志记录异常
private final Logger logger = (Logger) LoggerFactory.getLogger(this.getClass());
// 返回一个错误页面
@ExceptionHandler(Exception.class)//表示只要是Exception级别的就可以拦截
public ModelAndView exceptionHander(HttpServletRequest request, Exception e) {
// 做一个拦截记录
logger.error("Request URL : {}, Exception", request.getRequestURL(), e);
ModelAndView mv = new ModelAndView();
mv.addObject("url", request.getRequestURI());
mv.addObject("exception",e);
//再返回到error页面上
mv.setViewName("error/error");
return mv;
}
}
(3)打开(如图)
5、日志处理
(1)切面编程(aop)
如果@Aspect报错,手动在pom里面添加依赖
<!-- aop切面 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-aop</artifactId>
</dependency>
创建一个LogAspect
package com.demo.blog.aspect;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import javax.servlet.http.HttpServletRequest;
import java.util.Arrays;
/**
*
* @author demo
* @date 2021/12/20 17:26
* @param null
* @return null
*/
//切面编程(aop)
@Aspect
@Component
public class LogAspect {
private final Logger logger = LoggerFactory.getLogger(this.getClass());
// 通过@Pointcut注解声明其为一个切面,execution规定切面拦截哪些类
@Pointcut("execution(* com.demo.blog.web.*.*(..))")
public void log(){
}
@Before("log()")//此方法在切面之前执行
public void doBefore(JoinPoint joinPoint){
ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
HttpServletRequest request = attributes.getRequest();
String url = request.getRequestURI().toString();
String ip = request.getRemoteAddr();
String classMethod = joinPoint.getSignature().getDeclaringTypeName() + "."+ joinPoint.getSignature().getName();
Object[] args = joinPoint.getArgs();
RequestLog requestLog = new RequestLog(url,ip,classMethod,args);
logger.info("Request : {}", requestLog);
}
@After("log()")
public void doAfter(){
// logger.info("------------doAfter------");
}
@AfterReturning(returning = "result",pointcut="log()")
public void doAfterReturn(Object result){
logger.info("Result : {}" + result);
}
public class RequestLog{
private String url;//请求URL
private String ip;//访问者ip
private String classMethod;//调用方法classMethod
private Object[] args;//参数args
public RequestLog(String url, String ip, String classMethod, Object[] args) {
this.url = url;
this.ip = ip;
this.classMethod = classMethod;
this.args = args;
}
@Override
public String toString() {
return "{" +
"url='" + url + '\'' +
", ip='" + ip + '\'' +
", classMethod='" + classMethod + '\'' +
", args=" + Arrays.toString(args) +
'}';
}
}
}
(2)IndexController改为:
@GetMapping里传值
(3)启动
6、页面处理(略写)
(1)在引入静态页面之后,使用thymeleaf布局
公共部分放入 _fragments.html 文件
(2)引用
总结:建议先下载源码,再拿出来敲,拿出来看。
- jpa和mybatis的区别是什么?
- 生产和开发环境的配置?
- 异常处理?
- 什么是切面编程?