zoukankan      html  css  js  c++  java
  • 输出参数resultType

    1.简单类型(八个基本类型+String)

    /MyBatisProject3/src/org/myy/mapper/studentMapper.xml

    <select id="queryStudentCount" resultType="int">
        select count(1) from student1
    </select>

    /MyBatisProject3/src/org/myy/mapper/StudentMapper.java

    int queryStudentCount();

    /MyBatisProject3/src/org/myy/test/Test.java

    //Connection - SqlSession操作Mybatis
    //conf.xml->reader
    Reader reader = Resources.getResourceAsReader("conf.xml");
    //reader->sqlSession
    //可以通过build的第二参数 指定数据库环境
    SqlSessionFactory sessionFactory=new SqlSessionFactoryBuilder().build(reader,"development");
    SqlSession session = sessionFactory.openSession();
                                
                                
    StudentMapper studentMapper = session.getMapper(StudentMapper.class);
    int count=studentMapper.queryStudentCount();
    System.out.println(count);
    session.close();

    2.输出参数为实体对象类型

    /MyBatisProject3/src/org/myy/mapper/studentMapper.xml

    <select id="queryStuByStuno" parameterType="int" resultType="student">
        select * from student1 where stuno=${value}
    </select>

    /MyBatisProject3/src/org/myy/mapper/StudentMapper.java

    Student queryStuByStuno(int stuno);

    /MyBatisProject3/src/org/myy/test/Test.java

    //Connection - SqlSession操作Mybatis
    //conf.xml->reader
    Reader reader = Resources.getResourceAsReader("conf.xml");
    //reader->sqlSession
    //可以通过build的第二参数 指定数据库环境
    SqlSessionFactory sessionFactory=new SqlSessionFactoryBuilder().build(reader,"development");
    SqlSession session = sessionFactory.openSession();
                                    
                                    
    StudentMapper studentMapper = session.getMapper(StudentMapper.class);
                            
    Student student=studentMapper.queryStuByStuno(1);
    System.out.println(student);
    session.close();

    3.输出参数为实体对象类型的集合:虽然输出类型为集合,但是resultType依然写集合的元素类型()

    /MyBatisProject3/src/org/myy/mapper/studentMapper.xml

    <select id="queryAllStudent" resultType="student">
        select * from student1
    </select>

    /MyBatisProject3/src/org/myy/mapper/StudentMapper.java

    List<Student> queryAllStudent();

    /MyBatisProject3/src/org/myy/test/Test.java

    // Connection - SqlSession操作Mybatis
    // conf.xml->reader
    Reader reader = Resources.getResourceAsReader("conf.xml");
    // reader->sqlSession
    // 可以通过build的第二参数 指定数据库环境
    SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(reader, "development");
    SqlSession session = sessionFactory.openSession();
    
    StudentMapper studentMapper = session.getMapper(StudentMapper.class);
    List<Student> students = studentMapper.queryAllStudent();
    System.out.println(students);
    session.close();

    4.输出参数类型为HashMap

    Hashmap本身是一个集合,但是可以存放多个元素

      但是根据提示发现,返回值为Hashmap时,查询的结果只能是一个学生(no,name)

    ->结论:一个HashMap对应一个学生的多个元素(多个属性)

    /MyBatisProject3/src/org/myy/mapper/studentMapper.xml

    <select id="queryAllStudentOutByHashMap" resultType="HashMap">
        select stuno "no",stuname "name" from student1
    </select>

    /MyBatisProject3/src/org/myy/mapper/StudentMapper.java

    List<HashMap<String, Object>> queryAllStudentOutByHashMap();

    /MyBatisProject3/src/org/myy/test/Test.java

    // Connection - SqlSession操作Mybatis
    // conf.xml->reader
    Reader reader = Resources.getResourceAsReader("conf.xml");
    // reader->sqlSession
    // 可以通过build的第二参数 指定数据库环境
    SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(reader, "development");
    SqlSession session = sessionFactory.openSession();
    
    StudentMapper studentMapper = session.getMapper(StudentMapper.class);
    
    List<HashMap<String, Object>> studentMap = studentMapper.queryAllStudentOutByHashMap();
    System.out.println(studentMap);
    session.close();

    resultType:一般情况下

    resultMap:实体类的属性、数据表的字段:类型、名字不同时

    当属性名和字段名不一致时,除了使用resultMap以外,还可以使用resultType+HashMap

    a.result

    /MyBatisProject3/src/org/myy/mapper/studentMapper.xml

    <select id="qureyStudentById"  parameterType="int" resultMap="qureyStudentByIdMap">
            select id,name from student1 where id=#{id}
    </select>
    <resultMap type="student" id="qureyStudentByIdMap">
        <!--指定类中的属性和表中的字段对应关系  -->
        <id property="stuNo" column="id"/>
        <result property="stuName" column="name"/>
    </resultMap>

    /MyBatisProject3/src/org/myy/mapper/StudentMapper.java

        Student qureyStudentById(int stuno);

    /MyBatisProject3/src/org/myy/test/Test.java

    // Connection - SqlSession操作Mybatis
    // conf.xml->reader
    Reader reader = Resources.getResourceAsReader("conf.xml");
    // reader->sqlSession
    // 可以通过build的第二参数 指定数据库环境
    SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(reader, "development");
    SqlSession session = sessionFactory.openSession();
    StudentMapper studentMapper
    = session.getMapper(StudentMapper.class); Student student = studentMapper.qureyStudentById(1); System.out.println(student); session.close();

    b.resultType+HashMap

    /MyBatisProject3/src/org/myy/mapper/studentMapper.xml

    <select id="qureyStudentByIdWithHashMap"  parameterType="int" resultType="HashMap">
            select id "stuNo",name "stuName" from student1 where id=#{id}
    </select>

    /MyBatisProject3/src/org/myy/mapper/StudentMapper.java

    Student qureyStudentByIdWithHashMap(int stuno);

    /MyBatisProject3/src/org/myy/test/Test.java

    // Connection - SqlSession操作Mybatis
    // conf.xml->reader
    Reader reader = Resources.getResourceAsReader("conf.xml");
    // reader->sqlSession
    // 可以通过build的第二参数 指定数据库环境
    SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(reader, "development");
    SqlSession session = sessionFactory.openSession();
    
    StudentMapper studentMapper = session.getMapper(StudentMapper.class);
    
    Student student = studentMapper.qureyStudentByIdWithHashMap(1);
    System.out.println(student);
    session.close();

    注意:如果n个字段,发现其中某一个字段始终为默认值,则可能是表的字段和类的属性名字写错。

  • 相关阅读:
    bzoj3670 [Noi2014]动物园
    bzoj2882 工艺
    bzoj3097 Hash Killer I
    bzoj3729 Gty的游戏
    【BZOJ4555】[TJOI&HEOI2016]求和 斯特林数+NTT
    【bzoj4869】[Shoi2017]相逢是问候 线段树+扩展欧拉定理
    【BZOJ1853】[Scoi2010]幸运数字 容斥原理+搜索
    【BZOJ2839】集合计数 容斥原理+组合数
    【BZOJ3622】已经没什么好害怕的了 容斥原理+dp
    【BZOJ3589】动态树 树链剖分+线段树
  • 原文地址:https://www.cnblogs.com/mayouyou/p/13221322.html
Copyright © 2011-2022 走看看