封装MyBatis输出结果

一、resultType

resultType: 执行 sql 得到 ResultSet 转换的类型,也就是要返回的结果类型,使用类型的完全限定名或别名。

注意如果返回的是集合,那应该设置为集合包含的类型,而不是集合本身。

resultType 和 resultMap,不能同时使用

1. 简单类型(掌握)

接口方法:

int countStudent();

mapper文件

<!--sql执行后返回一行一列-->
<select id="countStudent" resultType="int">

select count(*) from student

</select>

测试方法

@Test
public void testRetunInt(){
	int count = studentDao.countStudent();
	System.out.println(" 学生总人数:"+ count);
}

2. 对象类型(掌握)

接口方法

Student selectById(int id);

mapper文件

<select id="selectById" resultType="com.md.domain.Student">
select id,name,email,age from student where id=#{studentId}
</select>

返回的是集合

接口方法

List<Student> selectStudents();

mapper文件,返回的结果类型是这个集合所包含的集合类型

<select id="selectStudents" resultType="com.md.domain.Student">
	select id,age from student
</select>

Student类中要写set和get方法

3. Map(了解)

sql 的查询结果作为 Map 的 key 和 value。推荐使用 Map<Object,Object>。
注意:Map 作为接口返回值,sql 语句的查询结果最多只能有一条记录。大于一条记录是错误。

列名是map的key, 列值是map的value

接口方法

//定义方法返回Map
    Map<Object,Object> selectMapById(Integer id);

mapper文件

<!--使用的少-->
    <select id="selectMapById" resultType="java.util.HashMap">
        select id,email from student where id=#{stuid}
    </select>

测试方法

@Test
public void testReturnMap(){
	Map<Object,Object> retMap = studentDao.selectMapById(1002);
	System.out.println(" 查询结果是 Map:"+retMap);
}

二、resultMap(了解)

resultMap 可以自定义 sql 的结果和 java 对象属性的映射关系。更灵活的把列值赋值给指定属性。
常用在列名和 java 对象属性名不一样的情况,具体看下面

使用方式:

  • 先定义 resultMap,指定列名和属性的对应关系
  • 在<select>中把 resultType 替换为 resultMap

接口方法

 List<Student> selectAllStudents();

mapper文件

 <!--定义resultMap
        id:自定义名称,表示你定义的这个resultMap
        type:java类型的全限定名称
    -->
    <resultMap id="studentMap" type="com.md.domain.Student">
        <!--列名和java属性的关系-->
        <!--主键列,使用id标签
            column :列名
            property:java类型的属性名
        -->
        <id column="id" property="id" />
        <!--非主键列,使用result-->
        <result column="name" property="name" />
        <result column="email" property="email" />
        <result column="age" property="age" />

  	</resultMap>

  <select id="selectAllStudents" resultMap="studentMap">
        select id,age from student
    </select>

三、实体类属性名和列名不同

1. 使用resultMap

接口方法

List<MyStudent> selectMyStudent();

此时的情况就是实体类的属性名和表中的列名不同,

实体类:

public class MyStudent {
    private Integer stuid;
    private String stuname;
    private String stuemail;
    private Integer stuage;
    // get、set、等方法
}    

mapper文件

<resultMap id="myStudentMap" type="com.md.domain.MyStudent">
        <!--列名和java属性的关系-->

        <id column="id" property="stuid" />
        <!--非主键列,使用result-->
        <result column="name" property="stuname" />
        <result column="email" property="stuemail" />
        <result column="age" property="stuage" />


    </resultMap>
    <!--列名和属性名不一样:第一种方式-->
    <select id="selectMyStudent" resultMap="myStudentMap">

         select id,age from student
    </select>

测试方法

 @Test
    public void testSelectMyStudent(){
        SqlSession sqlSession = MybatisUtils.getSqlSession();
        StudentDao dao = sqlSession.getMapper(StudentDao.class);

        List<MyStudent> myStudentList = dao.selectMyStudent();
        myStudentList.forEach(stu-> System.out.println(stu));

        sqlSession.close();

    }

// 打印的结果
MyStudent{stuid=1001,stuname='唐三',stuemail='ts@qq.com',stuage=18}
MyStudent{stuid=1002,stuname='无邪',stuemail='wx@qq.com',stuage=20}
MyStudent{stuid=1003,stuname='白昊天',stuemail='ht@qq.com',stuage=18}
MyStudent{stuid=1004,stuname='刘桑',stuemail='ls@qq.com',stuage=18}
MyStudent{stuid=1005,stuname='李白',stuemail='li@qq.com',stuage=30}
MyStudent{stuid=1006,stuname='王昭君',stuemail='wzj@qq.com',stuage=30}

2. 使用列别名和resultType

此时的实体类还是

public class MyStudent {
    private Integer stuid;
    private String stuname;
    private String stuemail;
    private Integer stuage;
    // get、set、等方法
} 

接口方法

List<MyStudent> selectDiffColProperty();

mapper文件

 <!--列名和属性名不一样:第二种方式
       resultType的默认原则是 同名的列值赋值给同名的属性, 使用列别名(java对象的属性名)
    -->
    <select id="selectDiffColProperty" resultType="com.md.domain.MyStudent">
        select id as stuid,name as stuname,email as stuemail,age stuage from student
    </select>

测试方法

    @Test
    public void testSelectDiffColProperty(){
        SqlSession sqlSession = MybatisUtils.getSqlSession();
        StudentDao dao = sqlSession.getMapper(StudentDao.class);

        List<MyStudent> myStudentList = dao.selectDiffColProperty();
        myStudentList.forEach(stu-> System.out.println(stu));

        sqlSession.close();
    }

结果和使用resultMap是一样的,所以这两种方法使用哪一种都行

四、模糊查询like

模糊查询的实现有两种方式

  1. java 代码中给查询数据加上“%”
  2. 在 mapper 文件 sql 语句的条件位置加上“%”

1. 第一种

接口方法

    /*第一种模糊查询, 在java代码指定 like的内容*/
//   例如:唐%
    List<Student> selectLikeOne(String name);

mapper文件

<!--第一种 like , java代码指定 like的内容-->
    <select id="selectLikeOne" resultType="com.md.domain.Student">
        select id,age from student where name like #{name}
    </select>

测试文件

   @Test
    public void testSelectLikeOne(){
        SqlSession sqlSession = MybatisUtils.getSqlSession();
        StudentDao dao = sqlSession.getMapper(StudentDao.class);

        List<Student> studentList = dao.selectLikeOne("唐%");
        studentList.forEach(stu-> System.out.println(stu));

        sqlSession.close();
    }

可以看出这种方法非常的方便

2. 第二种

接口方法

/* name就是唐这个值, 在mapper中拼接 like  "%" 李 "%" */
    List<Student> selectLikeTwo(String name);

mapper文件

 <!--第二种方式:在mapper文件中拼接 like的内容-->
    <select id="selectLikeTwo" resultType="com.md.domain.Student">
        select id,age from student where name  like "%" #{name} "%"
    </select>

测试

 @Test
    public void testSelectLikeTwo(){
        SqlSession sqlSession = MybatisUtils.getSqlSession();
        StudentDao dao = sqlSession.getMapper(StudentDao.class);

        List<Student> studentList = dao.selectLikeTwo("白");
        studentList.forEach(stu-> System.out.println(stu));

        sqlSession.close();
    }

这种的是直接在mapper中写死了sql模糊查询,不利于扩展

推荐第一种方式

五、总结

1. resultType

表示sql语句的执行结果,转换为java对象的类型

  • 类型的全限定名称

2. resultMap

自定义列名和java对象的属性名对应关系

3. 列名和属性名不同

  • 使用resultMap
  • 使用列别名

4. like

  • 在java代码中指定like的内容
  • 在mapper文件中拼接like

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


1.pom.xml引入依赖 &lt;dependency&gt; &lt;groupId&gt;com.github.pagehelper&lt;/groupId&gt; &lt;artifactId&gt;pagehelper&lt;/artifactId&gt; &lt;version&gt;5
&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot; ?&gt; &lt;!DOCTYPE configuration PUBLIC &quot;-//mybatis.org//DTD Config 3.0//EN&quot; &qu
准备工作 ① 创建数据库&amp;数据表 ## 创建数据库 CREATE DATABASE `dbtest1`; ## 创建数据表 CREATE TABLE `t_user` ( `id` INT NOT NULL AUTO_INCREMENT, `username` VARCHAR(20) DEF
MyBatis逆向工程是指根据数据库表结构自动生成对应的实体类、Mapper接口以及SQL映射文件的过程。这个过程可以通过MyBatis提供的逆向工程工具来完成,极大地方便了开发人员,避免了重复的代码编写,提高了开发效率。 创建逆向工程的步骤 1、添加依赖&amp;插件 &lt;!-- 控制Mave
MyBatis获取参数值的两种方式:${}和#{} ${}的本质就是字符串拼接,#{}的本质就是占位符赋值。 ${}使用字符串拼接的方式拼接sql,若为字符串类型或日期类型的字段进行赋值时,需要手动加单引号;但是#{}使用占位符赋值的方式拼接sql,此时为字符串类型或日期类型的字段进行赋值时,可以自
resultMap作用是处理数据表中字段与java实体类中属性的映射关系。 准备工作 ① 创建数据库&amp;数据表 CREATE DATABASE `dbtest1`; CREATE TABLE `t_emp` ( `emp_id` int NOT NULL AUTO_INCREMENT, `em
EHCache缓存针对于MyBatis的二级缓存。 MyBatis默认二级缓存是SqlSessionFactory级别的。 添加依赖 &lt;!-- MyBatis-EHCache整合包 --&gt; &lt;dependency&gt; &lt;groupId&gt;org.mybatis.cac
MyBatis 提供了一级缓存和二级缓存的支持,用于提高数据库查询的性能,减少不必要的数据库访问。 一级缓存(SqlSession 级别的缓存) 一级缓存是 MyBatis 中最细粒度的缓存,也称为本地缓存。它存在于每个 SqlSession 的生命周期中,当 SqlSession 被关闭或清空时,
动态SQL是 MyBatis 中非常强大且灵活的功能,允许你根据不同的条件构建SQL查询。 这主要通过 &lt;if&gt;、&lt;choose&gt;、&lt;when&gt;、&lt;otherwise&gt;、&lt;foreach&gt;等标签实现。 查询场景 /** * 根据条件查询员工
本教程操作系统:windows10系统、DELL G3电脑。 MyBatis 是一个优秀的持久层框架,它支持定制化 SQL、存储过程以及高级映射。在 MyBatis 中,配置数据库连接是非常重要的第一步。下面将详细介绍如何配置 MyBatis 的
今天小编给大家分享的是MyBatis批量查询、插入、更新、删除如何实现,相信很多人都不太了解,为了让大家更加了解,所以给大家总结了以下内容,一起往下看吧。
今天小编给大家分享的是Mybatis操作多数据源实现的方法,相信很多人都不太了解,为了让大家更加了解,所以给大家总结了以下内容,一起往下看吧。一定会有所收获...
本篇文章和大家了解一下mybatis集成到spring的方式有哪些。有一定的参考价值,有需要的朋友可以参考一下,希望对大家有所帮助。1 前言1.1 集成spring前使用mybat...
今天小编给大家分享的是mybatis-plus分页查询的3种方法,相信很多人都不太了解,为了让大家更加了解,所以给大家总结了以下内容,一起往下看吧。一定会有所收获...
本篇内容主要讲解“mybatis之BaseTypeHandler怎么使用”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“mybatis...
这篇文章主要介绍了mybatisforeach怎么传两个参数的相关知识,内容详细易懂,操作简单快捷,具有一定借鉴价值,相信大家阅读完这篇mybatisforeach怎...
这篇“MyBatis映射文件中parameterType与resultType怎么使用”文章的知识点大部分人都不太理解,所以小编给大家总结了以下内容,内容详细,步骤清晰,具有一定的...
这篇文章主要介绍“MyBatis怎么获取自动生成的键值”,在日常操作中,相信很多人在MyBatis怎么获取自动生成的键值问题上存在疑惑,小编查阅了各式资料,整理出
这篇文章主要讲解了“怎么去掉IntelliJIDEA中mybatis对应的xml文件警告”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入...
这篇文章主要介绍“MybatisPlus使用@TableId主键id自增长无效如何解决”的相关知识,小编通过实际案例向大家展示操作过程,操作方法简单快捷,实用性强,希望这...