映射问题的解决办法(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 mapper
        PUBLIC "-//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);
}
相关推荐
自强的小白13 分钟前
nacos配置中心面试题(导入配置的优先级),注意是如果后导入相同配置是以 后导入优先。外部入优先和nacos的数据隔离。
java·微服务
Wang's Blog14 分钟前
Java框架 SpringCloud 快速入门: Feign 的自定义配置与日志级别
java·开发语言·spring cloud
Vex2une15 分钟前
Windows 系统还原发展史:从快照回滚到现代恢复生态
windows·系统还原·系统重置·卷影复制·系统保护·windows re·系统映像备份
Hello_Pyhx20 分钟前
挖兔硬盘精灵 WatuDisk:从首次检测到日常检查的使用指南
运维·windows·硬盘·维护
成旭先生25 分钟前
企业全景信息查询 API:工商照面 33 项、股东出资、变更与社保一次查全
java·开发语言·api接口·企业信息查询·工商数据·风控尽调·供应商准入
她的男孩1 小时前
分片上传的大文件人人可下载:文件模块 isPrivate 在合并时被抹成 false,另有 4 个静默坑
java·后端·架构
SWAGGY..1 小时前
【C++进阶】:(7)红黑树的原理与 C++ 实现:结构设计、插入调整及性质验证
android·java·开发语言·c++·算法
Wang's Blog1 小时前
Java框架 SpringCloud 快速入门: Eureka 服务发现与服务名调用改造
java·spring cloud·eureka
夕除1 小时前
redis--OpenResty
java·redis
Wang's Blog1 小时前
Java框架 SpringCloud 快速入门: Eureka 注册中心原理分析
java·spring cloud·eureka