大佬教程收集整理的这篇文章主要介绍了Spring和MyBatis整合自动生成代码里面text类型遇到的坑,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
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 --> <@L_899_24@mapGenerator targetPackage="/" targetProject="src/main/resources/mapper"> <property name="enableSubPackages" value="true"/> </@L_899_24@mapGenerator> <!-- 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类型遇到的坑全部内容,希望文章能够帮你解决Spring和MyBatis整合自动生成代码里面text类型遇到的坑所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。