• 使用JPA

    • 虽然JPA是一个标准,但spring中一般就是使用hibernate实现的
    • 使用JPA(Java Persistence API,Java持久化API,是个规范,其实是借助Hibernate实现的)操作数据库,使用其CrudRepository接口。
    • 数据库建好后,表会在使用时自动创建。
    • 依赖是通过Gradle的dependencies中加入compile('org.springframework.boot:spring-boot-starter-data-jpa'),会简介引用一些其他的包如spring.rabbit-1.7.8.RELEASE.jar等
      • 也可能是通过引入上层的配置
    • application.properties中配置数据库信息,如果是Cloud Foundry等平台,app部署后的环境变量中如果有同名参数则会覆盖properties文件
# postgresql
spring.datasource.driverClassName=org.postgresql.Driver
spring.datasource.url=jdbc:postgresql://localhost:5432/amp
spring.datasource.username=postgres
spring.datasource.password=deb.deb
spring.jpa.database=POSTGRESQL
spring.jpa.database-platform=org.hibernate.dialect.PostgresPlusDialect
spring.jpa.show-sql=false
spring.jpa.hibernate.ddl-auto=update
* 定义用户POJO(算是数据库表的映射类)
    * 没有表、索引等时,会自动创建
    * 会用到一些数据库相关的注解,如@Entity(name = "assignment")、@Table、@Index、@NoArgsConstructor、@Id、@GeneratedValue(strategy = GenerationType.AUTO)、@Column(name = "tenant_id", nullable = false)、、
@Entity(name = "assignment")
@Table(indexes = {
        @Index(name = AMPShiftCalendarConstants.IDX_TENANT_ID_ASSET_ID_ASSIGNMENT_START_DATE, columnList = "tenant_id, asset_id, assignment_start_date"),
        @Index(name = AMPShiftCalendarConstants.IDX_TENANT_ID_ASSIGNMENT_START_DATE, columnList = "tenant_id, assignment_start_date") })
@NoArgsConstructor
@Getter
@Setter
public class Assignment {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name = "assignment_id")
    private Long assignmentId;

    @Column(name = "tenant_id", nullable = false)
    private String tenantId;

    @Column(name = "asset_id", nullable = false)
    private String assetId;

    @Column(name = "content", columnDefinition = "TEXT")
    private String content;

    @Column(name = "assignment_start_date", columnDefinition = "timestamp with time zone", nullable = false)
    private Timestamp assignmentStartDate;

    @Column(name = "inserted_date", columnDefinition = "timestamp with time zone", nullable = false)
    private Timestamp insertedDate;

    @Enumerated(EnumType.STRING)
    @Column(name = "status", nullable = false)
    private AssignmentStatus status;

    public Assignment(String tenantId, String assetId, ShiftCalendar content, Timestamp assignmentStartDate, Timestamp insertedDate, AssignmentStatus status) {
        this.tenantId = tenantId;
        this.assetId = assetId;
        this.setShiftCalendar(content);
        this.assignmentStartDate = assignmentStartDate;
        this.insertedDate = insertedDate;
        this.status = status;
    }

    public ShiftCalendar getShiftCalendar() {
        return content != null ? Misc.deserializeSafeJson(content, ShiftCalendar.class) : null;
    }

    public void setShiftCalendar(ShiftCalendar template) {
        Check.notNull(template, "shiftCalendar");

        this.content = Misc.serializeSafeJson(template);
    }

    @Override
    public String toString() {
        return "Assignment [assignment_id=" + assignmentId + ", asset=" + assetId + ", assignmentStatus=" + status + ", content=" + content
                + ", assignment_start_date=" + assignmentStartDate + "]";
    }
}
* 自定义Repository类
    * 基于表及其映射类,也可以直接用CrudRepository
    * 用于数据库操作,有点类似于.NET的EF的DBContext类,基类已实现了部分通用简单的基于数据库表的操作,复杂的还是要自己拼sql(用xml或动态sql或者如下的方式)。
    * 其实还有其他更具体的功能更强的子类可以用,比如可以分页什么的。
    * getAllByTenantIdAndAssetId这种其实是利用了JPA的命名查询功能,可以指定通过哪些列来查询,还可以使用like功能。
    * @Query注解其实是JPA提供查询语言的JPQL,与Hibernate提供的HQL十分接近。
    * JPA(Hibernate)其实慢慢被mybatis替代了??
@Repository
public interface AssignmentRepository extends CrudRepository<Assignment, Long> {

    List<Assignment> getAllByTenantId(String tenantId);

    List<Assignment> getAllByTenantIdAndAssetId(String tenantId, String assetId);

    @Query(value = "" +
            "  SELECT " +
            "DISTINCT a.asset_id AS asset_id" +
            "    FROM assignment a " +
            "   WHERE a.tenant_id = :tenant_id" +
            "     AND a.assignment_start_date < :end_date" +
            "     AND a.status='ACTIVE'",
            nativeQuery = true)
    List<String> getAllAssetsWithActiveWorkplan(
            @Param("tenant_id") String tenantId,
            @Param("end_date") Timestamp endDate);

}

public interface MissingWorkPieceRepository extends CrudRepository<MissingWorkPiece, Long> {

    List<MissingWorkPiece> getAllByAssetIdAndTenantIdAndDateBetweenOrderByDateDesc(String assetId, String tenantId, Timestamp from, Timestamp to);

    Optional<MissingWorkPiece> getByAssetIdAndTenantIdAndDate(String assetId, String tenantId, Timestamp date);

    @Transactional
    void deleteByAssetIdAndTenantIdAndDate(String assetId, String tenantId, Timestamp date);

}

Spring Boot - Spring Data的更多相关文章

  1. Spring Boot + Spring Data + Elasticsearch实例

    Spring Boot + Spring Data + Elasticsearch实例 学习了:https://blog.csdn.net/huangshulang1234/article/detai ...

  2. 255.Spring Boot+Spring Security:使用md5加密

    说明 (1)JDK版本:1.8 (2)Spring Boot 2.0.6 (3)Spring Security 5.0.9 (4)Spring Data JPA 2.0.11.RELEASE (5)h ...

  3. 256.Spring Boot+Spring Security: MD5是加密算法吗?

    说明 (1)JDK版本:1.8 (2)Spring Boot 2.0.6 (3)Spring Security 5.0.9 (4)Spring Data JPA 2.0.11.RELEASE (5)h ...

  4. Spring Boot+Spring Security:获取用户信息和session并发控制

    说明 (1)JDK版本:1.8(2)Spring Boot 2.0.6(3)Spring Security 5.0.9(4)Spring Data JPA 2.0.11.RELEASE(5)hiber ...

  5. springboot成神之——spring boot,spring jdbc和spring transaction的使用

    本文介绍spring boot,spring jdbc和spring transaction的使用 项目结构 依赖 application model层 mapper层 dao层 exception层 ...

  6. spring Boot+spring Cloud实现微服务详细教程第二篇

    上一篇文章已经说明了一下,关于spring boot创建maven项目的简单步骤,相信很多熟悉Maven+Eclipse作为开发常用工具的朋友们都一目了然,这篇文章主要讲解一下,构建spring bo ...

  7. spring Boot+spring Cloud实现微服务详细教程第一篇

    前些天项目组的大佬跟我聊,说项目组想从之前的架构上剥离出来公用的模块做微服务的开发,恰好去年的5/6月份在上家公司学习了国内开源的dubbo+zookeeper实现的微服务的架构.自己平时对微服务的设 ...

  8. Spring boot +Spring Security + Thymeleaf 认证失败返回错误信息

    [Please make sure to select the branch corresponding to the version of Thymeleaf you are using] Stat ...

  9. Cola Cloud 基于 Spring Boot, Spring Cloud 构建微服务架构企业级开发平台

    Cola Cloud 基于 Spring Boot, Spring Cloud 构建微服务架构企业级开发平台: https://gitee.com/leecho/cola-cloud

  10. spring boot + spring batch 读数据库文件写入文本文件&读文本文件写入数据库

    好久没有写博客,换了一家新公司,原来的公司用的是spring,现在这家公司用的是spring boot.然后,项目组布置了一个任务,关于两个数据库之间的表同步,我首先想到的就是spring batch ...

随机推荐

  1. Linux网络编程---htons函数的使用

    htons是将整型变量从主机字节顺序转变成网络字节顺序, 就是整数在地址空间存储方式变为高位字节存放在内存的低地址处. htonl就是把本机字节顺序转化为网络字节顺序所谓网络字节顺序(大尾顺序)就是指 ...

  2. html标签的总结-重复

    <!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8" ...

  3. 清空表中数据 id从1开始

    删除表的记录以后,如何使新记录的编号仍然从1开始有两种方法: 方法1: truncate table 你的表名 --这样不但将数据删除,而且可以重新置位identity属性的字段. 方法2: dele ...

  4. 3-QT程序运行时报错E:\SogouInput\6.7.0.0329\程序异常终止,该怎么解决?

    https://bbs.csdn.net/topics/390653779 出现这个错误的原因,使用声明的对象时,没有使用new对对象进行实例化. 包括:数组.

  5. vim之quickfix

    [vim之quickfix] quickfix功能将编译过程中产生的错误信息保存到文件中,然后vim利用这些信息跳转到源文件的对应位置,我们就可以进行错误的修正,之后跳到下一个错误重复上述操作,从而极 ...

  6. cmake重新编译

    删除文件夹下的文件 rm CMakeCache.txt 重新编译即可 安装g++ yum install gcc-c++

  7. 如何删除win8自带输入法

    如何删除 win8 自带输入法 win8 自带的那个中文输入法太坑了,想删又删不了.试了半个小时才试出一个方法.方法如下: 第一步:在更改语言道选项下面点击[添加语言] [控制面板] -> [时 ...

  8. 2018.07.03 POJ 3348 Cows(凸包)

    Cows Time Limit: 2000MS Memory Limit: 65536K Description Your friend to the south is interested in b ...

  9. 2018.09.05 任务安排(斜率优化dp)

    描述 这道题目说的是,给出了n项必须按照顺序完成的任务,每项任务有它需要占用机器的时间和价值.现在我们有一台机器可以使用,它每次可以完成一批任务,完成这批任务所需的时间为一个启动机器的时间S加上所有任 ...

  10. 2018.08.27 lucky(模拟)

    描述 Ly 喜欢幸运数字,众所周知,幸运数字就是数字位上只有 4 和 7 的数字. 但是本题的幸运序列和幸运数字完全没关系,就是一个非常非常普通的序列. 哈哈,是 不是感觉被耍了,没错,你就是被耍了. ...