mybatis_SQL映射(2)
文章摘录自: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