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的removeIterator<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=truedriver: 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踩坑记之Whitelabel Error Page问题
在学习Spring Boot时,了解到页面推荐采用freemarker的ftl格式文件,好像这玩意比jsp功能强大吧,那我也就学习下这个,毕竟多学总没有坏处.然后在后台跳转前端时浏览器报错Whitel ...
- spring boot 踩坑日记: 错误: 找不到或无法加载主类 xxx.xxx.xxx
错误信息: 错误: 找不到或无法加载主类 io.sr.SrDhTraApplication 解决: 在pom.xm文件中指明启动类位置: 代码: <configuration><fo ...
- Spring Boot (一)Spring Boot 概述
Spring Boot(一) 一 . Spring Boot 是什么? 首先Spring Boot不是一个框架,它是一种用来轻松创建具有最小或零配置的独立应用程序的方式.这是方法用来开发基于Sprin ...
- Spring Boot葵花宝典:初现江湖
[视频&交流平台] à SpringBoot视频 http://study.163.com/course/introduction.htm?courseId=1004329008&ut ...
- 【Spring Boot】闲聊Spring Boot(一)
背景 工作一直比较忙,好久都没写博客了,最近项目空闲期刚好能好好研究研究技术,顺便写写博客. 本人一直想写一个自己的博客系统,但是前端技术不好而搁置好久,学习了下HTML,CSS,JS...感觉自己小 ...
- 什么?Spring Boot CommandLineRunner 有坑!?
作者 | 狮子头儿 来源 | https://blog.csdn.net/zwq_zwq_zwq/article/details/81059017 使用场景 在应用程序开发过程中,往往我们需要在容器启 ...
- Spring boot处理附件的一个坑
最近在做项目的时候由以前的war包部署在tomcat中运行,改成了Spring boot框架,Spring boot框架更加简单方便的搭建一个web应用. 之前的代码在改造的过程中遇到了一个关于附件处 ...
- Spring boot升级到2.3.2.Release和Spring framework升级到5.28.Release踩过的坑
目录 1. 利用下面方法启动spring boot 项目是系统参数不生效 2. org.drools.template.parser.DecisionTableParseException: Fail ...
- Spring Boot 从1.0 升级到 2.0 所踩的坑
先给大家晒一下云收藏的几个数据,作为一个 Spring Boot 的开源项目(https://github.com/cloudfavorites/favorites-web)目前在 Github 上面 ...
最新文章
- mpi并行 java_【并行计算】用MPI进行分布式内存编程(一)
- new char[x]和new char(x)的差别
- MediaSource 缓存
- JQuery:多张图片的淡入淡出效果。
- 前端常见知识点一之HTTP
- 【整理】MySQL 之 autocommit
- 批量导数据之利器-load data[2016-07-11]
- U盘的针脚板竟然掉了
- 梅特勒托利多电子秤显示EEP服务器错误,梅特勒-托利多电子天平常见故障的解决方法...
- 跟着海盗头子创业是一种怎样的体验?
- 港大计算机系教授中科大毕业的吗,女儿在香港大学(港大)
- uniapp定位和选择城市
- java淡蓝色怎么表示_最淡的蓝是什么颜色(淡蓝色配什么颜色好看)
- 完美解决win7系统中IE占用CPU过高问题(转)
- 手机项目人力投入评估
- sql的datetime 数据类型
- 交叉验证(s折、分层、留一法)
- Spring4 对Bean Validation规范的新支持(方法级别验证)
- 平衡面板数据中的缺失值可以存在吗?
- python 邮件_Python发送邮件(常见四种邮件内容)