Long id = billingWaterDTO.getBrokerEnterprise().getId(); StringBuilder sqlBuilder = new StringBuilder(); sqlBuilder.append("insert into billingwater (balance,billDate,feeType,iOFlag,iOmoney,remake,brokerEnterpriseFeeId) "); sqlBuilder.append("VALUES ("); sqlBuilder.append("(Select t.balance from "); sqlBuilder.append("(select balance"); sqlBuilder.append("-"); sqlBuilder.append(billingWaterDTO.getiOmoney()); sqlBuilder.append(" as balance from billingwater where brokerEnterpriseFeeId="); sqlBuilder.append(id); sqlBuilder.append(" ORDER BY id desc limit 0,1) t), "); if (billingWaterDTO.getBillDate() == null) { sqlBuilder.append("NOW(),'"); } else { sqlBuilder.append("'"); sqlBuilder.append(DateFormatUtils.format(billingWaterDTO.getBillDate(), DateUtil.DATE_TIME)); sqlBuilder.append("','"); } sqlBuilder.append(billingWaterDTO.getFeeType()); sqlBuilder.append("','"); sqlBuilder.append(billingWaterDTO.getiOFlag()); sqlBuilder.append("','"); sqlBuilder.append(billingWaterDTO.getiOmoney()); sqlBuilder.append("','"); sqlBuilder.append(billingWaterDTO.getRemake()); sqlBuilder.append("','"); sqlBuilder.append(id); sqlBuilder.append("')"); 改成jpa
时间: 2024-03-05 13:52:32 浏览: 57
可以使用JPA的EntityManager来进行插入操作,首先需要在实体类上添加@Entity注解,并且在实体类中添加对应的属性和getter/setter方法。例如,假设实体类为BillingWater:
```
@Entity
@Table(name = "billingwater")
public class BillingWater {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private BigDecimal balance;
@Column(name = "billDate")
private Date billDate;
@Column(name = "feeType")
private String feeType;
@Column(name = "iOFlag")
private String iOFlag;
@Column(name = "iOmoney")
private BigDecimal iOmoney;
private String remake;
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "brokerEnterpriseFeeId")
private BrokerEnterprise brokerEnterprise;
// 省略getter/setter方法
}
```
然后可以在对应的Repository中定义插入方法,例如:
```
public interface BillingWaterRepository extends JpaRepository<BillingWater, Long> {
@Modifying
@Query(value = "insert into billingwater (balance,billDate,feeType,iOFlag,iOmoney,remake,brokerEnterpriseFeeId) " +
"VALUES (:balance, :billDate, :feeType, :iOFlag, :iOmoney, :remake, :brokerEnterpriseFeeId)", nativeQuery = true)
void insertBillingWater(@Param("balance") BigDecimal balance,
@Param("billDate") Date billDate,
@Param("feeType") String feeType,
@Param("iOFlag") String iOFlag,
@Param("iOmoney") BigDecimal iOmoney,
@Param("remake") String remake,
@Param("brokerEnterpriseFeeId") Long brokerEnterpriseFeeId);
}
```
然后在插入之前可以判断字段是否为空,例如:
```
if(StringUtils.isNotBlank(billingWaterDTO.getBrokerEnterprise().getId().toString())
&& StringUtils.isNotBlank(billingWaterDTO.getFeeType())
&& StringUtils.isNotBlank(billingWaterDTO.getiOFlag())
&& billingWaterDTO.getiOmoney() != null) {
// 执行插入操作
BillingWater billingWater = new BillingWater();
BigDecimal balance = entityManager.createNativeQuery("select balance - :iOmoney as balance from billingwater where brokerEnterpriseFeeId = :id order by id desc limit 0,1")
.setParameter("iOmoney", billingWaterDTO.getiOmoney())
.setParameter("id", billingWaterDTO.getBrokerEnterprise().getId())
.getSingleResult();
billingWater.setBalance(balance);
billingWater.setFeeType(billingWaterDTO.getFeeType());
billingWater.setiOFlag(billingWaterDTO.getiOFlag());
billingWater.setiOmoney(billingWaterDTO.getiOmoney());
billingWater.setRemake(billingWaterDTO.getRemake());
billingWater.setBrokerEnterprise(brokerEnterpriseRepository.findById(billingWaterDTO.getBrokerEnterprise().getId()).orElse(null));
if (billingWaterDTO.getBillDate() == null) {
billingWater.setBillDate(new Date());
} else {
billingWater.setBillDate(billingWaterDTO.getBillDate());
}
billingWaterRepository.save(billingWater);
}
```
其中,使用EntityManager的createNativeQuery()方法执行原生SQL查询,然后获取查询结果并设置到实体类中,最后使用JpaRepository的save()方法进行保存操作。
需要注意的是,在使用JpaRepository进行插入操作时,需要在对应的方法上添加@Modifying注解和@Query注解,并且将nativeQuery属性设置为true,表示使用原生SQL语句进行操作。同时,在设置参数时需要使用@Param注解来指定参数的名称。
阅读全文