码迷,mamicode.com
首页 > 其他好文 > 详细

jpa @Query()参数设置,?、?X、实体类对象参数设置

时间:2019-11-04 20:11:13      阅读:880      评论:0      收藏:0      [点我收藏+]

标签:div   参数设置   rgs   arch   gen   lis   extend   table   modifying   

一、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;
}

 

jpa @Query()参数设置,?、?X、实体类对象参数设置

标签:div   参数设置   rgs   arch   gen   lis   extend   table   modifying   

原文地址:https://www.cnblogs.com/foolash/p/11794063.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!