热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

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

数据库脚本(MYsqL)createtableOrders(OrderIdvarchar(10)primarykey,Customervarchar(10));createtable

数据库脚本(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.Drivervalue>
  
property>
  
<property name="username">
    
<value>rootvalue>
  
property>
  
<property name="password">
    
<value>1234value>
  
property>
  
<property name="url">
    
<value>jdbc:mysql://localhost:3306/springvalue>
  
property>
bean>

<bean id="sqlMapClient" class="org.springframework.orm.ibatis.SqlMapClientFactoryBean">
  

  
<property name="configLocation">
     
<value>ch10/SpringAndIbatisOneToMany/sqlMapConfig.xmlvalue>
  
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


推荐阅读
  • eclipse学习(第三章:ssh中的Hibernate)——11.Hibernate的缓存(2级缓存,get和load)
    本文介绍了eclipse学习中的第三章内容,主要讲解了ssh中的Hibernate的缓存,包括2级缓存和get方法、load方法的区别。文章还涉及了项目实践和相关知识点的讲解。 ... [详细]
  • Spring特性实现接口多类的动态调用详解
    本文详细介绍了如何使用Spring特性实现接口多类的动态调用。通过对Spring IoC容器的基础类BeanFactory和ApplicationContext的介绍,以及getBeansOfType方法的应用,解决了在实际工作中遇到的接口及多个实现类的问题。同时,文章还提到了SPI使用的不便之处,并介绍了借助ApplicationContext实现需求的方法。阅读本文,你将了解到Spring特性的实现原理和实际应用方式。 ... [详细]
  • 本文详细介绍了Spring的JdbcTemplate的使用方法,包括执行存储过程、存储函数的call()方法,执行任何SQL语句的execute()方法,单个更新和批量更新的update()和batchUpdate()方法,以及单查和列表查询的query()和queryForXXX()方法。提供了经过测试的API供使用。 ... [详细]
  • 前景:当UI一个查询条件为多项选择,或录入多个条件的时候,比如查询所有名称里面包含以下动态条件,需要模糊查询里面每一项时比如是这样一个数组条件:newstring[]{兴业银行, ... [详细]
  • MyBatis多表查询与动态SQL使用
    本文介绍了MyBatis多表查询与动态SQL的使用方法,包括一对一查询和一对多查询。同时还介绍了动态SQL的使用,包括if标签、trim标签、where标签、set标签和foreach标签的用法。文章还提供了相关的配置信息和示例代码。 ... [详细]
  • SpringBoot uri统一权限管理的实现方法及步骤详解
    本文详细介绍了SpringBoot中实现uri统一权限管理的方法,包括表结构定义、自动统计URI并自动删除脏数据、程序启动加载等步骤。通过该方法可以提高系统的安全性,实现对系统任意接口的权限拦截验证。 ... [详细]
  • Java String与StringBuffer的区别及其应用场景
    本文主要介绍了Java中String和StringBuffer的区别,String是不可变的,而StringBuffer是可变的。StringBuffer在进行字符串处理时不生成新的对象,内存使用上要优于String类。因此,在需要频繁对字符串进行修改的情况下,使用StringBuffer更加适合。同时,文章还介绍了String和StringBuffer的应用场景。 ... [详细]
  • 高质量SQL书写的30条建议
    本文提供了30条关于优化SQL的建议,包括避免使用select *,使用具体字段,以及使用limit 1等。这些建议是基于实际开发经验总结出来的,旨在帮助读者优化SQL查询。 ... [详细]
  • ALTERTABLE通过更改、添加、除去列和约束,或者通过启用或禁用约束和触发器来更改表的定义。语法ALTERTABLEtable{[ALTERCOLUMNcolu ... [详细]
  • 本文介绍了iOS数据库Sqlite的SQL语句分类和常见约束关键字。SQL语句分为DDL、DML和DQL三种类型,其中DDL语句用于定义、删除和修改数据表,关键字包括create、drop和alter。常见约束关键字包括if not exists、if exists、primary key、autoincrement、not null和default。此外,还介绍了常见的数据库数据类型,包括integer、text和real。 ... [详细]
  • web.py开发web 第八章 Formalchemy 服务端验证方法
    本文介绍了在web.py开发中使用Formalchemy进行服务端表单数据验证的方法。以User表单为例,详细说明了对各字段的验证要求,包括必填、长度限制、唯一性等。同时介绍了如何自定义验证方法来实现验证唯一性和两个密码是否相等的功能。该文提供了相关代码示例。 ... [详细]
  • 一、Hadoop来历Hadoop的思想来源于Google在做搜索引擎的时候出现一个很大的问题就是这么多网页我如何才能以最快的速度来搜索到,由于这个问题Google发明 ... [详细]
  • 使用Ubuntu中的Python获取浏览器历史记录原文: ... [详细]
  • 本文介绍了Oracle数据库中tnsnames.ora文件的作用和配置方法。tnsnames.ora文件在数据库启动过程中会被读取,用于解析LOCAL_LISTENER,并且与侦听无关。文章还提供了配置LOCAL_LISTENER和1522端口的示例,并展示了listener.ora文件的内容。 ... [详细]
  • 本文介绍了一个在线急等问题解决方法,即如何统计数据库中某个字段下的所有数据,并将结果显示在文本框里。作者提到了自己是一个菜鸟,希望能够得到帮助。作者使用的是ACCESS数据库,并且给出了一个例子,希望得到的结果是560。作者还提到自己已经尝试了使用"select sum(字段2) from 表名"的语句,得到的结果是650,但不知道如何得到560。希望能够得到解决方案。 ... [详细]
author-avatar
-路人甲___
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有