spring boot 采坑
2019年2月19日19:25:42
版本 2.1.3.RELEASE
1,本地开发需要加依赖库,保存实时热更新
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
</dependency>
配置是否开启
spring.devtools.add-properties=false
2,eclipse 格式化代码热键冲突,热键是ctrl+shift+f 如果使用搜狗输入法,在配置里面取消掉
3,java代码提示,Window ——> Preferences ——> Java ——> Editor ——> Content Assist
[auto activation triggers for java]自动补全触发器,默认是".", 这个位置可以设置成26个字母外加'.':.abcdefghijklmnopqrstuvwxyz(不区分大小写)
[auto activation triggers for javadoc]javadoc的触发器,默认是"@#".
4,@Controller 和 @RestController
@Controller 在使用模板的时候使用返回 是这个请求
如果只返回body数据在方法加上 @ResponseBody
如果只返回body数据就直接加上@RestController 注解
5,Loading class `com.mysql.jdbc.Driver'. This is deprecated.注意spring boot的版本
application.properties 修改
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
6,关于业务分层
@Service用于标注业务层组件,
@Controller用于标注控制层组件(如struts中的action),
@Repository用于标注数据访问组件,即DAO组件,
@Component泛指组件,当组件不好归类的时候,我们可以使用这个注解进行标注
7,开发手册 ghost win10缺少hh.exe
最简单的方式就是其他原装系统中复制hh.exe(windows目录),hhctrl.ocx,itss.dll,itircl.dll(windows/system32目录)。
1.将里面的hh.exe文件放置电脑 C:\Windows目录下;
2.将 hhctrl.ocx ,itss.dll ,itircl.dll三个文件放置 C:\Windows\System32 目录下;
3.以管理员方式打开cmd,输入:
4.regsvr32 hhctrl.ocx
5.regsvr32 itss.dll
6regsvr32 itircl.dll 提示成功即可
8,根据表生产表模型
网页工具:http://java.bejson.com/generator/
eclipse 自带工具jpa工具 mybatis也有类似工具
jpa工具添加
https://blog.csdn.net/xwnxwn/article/details/53304153
https://blog.csdn.net/abc997995674/article/details/80227396
建议使用jpa tools
9,freemarker 配置
10,设置eclipse设置IDE编码
https://blog.csdn.net/qq_20936333/article/details/81322007
11, @getMapping与@postMapping @RequestMapping
@getMapping与@postMapping
首先要了解一下@RequestMapping注解。 @RequestMapping用于映射url到控制器类的一个特定处理程序方法。可用于方法或者类上面。也就是可以通过url找到对应的方法。 @RequestMapping有8个属性。 value:指定请求的实际地址。 method:指定请求的method类型(GET,POST,PUT,DELETE)等。 consumes:指定处理请求的提交内容类型(Context-Type)。 produces:指定返回的内容类型,还可以设置返回值的字符编码。 params:指定request中必须包含某些参数值,才让该方法处理。 headers:指定request中必须包含某些指定的header值,才让该方法处理请求。 @getMapping与@postMapping是组合注解。 @getMapping = @requestMapping(method = RequestMethod.GET)。 @postMapping = @requestMapping(method = RequestMethod.POST)。
12,api返回restful风格的json数据格式,建议直接使用map做数据返回不用写 RequestMapping的一些参数
public class ResponseHelper { public static Map<String, Object> responseData(int code, String message, Object data) {
Map<String, Object> objects = new HashMap<String, Object>();
objects.put("code", code);
objects.put("message", message);
objects.put("data", data);
return objects;
} public static Map<String, Object> responseMessage(int code, String message) {
Map<String, Object> objects = new HashMap<String, Object>();
objects.put("code", code);
objects.put("message", message);
return objects;
} }
使用@RestController 注解,配置
spring.jackson.date-format=yyyy-MM-dd HH:mm:ss
spring.jackson.time-zone=Asia/Shanghai
13,关于数据存储时间和数据库返回时间不一致比如相差13,14个小时,是因为mysql的配置时区导致的
一般直接设置默认时区加8个小时就oK
[mysqld] default-time-zone='+8:00'
14,数据库主键生成策略
@GeneratedValue(strategy=GenerationType.AUTO)
@GeneratedValue:
@GeneratedValue 用于标注主键的生成策略,通过strategy 属性指定。默认情况下,JPA 自动选择一个最适合底层数据库的主键生成策略:SqlServer对应identity,MySQL 对应 auto increment。
在javax.persistence.GenerationType中定义了以下几种可供选择的策略:
–IDENTITY:采用数据库ID自增长的方式来自增主键字段,Oracle 不支持这种方式;
–AUTO: JPA自动选择合适的策略,是默认选项;
–SEQUENCE:通过序列产生主键,通过@SequenceGenerator 注解指定序列名,MySql不支持这种方式
–TABLE:通过表产生主键,框架借由表模拟序列产生主键,使用该策略可以使应用更易于数据库移植。
一般MySQL使用 @GeneratedValue(strategy = GenerationType.IDENTITY)
15字段自动更新
启动类加@EnableJpaAuditing
@EnableJpaAuditing @EntityListeners(AuditingEntityListener.class) 是用于监听实体类添加或者删除操作的。 @JsonIgnoreProperties(value = {"createdAt", "updatedAt"},
allowGetters = true) 这个注解是用于在除了在获取createAt,updateAt属性时进行操作,其他创建和更新操作都由jpa完成 @Column(nullable = false, updatable = false) 其中updatable = false表示不进行更新操作 @CreatedDate 表示该字为创建时间字段,在这个实体被insert时,设置值;@LastModifiedDate同理
16:请求参数接收的格式
@RequestParam post 请求不支持json格式
ajax的时候,为了方便可以使用get,但是不安全
$("#apply_link_form").submit(function(){
parent.layer.close(index); //再执行关闭
$.ajax({
async: false,
type: "POST",
url:'${pageContext.request.contextPath}/link/apply',
contentType : "application/x-www-form-urlencoded; charset=utf-8",
data:$("#apply_link_form").serialize(),
dataType: "text",
success: function () {
},
error: function () {
}
})
})
contentType : "application/x-www-form-urlencoded; charset=utf-8",
这个是关键
17:spring-boot @Component和@Bean的区别
@Component 是用在类上的
@Component
public class Student {
private String name = "lkm";
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
@Bean 需要在配置类中使用,即类上需要加上@Configuration注解
@Configuration
public class WebSocketConfig {
@Bean
public Student student(){
return new Student();
}
}
如果你想要将第三方库中的组件装配到你的应用中,在这种情况下,是没有办法在它的类上添加@Component注解的,因此就不能使用自动化装配的方案了,但是我们可以使用@Bean。
18.jpa参考手册
https://blog.csdn.net/qq_37939251/article/details/83052613
https://blog.csdn.net/yswknight/article/details/79257372
19,list的在for循环的时候使用add ,remove都会出现java.util.ConcurrentModificationException
解决办法:使用Iterator的add ,remove
20,Iterator引起的java.util.NoSuchElementException异常
不论什么情况
一个while里面不能调用两次next,不会边界溢出,异常错误名称很明显
demo
public List<AdminPermission> filterMenu(List<AdminPermission> permissionList, BigInteger adminId) throws Exception {
List<BigInteger> adminPermission = getAdminPermission(permissionList, adminId);
System.err.println(adminPermission.toString()); // 过滤菜单,目前固定三层
// list的在for循环的时候使用add ,remove都会出现java.util.ConcurrentModificationException
// 使用Iterator的remove
Iterator<AdminPermission> itr = permissionList.iterator(); while (itr.hasNext()) {
List<AdminPermission> child1 = itr.next().getChild();
Iterator<AdminPermission> itr1 = child1.iterator(); while (itr1.hasNext()) {
List<AdminPermission> child2 = itr1.next().getChild();
Iterator<AdminPermission> itr2 = child2.iterator(); while (itr2.hasNext()) {
if (!adminPermission.contains(itr2.next().getId())) {
itr2.remove();
}
}
// 必须这样写,不然会溢出边界
if (child2.size() == 0) {
itr1.remove();
}
}
if (child1.size() == 0) {
itr.remove();
} } return permissionList;
21:getOne 方法出现 org.hibernate.LazyInitializationException: could not initialize proxy [com.zs.logistics.model.New#656] - no Session
主要是因为 数据库模型有些字段懒加载导致的
解决办法:
spring.jpa.properties.hibernate.enable_lazy_load_no_trans=true
在操作的实体上加上
@Proxy(lazy = false)
这个任何使用对加载性能有影响的,自己斟酌,个人建议,配置为主,因为你不知要其他开发人员的开发习惯,规避bug
22,freemarker时间,数字格式化
https://blog.csdn.net/pengpengpeng85/article/details/52070602
23;PageRequest pageable 翻页问题是从0开始的,但是页面翻页是从1开始,减一就可以,需要添加判断
PageRequest pageRequest = PageRequest.of(pageNo - 1, pageSize, sort);
24,com.fasterxml.jackson.databind.exc.InvalidDefinitionException: No serializer found for class org.hibernate.proxy.pojo.javassist.JavassistLazyInitializer and no properties discovered to create BeanSerializer (to avoid exception, disable SerializationFeature.FAIL_ON_EMPTY_BEANS)
错误原因json化字段有null的值
发现是实体类中有的字段值为null,所以在json化的时候,fasterxml.jackson将对象转换为json报错
解决办法:
在实体类上面加上注解 @JsonIgnoreProperties(value = { "hibernateLazyInitializer", "handler" })
25,Spring JPA 使用@CreatedDate、@CreatedBy、@LastModifiedDate、@LastModifiedBy 自动生成时间和修改者
1.实体类加注解
/**
* 创建时间
*/
@CreatedDate
@Column(name = "create_time")
private Date createTime; /**
* 修改时间
*/
@LastModifiedDate
@Column(name = "modify_time")
private Date modifyTime; 2.实体类头加注解 @EntityListeners(AuditingEntityListener.class) 3.SpringBoot启动类加注解 @EnableJpaAuditing
在spring jpa中,支持在字段或者方法上进行注解@CreatedDate、@CreatedBy、@LastModifiedDate、@LastModifiedBy,从字面意思可以很清楚的了解,这几个注解的用处。
@CreatedDate
表示该字段为创建时间时间字段,在这个实体被insert的时候,会设置值
@CreatedBy
表示该字段为创建人,在这个实体被insert的时候,会设置值
@LastModifiedDate、@LastModifiedBy同理
25,添加阿里云镜像
pom.xml
<repositories>
<repository>
<id>central</id>
<name>aliyun maven</name>
<url>http://maven.aliyun.com/nexus/content/groups/public/</url>
<layout>default</layout>
<!-- 是否开启发布版构件下载 -->
<releases>
<enabled>true</enabled>
</releases>
<!-- 是否开启快照版构件下载 -->
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
</repositories>
简单版
<repositories>
<repository>
<id>aliyunmaven</id>
<url>http://maven.aliyun.com/nexus/content/groups/public/</url>
</repository>
</repositories>
26,This compilation unit is not on the build path of java project
.project 文件 加上
<nature>org.eclipse.jdt.core.javanature</nature>
27,editor does not contain a main type
在 解决方法: 对着:src 路径右键 -> Build Path -> Use as Source Folder
28,java.sql.SQLNonTransientConnectionException: Could not create connection to driver: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/yinliu?characterEncoding=utf8&useSSL=false&serverTimezone=UTC&rewriteBatchedStatements=true driver: com.mysql.cj.jdbc.Driver
29,com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure
mysql配置文件添加
wait_timeout=1814400
30 jpa映射 json格式数据
<!-- https://mvnrepository.com/artifact/com.vladmihalcea/hibernate-types-5 -->
<dependency>
<groupId>com.vladmihalcea</groupId>
<artifactId>hibernate-types-5</artifactId>
<version>2.4.3</version>
</dependency>
并将此行添加到实体 @TypeDef(name = "json", typeClass = JsonStringType.class)
@Type( type = "json" name = "json_value")
@Column( columnDefinition = "json" )
private List<String> jsonValue;
31,mysql tinyint 如果使用unsigned 是1-255,所以映射到jpa不能使用byte 需要int,java没有无符号定义,byte 是-127到128
32,单引号,双引号的区别
单引号引的数据 是char类型的——》单引号只能引一个字符(表示单个字符)
双引号引的数据 是String类型的——》而双引号可以引0个及其以上(引用字符串) char类型的值用单引号引起来的单个字符
如: char a = 'b'
而java中的双引号 表示字符串 一个或多个字符
如 String c = "abc"
String d="a"
和char d=‘a’
spring boot 采坑的更多相关文章
- Spring boot采坑记--- 在启动时RequstMappingHandlerMapping无法找到部分contorller类文件的解决方案
最近有一个心得需求,需要在一个现有的springboot项目中增加一些新的功能,于是就在controller文件包下面创建新的包和类文件,但是后端开发完之后,本地测试发现前端访问报404错误,第一反应 ...
- Spring Boot踩坑之路一
Takes an opinionated view of building production-ready Spring applications. Spring Boot favors conve ...
- caoni大业 spring boot 跳坑记
IDEA环境 win10 跑得刚刚,到xp系统就戈壁 报错 Caused by: java.lang.NoSuchMethodError: javax.servlet.ServletContext.g ...
- Spring Boot 踩坑之路之 Configuration Annotation Proessor not found in classpath
1. 出现spring boot Configuration Annotation Proessor not found in classpath的提示是在用了@ConfigurationProper ...
- spring boot踩坑记
Resolved exception caused by handler execution: org.springframework.http.converter.HttpMessageNotWri ...
- Spring事务采坑 —— timeout
版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明.本文链接:https://blog.csdn.net/qq_18860653/article/d ...
- 前后端分离,我怎么就选择了 Spring Boot + Vue 技术栈?
前两天又有小伙伴私信松哥,问题还是职业规划,Java 技术栈路线这种,实际上对于这一类问题我经常不太敢回答,每个人的情况都不太一样,而小伙伴也很少详细介绍自己的情况,大都是一两句话就把问题抛出来了,啥 ...
- Spring Boot文档
本文来自于springboot官方文档 地址:https://docs.spring.io/spring-boot/docs/current/reference/html/ Spring Boot参考 ...
- 从源码看Spring Security之采坑笔记(Spring Boot篇)
一:唠嗑 鼓捣了两天的Spring Security,踩了不少坑.如果你在学Spring Security,恰好又是使用的Spring Boot,那么给我点个赞吧!这篇博客将会让你了解Spring S ...
随机推荐
- 初步认识Promise
在解释什么是Promise之前,先看一道练习题,做完练习题也就知道Promise到底是干嘛用的了. 假设现在有个需求:你要封装一个方法,我给你一个要读取文件的路径,你这个方法能帮我读取文件,并把内容返 ...
- 【汇总目录】C#
[2019年04月29日] C# textbox 自动滚动 [2019年02月07日] C#利用VUDP.cs开发网络通讯应用程序 [2019年02月06日] C#利用VINI.cs操作INI文件 [ ...
- oracle建表 和 设置主键自增
1.新建table CREATE TABLE ysb_log( id ) primary key not null , tbdate ) NULL, tb_time ) NOT NULL, tblog ...
- 基于VC的MFC界面开发
教你熟悉VC6.0创建一个可视化软件的过程 UpdateData(TRUE);//将输入数据赋给文本框变量. UpdateData(FALSE);//将内容显示在文本框中 AfxMessageBox( ...
- Innodb和Myisam数据恢复
(转自)https://www.cnblogs.com/DwyaneTalk/p/4113829.html 背景 这次恢复oracle和sqlserver,想想也不能把mysql落下了吧.三剑合一.都 ...
- .NET垃圾回收机制(一)
垃圾收集器(GarbageCollection)是组成.Net平台一个很重要的部分,.NET垃圾回收机制降低了编程复杂度,使程序员不必分散精力去处理析构.不妨碍设计师进行系统抽象.减少了由于内存运用不 ...
- zt 正则
http://deerchao.net/tutorials/regex/regex.htm 正则表达式30分钟入门教程 版本:v2.3.5 (2017-6-12) 作者:deerchao 转载 ...
- 金蝶K3 WISE BOM多级展开_物料齐套表
/****** Object: StoredProcedure [dbo].[pro_bobang_ICItemQiTao] Script Date: 07/29/2015 16:12:10 **** ...
- Kafka(二)CentOS7.5搭建Kafka2.11-1.1.0集群与简单测试
一.下载 下载地址: http://kafka.apache.org/downloads.html 我这里下载的是Scala 2.11对应的 kafka_2.11-1.1.0.tgz 二.kaf ...
- 安装酷痞到IIS7.x共用80端口Windows(64位)系统下运行多个酷痞
需求: 1.酷痞直接运行的模式是自宿主运行.由于win系统一般都由iis提供多个网站服务,并首先占用了80端口,如果想酷痞可以直接通过主机头和iis共用80端口会出现这种运行模式的需求. 以下是实现方 ...