当前位置: 首页 > news >正文

映射问题的解决办法(mybaitis)

最初我用的是注解来操控数据库(注释掉的部分)

@Mapper
public interface ThreadMapper {//	@Select("SELECT * FROM thread LIMIT #{page}, #{size}")List<Thread> getListByPage(@Param("page") int page, @Param("size") int size);//	@Select("SELECT * FROM thread WHERE user_id = #{userId} LIMIT #{page}, #{size}")List<Thread> getListByPageAndUserId(@Param("page") int page, @Param("size") int size, @Param("userId") int userId);//	@Insert("Insert into thread(user_id,title,content,create_time,update_time)" +
//			" values(#{userId},#{title},#{content},now(),now())")void addInfo(@Param("userId")int userId, @Param("title")String title, @Param("content")String content);
}

但是后面测试的时候发现映射出错(就是数据库与实体类没完全对上)

{"code": 0,"message": "操作成功","data": [{"id": 2,"title": "123456","content": "123","userId": 0,"creatTime": null,"updateTime": null}]
}

解决办法:

第一种:

把数据库字段与实体类设为一样的(我没试过)

第二种:

用xml文件来配置映射(上步骤)

1、

mybatis:type-aliases-package: usx.xwt.taotao.domain //实体类mapper-locations: classpath:/mapper/*.xml  //xml文件

这需要写在resource文件的配置文件下(我这里是.yml)

2、

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapperPUBLIC "-//mybatis.org//DTD Mapper 3.0//EN""http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="usx.xwt.taotao.mapper.ThreadMapper"><resultMap id="threadResultMap" type="Thread"><id property="id" column="id"/><result property="title" column="title"/> //映射<result property="content" column="content"/><result property="userId" column="user_id"/><result property="createTime" column="create_time" javaType="java.time.LocalDateTime"/><result property="updateTime" column="update_time" javaType="java.time.LocalDateTime"/></resultMap><select id="getListByPageAndUserId" resultMap="threadResultMap">SELECT * FROM thread WHERE user_id = #{userId} LIMIT #{page}, #{size}</select><select id="getListByPage" resultMap="threadResultMap">SELECT * FROM thread LIMIT #{page}, #{size}</select><insert id="addInfo">Insert into thread(user_id,title,content,create_time,update_time) values(#{userId},#{title},#{content},now(),now())</insert>
</mapper>

3、这里的方法名需要与上面的数据库语句的id对应(记得加注解)

@Mapper
public interface ThreadMapper {List<Thread> getListByPage(@Param("page") int page, @Param("size") int size);List<Thread> getListByPageAndUserId(@Param("page") int page, @Param("size") int size, @Param("userId") int userId);void addInfo(@Param("userId")int userId, @Param("title")String title, @Param("content")String content);
}

http://www.lryc.cn/news/471038.html

相关文章:

  • 关于机器学习方向学习的一些建议(过来人)
  • 【云原生】云原生后端:网络架构详解
  • 期货资管子系统框架设计JS路径及源代码分享
  • 【YOLO 系列】基于YOLO的工业自动化轴承缺陷检测系统【python源码+Pyqt5界面+数据集+训练代码】
  • Word中Normal.dotm样式模板文件
  • 生成式 AI 与向量搜索如何扩大零售运营:巨大潜力尚待挖掘
  • WonderWorld:斯坦福与 MIT 联手打造实时交互生成图像,单图秒变 3D 虚拟世界
  • 2024年【制冷与空调设备安装修理】考试内容及制冷与空调设备安装修理最新解析
  • PHP const 和 define主要区别
  • 期中前学习复习总结
  • K8S如何基于Istio重新实现微服务
  • MediaPipe 与 OpenCV 的结合——给心爱的人画一个爱心吧~
  • 心觉:成大事,不怕慢,就怕站
  • 练习LabVIEW第二十三题
  • 集成对接案例分享:金蝶云与聚水潭数据对接
  • 高级主题-灾难恢复与业务连续性
  • R语言实现随机森林分析:从入门到精通
  • 【vs2022】windows可用的依赖预编译库
  • 基础设施即代码(IaC):自动化基础设施管理的未来
  • C# 创建型设计模式----原型模式
  • Python数据分析NumPy和pandas(十五、pandas 数据加载、存储和文件格式)
  • 正则表达式以及密码匹配案例手机号码脱敏案例
  • 五、数组切片make
  • SSA-CNN-LSTM-MATT多头注意力机制多特征分类预测
  • 51单片机完全学习——LCD1602液晶显示屏
  • 【知识科普】今天聊聊前端打包工具webpack
  • 雷池社区版中升级雷池遇到问题
  • C++基础:constexpr,类型转换和选择语句
  • STM32 RTC时间无法设置和读取
  • go语言中defer用法详解