美文网首页
Jpa的简单使用

Jpa的简单使用

作者: 自由主义者 | 来源:发表于2019-10-24 14:57 被阅读0次
实体写法(eg:batch类)
import lombok.Data;
import org.hibernate.annotations.GenericGenerator;
import javax.persistence.*;
import java.io.Serializable;
import java.math.BigDecimal;
import java.util.Date;

@Entity
@Data
@Table(name = "t_batch")
public class Batch implements Serializable {

    private static final long serialVersionUID = -3451188452990771293L;

    @Id
    @Column(length = 32, nullable = false)
    @GeneratedValue(generator = "uuid") //指定生成器名称
    @GenericGenerator(name = "uuid", strategy = "uuid") //生成器名称,uuid生成类
    protected String id;

    //批次编号
    private String number;

    //批次名称
    private String name;

    //发放金额
    private BigDecimal rewardMoney;

    //扣减金额
    private BigDecimal deductMoney;

    //任务数量
    private Integer taskNum;

    //创建时间
    private Date createTime;

}
Dao写法
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public interface BatchRepository extends JpaRepository<Batch, String>, JpaSpecificationExecutor<Batch> {

    @Modifying
    @Query("DELETE FROM Batch b where b.id in (?1) ")
    void deleteBatchById(List<String> idList);

    @Query(value = "select count(b.id) from t_batch b where b.name = ?1 or b.number = ?2", nativeQuery = true)
    Integer checkNameAndNum(String name, String num);
}

相关文章

网友评论

      本文标题:Jpa的简单使用

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