原创作者: 饕尽天下
阅读:26247次
评论:3条
更新时间:2011-05-26
如果你使用iBatis的Result Map来工作,那么你一定知道iBatis的Result Map是用来提供数据库查询结果和它的对象属性之间的映射的,这是iBatis最常见且重要的特性了.本章仅是一个ResultMap的简单介绍.我们的Contact.java and SqlMapConfig.xml文件和我们的上一个例子是一样的,没有任何变化. Contact POJO的代码如下 :
Contact.java
public class Contact {
private String firstName;
private String lastName;
private String email;
private int id;
public Contact() {}
public Contact(
String firstName,
String lastName,
String email) {
this.firstName = firstName;
this.lastName = lastName;
this.email = email;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
}
SqlMapConfig.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE sqlMapConfig
PUBLIC "-//ibatis.apache.org//DTD SQL Map Config 2.0//EN"
"http://ibatis.apache.org/dtd/sql-map-config-2.dtd">
<sqlMapConfig>
<settings useStatementNamespaces="true"/>
<transactionManager type="JDBC">
<dataSource type="SIMPLE">
<property name="JDBC.Driver" value="com.mysql.jdbc.Driver"/>
<property name="JDBC.ConnectionURL"
value="jdbc:mysql://192.168.10.112:3306/vin"/>
<property name="JDBC.Username" value="root"/>
<property name="JDBC.Password" value="root"/>
</dataSource>
</transactionManager>
<sqlMap resource="Contact.xml"/>
</sqlMapConfig>
要想使用ResultMap我们得使用<resultMap></resultMap>标签.它由一个id组成,该id需要在<select>标签下的resultMap属性中运行resultMap.这是Contact.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 namespace="Contact">
<!--- Showing data by ID -->
<resultMap id="result" class="Contact">
<result property="id" column="id"/>
<result property="firstName" column="firstName"/>
<result property="lastName" column="lastName"/>
<result property="email" column="email"/>
</resultMap>
<select id="getById" resultMap="result">
select * from contact where id=#id#
</select>
</sqlMap>
为了执行resultMap例子,我们需要将下面的Java代码引入进来.
sqlMap.queryForObject("Contact.getById",new Integer(1));
这里我们传递值为1的id来显示所有该id的信息.
IbatisResultMap.java
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 IbatisResultMap{
public static void main(String[] args)
throws IOException,SQLException{
Reader reader = Resources.getResourceAsReader("SqlMapConfig.xml");
SqlMapClient sqlMap =
SqlMapClientBuilder.buildSqlMapClient(reader);
//Output all contacts
System.out.println("*------Information by Contact Id--------*");
Contact contact =
(Contact)sqlMap.queryForObject("Contact.getById",new Integer(1));
System.out.println("|Id = " + contact.getId());
System.out.println("|First Name = " + contact.getFirstName());
System.out.println("|Last Name = " + contact.getLastName());
System.out.println("|Email Id = " + contact.getEmail());
System.out.println("==========================================");
}
}
为了运行该程序 :
创建Contact.xml和SqlMapConfig.xml
创建并编译Contact.java
创建并编译IbatisResultMap.java
在执行IbatisResultMap类文件的时候,该id的所有信息将会显示出来 :
3 楼 zhou1220 2013-03-16 14:37
2 楼 xjappsoft 2011-12-12 18:54
1 楼 xjappsoft 2011-12-12 18:54