spring boot 增刪改查

spring boot spring

1、增刪改查sql

一、增 CaInfoRepository.save()(自帶)數據庫

public interface CaInfoRepository extends JpaRepository<CaInfo, Long>{}

(1)當添加的數據id爲空時添加一條數據到數據庫json

(2)當有id值時,修改數據庫中的數據ide

 

二、改(刪除和修改時須要加上註解 @Modifying   ,與 @Transactional搭配使用)ui

@Modifying
@Query("UPDATE CaInfoSET detail_json=?1,dmp58building_id=?2,state=?3  where id=?4")
void updateXXXX(String detail_json, String dmp58building_id, Integer state,Long id);

三、查this

自帶.net

CaInfo getFirstByQihooShopId(String shop_id);

自定義1(原生 ca_info表名) ci

@Query(value = "SELECT *  from ca_info  WHERE id=(SELECT MIN(id) from ca_info WHERE state =?1 )",nativeQuery = true)
ShopInfo getFirstByState(Integer state);

自定義2(表對應的實體 caInfo)get

@Query("SELECT * from caInfo WHERE id=?1")
ShopInfo getById(Long id);

帶in的查詢 需綁定參數 

@Query(value = "SELECT m.name trademark,t.name from car_info t LEFT JOIN car_info m on m.id=t.parent_id WHERE t.id in (:ids)",nativeQuery = true)
List<Object[]> findTrademarkAndCarNameByIds(@Param("ids")List<String> ids);

四、補充1

原本是想反回list<map>來着結果報錯,用List<Object[]>來代替便可

@Query("select distinct projectId, projectName from ShopInfo")
List<Object[]> getProjectNameAndIdList();

實體類例子 須要加上@Entity註解, @Id @GeneratedValue主鍵自增,抽象類Serializable

@Entity
public class Info  implements Serializable {
    @Id
    @GeneratedValue
    private Long id;
    @Column
    private String projectName;
    @Column
    private String shopName;
    @Column
    private String position;
    @Column
    private Integer state;

    public  Info (){

    }

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getProjectName() {
        return projectName;
    }

    public void setProjectName(String projectName) {
        this.projectName = projectName;
    }

    public String getShopName() {
        return shopName;
    }

    public void setShopName(String shopName) {
        this.shopName = shopName;
    }

    public String getPosition() {
        return position;
    }

    public void setPosition(String position) {
        this.position = position;
    }

   
    public Integer getState() {
        return state;
    }

    public void setState(Integer state) {
        this.state = state;
    }

   

}

補充2

/**
     * 動態sql,分頁
     */
    private Page<CarInfo> queryCarInfo(Integer brandId,Integer trademark, Integer trademark2,Integer page,Integer pageSize) {
        Specification<CarInfo> specification = new Specification<CarInfo>() {
            @Override
            public Predicate toPredicate(Root<CarInfo> root, CriteriaQuery<?> criteriaQuery, CriteriaBuilder criteriaBuilder) {
                List<Predicate> predicates = new ArrayList<>();
                Path<Integer> id = root.get("id");
                Path<Integer> depth = root.get("depth");          
                Path<Integer> parentId = root.get("parentId");
                predicates.add(criteriaBuilder.equal(depth, 3));
                if (!StringUtils.isEmpty(brandId)) {
                    predicates.add(criteriaBuilder.equal(id, brandId));
                }            
                if (!StringUtils.isEmpty(trademark2)) {
                    predicates.add(criteriaBuilder.equal(parentId, trademark2));
                }
                // in使用
                if (!StringUtils.isEmpty(trademark) && StringUtils.isEmpty(trademark2)) {
                    List<Long> trademark2Ids=carInfoRepository.findBrandIdsByParentId(trademark);
                    CriteriaBuilder.In<Integer> in = criteriaBuilder.in(parentId.as(Integer.class));
                    for (Long pp : trademark2Ids) {
                        in.value(pp.intValue());
                    }
                    predicates.add(in);
                }
                Predicate and = criteriaBuilder.and(predicates.toArray(new Predicate[predicates.size()]));
                return and;
            }
        };

        Pageable pageable = new PageRequest(page-1, pageSize);
        Page<CarInfo> all = carInfoRepository.findAll(specification, pageable);
        return all;
    }
相關文章
相關標籤/搜索