Spring+Ibatis处理1对多数据表的例子
2007-08-28 17:26
393 查看
数据库脚本(MYsqL)
create table Orders (OrderId varchar(10) primary key,Customer varchar(10));
create table OrderLines(OrderLineId varchar(10) primary key,Product varchar(10),OrderId varchar(10),
foreign key(OrderId) references Orders(OrderId));
insert into Orders values("1","john");
insert into Orders values("2","marry");
insert into OrderLines values("1","computer","1");
insert into OrderLines values("2","bike","1");
insert into OrderLines values("3","cat","2");
insert into OrderLines values("4","dog","2");
insert into OrderLines values("5","basketball","2");
domain对象
package ch10.SpringAndIbatisOneToMany;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
public class Order implements Serializable...{
private String orderid;
private String customer;
private List orderLines;
public String getCustomer() ...{
return customer;
}
public void setCustomer(String customer) ...{
this.customer = customer;
}
public String getOrderid() ...{
return orderid;
}
public void setOrderid(String orderid) ...{
this.orderid = orderid;
}
public List getOrderLines() ...{
return orderLines;
}
public void setOrderLines(List orderLines) ...{
this.orderLines = orderLines;
}
}
package ch10.SpringAndIbatisOneToMany;
import java.io.Serializable;
public class OrderLine implements Serializable ...{
private String orderLineId;
private String product;
public OrderLine()...{
}
public OrderLine(String orderLineId, String product) ...{
super();
this.orderLineId = orderLineId;
this.product = product;
}
public String getOrderLineId() ...{
return orderLineId;
}
public void setOrderLineId(String orderLineId) ...{
this.orderLineId = orderLineId;
}
public String getProduct() ...{
return product;
}
public void setProduct(String product) ...{
this.product = product;
}
}
dao接口
package ch10.SpringAndIbatisOneToMany;
import java.util.List;
public interface IDAO ...{
public Object getOrderById(String id);
}
dao实现
package ch10.SpringAndIbatisOneToMany;
import java.util.List;
import org.springframework.orm.ibatis.support.SqlMapClientDaoSupport;
public class TestDAO extends SqlMapClientDaoSupport implements IDAO ...{
public Object getOrderById(String id) ...{
return this.getSqlMapClientTemplate().queryForObject("getOrderById",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/SpringAndIbatisOneToMany/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.SpringAndIbatisOneToMany.Order" alias="order"/>
<typeAlias type="ch10.SpringAndIbatisOneToMany.OrderLine" alias="order_line"/>
<resultMap class="order" id="result">
<result property="orderid" column="OrderId"/>
<result property="customer" column="Customer"/>
<result property="orderLines" select="getOrderLinesByOrder" column="OrderId"/>
</resultMap>
<resultMap class="order_line" id="resultLine">
<result property="orderLineId" column="orderLineId"/>
<result property="product" column="Product"/>
</resultMap>
<select id="getOrderLinesByOrder" resultMap="resultLine" parameterClass="string">select orderLineId,Product from orderlines where orderid=#value#</select>
<select id="getOrderById" resultMap="result" parameterClass="string">select OrderId,Customer,OrderId from Orders where OrderId=#value#</select>
</sqlMap>
Spring配置文件:
如果要使用1对1,1对多这样牵扯两个表的情况,一定要为SqlMapClientFactoryBean设置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">
<!-- 此处应注入ibatis配置文件,而非sqlMap文件,否则会出现“there is no statement.....异常” -->
<property name="configLocation">
<value>ch10/SpringAndIbatisOneToMany/sqlMapConfig.xml</value>
</property>
<property name="dataSource">
<ref bean="dataSource"/>
</property>
</bean>
<bean id="testDAO" class="ch10.SpringAndIbatisOneToMany.TestDAO">
<property name="dataSource">
<ref bean="dataSource"/>
</property>
<property name="sqlMapClient">
<ref bean="sqlMapClient"/>
</property>
</bean>
</beans>
测试代码:
package ch10.SpringAndIbatisOneToMany;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import org.omg.PortableInterceptor.SYSTEM_EXCEPTION;
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/SpringAndIbatisOneToMany/applicationContext.xml");
TestDAO testDAOImpl=(TestDAO)context.getBean("testDAO");
Order order=(Order)testDAOImpl.getOrderById("1");
System.out.println("Order info:");
System.out.println(order.getCustomer());
System.out.println("OrderLine info:");
List result=order.getOrderLines();
for (Iterator iter = result.iterator(); iter.hasNext();) ...{
OrderLine element = (OrderLine) iter.next();
System.out.println(element.getProduct());
}
}
}
结果:
Order info:
marry
OrderLine info:
cat
dog
basketball
create table Orders (OrderId varchar(10) primary key,Customer varchar(10));
create table OrderLines(OrderLineId varchar(10) primary key,Product varchar(10),OrderId varchar(10),
foreign key(OrderId) references Orders(OrderId));
insert into Orders values("1","john");
insert into Orders values("2","marry");
insert into OrderLines values("1","computer","1");
insert into OrderLines values("2","bike","1");
insert into OrderLines values("3","cat","2");
insert into OrderLines values("4","dog","2");
insert into OrderLines values("5","basketball","2");
domain对象
package ch10.SpringAndIbatisOneToMany;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
public class Order implements Serializable...{
private String orderid;
private String customer;
private List orderLines;
public String getCustomer() ...{
return customer;
}
public void setCustomer(String customer) ...{
this.customer = customer;
}
public String getOrderid() ...{
return orderid;
}
public void setOrderid(String orderid) ...{
this.orderid = orderid;
}
public List getOrderLines() ...{
return orderLines;
}
public void setOrderLines(List orderLines) ...{
this.orderLines = orderLines;
}
}
package ch10.SpringAndIbatisOneToMany;
import java.io.Serializable;
public class OrderLine implements Serializable ...{
private String orderLineId;
private String product;
public OrderLine()...{
}
public OrderLine(String orderLineId, String product) ...{
super();
this.orderLineId = orderLineId;
this.product = product;
}
public String getOrderLineId() ...{
return orderLineId;
}
public void setOrderLineId(String orderLineId) ...{
this.orderLineId = orderLineId;
}
public String getProduct() ...{
return product;
}
public void setProduct(String product) ...{
this.product = product;
}
}
dao接口
package ch10.SpringAndIbatisOneToMany;
import java.util.List;
public interface IDAO ...{
public Object getOrderById(String id);
}
dao实现
package ch10.SpringAndIbatisOneToMany;
import java.util.List;
import org.springframework.orm.ibatis.support.SqlMapClientDaoSupport;
public class TestDAO extends SqlMapClientDaoSupport implements IDAO ...{
public Object getOrderById(String id) ...{
return this.getSqlMapClientTemplate().queryForObject("getOrderById",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/SpringAndIbatisOneToMany/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.SpringAndIbatisOneToMany.Order" alias="order"/>
<typeAlias type="ch10.SpringAndIbatisOneToMany.OrderLine" alias="order_line"/>
<resultMap class="order" id="result">
<result property="orderid" column="OrderId"/>
<result property="customer" column="Customer"/>
<result property="orderLines" select="getOrderLinesByOrder" column="OrderId"/>
</resultMap>
<resultMap class="order_line" id="resultLine">
<result property="orderLineId" column="orderLineId"/>
<result property="product" column="Product"/>
</resultMap>
<select id="getOrderLinesByOrder" resultMap="resultLine" parameterClass="string">select orderLineId,Product from orderlines where orderid=#value#</select>
<select id="getOrderById" resultMap="result" parameterClass="string">select OrderId,Customer,OrderId from Orders where OrderId=#value#</select>
</sqlMap>
Spring配置文件:
如果要使用1对1,1对多这样牵扯两个表的情况,一定要为SqlMapClientFactoryBean设置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">
<!-- 此处应注入ibatis配置文件,而非sqlMap文件,否则会出现“there is no statement.....异常” -->
<property name="configLocation">
<value>ch10/SpringAndIbatisOneToMany/sqlMapConfig.xml</value>
</property>
<property name="dataSource">
<ref bean="dataSource"/>
</property>
</bean>
<bean id="testDAO" class="ch10.SpringAndIbatisOneToMany.TestDAO">
<property name="dataSource">
<ref bean="dataSource"/>
</property>
<property name="sqlMapClient">
<ref bean="sqlMapClient"/>
</property>
</bean>
</beans>
测试代码:
package ch10.SpringAndIbatisOneToMany;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import org.omg.PortableInterceptor.SYSTEM_EXCEPTION;
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/SpringAndIbatisOneToMany/applicationContext.xml");
TestDAO testDAOImpl=(TestDAO)context.getBean("testDAO");
Order order=(Order)testDAOImpl.getOrderById("1");
System.out.println("Order info:");
System.out.println(order.getCustomer());
System.out.println("OrderLine info:");
List result=order.getOrderLines();
for (Iterator iter = result.iterator(); iter.hasNext();) ...{
OrderLine element = (OrderLine) iter.next();
System.out.println(element.getProduct());
}
}
}
结果:
Order info:
marry
OrderLine info:
cat
dog
basketball
相关文章推荐
- Spring+Ibatis处理1对1数据表的例子
- tensorflow中的seq2seq例子为什么需要bucket? 有关tensorflow 的sequence2sequence例子,为什么需要用到buckets,rnn不是可以处理任意长度的数据吗
- Spring+Ibatis 多DataSource处理
- java读取文件数据进行一些简单处理的例子
- 如何处理原始文本数据(CNN情感分析yoom例子二)
- java读取文件数据进行一些简单处理的例子
- 原生JS的ajax处理json数据格式的异步请求完整例子
- 数据降维处理:PCA之特征值分解法例子解析
- netty处理客户端传过来的get、post、websocket数据例子
- ibatis中处理数据的方式(特殊)
- 使用pandas处理数据并绘图的例子
- ibatis处理数据,更新数据后服务器需重启才能查询
- 处理注册信息的小例子(bundle传递数据)
- 使用 ibatis 处理复杂对象数据关系的实例
- Oracle中的LOB数据类型以及ibatis中处理该类型的typeHandler
- matlab批处理多个文件数据及保存多个输出图像的方法及例子
- AJAX 调用 WEBSERVICE的例子,包括对XML数据的处理,包含源码下载
- 数据分页处理方法汇总(例子)
- 数据分页处理方法汇总(例子)
- 一个jdbc的例子(包含sql语句的批处理,事务处理,数据绑定prepare)