有关mybatis的一些异常解法
org.mybatis.spring.MyBatisSystemException: nested exception is org.apache.ibatis.builder.BuilderException: Error resolving JdbcType. Cause: java.lang.IllegalArgumentException: No enum const class org.apache.ibatis.type.JdbcType.Date org.mybatis.spring.MyBatisExceptionTranslator.translateExceptionIfPossible(MyBatisExceptionTranslator.java:75) org.mybatis.spring.SqlSessionTemplate$SqlSessionInterceptor.invoke(SqlSessionTemplate.java:368)更坑爹的在后面,上面insert时的时候用#{endTime,jdbcType=DATE},可以将时间插入成功,且可以精确到时分秒
但如果在update语句中也这样使用,那你得到的只会有日期,这够坑爹的了吧 ,尼玛 比起ibatis方便之处差远了
要想在update语句中 将时间格式化成时分秒 不得不再加一个类型 如下面:
startTime = #{startTime,javaType=DATE, jdbcType=VARCHAR}
转载来源于:http://www.cnblogs.com/rollenholt/p/3365866.html
高级结果映射
对于resultMap中的字段一定要和select中查询出的字段一一对应,而且是数量相等的。特别是在select使用*取值和insert中使用所有字段插入值时
MyBatis 创建的一个想法:数据库不用永远是你想要的或需要它们是什么样的。而我们 最喜欢的数据库最好是第三范式或 BCNF 模式,但它们有时不是。如果可能有一个单独的 数据库映射,所有应用程序都可以使用它,这是非常好的,但有时也不是。结果映射就是 MyBatis 提供处理这个问题的答案。
比如,我们如何映射下面这个语句?
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
|
<!-- Very Complex Statement -->
<select id=
"selectBlogDetails"
parameterType=
"int"
resultMap=
"detailedBlogResultMap"
>
select
B.id as blog_id,
B.title as blog_title,
B.author_id as blog_author_id,
A.id as author_id,
A.username as author_username,
A.password as author_password,
A.email as author_email,
A.bio as author_bio,
A.favourite_section as author_favourite_section,
P.id as post_id,
P.blog_id as post_blog_id,
P.author_id as post_author_id,
P.created_on as post_created_on,
P.section as post_section,
P.subject as post_subject,
P.draft as draft,
P.body as post_body,
C.id as comment_id,
C.post_id as comment_post_id,
C.name as comment_name,
C.comment as comment_text,
T.id as tag_id,
T.name as tag_name
from Blog B
left outer join Author A on B.author_id = A.id
left outer join Post P on B.id = P.blog_id
left outer join Comment C on P.id = C.post_id
left outer join Post_Tag PT on PT.post_id = P.id
left outer join Tag T on PT.tag_id = T.id
where B.id = #{id}
</select>
|
你可能想把它映射到一个智能的对象模型,包含一个作者写的博客,有很多的博文,每 篇博文有零条或多条的评论和标签。 下面是一个完整的复杂结果映射例子 (假设作者, 博客, 博文, 评论和标签都是类型的别名) 我们来看看, 。 但是不用紧张, 我们会一步一步来说明。 当天最初它看起来令人生畏,但实际上非常简单。
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
|
<!-- Very Complex Result Map -->
<resultMap id=
"detailedBlogResultMap"
type=
"Blog"
>
<constructor>
<idArg column=
"blog_id"
javaType=
"int"
/>
</constructor>
<result property=
"title"
column=
"blog_title"
/>
<association property=
"author"
javaType=
"Author"
>
<id property=
"id"
column=
"author_id"
/>
<result property=
"username"
column=
"author_username"
/>
<result property=
"password"
column=
"author_password"
/>
<result property=
"email"
column=
"author_email"
/>
<result property=
"bio"
column=
"author_bio"
/>
<result property=
"favouriteSection"
column=
"author_favourite_section"
/>
</association>
<collection property=
"posts"
ofType=
"Post"
>
<id property=
"id"
column=
"post_id"
/>
<result property=
"subject"
column=
"post_subject"
/>
<association property=
"author"
javaType=
"Author"
/>
<collection property=
"comments"
ofType=
"Comment"
>
<id property=
"id"
column=
"comment_id"
/>
</collection>
<collection property=
"tags"
ofType=
"Tag"
>
<id property=
"id"
column=
"tag_id"
/>
</collection>
<discriminator javaType=
"int"
column=
"draft"
>
<
case
value=
"1"
resultType=
"DraftPost"
/>
</discriminator>
</collection>
</resultMap>
|
resultMap 元素有很多子元素和一个值得讨论的结构。 下面是 resultMap 元素的概念视图
resultMap
- constructor - 类在实例化时,用来注入结果到构造方法中
- idArg - ID 参数;标记结果作为 ID 可以帮助提高整体效能
- arg - 注入到构造方法的一个普通结果
- id – 一个 ID 结果;标记结果作为 ID 可以帮助提高整体效能
- result – 注入到字段或 JavaBean 属性的普通结果
- association – 一个复杂的类型关联;许多结果将包成这种类型
- 嵌入结果映射 – 结果映射自身的关联,或者参考一个
- collection – 复杂类型的集
- 嵌入结果映射 – 结果映射自身的集,或者参考一个
- discriminator – 使用结果值来决定使用哪个结果映射
- case – 基于某些值的结果映射
- 嵌入结果映射 – 这种情形结果也映射它本身,因此可以包含很多相 同的元素,或者它可以参照一个外部的结果映射。
- case – 基于某些值的结果映射
Attribute | Description |
---|---|
id | A unique identifier in this namespace that can be used to reference this result map. |
type | A fully qualified Java class name, or a type alias (see the table above for the list of built-in type aliases). |
autoMapping | If present, MyBatis will enable or disable the automapping for this ResultMap. This attribute overrides the global autoMappingBehavior. Default: unset. |
最佳实践 通常逐步建立结果映射。单元测试的真正帮助在这里。如果你尝试创建 一次创建一个向上面示例那样的巨大的结果映射, 那么可能会有错误而且很难去控制它 来工作。开始简单一些,一步一步的发展。而且要进行单元测试!使用该框架的缺点是 它们有时是黑盒(是否可见源代码) 。你确定你实现想要的行为的最好选择是编写单元 测试。它也可以你帮助得到提交时的错误。
下面一部分将详细说明每个元素。
id & result
1
2
|
<id property=
"id"
column=
"post_id"
/>
<result property=
"subject"
column=
"post_subject"
/>
|
这些是结果映射最基本内容。id 和 result 都映射一个单独列的值到简单数据类型(字符 串,整型,双精度浮点数,日期等)的单独属性或字段。
这两者之间的唯一不同是 id 表示的结果将是当比较对象实例时用到的标识属性。这帮 助来改进整体表现,特别是缓存和嵌入结果映射(也就是联合映射) 。
每个都有一些属性:
属性 | 描述 |
---|---|
property | 映射到列结果的字段或属性。如果匹配的是存在的,和给定名称相同 的 JavaBeans 的属性,那么就会使用。否则 MyBatis 将会寻找给定名称 property 的字段。这两种情形你可以使用通常点式的复杂属性导航。比如,你 可以这样映射一些东西: “username” ,或者映射到一些复杂的东西: “address.street.number” 。 |
column | 从数据库中得到的列名,或者是列名的重命名标签。这也是通常和会 传递给 resultSet.getString(columnName)方法参数中相同的字符串。 |
javaType | 一个 Java 类的完全限定名,或一个类型别名(参加上面内建类型别名 的列表) 。如果你映射到一个 JavaBean,MyBatis 通常可以断定类型。 然而,如果你映射到的是 HashMap,那么你应该明确地指定 javaType 来保证所需的行为。 |
jdbcType | 在这个表格之后的所支持的 JDBC 类型列表中的类型。JDBC 类型是仅 仅需要对插入,更新和删除操作可能为空的列进行处理。这是 JDBC jdbcType 的需要,而不是 MyBatis 的。如果你直接使用 JDBC 编程,你需要指定 这个类型-但仅仅对可能为空的值。 |
typeHandler | 我们在前面讨论过默认的类型处理器。使用这个属性,你可以覆盖默 认的类型处理器。这个属性值是类的完全限定名或者是一个类型处理 器的实现,或者是类型别名。 |
支持的 JDBC 类型
为了未来的参考,MyBatis 通过包含的 jdbcType 枚举型,支持下面的 JDBC 类型。
BIT | FLOAT | CHAR | TIMESTAMP | OTHER | UNDEFINED |
TINYINT | REAL | VARCHAR | BINARY | BLOG | NVARCHAR |
SMALLINT | DOUBLE | LONGVARCHAR | VARBINARY | CLOB | NCHAR |
INTEGER | NUMERIC | DATE | LONGVARBINARY | BOOLEAN | NCLOB |
BIGINT | DECIMAL | TIME | NULL | CURSOR | ARRAY |