zoukankan      html  css  js  c++  java
  • Mybatis进阶学习笔记——输出映射

    输出映射(例如一个方法的返回至使用什么类型去接收)

    1.基本类型

    1     <!-- 统计记录数 -->
    2     <select id="queryTotalCount" resultType="long">
    3         SELECT COUNT(*) FROM t_customer
    4     </select>

     1 public Long queryTotalCount(); 

        /**
         * 输出映射
         */
        @Test
        public void test2() {
            SqlSession sqlSession = SessionUtils.getSession();
            // getMapper(): 返回指定接口的动态代理的实现类对象
            CustomerDao dao = sqlSession.getMapper(CustomerDao.class);
            Long count = dao.queryTotalCount();
            System.out.println(count);
            sqlSession.commit();
            sqlSession.close();
        }

    2.JavaBean类型(*常用类型)

    1     <select id="queryCustomer" parameterType="int" resultType="Customer">
    2         SELECT * FROM t_customer WHERE id=#{value}
    3     </select>

     1 public Customer queryCustomer(Integer id); 

     1     /**
     2      * 输出映射
     3      */
     4     @Test
     5     public void test2() {
     6         SqlSession sqlSession = SessionUtils.getSession();
     7         // getMapper(): 返回指定接口的动态代理的实现类对象
     8         CustomerDao dao = sqlSession.getMapper(CustomerDao.class);
     9         Customer c = dao.queryCustomer(1);
    10         System.out.println(c);
    11         sqlSession.commit();
    12         sqlSession.close();
    13     }

    3.ResultMap类型(用于解决表的字段名称和实体类的属性名称不一致的情况)

    resultType使用要求:JavaBean中的属性名要和数据库字段名保持一致。

     1     <!-- 定义ResultMap -->
     2     <!-- 
     3         含义说明:
     4         type:我们需要封装成的实体类
     5         id:定义的名称,供下方代码使用
     6      -->
     7     <resultMap type="CustomerRM" id="customerResultMap">
     8         <!-- 
     9             id:映射主键
    10             column:数据库字段
    11             property:实体类中的命名
    12          -->
    13         <id column="id" property="custId"/>
    14         <result column="name" property="custName"/>
    15         <result column="gender" property="custGender"/>
    16         <result column="telephone" property="custTelephone"/>
    17     </resultMap>
    1     <select id="queryCustomerResultMap" parameterType="int" resultMap="customerResultMap">
    2     <!-- resultMap:方法的返回值类型,也就是上方定义的type="CustomerRM"中的CustomerRM对象 -->
    3         SELECT * FROM t_customer WHERE id=#{value}
    4     </select>
     1     /**
     2      * 输出映射
     3      */
     4     @Test
     5     public void test2() {
     6         SqlSession sqlSession = SessionUtils.getSession();
     7         // getMapper(): 返回指定接口的动态代理的实现类对象
     8         CustomerDao dao = sqlSession.getMapper(CustomerDao.class);
     9         CustomerRM c =  dao.queryCustomerResultMap(1);
    10         System.out.println(c);
    11         sqlSession.commit();
    12         sqlSession.close();
    13     }

    CustomerRM.java:

     1 package cn.sm1234.domain;
     2 
     3 public class CustomerRM {
     4 
     5     private Integer custId;
     6     private String custName;
     7     private String custGender;
     8     private String custTelephone;
     9     public Integer getCustId() {
    10         return custId;
    11     }
    12     public void setCustId(Integer custIid) {
    13         this.custId = custIid;
    14     }
    15     public String getCustName() {
    16         return custName;
    17     }
    18     public void setCustName(String custName) {
    19         this.custName = custName;
    20     }
    21     public String getCustGender() {
    22         return custGender;
    23     }
    24     public void setCustGender(String custGender) {
    25         this.custGender = custGender;
    26     }
    27     public String getCustTelephone() {
    28         return custTelephone;
    29     }
    30     public void setCustTelephone(String custTelephone) {
    31         this.custTelephone = custTelephone;
    32     }
    33     @Override
    34     public String toString() {
    35         return "CustomerRM [custId=" + custId + ", custName=" + custName + ", custGender=" + custGender
    36                 + ", custTelephone=" + custTelephone + "]";
    37     }
    38     
    39 }

    数据库字段截图:

  • 相关阅读:
    利用BitLocker和vhdx创建一个有加密的Win10系统
    macOS 10.12 任何来源
    Xcode 8 GM 编译缺失 /Users/usr/lib/libresolv.9.dylib
    基于inline-block的列表布局
    markdown 的基本操作
    easyui1.32 各种问题汇总
    angular笔记
    underscore 笔记
    我的问道游戏主题皮肤
    在bootstrap ace样式框架上修改的后台管理型模板(Tab页后台管理模板)
  • 原文地址:https://www.cnblogs.com/116970u/p/10164175.html
Copyright © 2011-2022 走看看