前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >SpringDataJPA笔记(3)-基于SpringBoot基础用法

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

作者头像
yingzi_code
发布2019-08-31 12:23:59
5110
发布2019-08-31 12:23:59
举报
文章被收录于专栏:生活不止眼前的代码

基于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);
    }
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2019年05月06日,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • STEP1. 引入POM依赖
  • STEP2. 配置文件application.yml
  • STEP3. 实体类
  • STEP4. repository类
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档