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/