mybatis_SQL映射(2)出现的错误

来源:互联网 发布:朋友圈 知乎 编辑:程序博客网 时间:2024/05/20 07:53

(转载)http://blog.csdn.net/y172158950/article/details/17258377

1. sql的重用:定义一个sql片段,可在任何SQL语句中重用该片段。

[java] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. <sql id="personColumns"> name, sex, updateTime</sql>  
  2. <select id="selectPerson" parameterType="int" resultType="hashmap">  
  3.     select id, <include refid="personColumns"/> from person where id =#{id};  
  4. </select>  

2. javabean别名:不用每次写包路径

[java] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. <!-- In Config XML file,定义 -->  
  2. <typeAlias type=”com.someapp.model.User” alias=”User”/>  
  3. <!-- In SQL Mapping XML file,使用 -->  
  4. <select id=”selectUsers” parameterType=”int” resultType=”User”>  
  5.     select id, username, hashedPassword from some_table where id = #{id}  
  6. </select>  

3. 表与实体列名不匹配的解决

a) SQL的别名
[java] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. <select id=”selectUsers” parameterType=”int” resultType=”User”>  
  2. select user_id as "id", user_name as userName, hashed_password as hashedPassword from some_table where id = #{id}  
  3. </select>  
b)定义外部的resultMap
[java] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. <resultMap id="userResult" type="User">  
  2.     <id property="id" column="_id" />  
  3.     <result property="name" column="_name" />  
  4.     <result property="password" column="_password" />  
  5. </resultMap>  
  6.   
  7. <select id="selectUser" parameterType="int" resultMap="userResult">  
  8.     select _id, _name, _password from _user where _id =#{id};  
  9. </select>  
c) 异常及解决:
[java] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. Exception in thread "main" org.apache.ibatis.exceptions.PersistenceException:   
  2. ### Error building SqlSession.  
  3. ### The error may exist in com/yjq/entity/User.xml  
  4. ### The error occurred while processing mapper_resultMap[userResult]  
  5. ### 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  
  6.     at org.apache.ibatis.exceptions.ExceptionFactory.wrapException(ExceptionFactory.java:8)  
  7.     at org.apache.ibatis.session.SqlSessionFactoryBuilder.build(SqlSessionFactoryBuilder.java:32)  
  8.     at org.apache.ibatis.session.SqlSessionFactoryBuilder.build(SqlSessionFactoryBuilder.java:16)  
  9.     at com.yjq.db.DbFactory.getInstance(DbFactory.java:23)  
  10.     at com.yjq.dao.UserDao.selectUserById(UserDao.java:22)  
  11.     at com.yjq.dao.UserDao.main(UserDao.java:36)  
  12. 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  
  13.     at org.apache.ibatis.builder.xml.XMLConfigBuilder.parseConfiguration(XMLConfigBuilder.java:85)  
  14.     at org.apache.ibatis.builder.xml.XMLConfigBuilder.parse(XMLConfigBuilder.java:69)  
  15.     at org.apache.ibatis.session.SqlSessionFactoryBuilder.build(SqlSessionFactoryBuilder.java:30)  
  16.     ... 4 more  
  17. 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  
  18.     at org.apache.ibatis.builder.xml.XMLMapperBuilder.configurationElement(XMLMapperBuilder.java:97)  
  19.     at org.apache.ibatis.builder.xml.XMLMapperBuilder.parse(XMLMapperBuilder.java:73)  
  20.     at org.apache.ibatis.builder.xml.XMLConfigBuilder.mapperElement(XMLConfigBuilder.java:255)  
  21.     at org.apache.ibatis.builder.xml.XMLConfigBuilder.parseConfiguration(XMLConfigBuilder.java:83)  
  22.     ... 6 more  
  23. 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  
  24.     at org.apache.ibatis.builder.BaseBuilder.resolveClass(BaseBuilder.java:69)  
  25.     at org.apache.ibatis.builder.xml.XMLStatementBuilder.parseStatementNode(XMLStatementBuilder.java:40)  
  26.     at org.apache.ibatis.builder.xml.XMLMapperBuilder.buildStatementFromContext(XMLMapperBuilder.java:105)  
  27.     at org.apache.ibatis.builder.xml.XMLMapperBuilder.configurationElement(XMLMapperBuilder.java:95)  
  28.     ... 9 more  
  29. Caused by: org.apache.ibatis.type.TypeException: Could not resolve type alias 'userResult'.  Cause: java.lang.ClassNotFoundException: Cannot find class: userResult  
  30.     at org.apache.ibatis.type.TypeAliasRegistry.resolveAlias(TypeAliasRegistry.java:92)  
  31.     at org.apache.ibatis.builder.BaseBuilder.resolveAlias(BaseBuilder.java:93)  
  32.     at org.apache.ibatis.builder.BaseBuilder.resolveClass(BaseBuilder.java:67)  
  33.     ... 12 more  
  34. Caused by: java.lang.ClassNotFoundException: Cannot find class: userResult  
  35.     at org.apache.ibatis.io.ClassLoaderWrapper.classForName(ClassLoaderWrapper.java:173)  
  36.     at org.apache.ibatis.io.ClassLoaderWrapper.classForName(ClassLoaderWrapper.java:72)  
  37.     at org.apache.ibatis.io.Resources.classForName(Resources.java:235)  
  38.     at org.apache.ibatis.type.TypeAliasRegistry.resolveAlias(TypeAliasRegistry.java:88)  
  39.     ... 14 more  

<select id="selectUser" parameterType="int" resultType="userResult">  修改为resultMap


0 0