热门标签 | 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


推荐阅读
  • Python SQLAlchemy库的使用方法详解
    本文详细介绍了Python中使用SQLAlchemy库的方法。首先对SQLAlchemy进行了简介,包括其定义、适用的数据库类型等。然后讨论了SQLAlchemy提供的两种主要使用模式,即SQL表达式语言和ORM。针对不同的需求,给出了选择哪种模式的建议。最后,介绍了连接数据库的方法,包括创建SQLAlchemy引擎和执行SQL语句的接口。 ... [详细]
  • Spring特性实现接口多类的动态调用详解
    本文详细介绍了如何使用Spring特性实现接口多类的动态调用。通过对Spring IoC容器的基础类BeanFactory和ApplicationContext的介绍,以及getBeansOfType方法的应用,解决了在实际工作中遇到的接口及多个实现类的问题。同时,文章还提到了SPI使用的不便之处,并介绍了借助ApplicationContext实现需求的方法。阅读本文,你将了解到Spring特性的实现原理和实际应用方式。 ... [详细]
  • MyBatis多表查询与动态SQL使用
    本文介绍了MyBatis多表查询与动态SQL的使用方法,包括一对一查询和一对多查询。同时还介绍了动态SQL的使用,包括if标签、trim标签、where标签、set标签和foreach标签的用法。文章还提供了相关的配置信息和示例代码。 ... [详细]
  • r2dbc配置多数据源
    R2dbc配置多数据源问题根据官网配置r2dbc连接mysql多数据源所遇到的问题pom配置可以参考官网,不过我这样配置会报错我并没有这样配置将以下内容添加到pom.xml文件d ... [详细]
  • web.py开发web 第八章 Formalchemy 服务端验证方法
    本文介绍了在web.py开发中使用Formalchemy进行服务端表单数据验证的方法。以User表单为例,详细说明了对各字段的验证要求,包括必填、长度限制、唯一性等。同时介绍了如何自定义验证方法来实现验证唯一性和两个密码是否相等的功能。该文提供了相关代码示例。 ... [详细]
  • 2018深入java目标计划及学习内容
    本文介绍了作者在2018年的深入java目标计划,包括学习计划和工作中要用到的内容。作者计划学习的内容包括kafka、zookeeper、hbase、hdoop、spark、elasticsearch、solr、spring cloud、mysql、mybatis等。其中,作者对jvm的学习有一定了解,并计划通读《jvm》一书。此外,作者还提到了《HotSpot实战》和《高性能MySQL》等书籍。 ... [详细]
  • 本文介绍了在开发Android新闻App时,搭建本地服务器的步骤。通过使用XAMPP软件,可以一键式搭建起开发环境,包括Apache、MySQL、PHP、PERL。在本地服务器上新建数据库和表,并设置相应的属性。最后,给出了创建new表的SQL语句。这个教程适合初学者参考。 ... [详细]
  • 本文介绍了如何使用php限制数据库插入的条数并显示每次插入数据库之间的数据数目,以及避免重复提交的方法。同时还介绍了如何限制某一个数据库用户的并发连接数,以及设置数据库的连接数和连接超时时间的方法。最后提供了一些关于浏览器在线用户数和数据库连接数量比例的参考值。 ... [详细]
  • 原文地址:https:www.cnblogs.combaoyipSpringBoot_YML.html1.在springboot中,有两种配置文件,一种 ... [详细]
  • 本文介绍了在SpringBoot中集成thymeleaf前端模版的配置步骤,包括在application.properties配置文件中添加thymeleaf的配置信息,引入thymeleaf的jar包,以及创建PageController并添加index方法。 ... [详细]
  • 本文介绍了在Mac上搭建php环境后无法使用localhost连接mysql的问题,并通过将localhost替换为127.0.0.1或本机IP解决了该问题。文章解释了localhost和127.0.0.1的区别,指出了使用socket方式连接导致连接失败的原因。此外,还提供了相关链接供读者深入了解。 ... [详细]
  • ALTERTABLE通过更改、添加、除去列和约束,或者通过启用或禁用约束和触发器来更改表的定义。语法ALTERTABLEtable{[ALTERCOLUMNcolu ... [详细]
  • Oracle优化新常态的五大禁止及其性能隐患
    本文介绍了Oracle优化新常态中的五大禁止措施,包括禁止外键、禁止视图、禁止触发器、禁止存储过程和禁止JOB,并分析了这些禁止措施可能带来的性能隐患。文章还讨论了这些禁止措施在C/S架构和B/S架构中的不同应用情况,并提出了解决方案。 ... [详细]
  • Java学习笔记之使用反射+泛型构建通用DAO
    本文介绍了使用反射和泛型构建通用DAO的方法,通过减少代码冗余度来提高开发效率。通过示例说明了如何使用反射和泛型来实现对不同表的相同操作,从而避免重复编写相似的代码。该方法可以在Java学习中起到较大的帮助作用。 ... [详细]
  • 在Oracle11g以前版本中的的DataGuard物理备用数据库,可以以只读的方式打开数据库,但此时MediaRecovery利用日志进行数据同步的过 ... [详细]
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社区 版权所有