Mybatis映射器(二)
上一篇文章返回是resultType,但其无法定义多的属性,比如typeHandler,级联等。为了支持复杂映射,可以用resultMap属性,先定义resultmap属性:
<mapper namespace="com.ssm.chapter5.mapper.RoleMapper"> <!-- 测试一级缓存 --> <!-- <cache/> --> <resultMap id="roleMap" type="role"> <id property="id" column="id" /> <result property="roleName" column="role_name" /> <result property="note" column="note" /> </resultMap> <select id="getRoleUseResultMap" parameterType="long" resultMap="roleMap"> select id, role_name, note from t_role where id = #{id} </select> mapper: public Role getRoleUseResultMap(Long id); test: public static void testGetRoleUseResultMap() { SqlSession sqlSession = null; try { sqlSession = SqlSessionFactoryUtils.openSqlSession(); RoleMapper roleMapper = sqlSession.getMapper(RoleMapper.class); Role role = roleMapper.getRoleUseResultMap(1L); System.out.println(role.getRoleName()); } catch(Exception ex) { ex.printStackTrace(); } finally { if (sqlSession != null) { sqlSession.close(); } } }