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
- <sql id="Personcolumns" > Name, Sex, updatetime</sql>
- <select id="Selectperson" parametertype="int" resulttype="HashMap" >
- Select ID, <include refid="Personcolumns"/> from the person where ID =#{id};
- </select>
2. JavaBean aliases: Do not write the package path every time
[Java]View PlainCopy
- <!--in Config XML file, defined--
- <typealias type= "Com.someapp.model.User" alias= "User"/>
- <!--in SQL Mapping XML file, using--
- <select id= "Selectusers" parametertype= "int" resulttype= "User" >
- Select ID, username, hashedpassword from some_table where id = #{id}
- </select>
3. Table and entity column names that do not match the alias of a) SQL
[Java]View PlainCopy
- <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) define the external Resultmap
[Java]View PlainCopy
- <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 and resolution:
[Java]View PlainCopy
- 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: +)
- At Org.apache.ibatis.session.SqlSessionFactoryBuilder.build (Sqlsessionfactorybuilder.java: +)
- At Com.yjq.db.DbFactory.getInstance (Dbfactory.java:)
- At Com.yjq.dao.UserDao.selectUserById (Userdao.java:)
- At Com.yjq.dao.UserDao.main (Userdao.java:)
- 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:)
- At Org.apache.ibatis.builder.xml.XMLConfigBuilder.parse (Xmlconfigbuilder.java:)
- At Org.apache.ibatis.session.SqlSessionFactoryBuilder.build (Sqlsessionfactorybuilder.java: +)
- ... 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:)
- At Org.apache.ibatis.builder.xml.XMLMapperBuilder.parse (Xmlmapperbuilder.java:)
- At Org.apache.ibatis.builder.xml.XMLConfigBuilder.mapperElement (Xmlconfigbuilder.java:255)
- At Org.apache.ibatis.builder.xml.XMLConfigBuilder.parseConfiguration (Xmlconfigbuilder.java:)
- ... 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:)
- At Org.apache.ibatis.builder.xml.XMLStatementBuilder.parseStatementNode (Xmlstatementbuilder.java: +)
- At Org.apache.ibatis.builder.xml.XMLMapperBuilder.buildStatementFromContext (Xmlmapperbuilder.java:)
- At Org.apache.ibatis.builder.xml.XMLMapperBuilder.configurationElement (Xmlmapperbuilder.java:)
- ... 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:)
- At Org.apache.ibatis.builder.BaseBuilder.resolveAlias (Basebuilder.java:)
- At Org.apache.ibatis.builder.BaseBuilder.resolveClass (Basebuilder.java:)
- ... 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:)
- At Org.apache.ibatis.io.Resources.classForName (Resources.java:235)
- At Org.apache.ibatis.type.TypeAliasRegistry.resolveAlias (Typealiasregistry.java:)
- ... More
<select id= "Selectuser" parametertype= "int" resulttype= "Userresult" > Modified to Resultmap
Mybatis_sql Mapping (2)