spring boot
一、增删改查
1、增 CaInfoRepository.save()(自带)
public interface CaInfoRepository extends JpaRepository{}
(1)当添加的数据id为空时添加一条数据到数据库
(2)当有id值时,修改数据库中的数据
2、改(删除和修改时需要加上注解 @Modifying ,与 @Transactional搭配使用)
@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);
3、查
自带
CaInfo getFirstByQihooShopId(String shop_id);
自定义1(原生 ca_info表名)
@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)
@Query("SELECT * from caInfo WHERE id=?1")ShopInfo getById(Long id);
4、补充
本来是想反回list<map>来着结果报错,用List<Object[]>来代替即可
@Query("select distinct projectId, projectName from ShopInfo")List
实体类例子 需要加上@Entity注解, @GeneratedValue主键自增,抽象类Serializable
@Entitypublic 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; } }