写在前面的首先你得要了解ibatis框架
我就不说了。推荐你看看 《iBATIS-SqlMaps-2_cn.pdf》
一。javaBean
____________________________Admin_______________________________
package com.ibatisDemo.model;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
public class Admin implements Serializable{
private Integer id;
private String name;
private String password;
private Integer qx;
private List adminlogs=new ArrayList(0);
public List getAdminlogs() {
return adminlogs;
}
public void setAdminlogs(List adminlogs) {
this.adminlogs = adminlogs;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public Integer getQx() {
return qx;
}
public void setQx(Integer qx) {
this.qx = qx;
}
}
____________________________AdminLog____________________________
package com.ibatisDemo.model;
import java.io.Serializable;
public class AdminLog implements Serializable{
private Integer id;
private Admin admin;
private String value;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public Admin getAdmin() {
return admin;
}
public void setAdmin(Admin admin) {
this.admin = admin;
}
public String getValue() {
return value;
}
public void setValue(String value) {
this.value = value;
}
}
二。admin.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="t_admin">
<typeAlias alias="Admin" type="com.ibatisDemo.model.Admin" />
<cacheModel type="LRU" id="adminCache">
<flushInterval hours="24"></flushInterval>
<flushOnExecute statement="updateAuthor"/>
<property value="1000" name="size"/>
</cacheModel>
<resultMap id="adminResult" class="Admin" >
<result column="id" property="id"/>
<result column="name" property="name"/>
<result column="password" property="password" />
<result column="qx" property="qx"/>
<result column="id" property="adminlogs" select="t_admin.getAdminlogList"/>
</resultMap>
<resultMap id="getAdminLog" class="com.ibatisDemo.model.AdminLog" >
<result column="id" property="id"/>
<result column="value" property="value"/>
<result column="admin_id" property="admin" select="t_admin.getAdminForAdminLog"/>
</resultMap>
<select id="getAdmin" resultMap="adminResult">
select * from admin
<dynamic prepend="where">
<isNotNull >
id=#id#
</isNotNull>
</dynamic>
</select>
<statement id="getAdminlogList" resultMap="getAdminLog">
select * from adminlog
<dynamic prepend="where">
<isNotNull >
admin_id=#id#
</isNotNull>
</dynamic>
</statement>
<statement id="getAdminForAdminLog" resultClass="Admin">
select * from admin
<dynamic prepend="where">
<isNotNull >
id=#value#
</isNotNull>
</dynamic>
</statement>
</sqlMap>
三。配置sql-map.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>
<properties resource="jdbc.properties" />
<settings
useStatementNamespaces="true"
cacheModelsEnabled="false"
enhancementEnabled="false"
lazyLoadingEnabled="false" />
<transactionManager type="JDBC" >
<dataSource type="SIMPLE">
<property name="JDBC.Driver" value="${jdbc.driverClassName}"/>
<property name="JDBC.ConnectionURL" value="${jdbc.url}"/>
<property name="JDBC.Username" value="${jdbc.username}"/>
<property name="JDBC.Password" value="${jdbc.password}"/>
<property name="JDBC.DefaultAutoCommit" value="true" />
<property name="Pool.MaximumActiveConnections" value="10"/>
<property name="Pool.MaximumIdleConnections" value="5"/>
<property name="Pool.MaximumCheckoutTime" value="120000"/>
<property name="Pool.TimeToWait" value="500"/>
<property name="Pool.PingQuery" value="select 1 from admin"/>
<property name="Pool.PingEnabled" value="false"/>
<property name="Pool.PingConnectionsOlderThan" value="1"/>
<property name="Pool.PingConnectionsNotUsedFor" value="1"/>
</dataSource>
</transactionManager>
<!-- =================basic model=================== -->
<sqlMap resource="com/ibatisDemo/model/sqlMap/admin.xml" />
</sqlMapConfig>
四。测试类
package com.ibatisDemo.test;
import java.io.IOException;
import java.io.Reader;
import java.net.URL;
import java.sql.SQLException;
import java.util.List;
import com.ibatis.common.resources.Resources;
import com.ibatis.sqlmap.client.SqlMapClient;
import com.ibatis.sqlmap.client.SqlMapClientBuilder;
public class TestMain {
//初始化SqlMapClient
private static SqlMapClient sqlmapclient;
public static SqlMapClient getInstance(){
//返回sqlmapclient,SqlMapClient是ibatis的核心主建,提供数据操作的基础平台
try {
String resource="sql-map-config.xml";
//读取ibatis配置文件
Reader reader=Resources.getResourceAsReader(resource);
//通过SqlMapClientBuilder创建SqlMapClient
sqlmapclient=SqlMapClientBuilder.buildSqlMapClient(reader);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
System.out.println("找不到SqlMapConfig.xml文件~~");
}
return sqlmapclient;
}
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
TestMain.getInstance();
try {
//List list=TestMain.sqlmapclient.queryForList("t_admin.getAdmin");
List list2=TestMain.sqlmapclient.queryForList("t_admin.getAdminlogList",null);
System.out.println("sdfdsf");
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}