首页
学习
活动
专区
圈层
工具
发布

SpringDataJPA笔记(3)-基于SpringBoot基础用法

基于SpringBoot的基础用法

STEP1. 引入POM依赖

代码语言:javascript
复制
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>

这里有一个使用Specification查询时很好用的工具包,感谢做工具包的同学

代码语言:javascript
复制
<!-- 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

代码语言:javascript
复制
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

代码语言:javascript
复制
@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

代码语言:javascript
复制
@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

代码语言:javascript
复制
@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类和两个对应的操作类

代码语言:javascript
复制
@NoRepositoryBean
public interface BaseRepository<T, ID extends Serializable> extends JpaRepository<T, ID>, JpaSpecificationExecutor<T>, Serializable {
}
代码语言:javascript
复制
public interface CatRepository extends BaseRepository<CatEntity, Long> {
}
代码语言:javascript
复制
public interface DogRepository extends BaseRepository<DogEntity, Long> {
}

然后写一个测试的controller来测试

这是几个基础的单表查询

ChapterThreeController

代码语言:javascript
复制
 @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);
    }
下一篇
举报
领券