subEntityList.stream().forEach(subEntity -> { ClmPlanBean clmPlanBean = new ClmPlanBean(); clmPlanBean.setId(entity.getId() + "#" + subEntity.getId()); // 合同id+计划id clmPlanBean.setPaymentType(subEntity.getPaymentType()); clmPlanBean.setPaymentCondition("");// 结算条件-新增字段 clmPlanBean.setPlansDate(sdf.format(subEntity.getPlansDate())); clmPlanBean.setVendorName("");// 结算单位-从已选择相对方中选择-暂时处理不了 clmPlanBean.setAmount(subEntity.getAmount().toString()); clmPlanBean.setBeginCavAmount("");//期初已结算金额-新增字段 clmPlanBean.setControlType("003");//控制方式 clmPlanBean.setPaymentMethod("");//支付方式-新增字段 clmPlanBeanList.add(clmPlanBean); }
时间: 2024-02-14 09:35:42 浏览: 45
这段代码是使用Java 8的Stream API对subEntityList进行遍历,并对每个subEntity对象进行处理,创建一个新的ClmPlanBean对象并将其添加到clmPlanBeanList中。具体来说,这个ClmPlanBean对象包含以下属性:
- id: 合同id+计划id
- paymentType: subEntity的paymentType属性
- paymentCondition: 空字符串
- plansDate: subEntity的plansDate属性,格式化为字符串
- vendorName: 空字符串
- amount: subEntity的amount属性,格式化为字符串
- beginCavAmount: 空字符串
- controlType: 固定值"003"
- paymentMethod: 空字符串
值得注意的是,这段代码中的sdf是一个SimpleDateFormat对象,用于将日期格式化为字符串。另外,这段代码中的注释提到了一些新增字段,但是没有给出具体的解释。
相关问题
使用Java编程和springboot框架,连接mysql数据库(所需的三个表分别为奖品表名为a,活动表名为b,两者关联的表为c(中奖概率字段位于c表))实现下面两个功能 :1.创建活动并关联奖品,一个活动必须关联8个奖品,总概率为100% 2.修改活动 活动名称不能重复 并更新关联奖品。给出具体程序,包括给出相应的controller层,service层,service实现类,dao层及mapper.xml文件
好的,以下是Java编写的代码示例,包括controller、service、dao和mapper.xml文件。
Controller层:
```java
@RestController
@RequestMapping("/activity")
public class ActivityController {
@Autowired
private ActivityService activityService;
@PostMapping("/create")
public ResponseEntity createActivity(@RequestBody ActivityDto activityDto) {
activityService.createActivity(activityDto);
return ResponseEntity.ok("Create activity success");
}
@PutMapping("/update")
public ResponseEntity updateActivity(@RequestBody ActivityDto activityDto) {
activityService.updateActivity(activityDto);
return ResponseEntity.ok("Update activity success");
}
}
```
Service层:
```java
public interface ActivityService {
void createActivity(ActivityDto activityDto);
void updateActivity(ActivityDto activityDto);
}
```
Service实现类:
```java
@Service
public class ActivityServiceImpl implements ActivityService {
@Autowired
private ActivityMapper activityMapper;
@Autowired
private PrizeMapper prizeMapper;
@Autowired
private ActivityPrizeMapper activityPrizeMapper;
@Override
public void createActivity(ActivityDto activityDto) {
// 1. 创建活动
Activity activity = new Activity();
activity.setName(activityDto.getName());
activityMapper.insert(activity);
// 2. 关联奖品
List<PrizeDto> prizeDtos = activityDto.getPrizes();
List<ActivityPrize> activityPrizes = new ArrayList<>();
double totalProbability = 0;
for (PrizeDto prizeDto : prizeDtos) {
ActivityPrize activityPrize = new ActivityPrize();
activityPrize.setActivityId(activity.getId());
activityPrize.setPrizeId(prizeDto.getId());
activityPrize.setProbability(prizeDto.getProbability());
activityPrizes.add(activityPrize);
totalProbability += prizeDto.getProbability();
}
// 3. 校验概率是否为100%
if (totalProbability != 100) {
throw new RuntimeException("Total probability must be 100%");
}
// 4. 保存关联关系
activityPrizeMapper.batchInsert(activityPrizes);
}
@Override
public void updateActivity(ActivityDto activityDto) {
// 1. 查询活动
Activity activity = activityMapper.selectByPrimaryKey(activityDto.getId());
if (activity == null) {
throw new RuntimeException("Activity not exists");
}
// 2. 更新活动名称
if (!activity.getName().equals(activityDto.getName())) {
Activity activityByName = activityMapper.selectByName(activityDto.getName());
if (activityByName != null) {
throw new RuntimeException("Activity name already exists");
}
activity.setName(activityDto.getName());
activityMapper.updateByPrimaryKey(activity);
}
// 3. 更新关联奖品
List<PrizeDto> prizeDtos = activityDto.getPrizes();
List<ActivityPrize> activityPrizes = new ArrayList<>();
double totalProbability = 0;
for (PrizeDto prizeDto : prizeDtos) {
ActivityPrize activityPrize = new ActivityPrize();
activityPrize.setActivityId(activity.getId());
activityPrize.setPrizeId(prizeDto.getId());
activityPrize.setProbability(prizeDto.getProbability());
activityPrizes.add(activityPrize);
totalProbability += prizeDto.getProbability();
}
// 4. 校验概率是否为100%
if (totalProbability != 100) {
throw new RuntimeException("Total probability must be 100%");
}
// 5. 删除旧的关联关系,保存新的关联关系
activityPrizeMapper.deleteByActivityId(activity.getId());
activityPrizeMapper.batchInsert(activityPrizes);
}
}
```
DAO层:
```java
@Repository
public interface ActivityMapper extends BaseMapper<Activity> {
Activity selectByName(String name);
}
```
```java
@Repository
public interface PrizeMapper extends BaseMapper<Prize> {
}
```
```java
@Repository
public interface ActivityPrizeMapper extends BaseMapper<ActivityPrize> {
void deleteByActivityId(Integer activityId);
void batchInsert(List<ActivityPrize> activityPrizes);
}
```
Mapper.xml文件:
```xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.example.mapper.ActivityMapper">
<resultMap id="BaseResultMap" type="com.example.entity.Activity">
<id column="id" jdbcType="INTEGER" property="id" />
<result column="name" jdbcType="VARCHAR" property="name" />
</resultMap>
<select id="selectByPrimaryKey" resultMap="BaseResultMap" parameterType="java.lang.Integer">
select id, name
from activity
where id = #{id,jdbcType=INTEGER}
</select>
<select id="selectByName" resultMap="BaseResultMap" parameterType="java.lang.String">
select id, name
from activity
where name = #{name,jdbcType=VARCHAR}
</select>
<insert id="insert" parameterType="com.example.entity.Activity">
insert into activity (name)
values (#{name,jdbcType=VARCHAR})
</insert>
<update id="updateByPrimaryKey" parameterType="com.example.entity.Activity">
update activity
set name = #{name,jdbcType=VARCHAR}
where id = #{id,jdbcType=INTEGER}
</update>
</mapper>
```
```xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.example.mapper.PrizeMapper">
<resultMap id="BaseResultMap" type="com.example.entity.Prize">
<id column="id" jdbcType="INTEGER" property="id" />
<result column="name" jdbcType="VARCHAR" property="name" />
</resultMap>
<select id="selectByPrimaryKey" resultMap="BaseResultMap" parameterType="java.lang.Integer">
select id, name
from prize
where id = #{id,jdbcType=INTEGER}
</select>
</mapper>
```
```xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.example.mapper.ActivityPrizeMapper">
<resultMap id="BaseResultMap" type="com.example.entity.ActivityPrize">
<result column="activity_id" jdbcType="INTEGER" property="activityId" />
<result column="prize_id" jdbcType="INTEGER" property="prizeId" />
<result column="probability" jdbcType="DOUBLE" property="probability" />
</resultMap>
<delete id="deleteByActivityId" parameterType="java.lang.Integer">
delete from activity_prize
where activity_id = #{activityId,jdbcType=INTEGER}
</delete>
<insert id="insert" parameterType="com.example.entity.ActivityPrize">
insert into activity_prize (activity_id, prize_id, probability)
values (#{activityId,jdbcType=INTEGER}, #{prizeId,jdbcType=INTEGER}, #{probability,jdbcType=DOUBLE})
</insert>
<insert id="batchInsert">
<foreach collection="list" item="item" index="index" separator=";">
insert into activity_prize (activity_id, prize_id, probability)
values (#{item.activityId,jdbcType=INTEGER}, #{item.prizeId,jdbcType=INTEGER}, #{item.probability,jdbcType=DOUBLE})
</foreach>
</insert>
</mapper>
```
以上是Java编写的实现,使用了SpringBoot和MyBatis框架,其中controller层提供了两个接口,分别用于创建和更新活动,service层提供了对应的接口实现,dao层提供了数据库操作的接口,mapper.xml文件提供了对应的SQL语句。
定时任务实时抓取数据将查询的数据实时更新进表里,对于表里有的数据现在查询的数据还有那就更新,如果之前没有现在有数据那就插入,如果之前有现在没有那就删除,使用java语言使用tk-mybatis使用postgresql
你可以使用tk-mybatis的Mapper接口和注解来实现这个功能,具体步骤如下:
1. 定义一个实体类,用于存储要更新的数据。示例代码如下:
```
public class DataEntity {
private Long id;
private String col1;
private String col2;
// 省略getter和setter方法
}
```
2. 在Mapper接口中定义三个方法,分别用于插入、更新和删除数据。示例代码如下:
```
public interface DataMapper extends Mapper<DataEntity> {
@InsertProvider(type = DataSqlProvider.class, method = "insertBatch")
void insertBatch(List<DataEntity> dataList);
@UpdateProvider(type = DataSqlProvider.class, method = "updateBatch")
void updateBatch(List<DataEntity> dataList);
@DeleteProvider(type = DataSqlProvider.class, method = "deleteBatch")
void deleteBatch(List<Long> idList);
}
```
3. 在DataSqlProvider类中编写动态SQL语句,根据数据是否存在进行相应的操作。示例代码如下:
```
public class DataSqlProvider {
public String insertBatch(Map<String, Object> paramMap) {
List<DataEntity> dataList = (List<DataEntity>) paramMap.get("list");
StringBuilder sqlBuilder = new StringBuilder();
sqlBuilder.append("INSERT INTO table_name (id, col1, col2) VALUES ");
for (DataEntity data : dataList) {
sqlBuilder.append("(")
.append(data.getId()).append(", ")
.append("'").append(data.getCol1()).append("', ")
.append("'").append(data.getCol2()).append("'),");
}
sqlBuilder.deleteCharAt(sqlBuilder.length() - 1);
return sqlBuilder.toString();
}
public String updateBatch(Map<String, Object> paramMap) {
List<DataEntity> dataList = (List<DataEntity>) paramMap.get("list");
StringBuilder sqlBuilder = new StringBuilder();
sqlBuilder.append("<foreach collection=\"list\" item=\"data\" separator=\";\">")
.append("UPDATE table_name SET ")
.append("col1 = #{data.col1}, ")
.append("col2 = #{data.col2} ")
.append("WHERE id = #{data.id}")
.append("</foreach>");
return sqlBuilder.toString();
}
public String deleteBatch(Map<String, Object> paramMap) {
List<Long> idList = (List<Long>) paramMap.get("list");
StringBuilder sqlBuilder = new StringBuilder();
sqlBuilder.append("DELETE FROM table_name WHERE id NOT IN (")
.append("<foreach collection=\"list\" item=\"id\" separator=\",\">")
.append("#{id}")
.append("</foreach>")
.append(")");
return sqlBuilder.toString();
}
}
```
4. 在Java代码中调用Mapper接口的方法,将List传入Mapper中执行即可。示例代码如下:
```
@Autowired
private DataMapper dataMapper;
public void updateData(List<DataEntity> dataList) {
List<Long> idList = dataList.stream().map(DataEntity::getId).collect(Collectors.toList());
List<DataEntity> existingDataList = dataMapper.selectByIdList(idList);
List<DataEntity> newDataList = new ArrayList<>();
for (DataEntity data : dataList) {
boolean exists = false;
for (DataEntity existingData : existingDataList) {
if (existingData.getId().equals(data.getId())) {
exists = true;
if (!existingData.equals(data)) {
dataMapper.updateByPrimaryKeySelective(data);
}
break;
}
}
if (!exists) {
newDataList.add(data);
}
}
dataMapper.insertBatch(newDataList);
dataMapper.deleteBatch(idList);
}
```
注意事项:
- 实体类、Mapper接口和SQL语句需要根据实际情况进行修改,如有多个字段需要更新或插入,需要在实体类、Mapper接口和SQL语句中添加相应的字段。
- 插入操作使用的是批量插入,可以提高插入效率。
- 更新操作使用的是selectByIdList方法查询表中已有的数据,如果数据量很大可能会影响查询效率。
- 删除操作使用的是NOT IN语句,将id不在dataList中的数据删除。
阅读全文