sqlMap文件
<?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 namespace="Account">
<typeAlias alias="Account" type="test.Account"/>
<!--列表查询,返回Account Object的List-->
<resultMap id="AccountResult" class="Account">
<result property="id" column="ID"/>
<result property="firstName" column="FIRST_NAME"/>
<result property="lastName" column="LAST_NAME"/>
<result property="emailAddress" column="EMAIL"/>
</resultMap>
<select id="selectAllAccounts" resultMap="AccountResult">
select * from ACCOUNT
</select>
<!--列表查询,返回HashMap的List -->
<!--resultMap无需再通过JDBC ResultSetMetaData 来动态获取字段信息,性能有提高-->
<resultMap id="mapResult" class="java.util.HashMap">
<result property="roleid" column="ROLEID"/>
<result property="rolename" column="ROLENAME"/>
<result property="id" column="ID"/>
<result property="firstName" column="FIRST_NAME"/>
<result property="lastName" column="LAST_NAME"/>
<result property="emailAddress" column="EMAIL"/>
<result property="dt" column="DT"/>
</resultMap>
<select id="selectAllAccountsWithMap" resultMap="mapResult">
select B.ROLEID, B.ROLENAME, A.ID, A.FIRST_NAME,A.LAST_NAME,A.EMAIL,A.DT
from ACCOUNT A left outer join ROLE B on A.ROLEID = B.ROLEID
ORDER BY A.ID DESC
</select>
<!--动态构造查询条件-->
<select id="getUsers" parameterClass="user" resultMap="get-user-result">
Select id,name,sex from t_user
<dynamic prepend="WHERE">
<isNotEmpty prepend="AND" property="name">
(name like #name#)
</isNotEmpty>
<isNotEmpty prepend="AND" property="address">
(address like #address#)
</isNotEmpty>
</dynamic>
</select>
<isNotEmpty prepend="AND" property="name">
( name=#name#
<isNotEmpty prepend="AND" property="address">
address=#address#
</isNotEmpty>
)
</isNotEmpty>
<select id="dynamicGetAccountList" resultMap="account-result" >
select * from ACCOUNT
<dynamic prepend="WHERE">
<isNotNull prepend="AND" property="firstName" open=”(“ close=”)”>
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
</select>
<isParameterPresent> <isNotParameterPresent> <isNull> <isNotNull> <isEmpty> <isNotEmpty>
<isEqual> <isNotEqual> <isGreaterThan> <isGreaterEqual> <isLessThan> <isLessEqual>
<!-- Sql片段的是用-->
<sql id="selectItem_fragment">
FROM items WHERE parentid = 6
</sql>
<select id="selectItemCount" resultClass="int">
SELECT COUNT(*) AS total
<include refid="selectItem_fragment"/>
</select>
<select id="selectItems" resultClass="Item">
SELECT id, name
<include refid="selectItem_fragment"/>
</select>
<!--缓存-->
<cacheModel id="product-cache" type="LRU">
<flushInterval hours="24"/>
<flushOnExecute statement="insertProduct"/>
<flushOnExecute statement="updateProduct"/>
<flushOnExecute statement="deleteProduct"/>
<property name=”size” value=”1000” />
</cacheModel>
<select id=”getProductList” parameterClass=”int” cacheModel=”product-cache”>
select * from PRODUCT where PRD_CAT_ID = #value#
</select>
<!—对XML支持 -->
<select id="getPerson" parameterClass=”int” resultClass="xml" xmlResultName=”person”>
SELECT
PER_ID as id,
PER_FIRST_NAME as firstName,
PER_LAST_NAME as lastName,
PER_BIRTH_DATE as birthDate,
PER_WEIGHT_KG as weightInKilograms,
PER_HEIGHT_M as heightInMeters
FROM PERSON
WHERE PER_ID = #value#
</select>
<person>
<id>1</id>
<firstName>Clinton</firstName>
<lastName>Begin</lastName>
<birthDate>1900-01-01</birthDate>
<weightInKilograms>89</weightInKilograms>
<heightInMeters>1.77</heightInMeters>
</person>
<!—字符串替换
select * from $tableName$
Important Note 1: This support will only substitute Strings, so it is not appropriate for complex data types like Date or Timestamp.
Important Note 2: If you use this support to alter a table name, or a column list, in an SQL select statement,then you should always specify remapResults=“true”
-->.
<!—关联查询方式,有1/N问题-->
<sqlMap namespace="User">
<typeAlias alias="user" type="com.ibatis.sample.User"/>
<typeAlias alias="address" type="com.ibatis.sample.Address"/>
<resultMap id="get-user-result" class="user">
<result property="id" column="id"/>
<result property="name" column="name"/>
<result property="sex" column="sex"/>
<result property="addresses" column="id" select="User.getAddressByUserId"/>
</resultMap>
<select id="getUsers" parameterClass="java.lang.String" resultMap="get-user-result">
<![CDATA[Select id,name,sex from t_user where id = #id#]]>
</select>
<select id="getAddressByUserId" parameterClass="int" resultClass="address">
<![CDATA[select address,zipcode from t_address where user_id = #userid# ]]>
</select>
</sqlMap>
<resultMap id="get-user-result" class="user">
<result property="id" column="id"/>
<result property="name" column="name"/>
<result property="sex" column="sex"/>
<result property="address" column="t_address.address"/>
<result property="zipCode" column="t_address.zipcode"/>
</resultMap>
<select id="getUsers" parameterClass="java.lang.String" resultMap="get-user-result">
<![CDATA[select* from t_user,t_address where t_user.id=t_address.user_id]]>
</select>
保证User 类中包含address和zipCode两个String型属性。
<!—关联查询,无1/N问题-->
<resultMap id="AccountResultWithRole" class="Account" groupBy="id">
<result property="id" column="ID"/>
<result property="firstName" column="FIRST_NAME"/>
<result property="lastName" column="LAST_NAME"/>
<result property="emailAddress" column="EMAIL"/>
<result property="role" resultMap="Account.roleResult"/>
</resultMap>
<resultMap id="roleResult" class="test.Role">
<result property="roleid" column="ROLEID"/>
<result property="rolename" column="ROLENAME"/>
</resultMap>
<select id="selectAccountByIdWithRole" parameterClass="int" resultMap="AccountResultWithRole">
select B.ROLEID, B.ROLENAME, A.ID, A.FIRST_NAME,A.LAST_NAME,A.EMAIL from ACCOUNT A left outer join ROLE B on A.ROLEID = B.ROLEID where A.ID = #id#
</select>
<!--查询-->
<select id="selectAccountById" parameterClass="int" resultClass="Account">
select ID as id,FIRST_NAME as firstName,LAST_NAME as lastName, EMAIL as emailAddress from ACCOUNT where ID = #id#
</select>
<!--新增-->
<insert id="insertAccount" parameterClass="Account">
insert into ACCOUNT (FIRST_NAME,LAST_NAME,EMAIL,PID,DT)
values (#firstName:VARCHAR#, #lastName:VARCHAR#, #emailAddress:VARCHAR#,#pid:INTEGER:0#,#dt:TIME#)
<selectKey resultClass="int" type="post" keyProperty="id">
SELECT @@IDENTITY AS ID
</selectKey>
</insert>
<!--更新-->
<update id="updateAccount" parameterClass="Account">
update ACCOUNT set
FIRST_NAME = #firstName:VARCHAR#,
LAST_NAME = #lastName:VARCHAR#,
EMAIL = #emailAddress:VARCHAR#
where
ID = #id#
</update>
<!--删除-->
<delete id="deleteAccountById" parameterClass="int">
delete from ACCOUNT where ID = #id#
</delete>
<!--存储过程,如果没有返回列表,procTest的resultMap可以省略-->
<parameterMap id="procParamMap" class="java.util.HashMap" >
<parameter property="id" jdbcType="INTEGER" javaType="java.lang.Integer" mode="IN"/>
<parameter property="outid" jdbcType="INTEGER" javaType="java.lang.Integer" mode="OUT"/>
<parameter property="errMsg" jdbcType="VARCHAR" javaType="java.lang.String" mode="OUT"/>
</parameterMap>
<resultMap id="procResultMap" class="java.util.HashMap" >
<result property="a" column="AAA"/>
<result property="b" column="BBB"/>
<result property="c" column="CCC"/>
</resultMap>
<procedure id="procTest" parameterMap="procParamMap" resultMap="procResultMap">
{call test_sp_1 (?,?,?)}
</procedure>
</sqlMap>
java 代码
public class TestDao extends SqlMapClientDaoSupport {
public List selectAllAccounts() throws SQLException {
return getSqlMapClientTemplate().queryForList("selectAllAccounts");
}
public List selectAllAccountsWithMap() throws SQLException {
return getSqlMapClientTemplate().queryForList(
"selectAllAccountsWithMap");
}
public Account selectAccountById(int id) throws SQLException {
return (Account) getSqlMapClientTemplate().queryForObject(
"selectAccountById", new Integer(id));
}
public List procTest(Map params) throws Exception {
List ret = (List) getSqlMapClientTemplate().queryForList("procTest",params);
return ret;
}
public Account selectAccountByIdWithRole(int id) throws SQLException {
return (Account) getSqlMapClientTemplate().queryForObject(
"selectAccountByIdWithRole", new Integer(id));
}
public void insertAccount(Account account) throws SQLException {
getSqlMapClientTemplate().insert("insertAccount", account);
}
public int updateAccount(Account account) throws SQLException {
return getSqlMapClientTemplate().update("updateAccount", account);
}
public int deleteAccount(int id) throws SQLException {
return getSqlMapClientTemplate().delete("deleteAccountById", new Integer(id));
}
}
SqlServer存储过程
CREATE procedure test_sp_1 @id int,@outid int output,@errMsg varchar(255) output
AS
update ACCOUNT set pid=@id
select @outid=isnull(@id,0)+1
select @errMsg='测试'
select AAA='AAAAAAA',BBB='BBBBBBB',CCC='CCCCCCC'
GO
http://www.iteye.com/topic/68962
分享到:
相关推荐
【ibatis学习总结】 在Java应用中,ORM(对象关系映射)框架是连接数据库与业务逻辑的关键。相比像Hibernate和Apache OJB这样的"一站式"ORM解决方案,iBATIS提供了一种更为灵活的"半自动化"策略。iBATIS允许开发者...
### ibatis 学习小结笔记 #### 一、ibatis 概述 ibatis 是一个基于 Java 的持久层框架,它提供了一种简便的方式来处理关系型数据库与 Java 对象之间的映射(O/R Mapping)。ibatis 在设计上强调的是 SQL 语句的...
在IT行业中,Ibatis、Oracle...希望这篇总结能为你的学习之路提供有价值的指导,进一步深化你对Ibatis、Oracle和敏捷开发的理解。在IT世界中,持续学习和实践是提升自身能力的关键,祝你在技术探索的道路上越走越远。
本文档旨在对iBATIS的学习进行深入的总结,帮助初学者快速上手。 一、iBATIS概述 iBATIS的核心理念是“SQL Map”,它将SQL与Java代码分离,通过XML配置文件或注解来定义SQL语句,实现了数据访问层的灵活定制。这...
《iBatis学习总结——深入理解SQLServer驱动与工具应用》 在软件开发领域,数据库操作是不可或缺的一部分,而iBatis作为一个优秀的持久层框架,为开发者提供了灵活的SQL映射功能,使得Java应用程序能够更好地与...
《iBatis学习资料汇总》 iBatis,作为一个轻量级的持久层框架,它在Java开发领域中扮演着重要的角色。这个框架允许开发者将SQL语句与Java代码分离,提高了开发效率并降低了维护成本。本文将深入探讨iBatis的核心...
"iBATIS学习总结 - 郭睿的专栏 - CSDN.NET_files"和"iBATIS与Spring集成及环境搭建 - 振华 - ITeye技术网站_files"可能是相关文章的图片或辅助资源。 通过这些资料,开发者可以系统地学习和掌握iBATIS 2.x版本的...
iBATIS2.0是一个基于Java的持久层框架,它主要负责将SQL查询与应用程序的业务逻辑解耦,使得开发者可以更加专注于SQL的编写和优化,而无需关心数据访问的底层细节。iBATIS并不像Hibernate那样是完全的对象关系映射...
4.iBatis2学习笔记:SqlMap的配置总结(18条).doc 5.iBatis2学习笔记:入参和返回值的问题.doc 6.iBatis2学习笔记:一对多映射(双向).doc 7.iBatis2学习笔记:多对多映射(双向) .doc 8.iBatis2学习笔记:总结与...
### J2EE学习:Ibatis开发资料概要 #### 一、Ibatis简介与特点 Ibatis是一款半自动化的ORM(Object Relational Mapping)框架,它不像Hibernate等其他ORM工具那样提供完全自动化的数据库操作,而是更侧重于SQL语句...
本文将深入探讨其中一个流行的选择——iBATIS,以及其在传智播客教程中的优点总结。 iBATIS,全称为“互联网应用程序基础工具包”(Internet Basics for Architecture with Transactions and SQL),是由Apache ...
通过提供的文件《IBatisNet开发使用小结.docx》和《iBatis[1].Net详细使用手册.docx》,你将能够找到具体的步骤和示例代码,这些实例将涵盖基本的CRUD操作(创建、读取、更新和删除),以及更高级的功能如存储过程...
### iBatis 学习笔记知识点总结 #### 一、iBatis 概念与特点 **1.1 iBatis 定义** - **iBatis** 是一个基于 Java 的开源持久层框架,它专注于 SQL 映射,提供了一种将对象与数据库交互过程中的 SQL 语句进行分离的...