基于SpringBoot的基础用法
STEP1. 引入POM依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
这里有一个使用Specification查询时很好用的工具包,感谢做工具包的同学
<!-- https://mvnrepository.com/artifact/com.github.wenhao/jpa-spec -->
<dependency>
<groupId>com.github.wenhao</groupId>
<artifactId>jpa-spec</artifactId>
<version>3.2.4</version>
</dependency>
STEP2. 配置文件application.yml
spring:
jpa:
hibernate:
ddl-auto: update
show-sql: true #打印sql语句,方便调试
database-platform: org.hibernate.dialect.MySQL5InnoDBDialect #设置数据库引擎为InnoDB
properties:
hibernate:
format-sql: true #是否格式化输出字符串,增强SQL的可读性
STEP3. 实体类
这里写了一个抽象实体类和两个具体的实体类
抽象类
AnimalEntity.java
@Data
@EqualsAndHashCode(callSuper = false)
@MappedSuperclass
public abstract class AnimalEntity<ID> implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private ID id;
@Temporal(TemporalType.TIMESTAMP)
@CreationTimestamp
@Column(name = "gmt_create", nullable = false, updatable = false)
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
protected Date gmtCreate;
@Temporal(TemporalType.TIMESTAMP)
@UpdateTimestamp
@Column(name = "gmt_update", nullable = true, insertable = false)
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
@JSONField
protected Date gmtUpdate;
private String name;
private Integer age;
private String sex;
private Integer height;
private Integer weight;
private Long pid;
}
实际会对应数据库表的实体类
CatEntity.java
@Data
@Entity
@Table(name = "cat_tb")
@EqualsAndHashCode(callSuper = false)
public class CatEntity extends AnimalEntity<Long> {
private static final long serialVersionUID = 7456065103323391049L;
private String miao;
}
DogEntity.java
@Data
@Entity
@Table(name = "dog_tb")
@EqualsAndHashCode(callSuper = false)
public class DogEntity extends AnimalEntity<Long> {
private static final long serialVersionUID = 7456065103323391049L;
private String wang;
}
STEP4. repository类
同样写了一个base类和两个对应的操作类
@NoRepositoryBean
public interface BaseRepository<T, ID extends Serializable> extends JpaRepository<T, ID>, JpaSpecificationExecutor<T>, Serializable {
}
public interface CatRepository extends BaseRepository<CatEntity, Long> {
}
public interface DogRepository extends BaseRepository<DogEntity, Long> {
}
然后写一个测试的controller来测试
这是几个基础的单表查询
ChapterThreeController
@ApiOperation(value = "获取所有的猫", httpMethod = "GET")
@GetMapping("/find/cat")
public List<CatEntity> findCat() {
return catRepository.findAll();
}
@ApiOperation(value = "findById", httpMethod = "GET")
@GetMapping("/find/cat/{id}")
public CatEntity findOneCatById(@PathVariable Long id) {
return catRepository.findById(id).orElse(null);
}
@ApiOperation(value = "分页查询", httpMethod = "GET")
@GetMapping("/find/cat/page")
public Page<CatEntity> findCatByPage(@RequestParam int pageSize, @RequestParam int pageNum) {
Pageable pageable = PageRequest.of(pageNum, pageSize);
return catRepository.findAll(pageable);
}
@ApiOperation(value = "分页排序查询", httpMethod = "GET")
@GetMapping("/find/cat/order")
public Page<CatEntity> findCatByPageOrder(@RequestParam int pageSize, @RequestParam int pageNum) {
Pageable pageable = PageRequest.of(pageNum, pageSize, new Sort(Sort.Direction.DESC, "id"));
return catRepository.findAll(pageable);
}
@ApiOperation(value = "分页条件查询", httpMethod = "GET")
@GetMapping("/find/cat/search")
public Page<CatEntity> findCatByPageOrder(@RequestParam int pageSize, @RequestParam int pageNum, @RequestParam String miao) {
Pageable pageable = PageRequest.of(pageNum, pageSize, new Sort(Sort.Direction.DESC, "id"));
Specification<CatEntity> specification = Specifications.<CatEntity>and().eq(!StringUtils.isEmpty(miao), "miao", miao).build();
return catRepository.findAll(specification, pageable);
}
源码参考 GITHUB
欢迎关注微信交流