使用Java MySQL springboot mybatis写一段短信模板的增删改查,需要用到 controller mapper mapperxml servic e serviceimpl dto,其中controller需要使用public restresponse,并抛出异常写出异常原因,表格字段id名称为seq_no
时间: 2024-02-01 18:02:14 浏览: 78
SpringBoot.docx
好的,以下是一段Java MySQL springboot mybatis的短信模板增删改查示例代码:
Controller:
```java
@RestController
@RequestMapping("/sms/template")
public class SmsTemplateController {
@Autowired
private SmsTemplateService smsTemplateService;
@PostMapping("/add")
public RestResponse addSmsTemplate(@RequestBody SmsTemplateDto smsTemplateDto) throws Exception {
try {
smsTemplateService.addSmsTemplate(smsTemplateDto);
return RestResponse.success();
} catch (Exception e) {
throw new Exception("添加短信模板失败:" + e.getMessage());
}
}
@PostMapping("/update")
public RestResponse updateSmsTemplate(@RequestBody SmsTemplateDto smsTemplateDto) throws Exception {
try {
smsTemplateService.updateSmsTemplate(smsTemplateDto);
return RestResponse.success();
} catch (Exception e) {
throw new Exception("更新短信模板失败:" + e.getMessage());
}
}
@GetMapping("/delete/{seqNo}")
public RestResponse deleteSmsTemplate(@PathVariable Long seqNo) throws Exception {
try {
smsTemplateService.deleteSmsTemplate(seqNo);
return RestResponse.success();
} catch (Exception e) {
throw new Exception("删除短信模板失败:" + e.getMessage());
}
}
@GetMapping("/get/{seqNo}")
public RestResponse<SmsTemplateDto> getSmsTemplate(@PathVariable Long seqNo) throws Exception {
try {
SmsTemplateDto smsTemplateDto = smsTemplateService.getSmsTemplate(seqNo);
return RestResponse.success(smsTemplateDto);
} catch (Exception e) {
throw new Exception("获取短信模板失败:" + e.getMessage());
}
}
}
```
Mapper:
```java
@Mapper
public interface SmsTemplateMapper {
void insert(SmsTemplateDto smsTemplateDto);
void update(SmsTemplateDto smsTemplateDto);
void delete(Long seqNo);
SmsTemplateDto selectOne(Long seqNo);
}
```
Mapper XML:
```xml
<mapper namespace="com.example.mapper.SmsTemplateMapper">
<insert id="insert" parameterType="com.example.dto.SmsTemplateDto">
INSERT INTO sms_template (seq_no, content, create_time, update_time)
VALUES (#{seqNo}, #{content}, NOW(), NOW())
</insert>
<update id="update" parameterType="com.example.dto.SmsTemplateDto">
UPDATE sms_template
SET content = #{content},
update_time = NOW()
WHERE seq_no = #{seqNo}
</update>
<delete id="delete" parameterType="java.lang.Long">
DELETE FROM sms_template
WHERE seq_no = #{seqNo}
</delete>
<select id="selectOne" parameterType="java.lang.Long" resultType="com.example.dto.SmsTemplateDto">
SELECT seq_no, content, create_time, update_time
FROM sms_template
WHERE seq_no = #{seqNo}
</select>
</mapper>
```
Service:
```java
@Service
public class SmsTemplateServiceImpl implements SmsTemplateService {
@Autowired
private SmsTemplateMapper smsTemplateMapper;
@Override
public void addSmsTemplate(SmsTemplateDto smsTemplateDto) {
smsTemplateMapper.insert(smsTemplateDto);
}
@Override
public void updateSmsTemplate(SmsTemplateDto smsTemplateDto) {
smsTemplateMapper.update(smsTemplateDto);
}
@Override
public void deleteSmsTemplate(Long seqNo) {
smsTemplateMapper.delete(seqNo);
}
@Override
public SmsTemplateDto getSmsTemplate(Long seqNo) {
return smsTemplateMapper.selectOne(seqNo);
}
}
```
Service接口:
```java
public interface SmsTemplateService {
void addSmsTemplate(SmsTemplateDto smsTemplateDto);
void updateSmsTemplate(SmsTemplateDto smsTemplateDto);
void deleteSmsTemplate(Long seqNo);
SmsTemplateDto getSmsTemplate(Long seqNo);
}
```
DTO:
```java
public class SmsTemplateDto {
private Long seqNo;
private String content;
private Date createTime;
private Date updateTime;
// Getter和Setter方法省略
}
```
表格创建SQL:
```sql
CREATE TABLE sms_template (
seq_no BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
content VARCHAR(1000) NOT NULL,
create_time DATETIME NOT NULL,
update_time DATETIME NOT NULL,
PRIMARY KEY (seq_no)
);
```
以上就是Java MySQL springboot mybatis的短信模板增删改查示例代码,希望能对你有所帮助。
阅读全文