Skip to main content

spring-boot

0x01 RESTful

User.java

  • lombok @Data getter setter 相比 1.x 版本教程中自定义 set 和 get 函数的方式,这里使用@Data 注解可以实现在编译器自动添加 set 和 get 函数的效果。该注解是 lombok 提供的

  • javax.validation.constraints valid

UserController.java

    // 创建线程安全的Map,模拟users信息的存储
static Map<Long, User> users = Collections.synchronizedMap(new HashMap<Long, User>());
  • @RestController res body as json
@PostMapping(path = "/add")
public @ResponseBody String addNewGhost(@RequestBody City city) {
cityService.insert(city);
return "Saved";
}

// =>

@PostMapping(path = "/add")
public String addNewGhost(@RequestBody City city) {
cityService.insert(city);
return "Saved";
}
  • @RequestMapping

  • @GetMapping @PostMapping @PutMapping @DeleteMapping req method

  • @PathVariable Long id uri param

  • @RequestParam req param

  • @RequestBody User user req body

0x02 Spring Data repository

Accessing data with MySQL

Hibernate

Ghost.java

  • javax.persistence @Entity 实体类 This tells Hibernate to make a table out of this class

  • @Id primire key

GhostRepository.java

GhostController.java

	@Autowired // This means to get the bean called userRepository
// Which is auto-generated by Spring, we will use it to handle the data
private GhostRepository ghostRepository;

ghostRepository.save(n);

ghostRepository.findAll();

0x00 Spring Data JPA

build.gradle

dependencies {
implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
}

A few examples to try out:

Repository

Entity <==> Repository

import java.util.List;

import org.springframework.data.repository.PagingAndSortingRepository;
import org.springframework.data.repository.query.Param;
import org.springframework.data.rest.core.annotation.RepositoryRestResource;

@RepositoryRestResource(collectionResourceRel = "people", path = "people")
public interface PersonRepository extends PagingAndSortingRepository<Person, Long> {

List<Person> findByLastName(@Param("name") String name);

}

JpaRepository

PagingAndSortingRepository

Extension of CrudRepository to provide additional methods to retrieve entities using the pagination and sorting abstraction.

interface JpaRepository<T, ID> extends PagingAndSortingRepository<T, ID>, QueryByExampleExecutor<T>

interface PagingAndSortingRepository<T, ID> extends CrudRepository<T, ID>

CrudRepository

https://www.jianshu.com/p/cbf0a7c5c985

@RepositoryRestResource

0x00 Database

redis

    implementation "org.springframework.boot:spring-boot-starter-data-redis"
redis:
database: 0
host: localhost
port: 6379
password:
pool:
# 连接池最大连接数(使用负值表示没有限制)
max-active: 8
# 连接池最大阻塞等待时间(使用负值表示没有限制)
max-wait: -1
# 连接池中的最大空闲连接
max-idle: 8
# 连接池中的最小空闲连接
min-idle: 0
# 连接超时时间(毫秒)
timeout: 0

0x04 Mybatis

Mybatis Comment

Mybatis XML

MyBatis Pagination

0x00 Bean

0x06 With Kotlin

https://www.jianshu.com/p/0d67771ae14c

https://spring.io/guides/tutorials/spring-boot-kotlin/

协程

Spring Dependency Injection Patterns

0x0X

org.springframework.beans.factory.annotation.Autowired @Autowired

0x03 Reactive

Spring WebFlux

  • Spring Web MVC -> Spring Data

  • Spring WebFlux -> Spring Data Reactive

Building a Reactive RESTful Web Service

聊聊 Spring Boot 2.0 的 WebFlux

RxJava

Vert.x

Async