MyBatis 读取操作
-
读取操作
在上一章中,我们讨论了如何通过执行CREATE操作使用MyBatis将值插入到STUDENT表中。本章介绍如何使用MyBatis读取表中的数据。我们在MySQL中有以下STUDENT表-在MySQL数据库中创建STUDENT表,如下所示-CREATE TABLE details.student( ID int(10) NOT NULL AUTO_INCREMENT, NAME varchar(100) NOT NULL, BRANCH varchar(255) NOT NULL, PERCENTAGE int(3) NOT NULL, PHONE int(11) NOT NULL, EMAIL varchar(255) NOT NULL, PRIMARY KEY (`ID`) );
假设此表有两个记录为+----+----------+--------+------------+-----------+--------------------+ | ID | NAME | BRANCH | PERCENTAGE | PHONE | EMAIL | +----+----------+--------+------------+-----------+--------------------+ | 1 | Mohammad | It | 80 | 984803322 | Mohammad@gmail.com | | 2 | shyam | It | 75 | 984800000 | shyam@gmail.com | +----+----------+--------+------------+-----------+--------------------+
-
STUDENT POJO 类
为了执行读取操作,我们将Student.java中的Student类修改为-public class Student { private int id; private String name; private String branch; private int percentage; private int phone; private String email; public Student(int id, String name, String branch, int percentage, int phone, String email) { super(); this.id = id; this.name = name; this.branch = branch; this.percentage = percentage; this.phone = phone; this.email = email; } public Student() {} public int getId() { return id; } public String getName() { return name; } public int getPhone() { return phone; } public String getEmail() { return email; } public String getBranch() { return branch; } public int getPercentage() { return percentage; } }
-
Student.xml文件
要使用MyBatis定义SQL映射语句,我们将在Student.xml文件中添加<select>标记,并且在该标记定义内,我们将定义一个“id” ,它将在mybatisRead.java文件中用于在数据库上执行SQL SELECT查询。在读取记录时,我们可以一次获取所有记录,也可以使用where子句获取特定记录。在下面给出的XML中,您可以观察这两个查询。要检索特定记录,我们需要一个唯一的键来表示该记录。因此,我们还定义了Student类型的结果映射“id”(唯一键),以将select查询的结果与Student类的变量进行映射。<?xml version = "1.0" encoding = "UTF-8"?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <mapper namespace = "Student"> <resultMap id = "result" type = "Student"> <result property = "id" column = "ID"/> </resultMap> <select id = "getAll" resultMap = "result"> SELECT * FROM STUDENT; </select> <select id = "getById" parameterType = "int" resultMap = "result"> SELECT * FROM STUDENT WHERE ID = #{id}; </select> </mapper>
-
获取所有记录
该文件具有应用程序级逻辑,可从Student表中读取所有记录。创建并保存mybatisRead_ALL.java文件,如下所示-import java.io.IOException; import java.io.Reader; import java.util.List; import org.apache.ibatis.io.Resources; import org.apache.ibatis.session.SqlSession; import org.apache.ibatis.session.SqlSessionFactory; import org.apache.ibatis.session.SqlSessionFactoryBuilder; public class mybatisRead_ALL { public static void main(String args[]) throws IOException{ Reader reader = Resources.getResourceAsReader("SqlMapConfig.xml"); SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader); SqlSession session = sqlSessionFactory.openSession(); //select contact all contacts List<Student> student = session.selectList("Student.getAll"); for(Student st : student ){ System.out.println(st.getId()); System.out.println(st.getName()); System.out.println(st.getBranch()); System.out.println(st.getPercentage()); System.out.println(st.getEmail()); System.out.println(st.getPhone()); } System.out.println("Records Read Successfully "); session.commit(); session.close(); } }
编译与执行:- 如上所示创建Student.xml。
- 如本教程的MYBATIS-配置XML章节中所示,创建SqlMapConfig.xml 。
- 如上所示创建Student.java并进行编译。
- 如上所示创建mybatisRead_ALL.java并进行编译。
- 执行mybatisRead_ALL二进制文件以运行程序。
您将获得学生表的所有记录为-++++++++++++++ details of the student who's id is :1 +++++++++++++++++++ 1 Mohammad It 80 Mohammad@gmail.com 984803322 ++++++++++++++ details of the student who's id is :2 +++++++++++++++++++ 2 shyam It 75 shyam@gmail.com 984800000 Records Read Successfully
-
获取特定记录
复制并保存以下名为mybatisRead_byID的程序-import java.io.IOException; import java.io.Reader; import org.apache.ibatis.io.Resources; import org.apache.ibatis.session.SqlSession; import org.apache.ibatis.session.SqlSessionFactory; import org.apache.ibatis.session.SqlSessionFactoryBuilder; public class mybatisRead_byID { public static void main(String args[]) throws IOException{ int i = 1; Reader reader = Resources.getResourceAsReader("SqlMapConfig.xml"); SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader); SqlSession session = sqlSessionFactory.openSession(); //select a particular student by id Student student = (Student) session.selectOne("Student.getById", 2); //Print the student details System.out.println(student.getId()); System.out.println(student.getName()); System.out.println(student.getBranch()); System.out.println(student.getPercentage()); System.out.println(student.getEmail()); System.out.println(student.getPhone()); session.commit(); session.close(); } }
编译与执行:- 如上所示创建Student.xml。
- 如本教程的MYBATIS-配置XML章节中所示,创建SqlMapConfig.xml 。
- 如上所示创建Student.java并进行编译。
- 如上所示创建mybatisRead_byID.java并进行编译。
- 执行mybatisRead_byID二进制文件以运行程序。
您将得到以下结果,并且将从Student表中读取一条记录,如下所示:2 shyam It 75 shyam@gmail.com 984800000