[Spring] common technical notes and summary

Configuration

 #配置 Jpa
 spring:
  datasource:
    type: com.alibaba.druid.pool.DruidDataSource
    driver-class-name: com.mysql.cj.jdbc.Driver
    url: jdbc:mysql://localhost:3306/test
    username: root
    password: root
  jpa:
    show-sql: true  #显示sql语句
    hibernate:
      ddl-auto: update # 更新 

annotation

Common Annotations

@Valid    //使用hibernate validation的时候使用

@Validated //用spring  Validator 校验机制使用

@Entity     //实体类

@Table(name=" 表名")  

@Id  // 主键

@Pattern(regexp = "([a-z0-9A-Z]+[-|\\.]?)+[a-z0-9A-Z]@([a-z0-9A-Z]+(-[a-z0-9A-Z]+)?\\.)+[a-zA-Z]{2,}", message = "邮箱错误")

@NotNull(message = "不能为空")

@NotBlank   //检查约束字符串是不是Null,最少一个非空白字符.

@Size(min=, max=) 验证对象(Array,Collection,Map,String)长度是否在给定的范围之内 


Primary key generation strategy notes

Primary key generation strategy
@ 32 generates a UUID uuid but not a standard bar is removed
@GenericGenerator (name = "idGenerator", Strategy = "uuid")
@GeneratedValue (= Generator "idGenerator")

//完整写法
	@GenericGenerator(name = "idGenerator", strategy = "org.hibernate.id.UUIDGenerator")
    @GeneratedValue(generator = "idGenerator")

Commonly used time annotation

The notes will be triggered when the insert

@CreatedDate Created
@CreatedBy Created
@LastModifiedDate
@LastModifiedBy

Instructions

  1. Add @EnableJpaAuditing on startup class
  2. Add @EntityListeners (AuditingEntityListener.class) based on the entity
  3. Add @CreatedDate on the field

Inquire

// 继承接口
public interface **Repository extends JpaRepository<PjPrj, String>, JpaSpecificationExecutor {
}

connection relation

One to One

// 一个人只养一只狗

// Person
 	@OneToOne(targetEntity = Dog.class, cascade = CascadeType.PERSIST)
 	
    @JoinColumn(name = "dogid", referencedColumnName = "id")
    private Dog dog;

//Dog

Here Insert Picture Description
dog person table will be the primary key as a foreign key id associating one association to complete

Many

Many to many

Conditions inquiry

  @Override
    public Page queryAll(PjPrj resources, Pageable pageable, String time) {

        Page all = pjPrjRepository.findAll(new Specification() {

            @Override
            public Predicate toPredicate(Root root, CriteriaQuery query, CriteriaBuilder criteriaBuilder) {
                List<Predicate> list = new ArrayList<Predicate>();
                //todo 条件查询

                if (!ObjectUtils.isEmpty(resources.getPjn())) {
                    list.add(criteriaBuilder.equal(root.get("pjn"), resources.getPjn()));
                }

                if (!ObjectUtils.isEmpty(time)) {
                    String[] split = time.split(",");

                    SimpleDateFormat sdfmat = new SimpleDateFormat("yyyy-MM-dd");
                    try {

                        list.add(criteriaBuilder.between(root.get("createtime"),
                                sdfmat.parse(split[0]),
                                sdfmat.parse(split[1])));
                    } catch (ParseException e) {
                        e.printStackTrace();
                    }
                }

                Predicate[] p = new Predicate[list.size()];
                return criteriaBuilder.and(list.toArray(p));
            }
        }, pageable);

        return all;
    }

Guess you like

Origin blog.csdn.net/java_sparrow/article/details/88204829