映射问题的解决办法(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);
}
相关推荐
天河归来44 分钟前
springboot框架redis开启管道批量写入数据
java·spring boot·redis
张先shen1 小时前
Elasticsearch RESTful API入门:全文搜索实战
java·大数据·elasticsearch·搜索引擎·全文检索·restful
codervibe1 小时前
如何用 Spring Security 构建无状态权限控制系统(含角色菜单控制)
java·后端
尽兴-1 小时前
如何将多个.sql文件合并成一个:Windows和Linux/Mac详细指南
linux·数据库·windows·sql·macos
codervibe1 小时前
项目中如何用策略模式实现多角色登录解耦?(附实战代码)
java·后端
TCChzp1 小时前
synchronized全链路解析:从字节码到JVM内核的锁实现与升级策略
java·jvm
大葱白菜1 小时前
🧩 Java 枚举详解:从基础到实战,掌握类型安全与优雅设计
java·程序员
笑衬人心。1 小时前
在 Mac 上安装 Java 和 IntelliJ IDEA(完整笔记)
java·macos·intellij-idea
SimonKing1 小时前
颠覆传统IO:零拷贝技术如何重塑Java高性能编程?
java·后端·程序员
sniper_fandc1 小时前
SpringBoot系列—MyBatis(xml使用)
java·spring boot·mybatis