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 采坑的更多相关文章

  1. Spring boot采坑记--- 在启动时RequstMappingHandlerMapping无法找到部分contorller类文件的解决方案

    最近有一个心得需求,需要在一个现有的springboot项目中增加一些新的功能,于是就在controller文件包下面创建新的包和类文件,但是后端开发完之后,本地测试发现前端访问报404错误,第一反应 ...

  2. Spring Boot踩坑之路一

    Takes an opinionated view of building production-ready Spring applications. Spring Boot favors conve ...

  3. caoni大业 spring boot 跳坑记

    IDEA环境 win10 跑得刚刚,到xp系统就戈壁 报错 Caused by: java.lang.NoSuchMethodError: javax.servlet.ServletContext.g ...

  4. Spring Boot 踩坑之路之 Configuration Annotation Proessor not found in classpath

    1. 出现spring boot Configuration Annotation Proessor not found in classpath的提示是在用了@ConfigurationProper ...

  5. spring boot踩坑记

    Resolved exception caused by handler execution: org.springframework.http.converter.HttpMessageNotWri ...

  6. Spring事务采坑 —— timeout

    版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明.本文链接:https://blog.csdn.net/qq_18860653/article/d ...

  7. 前后端分离,我怎么就选择了 Spring Boot + Vue 技术栈?

    前两天又有小伙伴私信松哥,问题还是职业规划,Java 技术栈路线这种,实际上对于这一类问题我经常不太敢回答,每个人的情况都不太一样,而小伙伴也很少详细介绍自己的情况,大都是一两句话就把问题抛出来了,啥 ...

  8. Spring Boot文档

    本文来自于springboot官方文档 地址:https://docs.spring.io/spring-boot/docs/current/reference/html/ Spring Boot参考 ...

  9. 从源码看Spring Security之采坑笔记(Spring Boot篇)

    一:唠嗑 鼓捣了两天的Spring Security,踩了不少坑.如果你在学Spring Security,恰好又是使用的Spring Boot,那么给我点个赞吧!这篇博客将会让你了解Spring S ...

随机推荐

  1. git的使用 (一)

    1.版本控制 版本控制(Version Control Systems)是一种记录一个或若干文件内容变化,以便将来查阅特定版本修订情况的系统.这个系统可以自动帮我们备份文件的每一次更改,并且可以非常方 ...

  2. 学习熟悉箭头函数, 类, 模板字面量, let和const声明

    箭头函数:https://blog.csdn.net/qq_30100043/article/details/53396517 类:https://blog.csdn.net/pcaxb/articl ...

  3. Angular7

    1.绑定html 在ts里面自定义一个html变量 html = '<a> 这是html变量 </a> '; 在html打印出来 直接打印:{{html}} <br /& ...

  4. 激活函数——sigmoid函数(理解)

    0 - 定义 $Sigmoid$函数是一个在生物学中常见的S型函数,也称为$S$型生长曲线.在信息科学中,由于其单增以及反函数单增等性质,$Sigmoid$函数常被用作神经网络的阈值函数,将变量映射到 ...

  5. 本地ssh设置多个git项目访问

    前因: 自己本地的~/.ssh里原本有个id_rsa,到了公司后新的git项目配置后,把自己原有的文件覆盖了,导致github和公司的项目我只能选一个,郁闷,怎么区分开呢? 大致逻辑是新生成一对密钥文 ...

  6. Java消息队列--ActiveMq 初体验

    1.下载安装ActiveMQ ActiveMQ官网下载地址:http://activemq.apache.org/download.html ActiveMQ 提供了Windows 和Linux.Un ...

  7. Lua中的表达式

    [算术操作符] Lua支持常规的算术操作符有:”+”(加法),”-“(减法),”*”(乘法),”/”(除法),”^”(指数),”%”(取模),一元的”-“(负号).所有的这些操作符都用于实数.例如:x ...

  8. 023_supervisorctl管理服务注意事项

    一. (1)问题 我在线上使用supervisorctl管理服务时遇到程序文件更新了,但是下次supervisorctl执行的时候并没有更新, 原因是supervisor更新后必须重新读取加载文件才行 ...

  9. OpenCV使用中的一些总结

    一.threshold阈值操作 1.阈值可以被视作最简单的图像分割方法.例如,从一副图像中利用阈值分割出我们需要的物体部分,这样的图像分割方法基于图像中的物体与背景之间的灰度差异. 2.thresho ...

  10. 【原创】大叔问题定位分享(19)spark task在executors上分布不均

    最近提交一个spark应用之后发现执行非常慢,点开spark web ui之后发现卡在一个job的一个stage上,这个stage有100000个task,但是绝大部分task都分配到两个execut ...