數(shù)據(jù)庫(kù)結(jié)構(gòu)(mysql)
create table customerDetail(CustomerDetailId varchar(10) primary key,DataDetail varchar(10));
create table customers(id varchar(10) primary key,
firstname varchar(10),
lastname varchar(10),
customerDetail varchar(10),
customerGross varchar(10),
foreign key (customerDetail) references customerDetail(CustomerDetailId),
foreign key (customerGross) references customerDetail(CustomerDetailId));
insert into customerdetail values("1","detail1");
insert into customerdetail values("2","detail2");
insert into customerdetail values("3","detail3");
insert into customers values("1","firstname1","lastname1","1",null);
insert into customers values("2","firstname2","lastname2","2","3");
customer和customerDetail是1對(duì)1的關(guān)系
customer和customerGossIp是1對(duì)1的關(guān)系 domain對(duì)象:
package ch10.SpringAndIbatisOneToOne;
import java.util.List;
public class Customers ...{
private String customerId;
private String firstName;
private String lastName;
private CustomerDetail customerDetail;
private CustomerDetail customerGossIp;
public CustomerDetail getCustomerDetail() ...{
return customerDetail;
}
public void setCustomerDetail(CustomerDetail customerDetail) ...{
this.customerDetail = customerDetail;
}
public CustomerDetail getCustomerGossIp() ...{
return customerGossIp;
}
public void setCustomerGossIp(CustomerDetail customerGossIp) ...{
this.customerGossIp = customerGossIp;
}
public String getCustomerId() ...{
return customerId;
}
public void setCustomerId(String customerId) ...{
this.customerId = customerId;
}
public String getFirstName() ...{
return firstName;
}
public void setFirstName(String firstName) ...{
this.firstName = firstName;
}
public String getLastName() ...{
return lastName;
}
public void setLastName(String lastName) ...{
this.lastName = lastName;
}
}
package ch10.SpringAndIbatisOneToOne;
public class CustomerDetail ...{
private String customerDetailId;
private String dataDetail;
public String getCustomerDetailId() ...{
return customerDetailId;
}
public void setCustomerDetailId(String customerDetailId) ...{
this.customerDetailId = customerDetailId;
}
public String getDataDetail() ...{
return dataDetail;
}
public void setDataDetail(String dataDetail) ...{
this.dataDetail = dataDetail;
}
}
DAO接口:
package ch10.SpringAndIbatisOneToOne;
import java.util.List;
public interface IDAO ...{
public Object getCustomersById(String id);
}
DAO實(shí)現(xiàn)類:
package ch10.SpringAndIbatisOneToOne;
import java.util.List;
import org.springframework.orm.ibatis.support.SqlMapClientDaoSupport;
public class TestDAO extends SqlMapClientDaoSupport implements IDAO ...{
public Object getCustomersById(String id) ...{
return this.getSqlMapClientTemplate().queryForObject("getCustomerById",id);
}
}
Ibatis配置文件:
<?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>
<sqlMap resource="ch10/SpringAndIbatisOneToOne/Ibatis.xml" />
</sqlMapConfig>
ibatis sql map文件:
<?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 type="ch10.SpringAndIbatisOneToOne.Customers" alias="customer"/>
<typeAlias type="ch10.SpringAndIbatisOneToOne.CustomerDetail" alias="customerDetail"/>
<!-- 定義基本的resultMap -->
<resultMap class="customer" id="result">
<result property="customerId" column="CustomerId"/>
<result property="firstName" column="FirstName"/>
<result property="lastName" column="LastName"/>
</resultMap>
<resultMap class="customerDetail" id="gossipDetail">
<result property="customerDetailId" column="CustomerDetailId"/>
<result property="dataDetail" column="DataDetail"/>
</resultMap>
<!-- 使用如下這種集成關(guān)系的好處是我們可以只返回我們需要的數(shù)據(jù),而不是總返回全部數(shù)據(jù)
比如我們只關(guān)心customer,而不關(guān)心他其中的customerDetail和customerGossIp,那我們之用
名為result的resultMap就可以了 -->
<!-- 定義符合resultMap,這種配置表明了customer的customerDetail不能為空,而customerGross可以為空 >-->
<resultMap class="customer" id="resultDetail" extends="result">
<result property="customerDetail.customerDetailId" column="CustomerDetailID"/>
<result property="customerDetail.dataDetail" column="DataDetail"/>
<result property="customerGossIp" column="CustomerGossIp" select="getCustomerGossById"/>
</resultMap>
<select id="getCustomerGossById" resultMap="gossipDetail" parameterClass="string">
select CustomerDetailId,DataDetail from CustomerDetail where CustomerDetailId=#value#
</select>
<!-- 根據(jù)customerid獲得Customer-->
<select id="getCustomerById" resultMap="resultDetail" parameterClass="string">
select c.CustomerId as CustomerId,
c.FirstName as FirstName,
c.LastName as LastName,
c.CustomerDetail as CustomerDetail,
c.CustomerGossIp as CustomerGossIp,
cd.CustomerDetailID as CustomerDetailID,
cd.Datadetail as DataDetail
from customers c
inner join customerdetail cd on c.CustomerDetail=cd.CustomerDetailId
where c.CustomerId=#value#
</select>
</sqlMap>
spring配置文件
如果要使用1對(duì)1,1對(duì)多這樣牽扯兩個(gè)表的情況,一定要為SqlMapClientFactoryBean設(shè)置dataSource屬性
<?xml version="1.0" encoding="UTF-8"?>
<beans
xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName">
<value>com.mysql.jdbc.Driver</value>
</property>
<property name="username">
<value>root</value>
</property>
<property name="password">
<value>1234</value>
</property>
<property name="url">
<value>jdbc:mysql://localhost:3306/spring</value>
</property>
</bean>
<bean id="sqlMapClient" class="org.springframework.orm.ibatis.SqlMapClientFactoryBean">
<!-- 此處應(yīng)注入ibatis配置文件,而非sqlMap文件,否則會(huì)出現(xiàn)“there is no statement.....異常” -->
<property name="configLocation">
<value>ch10/SpringAndIbatisOneToOne/sqlMapConfig.xml</value>
</property>
<property name="dataSource">
<ref bean="dataSource"/>
</property>
</bean>
<bean id="testDAO" class="ch10.SpringAndIbatisOneToOne.TestDAO">
<property name="dataSource">
<ref bean="dataSource"/>
</property>
<property name="sqlMapClient">
<ref bean="sqlMapClient"/>
</property>
</bean>
</beans>
測(cè)試代碼:
package ch10.SpringAndIbatisOneToOne;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import ch10.SpringAndIbatis.Ibatis;
public class Test ...{
/** *//**
* @param args
*/
public static void main(String[] args) ...{
ApplicationContext context=new ClassPathXmlApplicationContext("ch10/SpringAndIbatisOneToOne/applicationContext.xml");
TestDAO testDAOImpl=(TestDAO)context.getBean("testDAO");
Customers result1=(Customers)testDAOImpl.getCustomersById("1");
Customers result2=(Customers)testDAOImpl.getCustomersById("2");
System.out.println(result1.getFirstName()+"-"+result1.getLastName()+"-"+result1.getCustomerDetail().getDataDetail()+"-"+result1.getCustomerGossIp().getDataDetail());
System.out.println("-----------------");
System.out.println(result2.getFirstName()+"-"+result2.getLastName()+"-"+result2.getCustomerDetail().getDataDetail()+"-"+result2.getCustomerGossIp().getDataDetail());
}
}
結(jié)果:
firstname1-lastname1-detail1-detail2
-----------------
firstname2-lastname2-detail2-detail3
from:http://blog.csdn.net/daryl715/archive/2007/08/28/1762439.aspx