SqlMapConfig.xml配置文件
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE sqlMapConfig PUBLIC "-//iBATIS.com//DTD SQL Map Config 2.0//EN"
"http://www.ibatis.com/dtd/sql-map-config-2.dtd">
<sqlMapConfig>
<settings cacheModelsEnabled="true" enhancementEnabled="true"
lazyLoadingEnabled="true" maxRequests="32" maxSessions="10"
maxTransactions="5" useStatementNamespaces="true"/>
<transactionManager type="JDBC">
<dataSource type="SIMPLE">
<property name="JDBC.Driver" value="com.mysql.jdbc.Driver"/>
<property name="JDBC.ConnectionURL" value="jdbc:mysql://localhost/ibatistest"/>
<property name="JDBC.Username" value="root"/>
<property name="JDBC.Password" value="123456"/>
</dataSource>
</transactionManager>
<sqlMap resource="Contact.xml"/>
</sqlMapConfig>
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="namespaceTest">
<resultMap id="contact" class="Contact">
<result property="id" column="id" nullValue="0"/>
<result property="firstName" column="firstName" nullValue=""/>
<result property="lastName" column="lastName" nullValue=""/>
<result property="email" column="email" nullValue=""/>
</resultMap>
<select id="getAll" resultMap="contact" >
<![CDATA[
select * from contact
]]>
</select>
</sqlMap>
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;
}
}
Test.java Demo 类
import com.ibatis.sqlmap.client.*;
import com.ibatis.common.resources.*;
import java.io.IOException;
import java.io.Reader;
import java.sql.SQLException;
import java.util.List;
public class Test {
@SuppressWarnings("unchecked")
public static void main(String[] args) throws SQLException {
String resource ="SqlMapConfig.xml";
Reader reader;
try {
reader = Resources.getResourceAsReader(resource);
SqlMapClient sqlMap = SqlMapClientBuilder.buildSqlMapClient(reader);
sqlMap.queryForList("namespaceTest.getAll", null);
List<Contact> list= (List<Contact>)sqlMap.queryForList("namespaceTest.getAll", null);
for(int i=0;i<list.size();i++){
System.out.println(list.get(i).getEmail());
}
}catch (IOException e) {
e.printStackTrace();
}
}
}