Ibatis BASICS (2): Query all objects

Source: Internet
Author: User

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:

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.