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并抛出异常生成错误代码
时间: 2024-02-05 07:14:10 浏览: 65
MyBatis入门之增删改查+数据库字段和实体字段不一致问题处理方法
好的,下面是代码实现。
首先是SMSTemplateDTO实体类:
```java
public class SMSTemplateDTO {
private Integer seqNo;
private String templateId;
private String pushPlanTpCd;
private String templateName;
private String templateContent;
private String creatorNo;
private Date createTime;
private String modifierNo;
private Date modTime;
private String oprorInstno;
// getter和setter方法省略
}
```
接下来是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.SMSTemplateMapper">
<!-- 查询短信模板列表 -->
<select id="getSMSTemplateList" resultType="com.example.dto.SMSTemplateDTO">
SELECT seq_no AS seqNo, template_id AS templateId, push_plan_tp_cd AS pushPlanTpCd, template_name AS templateName, template_content AS templateContent, creator_no AS creatorNo, create_time AS createTime, modifier_no AS modifierNo, mod_time AS modTime, opror_instno AS oprorInstno
FROM t_sms_template
ORDER BY seq_no
</select>
<!-- 根据seq_no查询短信模板 -->
<select id="getSMSTemplateBySeqNo" parameterType="int" resultType="com.example.dto.SMSTemplateDTO">
SELECT seq_no AS seqNo, template_id AS templateId, push_plan_tp_cd AS pushPlanTpCd, template_name AS templateName, template_content AS templateContent, creator_no AS creatorNo, create_time AS createTime, modifier_no AS modifierNo, mod_time AS modTime, opror_instno AS oprorInstno
FROM t_sms_template
WHERE seq_no = #{seqNo}
</select>
<!-- 插入短信模板 -->
<insert id="insertSMSTemplate" parameterType="com.example.dto.SMSTemplateDTO">
INSERT INTO t_sms_template (seq_no, template_id, push_plan_tp_cd, template_name, template_content, creator_no, create_time, modifier_no, mod_time, opror_instno)
VALUES (#{seqNo}, #{templateId}, #{pushPlanTpCd}, #{templateName}, #{templateContent}, #{creatorNo}, #{createTime}, #{modifierNo}, #{modTime}, #{oprorInstno})
</insert>
<!-- 更新短信模板 -->
<update id="updateSMSTemplate" parameterType="com.example.dto.SMSTemplateDTO">
UPDATE t_sms_template
SET template_id = #{templateId}, push_plan_tp_cd = #{pushPlanTpCd}, template_name = #{templateName}, template_content = #{templateContent}, modifier_no = #{modifierNo}, mod_time = #{modTime}, opror_instno = #{oprorInstno}
WHERE seq_no = #{seqNo}
</update>
<!-- 删除短信模板 -->
<delete id="deleteSMSTemplate" parameterType="int">
DELETE FROM t_sms_template
WHERE seq_no = #{seqNo}
</delete>
</mapper>
```
然后是Mapper接口:
```java
public interface SMSTemplateMapper {
List<SMSTemplateDTO> getSMSTemplateList();
SMSTemplateDTO getSMSTemplateBySeqNo(int seqNo);
void insertSMSTemplate(SMSTemplateDTO smsTemplate);
void updateSMSTemplate(SMSTemplateDTO smsTemplate);
void deleteSMSTemplate(int seqNo);
}
```
接下来是Service实现类:
```java
@Service
public class SMSTemplateServiceImpl implements SMSTemplateService {
@Autowired
private SMSTemplateMapper smsTemplateMapper;
@Override
public List<SMSTemplateDTO> getSMSTemplateList() {
return smsTemplateMapper.getSMSTemplateList();
}
@Override
public SMSTemplateDTO getSMSTemplateBySeqNo(int seqNo) {
return smsTemplateMapper.getSMSTemplateBySeqNo(seqNo);
}
@Override
public void insertSMSTemplate(SMSTemplateDTO smsTemplate) {
smsTemplateMapper.insertSMSTemplate(smsTemplate);
}
@Override
public void updateSMSTemplate(SMSTemplateDTO smsTemplate) {
smsTemplateMapper.updateSMSTemplate(smsTemplate);
}
@Override
public void deleteSMSTemplate(int seqNo) {
smsTemplateMapper.deleteSMSTemplate(seqNo);
}
}
```
最后是Controller:
```java
@RestController
@RequestMapping("/smsTemplate")
public class SMSTemplateController {
@Autowired
private SMSTemplateService smsTemplateService;
@GetMapping("/list")
public RestResponse<List<SMSTemplateDTO>> getSMSTemplateList() {
List<SMSTemplateDTO> smsTemplateList = smsTemplateService.getSMSTemplateList();
return RestResponse.success(smsTemplateList);
}
@GetMapping("/{seqNo}")
public RestResponse<SMSTemplateDTO> getSMSTemplateBySeqNo(@PathVariable int seqNo) {
SMSTemplateDTO smsTemplate = smsTemplateService.getSMSTemplateBySeqNo(seqNo);
if (smsTemplate == null) {
throw new BusinessException("短信模板不存在");
}
return RestResponse.success(smsTemplate);
}
@PostMapping("/")
public RestResponse<Void> insertSMSTemplate(@RequestBody SMSTemplateDTO smsTemplate) {
smsTemplateService.insertSMSTemplate(smsTemplate);
return RestResponse.success();
}
@PutMapping("/{seqNo}")
public RestResponse<Void> updateSMSTemplate(@PathVariable int seqNo, @RequestBody SMSTemplateDTO smsTemplate) {
smsTemplate.setSeqNo(seqNo);
smsTemplateService.updateSMSTemplate(smsTemplate);
return RestResponse.success();
}
@DeleteMapping("/{seqNo}")
public RestResponse<Void> deleteSMSTemplate(@PathVariable int seqNo) {
SMSTemplateDTO smsTemplate = smsTemplateService.getSMSTemplateBySeqNo(seqNo);
if (smsTemplate == null) {
throw new BusinessException("短信模板不存在");
}
smsTemplateService.deleteSMSTemplate(seqNo);
return RestResponse.success();
}
}
```
其中RestResponse是自定义的响应类,用于封装返回给前端的数据和状态码。BusinessException是自定义的异常类,用于处理业务异常。
阅读全文