The previous article shows how to use mybatis to create an operation table. This chapter describes how to use mybatis to read tables.
We already have an employee table in MySQL:
1 CREATE TABLE EMPLOYEE (2 id INT NOT NULL auto_increment,3 first_name VARCHAR(20) default NULL,4 last_name VARCHAR(20) default NULL,5 salary INT default NULL,6 PRIMARY KEY (id)7 );
This table has only one record as follows:
1 mysql> select * from EMPLOYEE;2 +----+------------+-----------+--------+3 | id | first_name | last_name | salary |4 +----+------------+-----------+--------+5 | 1 | Zara | Ali | 5000 |6 +----+------------+-----------+--------+7 1 row in set (0.00 sec)
Employee pojo class:
To perform the read operation, we will modify the file "employee. Java" in the employee class, as shown below:
1 public class Employee { 2 private int id; 3 private String first_name; 4 private String last_name; 5 private int salary; 6 7 /* Define constructors for the Employee class. */ 8 public Employee() {} 9 10 public Employee(String fname, String lname, int salary) {11 this.first_name = fname;12 this.last_name = lname;13 this.salary = salary;14 }15 16 /* Here are the method definitions */17 public int getId() {18 return id;19 }20 public String getFirstName() {21 return first_name;22 }23 public String getLastName() {24 return last_name;25 }26 public int getSalary() {27 return salary;28 }29 } /* End of Employee */
Employee. xml file:
To define the ibing statement using ibatis SQL, we will add the <SELECT> flag in the employee. XML file. In this tag definition, we will define the tag that will be used in ibatisread. the "ID" of the SQL SELECT query executed by the database in the Java file ".
<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE sqlMap PUBLIC "-//ibatis.apache.org//DTD SQL Map 2.0//EN""http://ibatis.apache.org/dtd/sql-map-2.dtd"><sqlMap namespace="Employee"><insert id="insert" parameterClass="Employee"> INSERT INTO EMPLOYEE(first_name, last_name, salary) values (#first_name#, #last_name#, #salary#) <selectKey resultClass="int" keyProperty="id"> select last_insert_id() as id </selectKey></insert><select id="getAll" resultClass="Employee"> SELECT * FROM EMPLOYEE</select></sqlMap>
Here, the where clause and SQL select statements are not used. The subsequent sections will demonstrate how to use the where and select statement clauses and how to pass values to the WHERE clause.
Ibatisread. Java file:
This file reads application-level logic from the employee Employee table:
import com.ibatis.common.resources.Resources;import com.ibatis.sqlmap.client.SqlMapClient;import com.ibatis.sqlmap.client.SqlMapClientBuilder;import java.io.*;import java.sql.SQLException;import java.util.*;public class IbatisRead{ public static void main(String[] args) throws IOException,SQLException{ Reader rd = Resources.getResourceAsReader("SqlMapConfig.xml"); SqlMapClient smc = SqlMapClientBuilder.buildSqlMapClient(rd); /* This would read all records from the Employee table. */ System.out.println("Going to read records....."); List <Employee> ems = (List<Employee>) smc.queryForList("Employee.getAll", null); Employee em = null; for (Employee e : ems) { System.out.print(" " + e.getId()); System.out.print(" " + e.getFirstName()); System.out.print(" " + e.getLastName()); System.out.print(" " + e.getSalary()); em = e; System.out.println(""); } System.out.println("Records Read Successfully "); }}
Compile and run:
The following are the steps to compile and run the above application. Make sure that you have set path and classpath properly before compilation and execution.
Create employee. XML as shown above.
Create employee. Java, as shown in, and compile it.
Create ibatisread. Java, as shown in, and compile it.
Run the ibatisread binary file to run the program.
You will get the following results and read the records from the employee table.
1 Going to read records.....2 1 Zara Ali 50003 Record Reads Successfully
Series of articles:
Mybatis knows (1)
Mybatis knowledge (2)
Mybatis knowledge (3)
Mybatis knowledge (4) Advantages of mybatis
Mybatis knows (5) Business Object Model
Mybatis knowledge (6) presentation layer and business logic layer
Mybatis knows how many (7) Persistent Layers
How many relational databases does mybatis know? (8)
Mybatis knows how many (9) Different Types of databases
How many application databases does mybatis know (10 )?
Mybatis knowledge (11) enterprise databases
How many private databases does mybatis know (12 )?
Mybatis knows (13) How does mybatis solve common database problems?
Mybatis knows how many (14) Distributed Database Systems
How many data models does mybatis know (15 )?
Mybatis knows how many (16) mybatis ing
Mybatis (17) mybatis and JDBC
Mybatis knows how much (18) mybatis System
Mybatis knows how many (19) mybatis operations
Mybatis knows how many (17) mybatis read Operations