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个字段,发现其中某一个字段始终为默认值,则可能是表的字段和类的属性名字写错。