Spring和MyBatis整合自动生成代码里面text类型遇到的坑

2025-05-29 0 16

springmybatis整合以后,使用自动生成代码工具生成dao和mapper配置文件,生成步骤如下(以intelli idea为例)。

1.编写生成代码配置文件generatorconfig.xml。

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40
<?xml version="1.0" encoding="utf-8"?>

<!doctype generatorconfiguration

public "-//mybatis.org//dtd mybatis generator configuration 1.0//en"

"http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd">

<generatorconfiguration>

<classpathentry location="d:\\dev\\maven\\repository\\mysql\\mysql-connector-java\\5.1.39\\mysql-connector-java-5.1.39.jar"/>

<context id="db2tables" defaultmodeltype="flat" targetruntime="mybatis3">

<commentgenerator>

<property name="suppressdate" value="true"/>

<!-- 是否去除自动生成的注释 true:是 : false:否 -->

<property name="suppressallcomments" value="false"/>

</commentgenerator>

<jdbcconnection driverclass="com.mysql.jdbc.driver"

connectionurl="jdbc:mysql://localhost:3306/mycollege?characterencoding=utf-8"

userid="root"

password="root">

</jdbcconnection>

<javatyperesolver>

<property name="forcebigdecimals" value="false"/>

</javatyperesolver>

<!-- 生成模型的包名和位置 -->

<javamodelgenerator targetpackage="com.cx.elearnning.model"

targetproject="src/main/java">

<property name="enablesubpackages" value="true"/>

<property name="trimstrings" value="true"/>

</javamodelgenerator>

<!-- generate xml -->

<sqlmapgenerator targetpackage="/"

targetproject="src/main/resources/mapper">

<property name="enablesubpackages" value="true"/>

</sqlmapgenerator>

<!-- generate mapper -->

<javaclientgenerator type="xmlmapper" targetpackage="com.cx.elearnning.dao"

targetproject="src/main/java">

<property name="enablesubpackages" value="true"/>

</javaclientgenerator>

<!--需要自动生成的表名和对应的model名-->

<table tablename="sys_user" domainobjectname="sysuser"></table>

</context>

</generatorconfiguration>

2.配置如下maven运行命令。

Spring和MyBatis整合自动生成代码里面text类型遇到的坑

3.运行generatorcode即可。

问题描述

假如数据库表里面存在text或者blob字段。自动生成的数据库配置文件如下,会多出几个以withblobs结尾的方法和resultmap:

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28
<!--仅仅贴上不一样的部分-->

<resultmap extends="baseresultmap" id="resultmapwithblobs" type="com.cx.elearnning.model.eduwebsiteprofile">

<!--

warning - @mbggenerated

this element is automatically generated by mybatis generator, do not modify.

-->

<result column="desciption" jdbctype="longvarchar" property="desciption" />

</resultmap>

<select id="selectbyexamplewithblobs" parametertype="com.cx.elearnning.model.eduwebsiteprofileexample" resultmap="resultmapwithblobs">

<!--

warning - @mbggenerated

this element is automatically generated by mybatis generator, do not modify.

-->

select

<if test="distinct">

distinct

</if>

<include refid="base_column_list" />

,

<include refid="blob_column_list" />

from edu_website_profile

<if test="_parameter != null">

<include refid="example_where_clause" />

</if>

<if test="orderbyclause != null">

order by ${orderbyclause}

</if>

</select>

假如此时查询数据或者更新数据的使用仍然使用selectbyexample或者updatebyexample,得到的text或者blob数据是null。

正确做法

应该使用selectbyexamplewithblobs或者updatebyexamplewithblobs这两个方法。

总结

以上所述是小编给大家介绍的springmybatis整合自动生成代码里面text类型遇到的坑,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对快网idc网站的支持!

原文链接:https://www.jianshu.com/

收藏 (0) 打赏

感谢您的支持,我会继续努力的!

打开微信/支付宝扫一扫,即可进行扫码打赏哦,分享从这里开始,精彩与您同在
点赞 (0)

声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。

快网idc优惠网 建站教程 Spring和MyBatis整合自动生成代码里面text类型遇到的坑 https://www.kuaiidc.com/112799.html

相关文章

发表评论
暂无评论