spring和mybatis整合以后,使用自動生成代碼工具生成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運行命令。
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這兩個方法。
總結
以上所述是小編給大家介紹的spring和mybatis整合自動生成代碼里面text類型遇到的坑,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對服務器之家網站的支持!
原文鏈接:https://www.jianshu.com/