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;
}
}
分享到:
相关推荐
Spring与iBATIS的集成 iBATIS似乎已远离众说纷纭的OR框架之列,通常人们对非常流行的Hibernate情有独钟。但正如Spring A Developer's Notebook作者Bruce Tate 和Justin Gehtland所说的那样,与其他的OR框架相比...
ibatis学习 ibatis总结 ibatis ibatis ibatis
最近想在最新的Spring5.0中集成ibatis(不是mybatis),发现已经不在支持SqlmapClientTemplate和SqlmapClientFactoryBean,于是搞了这个工具jar来进行支持如下配置 ...
《iBATIS实战》是讲述iBATIS框架的权威著作。书中既详实地介绍了iBATIS的设计理念和基础知识,也讨论了动态SQL、高速缓存、DAD框架等高级主题,还讲解了iBATIS在实际开发中的应用。《iBATIS实战》的最后给出了一个...
iBatis 中使用安全的拼接语句、动态查询、大于、小于、等于 iBatis 是一个流行的持久层框架,提供了许多强大的功能来帮助开发者构建高效、安全的数据库交互应用程序。本文将详细介绍 iBatis 中使用安全的拼接语句、...
Manning.iBATIS.in.Action.Jan.2007.eBook-BBL.pdf MyBatis 3 User Guide Simplified Chinese.pdf MyBatis-3.0.3-Migrations.pdf MyBatis-3.0.3-User-Guide.pdf MyBatis-3-Migrations.doc MyBatis-3-Migrations.pdf ...
ibatis 乱码解决 ibatis乱码 gbk iso_8859_1 乱码
ibatis代码自动生成,ibatis代码自动生成,ibatis代码自动生成,ibatis代码自动生成
ibatis资料ibatis资料ibatis资料ibatis资料ibatis资料ibatis资料ibatis资料ibatis资料ibatis资料
ibatis demo,ibatis例子,ibatis示例
ibatis 拼接字符串
iBATIS 级联iBATIS 级联iBATIS 级联
Ibatis3,手册,Ibatis3手册,Ibatis3使用手册,Ibatis3参考手册
目前网上罕见的JSF+iBATIS+MySQL示例代码,公司某软件项目所需而做的前期DEMO,只有两个Web页面,用户登录和用户列表,但已经能够说明JSF+iBATIS的典型应用方式。 MyEclipse6.5 项目工程文件,内含SQL建库指令。 ...
ibatis-2.3.4.726.jar,ibatis-2.3.0.677.jar,ibatis-2.3.3.720.jar下载,都已放入一个压缩包下。
iBATIS的是一个持久层框架,它能够自动在 Java, .NET, 和Ruby on Rails中与SQL数据库和对象之间的映射。映射是从应用程序逻辑封装在XML配置文件中的SQL语句脱钩。 iBATIS是一个轻量级的框架和持久性API适合持久化的...
ibatis教程 输入参数详解 ibatis快速入门
自己写了一个Ibatis入门文件 JDK用的1.4 ibatis用的2.3.0 一定要注意版本问题,不然出现本本不兼容很费劲,我调了一上午,注意一定要用JDK1.4 ibatis2.3.0! 数据库自己建一张简单的表就行了,特别说明 只适合新手...
一直以来ibatis的分页都是通过滚动ResultSet实现的,应该算是逻辑分页吧。逻辑分页虽然能很干净地独立于特定数据库,但效率 在多数情况下不及特定数据库支持的物理分页,而hibernate的分页则是直接组装sql,充分...