1.SpringBoot日志框架
Spring Boot在所有内部日志中使用Commons Logging,但是默认配置也提供了对常用日志的支持,如:Java Util Logging,Log4J, Log4J2和Logback。
springboot的提供的默认日志框架是Logback,spring-boot-starter其中包含了 spring-boot-starter-logging,所以你可以直接使用Logback日志。可以在yml中配置开启保存日志文件到本地:
logging:
file: init.log
properties中:
logging.file,设置文件,可以是绝对路径,也可以是相对路径。如:logging.file=my.log
logging.path,设置目录,会在该目录下创建spring.log文件,并写入日志内容,如:logging.path=/var/log
还可以 配置logging.level.*来具体输出哪些包的日志级别
logging.level.root=INFO logging.level.org.springframework.web=DEBUG logging.level.org.hibernate=ERROR
默认情况下,spring boot从控制台打印出来的日志级别只有ERROR, WARN 还有INFO,如果你想要打印debug级别的日志,可以通过application.properites配置debug=true,可以打印等多信息。
在一般类中使用:
import org.slf4j.LoggerFactory; import org.slf4j.Logger; private Logger logger = LoggerFactory.getLogger(this.getClass()); logger.info(" log."); logger.error("log."); logger.warn(" log."); logger.debug("log."); logger.trace("log.");
如果嫌弃每次都新建Logger对象,还可以使用@Slf4j注释,不过这个注释隶属于lombok,使用lombok你不止需要lombok的依赖,还需要lombok插件(VSCode、eclipse、idea均有支持)。
<dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> </dependency>
然后在要使用的类前面加上@Slf4j。
@Slf4j @Service public class TodoListServiceImpl implements TodoListService { @Override public void deleteTodoObject(String note) { log.info(" log."); log.error("log."); log.warn(" log."); log.debug("log."); log.trace("log."); } }
就可以使用进行使用。
@:https://www.cnblogs.com/lixuwu/p/5804793.html
https://blog.csdn.net/flysun3344/article/details/80555746
https://www.cnblogs.com/weiapro/p/7633645.html
2.Mybatis的使用
依赖:
<dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>1.3.2</version> </dependency>
Mybatis包扫描:
@MapperScan("com.chriswei.cache.mapper")
//在Main类上
Mybatis的CRUD:
@Mapper
public interface EmployeeMapper {
@Select("SELECT * FROM EMPLOYEE WHERE ID = #{ID}")
public Employee getEmployeeById(Integer id);
@Update("UPDATE EMPLOYEE SET LASTNAME=#{lastName},EMAIL=#{email},GENDER=#{gender},DID=#{dId} WHERE ID=#{id}")
public void updateEmployee(Employee employee);
@Delete("DELETE FROM EMPLOYEE WHERE ID=#{ID}")
public void deleteEmployeeById(Integer id);
@Insert("INSERT INTO EMPLOYEE(LASTNAME,EMAIL,GENDER,DID) VALUES(#{lastName},#{email},#{gender},#{dId})")
public void insertEmployee(Employee employee);
}
//只需要写接口就行了
//特别注意,SQL语句中的实体类属性要区分大小写,#{lastName}不能写成#{LASTNAME}。
另外,关于@Param标签,有时候或者只有一个数据传入的时候,往往不需要也能映射到,但是有多个参数的时候,就需要加上,不然会出现映射不到的情况。
@Update("Update TODOLIST SET note=#{newNote} where note=#{oldNote}") public void updateNote(@Param("oldNote")String oldNote, @Param("newNote")String newNote);
报错类型为:error:nested exception is org.apache.ibatis.binding.BindingException: Parameter 'newNote' not found. Available parameters are [0, 1, param1, param2]
也可以使用配置文件来写SQL语句,关键配置就一句
#配置.xml文件路径
mybatis.mapper-locations=classpath:mapper/*.xml
然后你可以在mapper.xml中指定一些sql了。
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//ibatis.apache.org//DTD Mapper 3.0//EN" "http://ibatis.apache.org/dtd/ibatis-3-mapper.dtd">
<mapper namespace="com.chriswei.cache.mapper.EmployeeMapper">
<select id="getEmployeeById" parameterType="Integer" resultType="com.chriswei.cache.bean.Employee">
SELECT * FROM Employee WHERE ID = #{id}
</select>
</mapper>
在namespace指定好路径之后,只要id跟service层的方法名字一样,就可以查询了。
3.Springboot热加载
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> <scope>runtime</scope> </dependency>
4.Springboot aop的使用
依赖:
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-aop</artifactId> </dependency>
然后就可以使用了,新建一个切面类LogAnnotation
package com.example.demo.Util; import org.aspectj.lang.JoinPoint; import org.aspectj.lang.Signature; import org.aspectj.lang.annotation.AfterReturning; import org.aspectj.lang.annotation.Aspect; import org.aspectj.lang.annotation.Before; import org.aspectj.lang.annotation.Pointcut; import org.springframework.stereotype.Component; import lombok.extern.slf4j.Slf4j; @Component @Aspect @Slf4j public class LogAnnotation{ private final String POINT_CUT = "execution(* com.example.demo.service.impl.*.*(..))"; @Pointcut(POINT_CUT) public void pointCut(){} @Before(value = "pointCut()") public void before(JoinPoint joinPoint){ Signature signature = joinPoint.getSignature(); log.info("进入:"+signature.toLongString()); } @AfterReturning(value = POINT_CUT,returning = "result") public void doAfterReturningAdvice1(JoinPoint joinPoint,Object result){ Signature signature = joinPoint.getSignature(); log.info(signature.toShortString()+"执行完毕。"); if(result!=null && result instanceof String){ log.info("返回值:"+result,toString()); } } }
第一步加@Component@Aspect 两条注释,然后注册一个切入点@Pointcut(POINT_CUT) ,就可以使用其他的功能了。
对于execution的使用:
execution(
modifier-pattern? //修饰符
ret-type-pattern //返回类型
declaring-type-pattern? //方法模式
name-pattern(param-pattern) //参数模式
throws-pattern? //异常模式
)
/*
整个表达式可以分为五个部分:
1、execution(): 表达式主体。
2、第一个*号:表示返回类型,*号表示所有的类型。
3、包名:表示需要拦截的包名,后面的两个句点表示当前包和当前包的所有子包,com.sample.service.impl包、子孙包下所有类的方法。
4、第二个*号:表示类名,*号表示所有的类。
5、*(..):最后这个星号表示方法名,*号表示所有的方法,后面括弧里面表示方法的参数,两个句点表示任何参数。
*/
@pointcut("execution(* com.sample.service.impl..*.*(..))")
此外,这里还有一种跟自定义注解一起使用的方法。
首先新建一个你的自定义注释:
import java.lang.annotation.*; /** * 定义系统日志注解 * @author zhuzhe * @date 2018/6/4 9:24 * @email 1529949535@qq.com */ @Target(ElementType.METHOD) @Retention(RetentionPolicy.RUNTIME) @Documented public @interface SysLog { String value() default ""; }
然后在切入点这么写:
@Pointcut("@annotation(com.space.aspect.anno.SysLog)")
接下来你就可以使用自定义注解作为切入点使用了。
@SysLog("测试") @GetMapping("/test") public String test(@RequestParam("name") String name){ return name; }
但凡使用了注解的方法,都可以被LogAnnotation类监测到了。
更邪门的是使用环绕通知来对方法实现增强。
@:https://blog.csdn.net/zhuzhezhuzhe1/article/details/80565067