文章摘录自:http://blog.csdn.net/y172158950/article/details/17258377
1. sql的重用:定义一个sql片段,可在任何SQL语句中重用该片段。
- <sql id="personColumns"> name, sex, updateTime</sql>
- <select id="selectPerson" parameterType="int" resultType="hashmap">
- select id, <include refid="personColumns"/> from person where id =#{id};
- </select>
2. javabean别名:不用每次写包路径
- <!-- In Config XML file,定义 -->
- <typeAlias type=”com.someapp.model.User” alias=”User”/>
- <!-- In SQL Mapping XML file,使用 -->
- <select id=”selectUsers” parameterType=”int” resultType=”User”>
- select id, username, hashedPassword from some_table where id = #{id}
- </select>
3. 表与实体列名不匹配的解决
a) SQL的别名
- <select id=”selectUsers” parameterType=”int” resultType=”User”>
- select user_id as "id", user_name as userName, hashed_password as hashedPassword from some_table where id = #{id}
- </select>
b)定义外部的resultMap
- <resultMap id="userResult" type="User">
- <id property="id" column="_id" />
- <result property="name" column="_name" />
- <result property="password" column="_password" />
- </resultMap>
- <select id="selectUser" parameterType="int" resultMap="userResult">
- select _id, _name, _password from _user where _id =#{id};
- </select>
c) 异常及解决:
- Exception in thread "main" org.apache.ibatis.exceptions.PersistenceException:
- ### Error building SqlSession.
- ### The error may exist in com/yjq/entity/User.xml
- ### The error occurred while processing mapper_resultMap[userResult]
- ### Cause: org.apache.ibatis.builder.BuilderException: Error parsing SQL Mapper Configuration. Cause: java.lang.RuntimeException: Error parsing Mapper XML. Cause: org.apache.ibatis.builder.BuilderException: Error resolving class . Cause: org.apache.ibatis.type.TypeException: Could not resolve type alias 'userResult'. Cause: java.lang.ClassNotFoundException: Cannot find class: userResult
- at org.apache.ibatis.exceptions.ExceptionFactory.wrapException(ExceptionFactory.java:8)
- at org.apache.ibatis.session.SqlSessionFactoryBuilder.build(SqlSessionFactoryBuilder.java:32)
- at org.apache.ibatis.session.SqlSessionFactoryBuilder.build(SqlSessionFactoryBuilder.java:16)
- at com.yjq.db.DbFactory.getInstance(DbFactory.java:23)
- at com.yjq.dao.UserDao.selectUserById(UserDao.java:22)
- at com.yjq.dao.UserDao.main(UserDao.java:36)
- Caused by: org.apache.ibatis.builder.BuilderException: Error parsing SQL Mapper Configuration. Cause: java.lang.RuntimeException: Error parsing Mapper XML. Cause: org.apache.ibatis.builder.BuilderException: Error resolving class . Cause: org.apache.ibatis.type.TypeException: Could not resolve type alias 'userResult'. Cause: java.lang.ClassNotFoundException: Cannot find class: userResult
- at org.apache.ibatis.builder.xml.XMLConfigBuilder.parseConfiguration(XMLConfigBuilder.java:85)
- at org.apache.ibatis.builder.xml.XMLConfigBuilder.parse(XMLConfigBuilder.java:69)
- at org.apache.ibatis.session.SqlSessionFactoryBuilder.build(SqlSessionFactoryBuilder.java:30)
- ... 4 more
- Caused by: java.lang.RuntimeException: Error parsing Mapper XML. Cause: org.apache.ibatis.builder.BuilderException: Error resolving class . Cause: org.apache.ibatis.type.TypeException: Could not resolve type alias 'userResult'. Cause: java.lang.ClassNotFoundException: Cannot find class: userResult
- at org.apache.ibatis.builder.xml.XMLMapperBuilder.configurationElement(XMLMapperBuilder.java:97)
- at org.apache.ibatis.builder.xml.XMLMapperBuilder.parse(XMLMapperBuilder.java:73)
- at org.apache.ibatis.builder.xml.XMLConfigBuilder.mapperElement(XMLConfigBuilder.java:255)
- at org.apache.ibatis.builder.xml.XMLConfigBuilder.parseConfiguration(XMLConfigBuilder.java:83)
- ... 6 more
- Caused by: org.apache.ibatis.builder.BuilderException: Error resolving class . Cause: org.apache.ibatis.type.TypeException: Could not resolve type alias 'userResult'. Cause: java.lang.ClassNotFoundException: Cannot find class: userResult
- at org.apache.ibatis.builder.BaseBuilder.resolveClass(BaseBuilder.java:69)
- at org.apache.ibatis.builder.xml.XMLStatementBuilder.parseStatementNode(XMLStatementBuilder.java:40)
- at org.apache.ibatis.builder.xml.XMLMapperBuilder.buildStatementFromContext(XMLMapperBuilder.java:105)
- at org.apache.ibatis.builder.xml.XMLMapperBuilder.configurationElement(XMLMapperBuilder.java:95)
- ... 9 more
- Caused by: org.apache.ibatis.type.TypeException: Could not resolve type alias 'userResult'. Cause: java.lang.ClassNotFoundException: Cannot find class: userResult
- at org.apache.ibatis.type.TypeAliasRegistry.resolveAlias(TypeAliasRegistry.java:92)
- at org.apache.ibatis.builder.BaseBuilder.resolveAlias(BaseBuilder.java:93)
- at org.apache.ibatis.builder.BaseBuilder.resolveClass(BaseBuilder.java:67)
- ... 12 more
- Caused by: java.lang.ClassNotFoundException: Cannot find class: userResult
- at org.apache.ibatis.io.ClassLoaderWrapper.classForName(ClassLoaderWrapper.java:173)
- at org.apache.ibatis.io.ClassLoaderWrapper.classForName(ClassLoaderWrapper.java:72)
- at org.apache.ibatis.io.Resources.classForName(Resources.java:235)
- at org.apache.ibatis.type.TypeAliasRegistry.resolveAlias(TypeAliasRegistry.java:88)
- ... 14 more
<select id="selectUser" parameterType="int" resultType="userResult"> 修改为resultMap
mybatis_SQL映射(2)的更多相关文章
-
mybatis_SQL映射(4)鉴别器
摘录自:http://blog.csdn.net/y172158950/article/details/17505739 鉴别器:有时一个单独的数据库查询也许返回很多不同(但是希望有些关联)数据类型的 ...
-
mybatis_SQL映射(1)
文章摘录自:http://blog.csdn.net/y172158950/article/details/17258377 1. select的映射 <select id="sele ...
-
mybatis_SQL映射(3)
文章摘录自:http://blog.csdn.net/y172158950/article/details/17304645 1. 表关联 a) 嵌套查询(传说中的1+N问题) <resultM ...
-
Hibernatel框架关联映射
Hibernatel框架关联映射 Hibernate程序执行流程: 1.集合映射 需求:网络购物时,用户购买商品,填写地址 每个用户会有不确定的地址数目,或者只有一个或者有很多.这个时候不能把每条地址 ...
-
hibernate多对多关联映射
关联是类(类的实例)之间的关系,表示有意义和值得关注的连接. 本系列将介绍Hibernate中主要的几种关联映射 Hibernate一对一主键单向关联Hibernate一对一主键双向关联Hiberna ...
-
Dapper逆天入门~强类型,动态类型,多映射,多返回值,增删改查+存储过程+事物案例演示
Dapper的牛逼就不扯蛋了,答应群友做个入门Demo的,现有园友需要,那么公开分享一下: 完整Demo:http://pan.baidu.com/s/1i3TcEzj 注 意 事 项:http:// ...
-
ElasticSearch 5学习(9)——映射和分析(string类型废弃)
在ElasticSearch中,存入文档的内容类似于传统数据每个字段一样,都会有一个指定的属性,为了能够把日期字段处理成日期,把数字字段处理成数字,把字符串字段处理成字符串值,Elasticsearc ...
-
.NET平台开源项目速览(14)最快的对象映射组件Tiny Mapper
好久没有写文章,工作甚忙,但每日还是关注.NET领域的开源项目.五一休息,放松了一下之后,今天就给大家介绍一个轻量级的对象映射工具Tiny Mapper:号称是.NET平台最快的对象映射组件.那就一起 ...
-
ASP.NET Core的路由[1]:注册URL模式与HttpHandler的映射关系
ASP.NET Core的路由是通过一个类型为RouterMiddleware的中间件来实现的.如果我们将最终处理HTTP请求的组件称为HttpHandler,那么RouterMiddleware中间 ...
随机推荐
-
js当中的声明和初始化的顺序
if(!("a" in window)) { var a=1; } alert(a); 这里的alert出来undefined 这句话就相当于 var a; if(!(“a” in ...
-
Python 学习记录----利用Python绘制奥运五环
import turtle #导入turtle模块 turtle.color("blue") #定义颜色 turtle.penup() #penup和pendown()设置画笔抬起 ...
-
Think Python - Chapter 16 - Classes and functions
16.1 TimeAs another example of a user-defined type, we’ll define a class called Time that records th ...
-
android开源项目---developer篇
本文转载于:http://blog.csdn.net/likebamboo/article/details/19081209 主要介绍和Android开发工具和测试工具相关的开源项目. Buck fa ...
-
[布局] bootstrap基本标签总结
文件头: <!DOCTYPE HTML> <html> <head> <meta charset="utf-8"> <titl ...
-
ORACLE基本SQL语句-用户及建表篇
一.用户相关SQL语句 /*新建用户*/create user ; 说明:SA用户名,2013密码 /*授权connect,resource给用户sa*/grant connect,resource ...
-
BNUOJ34973Liserious战队
Liserious战队 Time Limit: 1000ms Memory Limit: 65536KB 64-bit integer IO format: %lld Java class ...
-
JSP+Servlet实现上传下载
0.项目结构 1.在WebRoot下创建index.jsp页面 <%@ page language="java" import="java.util.*" ...
-
camera测试之MTF
1.MTF介绍 MTF(Modulation Transfer Function)模量传递函数.MTF是camera成像对比度和分辨率的综合表现.从另一个角度来看,camera成像过程可以简单看成下图 ...
-
GO入门——6. struct与方法
1 struct Go 中的struct与C中的struct非常相似,并且Go没有class 使用 type struct{} 定义结构,名称遵循可见性规则 支持指向自身的指针类型成员 支持匿名结构, ...