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. C# - LINQ 表达式树

    表达式树(Expression Tree) 表达式树是不可执行的代码,它只是用于表示一种树状的数据结构,树上的每一个节点都表示为某种表达式类型,大概有25种表达式类型,它们都派生自Expression ...

  2. 关于Linux的随笔笔记

    1.Ctrl+c.Ctrl+z和Ctrl+d的区别: Ctrl+c和Ctrl+z都是中断命令,但是他们的作用却不一样: Ctrl+c 是向当前进程发送SIGINT信号,用于终止进程: Ctrl+z 是 ...

  3. xheditor编辑器上传图片

    之前在用csdn的时候,觉得他们家的编辑器挺好用,精美,简洁,大方,功能强大.最近自己的项目也要用到编辑器,我就想起了xheditor. 好多大网站都用到它~好棒! 我把xheditor用于文章模块, ...

  4. 【原创】大叔经验分享(20)spark job之间会停顿几分钟

    今天遇到一个问题,spark应用中在一个循环里执行sql,每个sql都会向一张表写入数据,比如 insert overwrite table test_table partition(dt) sele ...

  5. 【原创】大叔问题定位分享(21)spark执行insert overwrite非常慢,比hive还要慢

    最近把一些sql执行从hive改到spark,发现执行更慢,sql主要是一些insert overwrite操作,从执行计划看到,用到InsertIntoHiveTable spark-sql> ...

  6. linux 乌班图 nginx php直接下载下来

    location ~ \.php(.*)$ { include snippets/fastcgi-php.conf; # # # With php-fpm (or other unix sockets ...

  7. AOP注解使用详解

    AOP为Aspect Oriented Programming的缩写,意为:面向切面编程,通过预编译方式和运行期动态代理实现程序功能的统一维护的一种技术.AOP是OOP的延续,是软件开发中的一个热点, ...

  8. RDay2-Problem 1 A

    题目描述 初始给你一个排列p[i],你可以执行以下操作任意多次. 选择一个i,交换p[i]和p[i+1]的值(其实就是交换排列当中两个相邻的元素). 我们现在希望对于任意的i满足p[i]不等于i,求最 ...

  9. [原创]Xilinx工具关联UEStudio

    UE安装目录如下: C:\Program Files (x86)\IDM Computer Solutions\UEStudio\UEStudio.exe 对于ISE工具,在Editor -> ...

  10. BZOJ3160 万径人踪灭 字符串 多项式 Manachar FFT

    原文链接http://www.cnblogs.com/zhouzhendong/p/8810140.html 题目传送门 - BZOJ3160 题意 给你一个只含$a,b$的字符串,让你选择一个子序列 ...