Assume that the database's student table contains the following fields and data:
The corresponding Student. java is as follows:
1 public class Student {
2 private Integer id;
3 private String name;
4 private float score;
5 public Integer getId() {
6 return id;
7 }
8 public void setId(Integer id) {
9 this.id = id;
10 }
11 public String getName() {
12 return name;
13 }
14 public void setName(String name) {
15 this.name = name;
16 }
17 public float getScore() {
18 return score;
19 }
20 public void setScore(float score) {
21 this.score = score;
22 }
23 @Override
24 public String toString() {
25 return this.id+"\t"+this.name+"\t"+this.score;
26 }
27 }
To query all objects, add the following code in Student. xml:
<? 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>
<! -- TypeAlias is abbreviated as -->
<TypeAlias alias = "Student" type = "com. jim. bean. Student"/>
<! -- Id indicates the index name. resultClass indicates the return type. -->
<Select id = "SelectAllStudent" resultClass = "Student">
Select *
From student
</Select>
</SqlMap>
Compile junit for testing:
1 @ Test
2 public void queryAllStudent () throws Exception {
3 Reader reader = Resources. getResourceAsReader ("SqlMapConfig. xml"); // read the total configuration file
4 SqlMapClient sqlmapclient = SqlMapClientBuilder. buildSqlMapClient (reader); // create SqlMapClient to operate the database
5 reader. close ();
6 List <Student> students = sqlmapclient. queryForList ("SelectAllStudent"); // call the SQL statement we wrote in xml
7 for (Student student: students ){
8 System. out. println (student );
9}
10}
Check the result and the student information is queried successfully: