Jpa update的参数是一个对象的做法

第一种方法:

先查询到要update的对象.然后对user对象中你需要的更新的数据重新进行set后,再去执行save(user)的方法

 CustomerAccount a =customerAccountDao.getOne(4435855);//可以根据自己的的方法得到对象,对象中必须要有@ID的主键的属性,是有值的
        a.setCustomerId("4435855");
        a.setBActive("1");
        a.setCustomerId("LIANDONG_SHA");
        a.setCurrType("RMB");
        a.setBankName("asdwssdwd");
        a.setAccountName("1");
        a.setAccountNumber("1");
        a.setCreateBy("SA");
        a.setCreateDate(new Date());
        a.setCityName("上海");
        a.setProvinceName("上海市");
        a.setCountryCode("黄浦区");
        customerAccountDao.save(a);

第二种:

自己手写sql。

这是springboot是2.1.5版本的写法

    @Modifying
    @Transactional
    @Query(nativeQuery = true,value = "update C_CUSTOMER_ACCOUNT set BANK_NAME=:#{#customerAccount.bankName},ACCOUNT_NAME=:#{#customerAccount.accountName}," +
            "ACCOUNT_NUMBER=:#{#customerAccount.accountNumber},COUNTRY_CODE=:#{#customerAccount.countryCode},PROVINCE_NAME=:#{#customerAccount.provinceName},CITY_NAME=:#{#customerAccount.cityName} where CUSTOMER_ID=:#{#customerAccount.customerId}")
    void updateCustomer(@Param("customerAccount") CustomerAccount customerAccount);

其余版本的大家自己可以试一下代码如下

 @Modifying
    @Transactional
    @Query(nativeQuery = true,value = "update C_CUSTOMER_ACCOUNT set BANK_NAME=#{#customerAccount.bankName},ACCOUNT_NAME=#{#customerAccount.accountName}," +
            "ACCOUNT_NUMBER=#{#customerAccount.accountNumber},COUNTRY_CODE=#{#customerAccount.countryCode},PROVINCE_NAME=#{#customerAccount.provinceName},CITY_NAME=#{#customerAccount.cityName} where CUSTOMER_ID=#{#customerAccount.customerId}")
    void updateCustomer( CustomerAccount customerAccount);
 @Modifying
    @Transactional
    @Query(nativeQuery = true,value = "update C_CUSTOMER_ACCOUNT set BANK_NAME=#{#customerAccount.bankName},ACCOUNT_NAME=#{#customerAccount.accountName}," +
            "ACCOUNT_NUMBER=#{#customerAccount.accountNumber},COUNTRY_CODE=#{#customerAccount.countryCode},PROVINCE_NAME=#{#customerAccount.provinceName},CITY_NAME=#{#customerAccount.cityName} where CUSTOMER_ID=#{#customerAccount.customerId}")
    void updateCustomer(@Param("customerAccount") CustomerAccount customerAccount);

 

你可能感兴趣的:(java,SPRING)