简单类型( 基本数据类型 )
java
HashMapsql
单个JavaBean( 自定义JavaBean类型 )数据库
传入JavaBean的包装类型( 用处:好比说用户的复杂查询( 关联查询 ),须要传入用户信息,商品信息,订单信息等 )app
介绍 JavaBean的包装类型( 定义VO和 Mapper.xml )dom
一、resultType:输出JavaBean类型:
测试
使用resultType进行映射,只有查询出来的列名和JavaBean中的属性名一致,才能够映射成功( 和dbUtils很相似 )。spa
SELECT username username_,sex sex_ from Users
若是查询出来的列名和JavaBean中的属性名彻底不一致,就不会建立JavaBean对象( null )code
若是查询出来的列名和JavaBean中的属性名有一个一致,就会建立JavaBean对象orm
2.resultType:输出简单类型:xml
查询出来的结果只有一行一列,才能使用简单类型
3.resultType:输出JavaBean 集合类型:
在mapper.xml中,查询出单个JavaBean列表,resultType指定的类型仍是JavaBean类型。在Java中获取返回值类型是集合类型。
定义resultMap
使用resultMap做为mapper.xml的输出映射类型
测试
<!-- 定义resultMap select id id_,name name_ from Users where id=#{id} 和 Users类中的属性作映射 type:resultMap最终所映射的JavaBean类型( 能够使用别名 ) id:对resultMap的惟一标识 --> <resultMap id="userResultMap" type="com.ts.domain.Users"> <!-- id标识查询结果中的惟一标识 column:数据库表的列名 property:JavaBean属性名 最后将column和property作映射 --> <id column="id_" property="id"/> <!-- result:对普通列的映射定义 --> <result column="name_" property="name"/> </resultMap> <select id="findUserByIdResultMap" parameterType="int" resultMap="userResultMap"> select id id_,name name_ from Users where id=#{id} </select>
@Test public void testResultMap() throws Exception { SqlSession sqlSession = sqlSessionFactory.openSession(); UserMapper userMapper = sqlSession.getMapper(UserMapper.class); Users user = userMapper.findUserByIdResultMap(2); System.out.println(user); }