zoukankan      html  css  js  c++  java
  • MyBatis(七) 自定义映射结果ResultMap

    (1)接口中对应的方法

    public Emp getEmpById(Integer id);

    (2)Mapper文件

     1  <resultMap type="com.eu.bean.Emp" id="emp">
     2       <id column="id" property="id"/>
     3       <result column="last_name" property="lastName"/>
     4       <result column="gender" property="geder"/>
     5       <result column="email" property="email"/>
     6   </resultMap>
     7   
     8   
     9   <select id="getEmpById" resultMap="emp">
    10       select * from emp where id = #{id}
    11   </select>

     (3)测试

     1   public SqlSessionFactory getSqlSessionFactory() throws IOException {
     2         String resource = "conf/mybatis-config.xml";
     3         InputStream inputStream = Resources.getResourceAsStream(resource);
     4         return new SqlSessionFactoryBuilder().build(inputStream);
     5     }
     6     
     7     @Test
     8     public void testMapper() throws IOException {
     9         SqlSessionFactory sqlSessionFactory = getSqlSessionFactory();
    10         //1.获取到sqlsession 不会自动提交数据
    11         SqlSession openSession = sqlSessionFactory.openSession();
    12         EmpDao mapper = openSession.getMapper(EmpDao.class);
    13         
    14         Emp emp = mapper.getEmpById(4);
    15         System.out.println(emp);
    16         
    17         //手动提交数据
    18         openSession.commit();
    19         openSession.close();
    20     }
  • 相关阅读:
    Miller-Rabin素性测试
    ###Canny边缘检测算子
    ###SIFT特征
    建立一个免费的网站
    ###C中的extern-static-const关键词
    ###Git使用问题
    ###Fedora下安装Retext
    ###使用phpmailer自动邮件提醒
    Markdown学习
    有线和无线同时连接,选择其一上网
  • 原文地址:https://www.cnblogs.com/wanerhu/p/10719006.html
Copyright © 2011-2022 走看看