酷代码 AI
菜单
全部AI  /  热门  /  AI写代码神器
服务商

insert into pacsdb.diagnose_report_del select *,now() from pacsdb..diagnose_report t where t.report_no = #{reportno} 把这条sql改写成mybatis-plus代码

在 MyBatis-Plus 中,你可以使用 `@Insert` 注解来实现插入操作。对于你的需求,可以将 SQL 语句转换为 MyBatis-Plus 的 XML 映射文件或注解方式。以下是两种方法的示例: 方法一:使用 XML 映射文件 1. 创建 Mapper 接口: ```java import com.baomidou.mybatisplus.core.mapper.BaseMapper; import org.apache.ibatis.annotations.InsertProvider; import org.apache.ibatis.annotations.Param; public interface DiagnoseReportDelMapper extends BaseMapper<DiagnoseReportDel> { @InsertProvider(type = DiagnoseReportDelSqlProvider.class, method = "insertFromDiagnoseReport") void insertFromDiagnoseReport(@Param("reportNo") String reportNo); } ``` 2. 创建 SQL 提供器类: ```java import org.apache.ibatis.jdbc.SQL; import org.apache.ibatis.mapping.MappedStatement; public class DiagnoseReportDelSqlProvider { public String insertFromDiagnoseReport(MappedStatement ms, Object parameter) { return new SQL() {{ INSERT_INTO("pacsdb.diagnose_report_del"); SELECT("*", "now()"); FROM("pacsdb.diagnose_report t"); WHERE("t.report_no = #{reportNo}"); }}.toString(); } } ``` 方法二:使用注解方式 1. 创建 Mapper 接口: ```java import com.baomidou.mybatisplus.core.mapper.BaseMapper; import org.apache.ibatis.annotations.Insert; import org.apache.ibatis.annotations.Param; public interface DiagnoseReportDelMapper extends BaseMapper<DiagnoseReportDel> { @Insert("<script>" + "INSERT INTO pacsdb.diagnose_report_del " + "SELECT *, now() FROM pacsdb.diagnose_report t " + "WHERE t.report_no = #{reportNo} " + "</script>") void insertFromDiagnoseReport(@Param("reportNo") String reportNo); } ``` 方法三:使用 MyBatis-Plus 的 LambdaQueryWrapper 和 BaseMapper 如果你希望使用 MyBatis-Plus 的链式调用方式,可以先查询数据再插入,但这种方式不如直接 SQL 高效: 1. 创建 Mapper 接口: ```java import com.baomidou.mybatisplus.core.mapper.BaseMapper; public interface DiagnoseReportMapper extends BaseMapper<DiagnoseReport> { } public interface DiagnoseReportDelMapper extends BaseMapper<DiagnoseReportDel> { } ``` 2. 创建 Service 类: ```java import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import java.util.List; @Service public class DiagnoseReportDelService extends ServiceImpl<DiagnoseReportDelMapper, DiagnoseReportDel> { @Autowired private DiagnoseReportMapper diagnoseReportMapper; @Autowired private DiagnoseReportDelMapper diagnoseReportDelMapper; public void insertFromDiagnoseReport(String reportNo) { LambdaQueryWrapper<DiagnoseReport> queryWrapper = new LambdaQueryWrapper<>(); queryWrapper.eq(DiagnoseReport::getReportNo, reportNo); List<DiagnoseReport> diagnoseReports = diagnoseReportMapper.selectList(queryWrapper); for (DiagnoseReport diagnoseReport : diagnoseReports) { DiagnoseReportDel diagnoseReportDel = new DiagnoseReportDel(); // 将 diagnoseReport 的属性复制到 diagnoseReportDel BeanUtils.copyProperties(diagnoseReport, diagnoseReportDel); diagnoseReportDel.setCreateTime(new Date()); // 假设有一个 setCreateTime 方法 diagnoseReportDelMapper.insert(diagnoseReportDel); } } } ``` 以上三种方法都可以实现你的需求,选择适合你项目的方式即可。 [2024-10-17 14:05:35 | AI写代码神器 | 868点数解答]

相关提问