SpringBoot系列

[TOC]

SpringBoot系列

官方文档:https://docs.spring.io/spring-boot/docs/2.0.8.RELEASE/reference/htmlsingle/

SpringBoot开启事务

一、介绍

查询规范

Spring Data JPA提供的一个查询规范,查询语句关键字,简单的SQL可根据方法命名来即可,省略了写sql语句。

关键字                    方法命名                                 sql where字句
And                     findByNameAndPwd                    where name= ? and pwd =?
Or                      findByNameOrSex                     where name= ? or sex=?
Is,Equals               findById,findByIdEquals             where id= ?
Between                 findByIdBetween                     where id between ? and ?
LessThan                findByIdLessThan                    where id < ?
LessThanEquals          findByIdLessThanEquals              where id <= ?
GreaterThan             findByIdGreaterThan                 where id > ?
GreaterThanEquals       findByIdGreaterThanEquals           where id > = ?
After                   findByIdAfter                       where id > ?
Before                  findByIdBefore                      where id < ?
IsNull                  findByNameIsNull                    where name is null
isNotNull,NotNull       findByNameNotNull                   where name is not null
Like                    findByNameLike                      where name like ?
NotLike                 findByNameNotLike                   where name not like ?
StartingWith            findByNameStartingWith              where name like ‘?%’
EndingWith              findByNameEndingWith                where name like ‘%?’
Containing              findByNameContaining                where name like ‘%?%’
OrderBy                 findByIdOrderByXDesc                where id=? order by x desc
Not                     findByNameNot                       where name <> ?
In                      findByIdIn(Collection<?> c)         where id in (?)
NotIn                   findByIdNotIn(Collection<?> c)      where id not in (?)
True                    findByAaaTue                        where aaa = true
False                   findByAaaFalse                      where aaa = false
IgnoreCase              findByNameIgnoreCase                where UPPER(name)=UPPER(?)

二、配置

以SpringDataJpa为例

1、配置jpa生成数据库平台

(注意:mysql的InnoDB支持事务,MyISAM不支持事务)

2、类方法配置注解

(注意:SpringBoot2.0后,动态代理默认使用Cglib代理,基于类方法的代理,而Jdk代理基于接口,这里先基于类方法代理,下面在具体说明)

3、写测试类

注意:别忘记方法类,手动抛异常,int i = 1 / 0;

Tip1:关于代理的proxy-target-class参数设置?

解决:proxy-target-class属性值决定是基于接口的还是基于类的代理被创建。如果proxy-target-class 属性值被设置为true,那么基于类的代理将起作用(这时需要cglib库)。如果proxy-target-class属值被设置为false或者这个属性被省略,那么标准的JDK 基于接口的代理将起作用

Tip2:@EnableTransactionManagement 咋回事,要不要在启动类设置?

解决:SpringBoot2.0后不需要,默认是加载事务管理器的

博客https://www.cnblogs.com/alimayun/p/10296340.htmlarrow-up-right

https://blog.csdn.net/equaker/article/details/81534922arrow-up-right

三、踩坑

SpringBoot引入AOP

一、介绍

二、配置

博客:https://blog.csdn.net/huang_550/article/details/53672321

https://www.cnblogs.com/onlymate/p/9630788.htmlarrow-up-right

1、配置

三、踩坑

SpringBoot上传文件

一、介绍

二、整合

三、采坑

1、上传文件受大小限制

解决:

Spring Boot1.4版本后配置更改为:

Spring Boot2.0之后的版本配置修改为:

2、复杂类型无法映射到对象中去

Controller:

entity

(待解决)

SpringBoot整合shiro

一、介绍

二、整合

1、导入jar

2、配置类

3、常用

A、FormAuthenticationFilter

继承FormAuthenticationFilter实现一些

SpringBoot整合SpringData

三、踩坑

1、save无法获取自增主键

解决方案:

  • 为实体类的id注解 @GeneratedValue(strategy=GenerationType.IDENTITY) 指定id的生成策略

  • 获取自增id

SpringBoot整合UrlRewrite

一、介绍

如楼主般配置,shiro和Urlrewirte整合,shiro的拦截器首先执行,后续才是urlrewrite的拦截器

验证:urlrewrite拦截器打上断点 org.tuckey.web.filters.urlrewrite.UrlRewriteFilter#doFilter

​ shiro的拦截器打上断点 org.apache.shiro.web.servlet.OncePerRequestFilter#doFilter

二、整合

1、导入jar

2、配置类

三、采坑

问题:SpringBoot整合shiro和UrlRewrite,请求接口一直报错。

推荐文章:Spring Boot整合shiro出现UnavailableSecurityManagerExceptionarrow-up-right

解决:

SpringBoot-Actuator 健康监控

一、介绍

推荐文章:https://www.jianshu.com/p/1aadc4c85f51

二、整合

1、导入jar

2、配置yml

注意:

监控本身不具有安全认证,所以一般如果要实际使用,需配合 spring-boot-starter-security 使用,另外,actuator自带监控可能不满足场景业务需求,所以可以集成自己的需要的监控。

SpringBoot-整合UrlRewriter

一、介绍

 Urlrewriter的作用主要是重写url路径,以此来隐藏真实的路径,如:"http://www.xxxx.com/crm_index.do",而真实访问的是"http://www.xxxx.com/crm/index.jsp"。还有一种就是,在高并发访问的时候,可以更具是否用静态文件来进行路径的重写。

二、整合

第一步 jar包

第二步 配置类

第三步 配置文件

到此整合完成,下面我们进行测试,启动项目,访问127.0.0.1:80/test.html,回车后url变为127.0.0.1:80/test,即成功。

小提示:

问:应用拆分,同一个域名,如何映射各个应用?

(总不至于,每个接口的url,前面都加上应用区分的url吧,如“/u/user/login”,"/a/admin/login",/u 和 /a 仅仅用来区分应用,如果我们在代码中都加上,肯定不是一个好的解决方案)

答:在应用端,使用UrlRewriter帮助我们重写请求,这样可以做到代码层面不加上区分应用的Url,直接配置在UrlRewriter中,重写Url即可。(解决方案不止这一种)

UrlRewriter快速了解文章入口: 关于UrlRewrite的使用arrow-up-right

Springboot整合Redis

Springboot整合Redisson

一、介绍

二、整合

1、引入POM

2、配置文件

Tips:新建redisson-config.yml配置文件

3、写配置类

4、测试类

三、踩坑

1、redisson连接池的连接无法释放

Springboot2.1.1 之前的版本在接入redisson的时候,redission无法自动释放redis连接,会导致池中连接都在占用状态,后续的请求将无法从连接池中获取连接,导致请求阻塞。

解决方案,使用 2.1.1及之后的版本即可

// TODO 具体原因待分析。

2、开发阶段应用经常重启,导致redis已经被应用占用的连接无法释放。

// TODO 待解决方案,在系统关闭时,调用redisson.shutdown()

Last updated