<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTO Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> 是什么
时间: 2023-08-09 11:09:25 浏览: 229
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTO Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> 是一个用于定义 MyBatis Mapper XML 文件的文档类型声明(Document Type Declaration)。它指定了 DTD(Document Type Definition)文件的位置,用于验证和解析 Mapper XML 文件的结构和语法。通过引用该 DTD 文件,MyBatis 可以确保 Mapper 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.ruifan.taihaolian.biz.mapper.AdvertMapper"><resultMap id="BaseResultMap" type="com.ruifan.taihaolian.biz.model.Advert"><id column="id" jdbcType="INTEGER" property="id" /><result column="name" jdbcType="VARCHAR" property="name" /><result column="position_id" jdbcType="INTEGER" property="positionId" /><result column="media_id" jdbcType="INTEGER" property="mediaId" /><result column="start_date" jdbcType="TIMESTAMP" property="startDate" /><result column="end_date" jdbcType="TIMESTAMP" property="endDate" /><result column="link" jdbcType="VARCHAR" property="link" /><result column="status" jdbcType="INTEGER" property="status" /><result column="create_time" jdbcType="TIMESTAMP" property="createTime" /><result column="update_time" jdbcType="TIMESTAMP" property="updateTime" /></resultMap><select id="findAdverts" resultMap="advertMap">SELECT a.*, p.id AS position_id, p.name AS position_name, m.id AS media_id, m.file_name, m.file_type, m.file_url FROM advert a LEFT JOIN ad_position p ON a.position_id = p.id LEFT JOIN media_file m ON a.media_id = m.id WHERE (id = :id OR :id IS NULL) </select> <resultMap id="advertMap" type="com.ruifan.taihaolian.biz.model.dto.AdvertDTO"><id property="id" column="id"/><result property="name" column="name"/><association property="adPosition" javaType="com.ruifan.taihaolian.biz.model.AdPosition"><id property="id" column="position_id"/><result property="name" column="position_name"/></association><association property="mediaFile" javaType="com.ruifan.taihaolian.biz.model.MediaFile"><id property="id" column="id"/><result property="fileName" column="name"/><result property="fileType" column="type"/><result property="fileUrl" column="url"/></association></resultMap></mapper>请指正错误
这段代码是 MyBatis 的 XML 配置文件,其中定义了一个名为 AdvertMapper 的 Mapper 接口,包含了一个名为 findAdverts 的查询语句。但是,代码中的 resultMap 标签中定义了一个名为 BaseResultMap 的 ResultMap,但在 select 标签中却使用了 advertMap,这是不一致的。建议将 select 标签中的 resultMap 属性改为 BaseResultMap。另外,该查询语句中使用了 LEFT JOIN 连接,需要注意当 a.position_id 或 a.media_id 为 NULL 时,可能会导致查询结果不准确。
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并抛出异常生成错误代码
好的,下面是代码实现。
首先是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是自定义的异常类,用于处理业务异常。
阅读全文