Spring+Ibatis处理1对多数据表的例子

本文介绍了一个使用MyBatis与Spring集成的例子,包括数据库脚本创建表与数据插入、domain对象定义、DAO接口及其实现、ibatis配置文件、Spring配置文件等内容,并通过测试代码验证了集成的有效性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

数据库脚本(MYsqL)

 

create table Orders (OrderId varchar(10primary key,Customer varchar(10));
create table OrderLines(OrderLineId varchar(10primary 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

评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值