jpa @Query()参数设置,:冒号方式、?NO.问号方式、实体类对象参数设置
一、service层事务(update/delete)
@Transactional(rollbackFor = Exception.class)
二、@Query()参数设置
?x 和:XX不能混用
package cc.ash.test; 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.data.repository.query.Param; public interface StuDao extends JpaRepository<Stu, Long>, JpaSpecificationExecutor<Stu> { //主键类型 @Modifying @Query(value = "update Stu wxpay set stu.name = :name, stu.alias = :alias, " + "stu.age = :age where stu.id = :id") void updatePayState(@Param("name") String stuName, @Param("alias") String stuAlias, @Param("age") int stuAge, @Param("id") String stuId); @Modifying @Query(value = "update Stu wxpay set stu.name = ?1, stu.alias = ?3, " + "stu.age = ?2 where stu.id = ?4") void updatePayState(String stuName, int stuAge, String stuAlias, String stuId); @Modifying @Query(value = "update Stu s set s.name = :#{#stu.name}, s.age = :#{#stu.age}, " + "s.alias = :#{#stu.alias} where s.id = :#{#stu.id}") int updatePayState2(@Param("stu") Stu stu); }
2.实体类
package cc.ash.test; import lombok.AllArgsConstructor; import lombok.Data; import lombok.NoArgsConstructor; import javax.persistence.*; @Data @Entity @NoArgsConstructor @AllArgsConstructor @Table(name = "tbl_stu", indexes = {@Index(name = "idx_索引名字", columnList = "建立索引的属性")}) public class Stu { @TableGenerator( name = "StuGenerator", table = "tbl_stu", pkColumnName = "app_seq_name", pkColumnValue = "tbl_stu_seq", valueColumnName = "app_seq_value", initialValue = 1000, allocationSize = 1 ) @Id @GeneratedValue(strategy = GenerationType.TABLE, generator = "WxpayFundFlowGenerator") private Long id; @Column(columnDefinition = "varchar(10) COMMENT '名字'") private String name; //省略 private int age; private String alias; }