Mybatis_sql Mapping (2)

Source: Internet
Author: User

Article excerpt from: http://blog.csdn.net/y172158950/article/details/172583771. SQL reuse: Defines a SQL fragment that can be reused in any SQL statement. [Java]View PlainCopy
    1. <sql id="Personcolumns" > Name, Sex, updatetime</sql>
    2. <select id="Selectperson" parametertype="int" resulttype="HashMap" >
    3. Select ID, <include refid="Personcolumns"/> from the person where ID =#{id};
    4. </select>
2. JavaBean aliases: Do not write the package path every time [Java]View PlainCopy
    1. <!--in Config XML file, defined--
    2. <typealias type= "Com.someapp.model.User" alias= "User"/>
    3. <!--in SQL Mapping XML file, using--
    4. <select id= "Selectusers" parametertype= "int" resulttype= "User" >
    5. Select ID, username, hashedpassword from some_table where id = #{id}
    6. </select>
3. Table and entity column names that do not match the alias of a) SQL [Java]View PlainCopy
    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) define the external Resultmap [Java]View PlainCopy
  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. <select id="Selectuser" parametertype="int" resultmap="Userresult" >
  7. Select _id, _name, _password from _user where _id =#{id};
  8. </select>
c) Exception and resolution: [Java]View PlainCopy
  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: +)
  8. At Org.apache.ibatis.session.SqlSessionFactoryBuilder.build (Sqlsessionfactorybuilder.java: +)
  9. At Com.yjq.db.DbFactory.getInstance (Dbfactory.java:)
  10. At Com.yjq.dao.UserDao.selectUserById (Userdao.java:)
  11. At Com.yjq.dao.UserDao.main (Userdao.java:)
  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:)
  14. At Org.apache.ibatis.builder.xml.XMLConfigBuilder.parse (Xmlconfigbuilder.java:)
  15. At Org.apache.ibatis.session.SqlSessionFactoryBuilder.build (Sqlsessionfactorybuilder.java: +)
  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:)
  19. At Org.apache.ibatis.builder.xml.XMLMapperBuilder.parse (Xmlmapperbuilder.java:)
  20. At Org.apache.ibatis.builder.xml.XMLConfigBuilder.mapperElement (Xmlconfigbuilder.java:255)
  21. At Org.apache.ibatis.builder.xml.XMLConfigBuilder.parseConfiguration (Xmlconfigbuilder.java:)
  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:)
  25. At Org.apache.ibatis.builder.xml.XMLStatementBuilder.parseStatementNode (Xmlstatementbuilder.java: +)
  26. At Org.apache.ibatis.builder.xml.XMLMapperBuilder.buildStatementFromContext (Xmlmapperbuilder.java:)
  27. At Org.apache.ibatis.builder.xml.XMLMapperBuilder.configurationElement (Xmlmapperbuilder.java:)
  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:)
  31. At Org.apache.ibatis.builder.BaseBuilder.resolveAlias (Basebuilder.java:)
  32. At Org.apache.ibatis.builder.BaseBuilder.resolveClass (Basebuilder.java:)
  33. ... 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:)
  37. At Org.apache.ibatis.io.Resources.classForName (Resources.java:235)
  38. At Org.apache.ibatis.type.TypeAliasRegistry.resolveAlias (Typealiasregistry.java:)
  39. ... More

<select id= "Selectuser" parametertype= "int" resulttype= "Userresult" > Modified to Resultmap

Mybatis_sql Mapping (2)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.