这篇文章将为大家详细讲解有关Spring和MyBatis整合自动生成代码里面text类型遇到的坑怎么处理,小编觉得挺实用的,因此分享给大家做个参考,希望大家阅读完这篇文章后可以有所收获。
成都创新互联公司从2013年开始,是专业互联网技术服务公司,拥有项目成都网站建设、网站建设网站策划,项目实施与项目整合能力。我们以让每一个梦想脱颖而出为使命,1280元蜀山做网站,已为上家服务,为蜀山各地企业和个人服务,联系电话:13518219792
Spring和MyBatis整合以后,使用自动生成代码工具生成dao和mapper配置文件,生成步骤如下(以Intelli idea为例)。
1.编写生成代码配置文件generatorConfig.xml。
<?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运行命令。
3.运行generatorcode即可。
问题描述
假如数据库表里面存在text或者blob字段。自动生成的数据库配置文件如下,会多出几个以withBlobs结尾的方法和resultMap:
<!--仅仅贴上不一样的部分--> <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这两个方法。
关于“Spring和MyBatis整合自动生成代码里面text类型遇到的坑怎么处理”这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,使各位可以学到更多知识,如果觉得文章不错,请把它分享出去让更多的人看到。
当前题目:Spring和MyBatis整合自动生成代码里面text类型遇到的坑怎么处理
本文来源:https://www.cdcxhl.com/article28/jhsocp.html
成都网站建设公司_创新互联,为您提供网站设计公司、定制开发、做网站、软件开发、微信小程序、品牌网站建设
声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 创新互联