`
vtyi
  • 浏览: 84361 次
  • 性别: Icon_minigender_1
  • 来自: 深圳
社区版块
存档分类
最新评论

ibatis

阅读更多

sql-map-config.xml

 

<?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>
 <settings cacheModelsEnabled="true"
     enhancementEnabled="true"
     lazyLoadingEnabled="true"
     maxRequests="32"
     maxSessions="10"
     maxTransactions="5"
     useStatementNamespaces="false" />
 <transactionManager type="JDBC" >
  <dataSource type="SIMPLE">
  <property name="JDBC.Driver" value="oracle.jdbc.driver.OracleDriver"/>
  <property  name="JDBC.ConnectionURL" value="jdbc:oracle:thin:@127.0.0.1:1521:orcl" />
  <property name="JDBC.Username" value="vtyi" />
  <property name="JDBC.Password" value="vtyi" />
  </dataSource>
 </transactionManager>
 <sqlMap resource="Account.xml" />
 <sqlMap resource="Product.xml" />
</sqlMapConfig>
 

 

Account.xml

 

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE sqlMap
  PUBLIC "-//iBATIS.com//DTD SQL Map 2.0//EN"
  "http://www.ibatis.com/dtd/sql-map-2.dtd">
<sqlMap namespace="Account">
<select id="getAccount" parameterClass="java.lang.Integer" resultClass="test.Account">
 select * from account where id= #id#
</select>

<insert id="insertAccount" parameterClass="test.Account">
 
 <selectKey resultClass="int" keyProperty="id">
  select accountSequence.nextval as id from dual
 </selectKey>
 
 insert into Account values(#id#,#name#,#subid#,#address#)
</insert>

<!-- 
<parameterMap id="insert_param" class="test.Account">
 <parameter property="id" jdbcType="NUMBER" javaType="java.lang.Integer" nullValue="0" />
 <parameter property="name" jdbcType="VARCHAR" javaType="java.lang.String" nullValue="aa" />
</parameterMap>
<statement id="insert_statement" parameterMap="insert_param">
 insert into Account(id,name) values(?,?);
</statement>
<parameterMap id="parameterMap" class="map">
 <parameter property="name" jdbcType="VARCHAR" javaType="java.lang.String" mode="INOUT" nullValue="aa">
 </parameter>
</parameterMap>
<procedure id="procedureMap" parameterMap="parameterMap">
 {call swap(?)}
</procedure>
-->
<resultMap id="one_many" class="test.Account">
 <result property="id" column="id" />
 <result property="name" column="name" />
 <result property="subid" column="subid" />
 <result property="address" column="address" />
 <!-- n+1
 <result property="address" column="address" />
 <result property="subid" column="subid" select="getProduct" />
  -->
 <result property="product.id" column="id" />
 <result property="product.name" column="name" />
</resultMap>
<!-- n+1
<resultMap id="product_result" class="test.Product" >
 <result property="id" column="id" />
 <result property="name" column="name" />
</resultMap>
-->
<select id="getAccountAndProduct" parameterClass="java.lang.Integer" resultMap="one_many">
 select * from Account ,Product  where Account.id= Product.id and Account.id=#id#
</select>
<!-- 
<select id="getProduct" parameterClass="java.lang.Integer" resultMap="product_result">
 select * from Product where id= #id#
</select>
-->

<cacheModel id="cache_one" type="MEMORY"><!-- FIFO  OSCACHE-->
 <flushInterval hours="24" />
 <flushOnExecute statement="insertAccount" />
 
 <property name="reference-type" value="WEAK" /><!-- STRONG,SOFT -->
</cacheModel>
<cacheModel id="cache_two" type="LRU" readOnly="true" serialize="true">
 <flushInterval hours="24" />
 <flushOnExecute statement="insertAccount" />

 <property name="cache-size" value="1000" />
</cacheModel>

<!-- dynamic -->
<select id="dynamic_sql" cacheModel="cache_two" resultMap="one_many">
 select * from Account
 <isGreaterThan prepend="and" property="id" compareValue="0" >
  where id=#id#
 </isGreaterThan>
 order by id
</select>
<statement id="dynamicGetAccountList" resultMap="one_many" >
 select * from ACCOUNT
 <dynamic prepend="WHERE">
 <isNotNull prepend="AND" property="firstName">
  ( ACC_FIRST_NAME = #firstName#
 <isNotNull prepend="OR" property="lastName">
  ACC_LAST_NAME = #lastName#
 </isNotNull>
  )
 </isNotNull>
 <isNotNull prepend="AND" property="emailAddress">
  ACC_EMAIL like #emailAddress#
 </isNotNull>
 <isGreaterThan prepend="AND" property="id" compareValue="0">
  ACC_ID = #id#
 </isGreaterThan>
 </dynamic>
 order by ACC_LAST_NAME
</statement>

<statement id="someName" resultMap="one_many" >
 select * from ACCOUNT
 <dynamic prepend="where">
 <isGreaterThan prepend="and" property="id" compareValue="0">
 ACC_ID = #id#
 </isGreaterThan>
 <isNotNull prepend="and" property="lastName">
 ACC_LAST_NAME = #lastName#
 </isNotNull>
 </dynamic>
 order by ACC_LAST_NAME
</statement>

<!-- xml -->
<select id="getByIdValueXml" resultClass="xml" xmlResultName="account" >
 select * from Account where id= #id#
</select>
</sqlMap>

 

Account.java

 

package test;

public class Account {

 private Integer id;
 private String name;
 private Integer subid;
 private String address;
 private Product product;
 public Integer getId() {
  return id;
 }
 public void setId(Integer id) {
  this.id = id;
 }
 public String getName() {
  return name;
 }
 public void setName(String name) {
  this.name = name;
 }
 public Integer getSubid() {
  return subid;
 }
 public void setSubid(Integer subid) {
  this.subid = subid;
 }
 public String getAddress() {
  return address;
 }
 public void setAddress(String address) {
  this.address = address;
 }
 
 public Product getProduct() {
  return product;
 }
 public void setProduct(Product product) {
  this.product = product;
 }
 @Override
 public String toString(){
  return "id= "+id+" name= "+name+" subid= "+subid+" address= "+address+
  " product={ "+product+" }";
 }
 
}

Product.java

 

package test;

public class Product {

 private Integer id;
 private String name;
 public Integer getId() {
  return id;
 }
 public void setId(Integer id) {
  this.id = id;
 }
 public String getName() {
  return name;
 }
 public void setName(String name) {
  this.name = name;
 }
 @Override
 public String toString(){
  return "id= "+id+" name= "+name;
 }
}

 

Ibatis.java

 

package test;

import java.beans.Introspector;
import java.beans.PropertyDescriptor;
import java.io.Reader;
import java.util.List;

import com.ibatis.common.resources.Resources;
import com.ibatis.sqlmap.client.SqlMapClient;
import com.ibatis.sqlmap.client.SqlMapClientBuilder;

public class Ibatis {

 /**
  * @param args
  */
 public static void main(String[] args) throws Exception {
  
  String resource="sql-map-config.xml";
  System.out.println(Ibatis.class.getClassLoader().getResource("E:\\workspace\\test\\src\\sql-map-config.xml"));
  Reader reader=Resources.getResourceAsReader(resource);
  SqlMapClient sqlMap=SqlMapClientBuilder.buildSqlMapClient(reader);
//  List list=sqlMap.queryForList("getAccount",1);
  List list=sqlMap.queryForList("getAccountAndProduct",1);
  if(list!=null && !list.isEmpty()){
   for(int i=0;i<list.size();i++){
    Account acc=(Account)list.get(i);
    System.out.println(acc);
   }
  }
  Account acc=new Account();
  acc.setAddress("深圳3");
//  acc.setId(new Integer(2));
  acc.setSubid(new Integer(3));
  acc.setName("cccca");
  sqlMap.insert("insertAccount", acc);
  
  String xml=(String)sqlMap.queryForObject("getByIdValueXml", new Integer(1));
  System.out.println(xml);
 }

 public String convertToXml(Object o,Class c,String name) throws Exception{
  StringBuffer sb=new StringBuffer("");
  if(o.getClass().isAssignableFrom(c)){
   PropertyDescriptor[] pd=Introspector.getBeanInfo(c).getPropertyDescriptors();
   if(pd.length>0){
     sb.append("<"+name+">");
     for(int i=0;i<pd.length;i++){
     
     }
   }
  }
  return null;
 }
}

 

分享到:
评论

相关推荐

    iBATIS-DAO-2.3.4.726.rar_com.ibatis.dao_iBATIS dao 2_iBatis DAO_

    iBATIS DAO是Java开发中的一个持久层框架,它提供了数据访问对象(DAO)的实现,使得数据库操作更加简单和灵活。在这个2.3.4.726版本的源码中,我们可以深入理解iBATIS DAO的工作原理,并通过添加注释来帮助我们更好...

    Spring与iBATIS的集成

    Spring与iBATIS的集成 iBATIS似乎已远离众说纷纭的OR框架之列,通常人们对非常流行的Hibernate情有独钟。但正如Spring A Developer's Notebook作者Bruce Tate 和Justin Gehtland所说的那样,与其他的OR框架相比...

    ibatis总结 ibatis ibatis ibatis ibatis

    Ibatis 是一款轻量级的Java持久层框架,它允许开发者将SQL语句与Java代码分离,从而使得数据库访问更加灵活、易于维护。本篇文章将深入探讨Ibatis的核心概念、最佳实践以及与其他框架如Struts和Spring的整合。 1. ...

    iBATIS2.3.4 jar包及源码

    iBATIS 是一款著名的开源Java持久层框架,它在2005年由Clinton Begin创建,最初命名为Apache MyBatis,后来发展为独立的项目。在本主题中,我们关注的是iBATIS 2.3.4版本的jar包及其源码。 首先,`ibatis-2.3.4.jar...

    ibatis demo,ibatis例子,ibatis示例

    Ibatis,全称为MyBatis,是一个优秀的Java持久层框架,它主要负责SQL映射,使得开发者能够将SQL语句与Java代码分离,从而更好地管理数据库操作。Ibatis的出现,解决了传统JDBC中手动编写SQL和结果集映射的繁琐工作,...

    ibatis乱码解决方法(ibatis独立)

    在IT行业中,数据库操作是必不可少的一部分,而Ibatis作为一款流行的Java持久层框架,它使得SQL与Java代码更好地结合,提供了灵活的数据库操作方式。然而,数据处理时遇到的编码问题,如乱码,常常会给开发者带来...

    ibatis应对批量update

    ### ibatis应对批量update 在处理大量数据更新时,传统的for循环方式往往会导致效率低下。这是因为每一次循环都需要执行一次数据库连接操作,对于批量更新来说,这样的处理方式显然不够高效。为了解决这个问题,...

    ibatis官方资料及所需要的Jar包

    Ibatis官方资料大全提供了全面的学习资源,包括ibatis-common.jar、ibatis-Dao.jar和ibatis-sqlmap.jar等核心组件的详细讲解和相关jar文件,是学习和使用Ibatis不可或缺的参考资料。 首先,我们来了解一下ibatis-...

    ibatis 自己学的一个ibatis项目(只是打通了Oracle) 非常适合入门

    自己写了一个Ibatis入门文件 JDK用的1.4 ibatis用的2.3.0 一定要注意版本问题,不然出现本本不兼容很费劲,我调了一上午,注意一定要用JDK1.4 ibatis2.3.0! 数据库自己建一张简单的表就行了,特别说明 只适合新手...

    ibatis_with_memcached

    本项目"ibatis_with_memcached"就是关于如何将Ibatis与Memcached集成,实现高效的数据库缓存策略的实例。 Ibatis是一个基于Java的SQL映射框架,它允许开发者编写SQL语句并与Java对象进行绑定,从而避免了传统的JDBC...

    ibatis教程 输入参数详解

    标题:ibatis教程 输入参数详解 描述:ibatis教程 输入参数详解 ibatis快速入门 标签:ibatis list 部分内容:这段部分提供了ibatis用户指南的警告,关于从文档复制代码的问题,以及ibatis的概述、如何开始使用...

    maven整合ibatis的简单例子

    在Java开发中,Maven和iBatis是两个非常重要的工具。Maven是一个项目管理和综合工具,它帮助开发者管理依赖、构建项目,并提供了一种标准化的项目结构。而iBatis则是一个SQL映射框架,它将SQL语句与Java代码分离,...

    Ibatis3手册 Ibatis3参考手册

    ### Ibatis3手册知识点概述 Ibatis3作为一款流行的持久层框架,在软件开发领域具有重要的地位。本篇文章基于“Ibatis3手册 Ibatis3参考手册”的标题及描述,深入解析Ibatis3的核心概念、架构特点以及如何进行实际...

    Ibatis 入门经典 实例

    《Ibatis 入门经典 实例》 Ibatis 是一款著名的轻量级 Java 持久层框架,它提供了一种映射 SQL 和 Java 对象的简单方式,从而减轻了开发人员在数据库操作中的工作负担。这篇实例教程将带你深入理解 Ibatis 的核心...

    ibatis拼接字符串

    在本篇文章中,我们将深入探讨如何使用 Java 编程语言结合 iBatis 框架进行 SQL 字符串的动态拼接。通过分析提供的代码片段,我们可以了解到在实际开发过程中,这种动态 SQL 的构建方式非常常见,尤其是在处理复杂的...

    spring ibatis整合所需jar包

    在Java Web开发中,Spring和iBatis是两个非常重要的框架。Spring是一个全面的后端开发框架,提供了依赖注入、AOP(面向切面编程)、事务管理等特性,而iBatis则是一个优秀的持久层框架,它将SQL语句与Java代码分离,...

    Ibatis入门例子,Ibatis教程

    Ibatis,全称为MyBatis,是一个优秀的Java持久层框架,它主要负责SQL映射,使得开发者能够将注意力集中在编写SQL语句上,而无需关注JDBC代码的编写。Ibatis消除了几乎所有的JDBC代码和手动设置参数以及获取结果集的...

    ibatis-2.3.0.677.jar

    标题 "ibatis-2.3.0.677.jar" 指向的是一个特定版本的 iBATIS 库,即版本号为 2.3.0.677 的 JAR 文件。iBATIS 是一个开源的 Java 框架,主要用于简化数据库与应用程序之间的交互。它在早期广泛应用于企业级应用开发...

    ibatis api,ibatis文档,ibatis说明文档

    Ibatis,全称为MyBatis,是一个优秀的Java持久层框架,它主要负责SQL映射,将数据库操作与业务逻辑解耦,使得开发者可以更专注于业务逻辑的实现,而无需关心繁琐的SQL语句编写。本篇文章将深入探讨Ibatis API、文档...

    ibatis教程,ibatis帮助文档

    iBATIS是一个由Clinton Begin创建,目前由Apache基金会支持的持久化框架,它专注于数据库查询的简化,尤其在JDBC编程方面提供了极大的便利。相对于全自动化ORM解决方案如Hibernate,iBATIS被称为“半自动化”ORM实现...

Global site tag (gtag.js) - Google Analytics