Java spring mybatis MySQL 编写短信模板的增删改查,使用SMStemplateDTO作为实体类名称,数据库字段有seq_no,template_id,push_plan_tp_cd,template_name,template_content,creator_no,create_time,modifier_no,mod_time,opror_instno,需要使用mapper.xml,mapper,controller,servive,serviceimpl,dto,controller需要使用public restresponse并抛出异常生成错误代码,并且把controller拆分出api,错误日志使用log.true,并在impl中判断pushplantpcd是否为空,不为空则无法删除和修改

时间: 2024-02-17 15:00:47 浏览: 19
好的,让我来回答您的问题。 对于Java Spring + MyBatis + MySQL编写短信模板的增删改查,您可以按照以下步骤进行: 1. 创建SMSTemplateDTO实体类,包含seq_no,template_id,push_plan_tp_cd,template_name,template_content,creator_no,create_time,modifier_no,mod_time,opror_instno等字段。 2. 创建mapper.xml文件,编写相应的SQL语句,例如: ``` <select id="getSMSTemplateById" parameterType="int" resultType="com.example.dto.SMSTemplateDTO"> SELECT * FROM sms_template WHERE seq_no = #{id} </select> <insert id="insertSMSTemplate" parameterType="com.example.dto.SMSTemplateDTO"> INSERT INTO sms_template (template_id, push_plan_tp_cd, template_name, template_content, creator_no, create_time, modifier_no, mod_time, opror_instno) VALUES (#{templateId}, #{pushPlanTpCd}, #{templateName}, #{templateContent}, #{creatorNo}, #{createTime}, #{modifierNo}, #{modTime}, #{oprorInstno}) </insert> <update id="updateSMSTemplate" parameterType="com.example.dto.SMSTemplateDTO"> UPDATE sms_template SET push_plan_tp_cd = #{pushPlanTpCd}, template_name = #{templateName}, template_content = #{templateContent}, modifier_no = #{modifierNo}, mod_time = #{modTime}, opror_instno = #{oprorInstno} WHERE seq_no = #{seqNo} AND push_plan_tp_cd IS NULL </update> <delete id="deleteSMSTemplate" parameterType="int"> DELETE FROM sms_template WHERE seq_no = #{id} AND push_plan_tp_cd IS NULL </delete> ``` 3. 创建对应的mapper接口,例如: ``` public interface SMSTemplateMapper { SMSTemplateDTO getSMSTemplateById(int id); void insertSMSTemplate(SMSTemplateDTO dto); void updateSMSTemplate(SMSTemplateDTO dto); void deleteSMSTemplate(int id); } ``` 4. 创建SMSTemplateService接口和实现类SMSTemplateServiceImpl,实现增删改查的业务逻辑。 5. 创建SMSTemplateController,包含对应的增删改查API,例如: ``` @RestController @RequestMapping("/api/sms/template") public class SMSTemplateController { @Autowired private SMSTemplateService smsTemplateService; @GetMapping("/{id}") public RestResponse getSMSTemplate(@PathVariable int id) { SMSTemplateDTO dto = smsTemplateService.getSMSTemplateById(id); if (dto == null) { throw new NotFoundException("SMS Template not found"); } return RestResponse.success(dto); } @PostMapping("/") public RestResponse insertSMSTemplate(@RequestBody SMSTemplateDTO dto) { smsTemplateService.insertSMSTemplate(dto); return RestResponse.success(); } @PutMapping("/{id}") public RestResponse updateSMSTemplate(@PathVariable int id, @RequestBody SMSTemplateDTO dto) { dto.setSeqNo(id); smsTemplateService.updateSMSTemplate(dto); return RestResponse.success(); } @DeleteMapping("/{id}") public RestResponse deleteSMSTemplate(@PathVariable int id) { smsTemplateService.deleteSMSTemplate(id); return RestResponse.success(); } @ExceptionHandler(Exception.class) public RestResponse handleException(Exception e) { log.error("Error occurred: {}", e.getMessage()); return RestResponse.error("Internal Server Error"); } } ``` 6. 在SMSTemplateServiceImpl中对push_plan_tp_cd进行判断,例如: ``` public void updateSMSTemplate(SMSTemplateDTO dto) { if (dto.getPushPlanTpCd() != null) { throw new BadRequestException("Cannot update SMS Template with push plan"); } smsTemplateMapper.updateSMSTemplate(dto); } public void deleteSMSTemplate(int id) { SMSTemplateDTO dto = smsTemplateMapper.getSMSTemplateById(id); if (dto == null) { throw new NotFoundException("SMS Template not found"); } if (dto.getPushPlanTpCd() != null) { throw new BadRequestException("Cannot delete SMS Template with push plan"); } smsTemplateMapper.deleteSMSTemplate(id); } ``` 7. 在SMSTemplateController中使用RestResponse和异常处理生成错误代码,并拆分出API。 以上就是Java Spring + MyBatis + MySQL编写短信模板的增删改查的步骤,希望对您有帮助。

相关推荐

最新推荐

recommend-type

Spring boot+mybatis+thymeleaf 实现登录注册增删改查功能的示例代码

主要介绍了Spring boot+mybatis+thymeleaf 实现登录注册增删改查功能的示例代码,本文通过实例图文相结合给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
recommend-type

解决mybatis使用char类型字段查询oracle数据库时结果返回null问题

主要介绍了mybatis使用char类型字段查询oracle数据库时结果返回null问题的解决方法,本文给大家介绍的非常详细,具有一定的参考借鉴价值,需要的朋友可以参考下
recommend-type

spring boot配置MySQL数据库连接、Hikari连接池和Mybatis的简单配置方法

主要介绍了spring boot配置MySQL数据库连接、Hikari连接池和Mybatis的简单配置方法,需要的朋友可以参考下
recommend-type

MyBatis实现Mysql数据库分库分表操作和总结(推荐)

主要介绍了MyBatis实现Mysql数据库分库分表操作和总结,需要的朋友可以参考下
recommend-type

Java的MyBatis框架中实现多表连接查询和查询结果分页

主要介绍了Java的MyBatis框架中实现多表连接查询和查询结果分页,借助MyBatis框架中带有的动态SQL查询功能可以比普通SQL查询做到更多,需要的朋友可以参考下
recommend-type

zigbee-cluster-library-specification

最新的zigbee-cluster-library-specification说明文档。
recommend-type

管理建模和仿真的文件

管理Boualem Benatallah引用此版本:布阿利姆·贝纳塔拉。管理建模和仿真。约瑟夫-傅立叶大学-格勒诺布尔第一大学,1996年。法语。NNT:电话:00345357HAL ID:电话:00345357https://theses.hal.science/tel-003453572008年12月9日提交HAL是一个多学科的开放存取档案馆,用于存放和传播科学研究论文,无论它们是否被公开。论文可以来自法国或国外的教学和研究机构,也可以来自公共或私人研究中心。L’archive ouverte pluridisciplinaire
recommend-type

【实战演练】MATLAB用遗传算法改进粒子群GA-PSO算法

![MATLAB智能算法合集](https://static.fuxi.netease.com/fuxi-official/web/20221101/83f465753fd49c41536a5640367d4340.jpg) # 2.1 遗传算法的原理和实现 遗传算法(GA)是一种受生物进化过程启发的优化算法。它通过模拟自然选择和遗传机制来搜索最优解。 **2.1.1 遗传算法的编码和解码** 编码是将问题空间中的解表示为二进制字符串或其他数据结构的过程。解码是将编码的解转换为问题空间中的实际解的过程。常见的编码方法包括二进制编码、实数编码和树形编码。 **2.1.2 遗传算法的交叉和
recommend-type

openstack的20种接口有哪些

以下是OpenStack的20种API接口: 1. Identity (Keystone) API 2. Compute (Nova) API 3. Networking (Neutron) API 4. Block Storage (Cinder) API 5. Object Storage (Swift) API 6. Image (Glance) API 7. Telemetry (Ceilometer) API 8. Orchestration (Heat) API 9. Database (Trove) API 10. Bare Metal (Ironic) API 11. DNS
recommend-type

JSBSim Reference Manual

JSBSim参考手册,其中包含JSBSim简介,JSBSim配置文件xml的编写语法,编程手册以及一些应用实例等。其中有部分内容还没有写完,估计有生之年很难看到完整版了,但是内容还是很有参考价值的。