ibatis使用


使用ibatis的十个理由,知道怎样操作10种以上的数据库
9 、可配置的caching(包括从属)
8、支持DataSource、local transaction managemen和global transaction
7、简单的XML配置文档
6、支持Map, Collection, List和简单类型包装(如Integer, String)
5、支持JavaBeans类(get/set 方法)
4、支持复杂的对象映射(如populating lists, complex object models)
3、对象模型从不完美(不需要修改)
2、数据模型从不完美(不需要修改)
1、你已经知道SQL,为什么还要学习其他东西



配至文件说明:DB.properties:

SimpleDriver=oracle.jdbc.OracleDriver
SimpleUrl=jdbc:oracle:thin:@10.0.0.5:1521:champion
SimpleUsername=root
SimplePassword=aa


sql-map-config-storedb.xml:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE sql-map-config
PUBLIC "-//iBATIS.com//DTD SQL Map Config 1.0//EN"
"http://www.ibatis.com/dtd/sql-map-config.dtd">

<sql-map-config>

<properties resource="properties/DB.properties" />

<settings
maxExecute="0"
maxExecutePerConnection="0"
maxTransactions="0"
cacheModelsEnabled="true"
statementCacheSize="175"
useBeansMetaClasses="false"
useGlobalTransactions="false" />

<datasource name="jpestoreSimple"
factory-class="com.ibatis.db.sqlmap.datasource.DbcpDataSourceFactory"
default="true" >
<property name="JDBC.Driver" value="${SimpleDriver}"/>
<property name="JDBC.ConnectionURL" value="${SimpleUrl}"/>
<property name="JDBC.Username" value="${SimpleUsername}"/>
<property name="JDBC.Password" value="${SimplePassword}"/>
<property name="Pool.MaximumActiveConnections" value="15"/>
<property name="Pool.MaximumIdleConnections" value="15"/>
<property name="Pool.MaximumWait" value="1000"/>
</datasource>

<sql-map resource="sqlmap/Category.xml" />

</sql-map-config>


这里真正实现了数据库连接,我们使用的是dbcp的连接池。JDBC的配置大家都很熟了。${SimpleDriver}就是指的前面DB.properties中的SimpleDriver的内容。


Category.xml:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE sql-map
PUBLIC "-//iBATIS.com//DTD SQL Map 1.0//EN"
"http://www.ibatis.com/dtd/sql-map.dtd">

<sql-map name="Category">

 

<result-map name="result" class="com.ewuxi.champion.persistence.vo.Category">
<property name="categoryId" column="CATID" columnIndex="1" />
<property name="name" column="NAME" columnIndex="2"/>
<property name="description" column="DESCN" columnIndex="3"/>
</result-map>

<mapped-statement name="findByPrimaryKeyCategoryDao" result-map="result">
select CATID, NAME, DESCN from CATEGORY where CATID = #categoryId#
</mapped-statement>

<dynamic-mapped-statement name="findCategoryDao" result-map="result">
select CATID, NAME, DESCN from CATEGORY
<dynamic prepend="where">
<isNotNull prepend="and" property="categoryId" >
CATID = #categoryId#
</isNotNull>
<isNotNull prepend="and" property="name" >
NAME = #name#
</isNotNull>
<isNotNull prepend="and" property="description">
DESCN = #description#
</isNotNull>
</dynamic>
</dynamic-mapped-statement>


<mapped-statement name="findCategoryDaoCount" result-class="java.lang.Integer">
select count(1) as value from CATEGORY
</mapped-statement>


<!-- =============================================
mapped-statement
============================================= -->

<dynamic-mapped-statement name="updateByPrimaryKeyCategoryDao">
update CATEGORY
<dynamic prepend="set">
<isNotNull prepend="," property="name" >
NAME = #name#
</isNotNull>
<isNotNull prepend="," property="description">
DESCN = #description#
</isNotNull>
</dynamic>
where
CATID =#categoryId#
</dynamic-mapped-statement>

<!-- =============================================
mapped-statement
============================================= -->

<mapped-statement name="deleteByPrimaryKeyCategoryDao">
delete from CATEGORY
where CATID =#categoryId#
</mapped-statement>


<!-- =============================================
OPTIONAL EXPLICIT PARAMETER MAP
============================================= -->

<parameter-map name="insert-params">
<property name="categoryId"/>
<property name="name" type="VARCHAR"/>
<property name="description" type="VARCHAR"/>
</parameter-map>

<!-- =============================================
MAPPED STATEMENTS - w/Explicit Parameter Map
============================================= -->

<mapped-statement name="insertCategoryDao" parameter-map="insert-params" >
insert into CATEGORY (
CATID,NAME,DESCN)
values (
?,?,?
)
</mapped-statement>
</sql-map>

上述文件就是真正的SQL所存在的地方。

你可能感兴趣的:(oracle,sql,xml,ibatis,jdbc)