使用Java MySQL springboot mybatis 写一段短信模板的增删改查,需要用到controller ,mapper ,mapper.xml,service,serviceimpl dto
时间: 2024-01-09 21:05:27 浏览: 69
好的,以下是一个简单的短信模板增删改查的示例代码:
1. 定义DTO类
```
public class MessageTemplateDTO {
private Long id;
private String name;
private String content;
// 省略getter和setter
}
```
2. 定义Mapper接口
```
@Mapper
public interface MessageTemplateMapper {
List<MessageTemplateDTO> findAll();
MessageTemplateDTO findById(Long id);
void save(MessageTemplateDTO messageTemplateDTO);
void update(MessageTemplateDTO messageTemplateDTO);
void deleteById(Long id);
}
```
3. 定义Mapper XML文件
```
<mapper namespace="com.example.mapper.MessageTemplateMapper">
<resultMap id="messageTemplateDTO" type="com.example.dto.MessageTemplateDTO">
<id property="id" column="id"/>
<result property="name" column="name"/>
<result property="content" column="content"/>
</resultMap>
<select id="findAll" resultMap="messageTemplateDTO">
SELECT id, name, content FROM message_template
</select>
<select id="findById" parameterType="java.lang.Long" resultMap="messageTemplateDTO">
SELECT id, name, content FROM message_template WHERE id = #{id}
</select>
<insert id="save" parameterType="com.example.dto.MessageTemplateDTO">
INSERT INTO message_template(name, content) VALUES(#{name}, #{content})
</insert>
<update id="update" parameterType="com.example.dto.MessageTemplateDTO">
UPDATE message_template SET name = #{name}, content = #{content} WHERE id = #{id}
</update>
<delete id="deleteById" parameterType="java.lang.Long">
DELETE FROM message_template WHERE id = #{id}
</delete>
</mapper>
```
4. 定义Service接口和实现类
```
public interface MessageTemplateService {
List<MessageTemplateDTO> findAll();
MessageTemplateDTO findById(Long id);
void save(MessageTemplateDTO messageTemplateDTO);
void update(MessageTemplateDTO messageTemplateDTO);
void deleteById(Long id);
}
@Service
public class MessageTemplateServiceImpl implements MessageTemplateService {
private final MessageTemplateMapper messageTemplateMapper;
public MessageTemplateServiceImpl(MessageTemplateMapper messageTemplateMapper) {
this.messageTemplateMapper = messageTemplateMapper;
}
@Override
public List<MessageTemplateDTO> findAll() {
return messageTemplateMapper.findAll();
}
@Override
public MessageTemplateDTO findById(Long id) {
return messageTemplateMapper.findById(id);
}
@Override
public void save(MessageTemplateDTO messageTemplateDTO) {
messageTemplateMapper.save(messageTemplateDTO);
}
@Override
public void update(MessageTemplateDTO messageTemplateDTO) {
messageTemplateMapper.update(messageTemplateDTO);
}
@Override
public void deleteById(Long id) {
messageTemplateMapper.deleteById(id);
}
}
```
5. 定义Controller
```
@RestController
@RequestMapping("/messageTemplate")
public class MessageTemplateController {
private final MessageTemplateService messageTemplateService;
public MessageTemplateController(MessageTemplateService messageTemplateService) {
this.messageTemplateService = messageTemplateService;
}
@GetMapping
public List<MessageTemplateDTO> findAll() {
return messageTemplateService.findAll();
}
@GetMapping("/{id}")
public MessageTemplateDTO findById(@PathVariable Long id) {
return messageTemplateService.findById(id);
}
@PostMapping
public void save(@RequestBody MessageTemplateDTO messageTemplateDTO) {
messageTemplateService.save(messageTemplateDTO);
}
@PutMapping
public void update(@RequestBody MessageTemplateDTO messageTemplateDTO) {
messageTemplateService.update(messageTemplateDTO);
}
@DeleteMapping("/{id}")
public void deleteById(@PathVariable Long id) {
messageTemplateService.deleteById(id);
}
}
```
阅读全文