MyBatis Generator对于mysql text类型数据的处理||MyBatis生成的selectByExample方法获取text类型数据得到空值

Mybatis自动生成的Xml文件,text数据类型会默认产生XXWithBlobs的方法,用以获取含有该类型的数据

  • 所以,读取含有text类型的数据的时候,应该使用XXWithBlobs的方法,否则会获取到空值
  • 其次,只能修改数据类型了,改用varchar了

以下是MyBatis对于数据类型映射的操作解析

  • 如下,可以看到MyBatis对于类型的处理:

表数据,其中introductiontext类型数据

MyBatis Generator生成的mapper.xml片段:

  <resultMap id="BaseResultMap" type="com.liantao.bookMS.entity.Book">
    <id column="book_id" jdbcType="BIGINT" property="bookId" />
    <result column="name" jdbcType="VARCHAR" property="name" />
    <result column="author" jdbcType="VARCHAR" property="author" />
    <result column="publish" jdbcType="VARCHAR" property="publish" />
    <result column="ISBN" jdbcType="VARCHAR" property="isbn" />
    <result column="language" jdbcType="VARCHAR" property="language" />
    <result column="price" jdbcType="DECIMAL" property="price" />
    <result column="pubdate" jdbcType="DATE" property="pubdate" />
    <result column="class_id" jdbcType="INTEGER" property="classId" />
    <result column="pressmark" jdbcType="INTEGER" property="pressmark" />
    <result column="state" jdbcType="SMALLINT" property="state" />
  </resultMap>
  <resultMap extends="BaseResultMap" id="ResultMapWithBLOBs" type="com.liantao.bookMS.entity.Book">
    <result column="introduction" jdbcType="LONGVARCHAR" property="introduction" />
  </resultMap>

对于text类型的映射是 jdbcType="LONGVARCHAR",普通类型处理则为VARCHAR,DATE,BIGINT之类的。

修改数据类型为varchar后的maper.xml应该为:

  <resultMap id="BaseResultMap" type="com.liantao.bookMS.entity.Book">
    <id column="book_id" jdbcType="BIGINT" property="bookId" />
    <result column="name" jdbcType="VARCHAR" property="name" />
    <result column="author" jdbcType="VARCHAR" property="author" />
    <result column="publish" jdbcType="VARCHAR" property="publish" />
    <result column="ISBN" jdbcType="VARCHAR" property="isbn" />
    <result column="language" jdbcType="VARCHAR" property="language" />
    <result column="price" jdbcType="DECIMAL" property="price" />
    <result column="pubdate" jdbcType="DATE" property="pubdate" />
    <result column="class_id" jdbcType="INTEGER" property="classId" />
    <result column="pressmark" jdbcType="INTEGER" property="pressmark" />
    <result column="state" jdbcType="SMALLINT" property="state" />
    <result column="introduction" jdbcType="VARCHAR" property="introduction" />
  </resultMap>

PS:我不知道为什么直接改不能生效,要改了数据库在用mbg.java在生成一次才行,如有知晓请告知 3q

END

猜你喜欢

转载自www.cnblogs.com/famine/p/10748035.html
今日推荐