jpa-api

作者: 泠泉 | 来源:发表于2020-01-08 11:17 被阅读0次

用法

  • nativeQuery
// [{nickName,age}, ..]
@Query(value = "SELECT u.nick_name nickName, u.age FROM user u where jh.recommend_id=?1", nativeQuery = true)
List<Map<String,Object>> userByPid(String pid);

  • select new com.xxx.DemoVO(...)
public interface MusicTypeRepository extends JpaRepository<MusicType,Integer> {
     @Query("select new cn.srblog.springbootcurd.vo.StudentTypeInfoVo(count(s.id),m.name) " +
             "FROM MusicType m  left JOIN Student s on s.musicTypeId = m.id group by m.id ")
     List<StudentTypeInfoVo> getTypeInfo();
}
  • @OneToOne to join
@OneToOne
@JoinColumn(name="addressId",referencedColumnName="id")
private Address address;

基础搭建

  • jpa conifg
@Configuration
@EnableJpaAuditing
@EnableTransactionManagement
@EnableSpringDataWebSupport
public class JPAConfig{
  • annotation on Entity
@Entity
@Data
@EqualsAndHashCode(callSuper = false)
@SQLDelete(sql = "UPDATE demo SET is_active = 0 where id = ?")
@Where(clause="is_active=1")
public class Demo extends BaseEntity{
  • annotation on BaseEntity
@Getter
@Setter
@MappedSuperclass
@EntityListeners(AuditingEntityListener.class)
public abstract class BaseEntity {

    @Id
    @GeneratedValue(generator = "system-uuid")
    @GenericGenerator(name="system-uuid",strategy = "uuid")
    @Column(name = "id", length = 32)
    private String id;

    @CreatedDate
    @JsonFormat(pattern = DateUtils.FORMAT_DATE_TIME)
    private Date createdDate;

    @LastModifiedDate
    @JsonFormat(pattern = DateUtils.FORMAT_DATE_TIME)
    private Date modifiedDate;

    @Column(name = "is_active")
    @JsonIgnore
    private boolean isActive = true;
}

相关文章

  • jpa-api

    JPA官方文档 用法 nativeQuery select new com.xxx.DemoVO(...) @On...

网友评论

      本文标题:jpa-api

      本文链接:https://www.haomeiwen.com/subject/gwnjactx.html