zoukankan      html  css  js  c++  java
  • DataJPA简述与注解

    1.什么是JPA?
    JPA是Java持久化规范(ORM规范,不是ORM框架),比较方便我们操作数据库。
    真正实现对应功能的是Hibernate,TopLink,默认是Hibernate。

    2.JPA优点:
    标准化;简单易用,集成方便;可以媲美JDBC的查询能力;支持面向对象的高级特性

    3.JPA不足:
    比较复杂的逻辑需要我们自己来写。

    3.使用jpa(springboot+maven)
    引入依赖

          <dependency>
              <groupId>org.springframework.boot</groupId>
              <artifactId>spring-boot-starter-data-jpa</artifactId>
          </dependency>
    

    配置文件添加信息

          spring.jpa.database=oracle()
          spring.jpa.properties.javax.persistence.validation.mode=none()
          spring.jpa.hibernate.ddl-auto=update()
          spring.jpa.properties.javax.persistence.sharedCache.mode=DISABLE_SELECTIVE()
    

    创建javabean,配置对应注解

    
    @Data
    @NoArgsConstructor
    @AllArgsConstructor
    @Table(name = "T_OTC_DER_VAL_BAL")
    public class OTCDerValBalDO implements Serializable{
    
    	private static final long serialVersionUID = -274718778810585426L;
    	// ID
    	@Id
    	@Column(name = "ID")
    	@KeySql(sql = ValqaSeqConfig.SEQ_OTC_DER_VAL_BAL, order = ORDER.BEFORE)
    	private Long id;
    
    	@Column(name = "BIZ_DATE")
    	private Date bizDate;
    
    	@Column(name = "PRODUCT_CODE")
    	private String productCode;
    
    	@Column(name = "CONTRACT_TYPE")
    	private String contractType;
    
    	@Column(name = "BROKER_CODE")
    	private String brokerCode;
    
    	@Column(name = "TRADE_ID")
    	private String tradeId;
    
    	@Column(name = "CONTRACT_VAL")
    	private BigDecimal contractVal;
    
    	@Column(name = "CONTRACT_COST")
    	private BigDecimal contractCost;
    
    	@Column(name = "PRINCIPAL")
    	private BigDecimal principal;
    
    	@Column(name = "SETTLE_CCY")
    	private String settleCcy;
    
    	@Column(name = "VAL_CCY")
    	private String valCcy;
    
    }
    

    注解解释:
    @Entity :指出该Java类为实体类,将映射到指定的数据库
    @Transient :忽略该属性,不需要映射到数据表的一列,否则默认为@Basic
    @Temporal :在属性上调整精度,比如Date
    @Table :标注常用name属性,用于指定数据库的表明
    @Id :映射主键(放在getter方法之前)
    @GeneratedValue :用于标注主键的生成策略,通过strategy属性指定
    @Column :映射数据表的列名,指定unique,length等

    @Entity和@Table的区别:
    @Entity说明这个class是实体类,并且使用默认的orm规则,即class名即数据库表中表名,class字段名即表中的字段名
    如果想改变这种默认的orm规则,就要使用@Table来改变class名与数据库中表名的映射规则,@Column来改变class中字段名与db中表的字段名的映射规则

    构建mapper

    
    public interface OTCDerValBalMapper  extends JpaRepository<User,Long>  {
    
       
    }
    
    

    此时就可以用JpaRepository中的方法了,主要的语法是 findXXBy、readAXXBy、queryXXBy、countXXBy、getXXBy 后面跟属性名称,
    利用这个功能仅需要在定义的 Repository 中添加对应的方法名即可,使用时 Spring Boot 会自动帮我们实现.

    关键字的使用和对应的SQL:

    Keyword Sample JPQL snippet
    And findByLastnameAndFirstname … where x.lastname = ?1 and x.firstname = ?2
    Or findByLastnameOrFirstname … where x.lastname = ?1 or x.firstname = ?2
    Is,Equals findByFirstnameIs,findByFirstnameEquals … where x.firstname = ?1
    Between findByStartDateBetween … where x.startDate between ?1 and ?2
    LessThan findByAgeLessThan … where x.age < ?1
    LessThanEqual findByAgeLessThanEqual … where x.age ⇐ ?1
    GreaterThan findByAgeGreaterThan … where x.age > ?1
    GreaterThanEqual findByAgeGreaterThanEqual … where x.age >= ?1
    After findByStartDateAfter … where x.startDate > ?1
    Before findByStartDateBefore … where x.startDate < ?1
    IsNull findByAgeIsNull … where x.age is null
    IsNotNull,NotNull findByAge(Is)NotNull … where x.age not null
    Like findByFirstnameLike … where x.firstname like ?1
    NotLike findByFirstnameNotLike … where x.firstname not like ?1
    StartingWith findByFirstnameStartingWith … where x.firstname like ?1 (parameter bound with appended %)
    EndingWith findByFirstnameEndingWith … where x.firstname like ?1 (parameter bound with prepended %)
    Containing findByFirstnameContaining … where x.firstname like ?1 (parameter bound wrapped in %)
    OrderBy findByAgeOrderByLastnameDesc … where x.age = ?1 order by x.lastname desc
    Not findByLastnameNot … where x.lastname <> ?1
    In findByAgeIn(Collection ages) … where x.age in ?1
    NotIn findByAgeNotIn(Collection age) … where x.age not in ?1
    TRUE findByActiveTrue() … where x.active = true
    FALSE findByActiveFalse() … where x.active = false
    IgnoreCase findByFirstnameIgnoreCase … where UPPER(x.firstame) = UPPER(?1)
  • 相关阅读:
    转:调试Release发布版程序的Crash错误
    [原创]桓泽学音频编解码(9):MP3 多相滤波器组算法分析
    [转] 一些你不知道但是超美的地方,一定要去
    [原创]桓泽学音频编解码(11):AC3 exponent(指数部分)模块解码算法分析
    [原创]桓泽学音频编解码(14):AC3 时频转换模块算法分析
    [原创]桓泽学音频编解码(15):AC3 最终章 多声道处理模块算法分析
    Android Supported Media Formats
    VoiceChatter 编译记录
    [原创]桓泽学音频编解码(4):MP3 和 AAC 中反量化原理,优化设计与参考代码中实现
    OPUS 视频PPT介绍
  • 原文地址:https://www.cnblogs.com/keyforknowledge/p/13569609.html
Copyright © 2011-2022 走看看