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

1.基本类型

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

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类型(*常用类型)

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

public Customer queryCustomer(Integer id);

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

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

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

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

CustomerRM.java:

 package cn.sm1234.domain;

 public class CustomerRM {

     private Integer custId;
private String custName;
private String custGender;
private String custTelephone;
public Integer getCustId() {
return custId;
}
public void setCustId(Integer custIid) {
this.custId = custIid;
}
public String getCustName() {
return custName;
}
public void setCustName(String custName) {
this.custName = custName;
}
public String getCustGender() {
return custGender;
}
public void setCustGender(String custGender) {
this.custGender = custGender;
}
public String getCustTelephone() {
return custTelephone;
}
public void setCustTelephone(String custTelephone) {
this.custTelephone = custTelephone;
}
@Override
public String toString() {
return "CustomerRM [custId=" + custId + ", custName=" + custName + ", custGender=" + custGender
+ ", custTelephone=" + custTelephone + "]";
} }

数据库字段截图:

Mybatis进阶学习笔记——输出映射的更多相关文章

  1. Mybatis进阶学习笔记——输入映射

    1.输入映射 输入映射支持的类型: 1) 基本的类型,int,String,double 等(*)2) JavaBean 类型(*)3) 包装JavaBean 类型(对象里面包含另一个对象) 1.1基 ...

  2. Mybatis进阶学习笔记——动态代理方式开发Dao接口、Dao层(推荐第二种)

    1.原始方法开发Dao Dao接口 package cn.sm1234.dao; import java.util.List; import cn.sm1234.domain.Customer; pu ...

  3. Mybatis进阶学习笔记——关系查询——一对多查询

    一个客户拥有多个订单 <resultMap type="User" id="UserOrderResultMap"> <id column=& ...

  4. Mybatis进阶学习笔记——关系查询——一对一查询

    用户和订单的需求 通过查询订单,查询用户,就是一对一查询 (1)自定义JavaBean(常用,推荐使用) <select id="queryOrderUser" result ...

  5. Mybatis进阶学习笔记——动态sql

    1.if标签 <select id="queryByNameAndTelephone" parameterType="Customer" resultTy ...

  6. ROS进阶学习笔记(11)- Turtlebot Navigation and SLAM - ROSMapModify - ROS地图修改

    ROS进阶学习笔记(11)- Turtlebot Navigation and SLAM - 2 - MapModify地图修改 We can use gmapping model to genera ...

  7. 爱了!阿里大神最佳总结“Flutter进阶学习笔记”,理论与实战

    前言 "小步快跑.快速迭代"的开发大环境下,"一套代码.多端运行"是很多开发团队的梦想,美团也一样.他们做了很多跨平台开发框架的尝试:React Native. ...

  8. mybatis的学习笔记

    前几天学习了mybatis,今天来复习一下它的内容. mybatis是一个基于Java的持久层框架,那就涉及到数据库的操作.首先来提出第一个问题:java有jdbc连接数据库,我们为什么还要使用框架呢 ...

  9. MyBatis:学习笔记(4)——动态SQL

    MyBatis:学习笔记(4)——动态SQL 如果使用JDBC或者其他框架,很多时候需要你根据需求手动拼装SQL语句,这是一件非常麻烦的事情.MyBatis提供了对SQL语句动态的组装能力,而且他只有 ...

随机推荐

  1. Docker报错总结

    [Docker push镜像报错] The push refers to a repository [192.168.200.103:5000/rancher/server]Get https://1 ...

  2. 分布式 NewSQL 对比

    1.TiDB: 说明: PingCAP 公司基于 Google Spanner / F1 论文实现的开源分布式 NewSQL 数据库. 开源分布式 NewSQL 关系型数据库 TiDB 是新一代开源分 ...

  3. 批量修改SharePoint2013 备用语言

    cls [System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint") foreach ($we ...

  4. Azure登陆的两种常见方式(user 和 service principal登陆)

    通过Powershell 登陆Azure(Azure MoonCake为例)一般常见的有两种方式 1. 用户交互式登陆 前提条件:有一个AAD account 此种登陆方式会弹出一个登陆框,让你输入一 ...

  5. AtCoder Grand Contest 010 D - Decrementing

    题目描述 有n个整数,其中第i个数为Ai.这些数字的gcd为1.两人轮流操作,每次操作把一个大于1的数减1,并把所有数除以所有数的最大公约数,最后无法操作者输,求是否先手必胜. 如果当前的sum为偶数 ...

  6. A1024. Palindromic Number

    A number that will be the same when it is written forwards or backwards is known as a Palindromic Nu ...

  7. Educational Codeforces Round 52 (Rated for Div. 2) E. Side Transmutations

    http://codeforces.com/contest/1065/problem/E 数学推导题 #include <bits/stdc++.h> using namespace st ...

  8. 03-body标签中相关标签

    今日主要内容: 列表标签 <ul>.<ol>.<dl> 表格标签 <table> 表单标签 <fom> 一.列表标签 列表标签分为三种. 1 ...

  9. poj 2976(二分搜索+最大化平均值)

    传送门:Problem 2976 参考资料: [1]:http://www.hankcs.com/program/cpp/poj-2976-dropping-tests-problem-solutio ...

  10. (大数 求余) Large Division Light OJ 1214

    Large Division Given two integers, a and b, you should check whether a is divisible by b or not. We ...