七、ResultType各种输出参数处理方法-Mybatis快速入门小白编

-简单类型
-对象
-对象集合
-HashMap

<注意resultType与resultMap不能同时使用>
resultType要求用于数据库中字段名与javaBean中属性名一致的情况下使用
字段与属性名不一致情况下使用resultMap,本篇使用的全部为resultType

1.输出类型为简单类型
studentMapper.xml

<!--    输出类型:简单类型-->
    <select id="selectSimple" resultType="int">
        select count (1) from student
    </select>

StudentMapper.java

package com.bb.mapper;
import com.bb.entity.Student;
import org.apache.ibatis.annotations.Select;
import java.util.HashMap;
public interface StudentMapper {
    /*
     * 1。方法名和StudentMapper.xml文件中标签的id值一致
     * 2。方法输入参数与StudentMapper.xml文件中标签的parameterType一致
     * 3。返回值与StudentMapper.xml文件中标签的resultType一致
     *
     */
    int selectSimple();
}

TestMybatis.java

package com.bb.service;

import com.bb.entity.Address;
import com.bb.entity.Student;
import com.bb.mapper.StudentMapper;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.IOException;
import java.io.Reader;
import java.util.HashMap;

public class TestMybatis {
    //输出类型:简单类型
    public static void queryCount() throws IOException {
        Reader resourceAsReader = Resources.getResourceAsReader("conf.xml");
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(resourceAsReader);
        SqlSession sqlSession = sqlSessionFactory.openSession();
        StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);
        int count = mapper.selectSimple();
        System.out.println(count);
    }
    public static void main(String[] args) throws IOException {
//       queryStudentById();
//       queryStudentByParams(1,"bb");
//       queryStudentByParams2(1,"bb");
//       queryWithResultMap();
//       queryStudentWithClasses();
       queryCount();

    }
}

结果输出:

2

2.输出参数为对象
之前均有用到,此处不表

3.输出参数为对象集合,resultType为对象类名
studentMapper.xml

<!--    输出类型:对象集合-->
    <select id="selectObjects" resultType="Student">
        select * from student
    </select>

StudentMapper.java,返回值类型为List<Student>

public interface StudentMapper {
    List<Student> selectObjects();
}

Student.java 重写toString()方法

    @Override
    public String toString() {
        return "Student{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", age=" + age +
                ", address=" + address +
                '}';
    }

TestMybatis.java

public class TestMybatis {
    //输出类型:对象集合
    public static void queryStudents() throws IOException {
        Reader resourceAsReader = Resources.getResourceAsReader("conf.xml");
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(resourceAsReader);
        SqlSession sqlSession = sqlSessionFactory.openSession();
        StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);
        List<Student> students = mapper.selectObjects();
        System.out.println(students);
    }
    public static void main(String[] args) throws IOException {
         queryStudents();
    }
}

输出结果:

[Student{id=1, name='bb', age=11, address=null}, Student{id=2, name='ss', age=22, address=null}]

4.输出为HashMap,与对象无关???
studentMapper.xml

<!--    输出类型:HashMap-->
    <select id="selectHashMap" resultType="HashMap">
        select id as id_hashmapKEY,name as name_hashmapKEY,age as age_hashmapKEY from student
    </select>

StudentMapper.java

public interface StudentMapper {
    List<HashMap<String,Object>> selectHashMap();
}

TestMybatis.java

    //输出类型:HashMap
  public static void queryStudentsByHashMap() throws IOException {
        Reader resourceAsReader = Resources.getResourceAsReader("conf.xml");
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(resourceAsReader);
        SqlSession sqlSession = sqlSessionFactory.openSession();
        StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);
        List<HashMap<String, Object>> hashMaps = mapper.selectHashMap();
        System.out.println(hashMaps);
    }

输出结果:

[{age_hashmapKEY=11, name_hashmapKEY=bb, id_hashmapKEY=1}, {age_hashmapKEY=22, name_hashmapKEY=ss, id_hashmapKEY=2}]

  1. 输出类型:HashMap 使用@MapKey
    studentMapper.xml
    <!--    输出类型:HashMap 使用@MapKey  -->
    <select id="selectHashMaps" resultType="HashMap">
        select id,name,age from student
    </select>

StudentMapper.java接口:

public interface StudentMapper {
    //出类型:HashMap 使用@MapKey,输出格式为  @MapKey的值={...}
    //本例为 id={Student}
    @MapKey("id")
    HashMap<Integer,Student> selectHashMaps();

}

TestMybatis.java

  //输出类型:HashMap 使用@MapKey
    public static void queryStudentsByHashMaps() throws IOException {
        Reader resourceAsReader = Resources.getResourceAsReader("conf.xml");
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(resourceAsReader);
        SqlSession sqlSession = sqlSessionFactory.openSession();
        StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);
        HashMap<Integer, Student> hashMaps = mapper.selectHashMaps();
        System.out.println("返回的全部值:"+hashMaps);
        System.out.println("根据MapKey的值获取单独一个map集合:"+hashMaps.get(1));
        //hashMaps.get(1)虽然是一个Student对象,但是sql的返回值是map集合,无法获得一个真是的 Student对象
        //直接使用Student对象会报错:HashMap cannot be cast to com.bb.entity.Student
        //Student student = hashMaps.get(1);
        //System.out.println(student);

        //所以要将单独的一个map集合转变为json对象,通过json对象的getString()方法获取map集合中具体字段的值
        JSONObject json = (JSONObject)JSON.toJSON(hashMaps.get(1));
        System.out.println("转为json对象后的值:"+json);
        String name = json.getString("name");
        System.out.println("json对象中name的值(MapKey中id为1的值的map集合中name的值):"+name);
    }

输出结果:

返回的全部值:{1={name=bb, id=1, age=11}, 2={name=ss, id=2, age=22}, 3={name=cc, id=3, age=33}, 5={name=dd, id=5, age=44}, 6={name=ee, id=6, age=55}}
根据MapKey的值获取单独一个map集合:{name=bb, id=1, age=11}
转为json对象后的值:{"name":"bb","age":11,"id":1}
json对象中name的值(MapKey中id为1的值的map集合中name的值):bb

最后编辑于 :
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • 1. 简介 1.1 什么是 MyBatis ? MyBatis 是支持定制化 SQL、存储过程以及高级映射的优秀的...
    笨鸟慢飞阅读 6,511评论 0赞 4
  • 编写日志输出环境配置文件 在开发过程中,最重要的就是在控制台查看程序输出的日志信息,在这里我们选择使用 log4j...
    我没有三颗心脏阅读 6,970评论 0赞 33
  • 最近做项目用到springboot整合mybatis,security。将其中遇到的问题做一个总结 注:本项目全程...
    huoyl0410阅读 873评论 1赞 2
  • JAVA面试题 1、作用域public,private,protected,以及不写时的区别答:区别如下:作用域 ...
    JA尐白阅读 1,320评论 1赞 0
  • 小编费力收集:给你想要的面试集合 1.C++或Java中的异常处理机制的简单原理和应用。 当JAVA程序违反了JA...
    八爷君阅读 5,285评论 1赞 114

友情链接更多精彩内容