• 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();
                }
            }
        }
  • 相关阅读:
    Lambda
    Thread&线程池
    异常
    Map
    List and Set
    Collection和迭代器Iterator
    Object类,常用API
    (一)自定义 mybatis 之框架介绍
    Nginx三大功能及高并发分流
    http协议改为https
  • 原文地址:https://www.cnblogs.com/daxiong225/p/9902169.html
Copyright © 2020-2023  润新知