【mybatis】Mapper.xml写sql踩坑记录(1)

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/wupan6688/article/details/88746817

mybatis-generator工具使用踩坑

先说下发生的问题是什么,用工具生成的查询sql表达式
执行后UserAnswer 中的mebId,pointStatus等部分字段为null,实际数据库中是有数据的

entity类

@Data
@AllArgsConstructor
@NoArgsConstructor
@Builder
public class UserAnswer {
  private Integer id;

  private Long mebId;

  private Byte pointStatus;

  private String content;

  private Byte type;

  private Date createTime;

  private Date updateTime;

  private String phone;

  private String deviceId;

  private String os;

  private String version;
}

Mapper.java中新增了一个方法,好了,为了方便,开始使用mybatis-generator自动生成
(我的环境是Intelij + mybatis-generator插件)

@Mapper
public interface UserAnswerMapper {
 //新增的方法
  List<UserAnswer> selectByParam2(@Param("startTime") String startTime, @Param("endTime") String endTime, @Param("pointStatus") int pointStatus);
}

下面是自动在Mapper.xml文件中生成的<select

<select id="selectByParam2" resultType="com.wh.mobile.entity.UserAnswer"></select>

问题原因就在自动生成的这个resultType,改成下面的就好了

 <select id="selectByParam2" resultMap="BaseResultMap"></select>

BaseResultMap

 <resultMap id="BaseResultMap" type="com.wh.mobile.entity.UserAnswer">
            <id column="id" jdbcType="INTEGER" property="id"/>
            <result column="meb_id" jdbcType="BIGINT" property="mebId"/>
            <result column="point_status" jdbcType="TINYINT" property="pointStatus"/>
            <result column="content" jdbcType="VARCHAR" property="content"/>
            <result column="type" jdbcType="TINYINT" property="type"/>
            <result column="create_time" jdbcType="TIMESTAMP" property="createTime"/>
            <result column="update_time" jdbcType="TIMESTAMP" property="updateTime"/>
            <result column="phone" jdbcType="VARCHAR" property="phone"/>
            <result column="device_id" jdbcType="VARCHAR" property="deviceId"/>
            <result column="os" jdbcType="VARCHAR" property="os"/>
            <result column="version" jdbcType="VARCHAR" property="version"/>
        </resultMap>

猜你喜欢

转载自blog.csdn.net/wupan6688/article/details/88746817