美文网首页
教练,我想学 Java - Java SpringBoot AP

教练,我想学 Java - Java SpringBoot AP

作者: aiingstan | 来源:发表于2020-07-29 11:19 被阅读0次

我作为一个 .NET 起步的半吊子全栈工程师,最近因为接触 SpringBootIntellij IDEA 感到身心舒适,突然有了学习一下 Java 的想法 - JetBrains 的东西确实好用,如果说,曾经的 Visual Studio 号称宇宙第一 IDE 的话,那么现在,我们有更多的选择,Visual Studio Code 还有 JetBrains 全家桶都是完全可以竞争宇宙第一 IDE 的名号。

首先 Java 给我感觉是一门非常成功的语言,不过到了今天,还是得去了解在当下还有没有必要去学习 Java。当然作为一门编程语言的 Java 一定是值得去了解一二的,这个可以应用到任何 VB 之外的编程语言上。我主要的顾虑是听说 Oracle JDK 收费的消息,担心会对 Java 的生态造成一些影响。毕竟如果一个语言的应用场景在缩窄,那么学习以后它的使用机会和价值都会降低。但是稍微了解了一下之后,我相信 OracleJDK 收费带来的影响还是有限的。

那么就开始逐步记录一下开发实践过程 (aka. bug producing) 中产生的疑问吧!

Java Language

javadoc - The Java API Documentation Generator

DESCRIPTION
The Javadoc™ tool parses the declarations and documentation comments in a set of Java source files and produces a corresponding set of HTML pages describing (by default) the public and protected classes, nested classes (but not anonymous inner classes), interfaces, constructors, methods, and fields. You can use it to generate the API (Application Programming Interface) documentation or the implementation documentation for a set of source files.

常用标注

  • {@link Class}
  • {@link Class#Symbol}

函数、方法参数默认值?

不支持
在参数较少的方法中使用 this(p1, p2, default1, default2) 的方式重载

Stream API 和 Fork/Join

Fork/Join
工作窃取模式

sequential and parallel 串行和并行

findFirst vs findAny
从方法名称上就能看的出来,findAny 要更加随意一些

Optional

orElse vs orElseGet

Optonal 的值是空值时,无论 orElse 还是 orElseGet 都会执行;
而当返回的 Optional 有值时,orElse 会执行,而 orElseGet 不会执行。

Optional.of()
Optional.empty()

Supplier<T>

Supplier<String> supplier = String::new;
supplier.get(); // -> ""

ThreadLocal

Security

SecurityContextHolder

注解

@Valid 可以用在请求相应方法的参数上

Marks a property, method parameter, or method return type for validation cascading.

Packages!

工欲善其事,必先利其器

  • com.google.guava
  • java.time
  • lombok

lombok 实践

@AllArgsConstructor 如何处理 @MappedSuperclass 继承得到的实体类的构造函数?

不要在 Entity 上标注 @Data
Lombok Data Objects Aren't Entities

[TBD]
builder 是建立在实体类上的一个静态方法

比较日期

判断两个日期是否相等 DateUtils.isSameDay

equalTo 方法?

其他语言常见的比较方式是 ==JavaScript 更加优秀,增加了严格相等 ===Java 同时提供 ==equalTo 判断。== 判断,对于元数据类型(Java里面叫什么名字?基础数据类型?)判断“有效”,对于引用类型的字符串、对象来说“无效”。有效、无效在这里指的是业务上的。

int? - C# is a GOD

需要设计一个可空整数型字段的时候,自然而然想到了 int?,C# 多么好的语法啊。
回到 Java,一个方案是使用 java.lang.Integer
how-to-present-the-nullable-primitive-type-int-in-java

Spring Boot

See Spring Boot

JPA

https://www.logicbig.com/tutorials/java-ee-tutorial/jpa/jpa-primary-key.html

事务

思考:新增一个实体对象之后需要手动完成以下操作,这是必须的吗?

val employee = new Employee();
val department = new Department();
employee.setDepartment(department);
department.addEmployee(employee);

Hibernate

可以认为 JPA 是接口和规范,Hibernate 是一种实现

配置

[TBD]
hibernate.query.substitutions 控制在 HQL 语句中的值的等价表述

HQL

[TBD]
SQL 不同的是,HQL 不支持在连接查询处使用 on 关键词,而是需要使用 with

select e from Employee e inner join fetch e.department d with d.id = :id

实体类 (Entity)

注解

javax.persistence vs hibernate
difference-between-javax-persistence-and-hibernate-annotations

You use javax.persistence to keep implementation-agnostic (i.e. you can change to use a different JPA implementation later.) If you want to just go with Hibernate, you can use org.hibernate annotations. But it's advisable to just import the javax classes rather. Then Hibernate will provide the implementation, but can be replaced by just switching in a different dependency.

The javax.* packages are defined by JSRs (java specification requests). Like an interface, the javax packages define contracts. Providers, such as hibernate, supply the implementation. By using the javax imports, you decouple yourself from a specific provider and retain the ability to switch to a different implementation in the future.
Here's a list of other JSRs beyond javax.persistence (https://jcp.org/ja/jsr/all). The javax.persistence (2.0) specification is defined by JSR-317.

  • @Entity
  • @Table
  • @Id

继承

核心 @MappedSuperclass
https://stackoverflow.com/questions/36991236/mappedsuperclass-is-not-an-entity-in-jpa

相关文章

网友评论

      本文标题:教练,我想学 Java - Java SpringBoot AP

      本文链接:https://www.haomeiwen.com/subject/ymkhrktx.html