iBatis学习笔记

来源:互联网 发布:淘宝漏洞券是什么意思 编辑:程序博客网 时间:2024/04/29 18:13

1.Hibernate框架通过对象和表字段映射来生成SQL完成持久层的工作,而IBatis框架是通过对象和SQL之间映射来完成持久层得工作。

2.IBaitisDAO框架要求程序员提供一个DAO接口,并以DAO实现类实现它,通过一个XML让DAO接口知道哪个DAO实现类会实现它。这样的好处是DAO接口来处理事务管理,根据不同得业务需求选择DAO实现类来提供各自的实现。

3.<settings>元素

(1)maxReuquest指定了同时执行SQL语句的最大运行数

(2)maxSession指定了同一时间内活动的对大Session数,所谓Session就是一个请求或一个SQLMap运行时实例自动获得的会话

(3)maxTransactions指定了最大事务处理的线程数

(4)lazyLoadingEnabled指定了持久化数据是否延迟加载

(5)enhancemenEnabled指定了全局性得启用或禁用字节码增强,以优化访问POJO变量属性的功能

(6)useStatementNamespaces 是否使用Satement命名空间,这里的命名空间指的是映射文件中sqlMap节点的namespace属性,如上例中针对Person表的映射文件sqlMap节点<sqlMap namespace="Person" xmlns="http://ibatis.apache.org/mapping" xmlns:xsi=http://www.w3.org/2001/XMLSchema-instance> 这里,指定了此sqlMap节点下定义的操作均丛属于“Person”命名空间在useStatementNamespaces=”true”的情况下,Statement调用需追加命名空间,如:sqlMap.Update(“Person.UpdatePerson”,person);否则直接通过Statement名称调用即可,如sqlMap.Update(“UpdatePerson”,person);但请注意此时需要保证所有映射文件中,statement定义无重名
Example: useStatementNamespaces=”false”
Default: false (disabled)


(7)cacheModelsEnabled 是否启用sqlMap上的缓存机制,Example: cacheModelsEnabled=”true”,Default: true (enabled)

(8)是配置要不要启示SqlMapConfig.xsd schema验证映射文件.
Example: validateSqlMap=”false”
Default: false (disabled)


4.transactionManager元素用来配置事务管理
(1)type:有JDBC,JTA,EXTERNAL三个事务管理,是别名,由IBatis框架提供,JTA提供全局的事务管理,EXTERNAL可以让程序员自行管理事务

(2)dataSource,type有三种方式

SimpleDataSourceFactory:当容器没有提供数据源时可以选择它,SimpleDataSourceFactory的别名由IBatis框架定义,名为SIMPLE,<dataSource type="SIMPLE">

DbcpDataSourceFactory:使用DBCP连接池所提供的服务,别名为DBCP

JndiDataSourceFactory:别名为JNDI,可以在容器中利用JNDI从上下文中找到连接池的实现,寻找服务器提供的连接池

5.<sqlMap resource="domain/BookInfo.xml" />
SQLMap映射文件可以从classpath中得到,以classpath为根目录开始的

6.映射文件

<resultMap id="fwAttacksolutionResult" class="com.sun.Attacksolution">
 <result column="code" property="code" jbdcType="VARCHAR" />
 <result column="mean" property="mean" jbdcType="VARCHAR" />
 <result column="action" property="action" jbdcType="VARCHAR" />
</resultMap>
resultMap可以定义俗话据返回的对象,返回的截获到VO对象Attacksolution。
column为数据库表的字段,property作为VO变量属性进行映射。返回的记过根据字段设置VO变量属性的值,jbdcType指定了数据库表中字段得数据库类型<p align="right"></p>


<sql id="example"> //定义一般得SQL语句,通常用来作为动态DQL语句的指定
 //可以划分出SQL语句的动态部分,它可以包含任意多得条件元素
 //prepend被用来覆盖第一条件元素
 <dynamic prepend="where">
 //isPropertyAvailable判断是否存在property属性所指定的某个对象的变量属性
  <isPropertyAvailable prepend="and" property="AND_code_NULL">
   code is null  //当对象存在时,使用<isPropertyAvailable>的实体内容
  </isPropertyAvailable>
  <isPropertyAvailable prepend="and" property="AND_code_NOT_NULL">
   code is not null
  </isPropertyAvailable>
  <isPropertyAvailable prepend="or" property="OR_code_NOT_NULL">
   code is not null
  </isPropertyAvailable>
  <isPropertyAvailable prepend="and" property="AND_code_EQUALS">
   code=#code# //表示某个对象的变量属性
  </isPropertyAvailable>
  <isPropertyAvailable prepend="or" property="OR_code_EQUALS">
   code=#code#
  </isPropertyAvailable>
</sql>
说明当某个对象存在变量属性OR_code_EQUALS时,将定义 or code = ? 的条件语句,并根据<select>节点判断是否是第一条件。如果是第一个条件,则会以where来替换or,成为where code = ? 而?将以某个对象中变量属性code的值来替换

OR_code_EQUALS并不是VO对象的变量属性,因此会有一个新的java类来提供该变量属性。

//parameterClass是传入参数的类
//reslutMap表示结果映射到映射到VO
<select id="selectByPrimaryKey" reslutMap="fwAttacksolutionResult"
    parameterClass="com.sun.Attacksolution">
 select code,mean,action from fw_attacksolution where code = #code#
<select>
根据上面<sql>节点和<select>判断where是第一节点,所有用where代替or,where code = ? 的 ? 将通过com.sun.Attacksolution的code变量属性得到

所以说<sql>节点和<select>节点,两个节点互相配合

 

<select id="selectByPrimaryKey" reslutMap="fwAttacksolutionResult"
    parameterClass="com.sun.Attacksolution">
 select code,mean,action from fw_attacksolution
 
 //inlude子元素可以包含动态SQL到<select>元素的实体内容中去
 <inlude refid="fw_attacksolution.exmple"> //refid指定被包含的动态SQL标识,该标识是全局的
 
 //判断property属性所指定的对象变量属性是否存在
 <isPropertyAvailable property="ORDER_BY_CLAUSE">
  order by $ORDER_BY_CLAUSE$
 </isPropertyAvailable>
<select>
本例的含义是当传入参数中存在"ORDER_BY_CLAUSE"变量属性时,将"ORDER_BY_CLAUSE"替换$ORDER_BY_CLAUSE$,形成一句完整的order by语句

$和#同样都是替换变量属性的值。"$"是一般的替换,用变量属性来替换"$"包含的内容。两者是处理方式的区别,包含在"#"中的变量属性值会被用来带"?"的SQL中替换"?",进行SQL预处理


<delete id="deleteByPrimaryKey">
 parameterClass="com.sun.Attacksolution">
 delete from fw_attacksolution
 where code = #code#
</delete>
只有id,parameterClass和parameterMap 3个属性,但是没有relsutClass和resultMap这两个返回性质的属性,没有返回值


<delete id="deleteByPrimaryKey">
 parameterClass="java.util.Map">
 delete from fw_attacksolution
 <include refid="fw_attacksolution.exmple">
</delete>
和上面不同的是它有了动态SQL的特性,所以<delete>支持所有的动态SQL

<insert id="insert" parameterClass="com.sun.Attacksolution">
 insert into fw_attacksolution(code,mean,action) values(#code:VARCHAR#,#mean:VARCHAR#,#action:VARCHAR#)
</insert>

<update id="update" prarameterClass="com.sun.Attacksolution">
 update fw_attacksolution
 set mean = #mean:VARCHAR#,#action:VARCHAR# where #cod#
</update>

7.其他判断条件和属性
<isPropertyAvailable>

  <isPropertyAvailable prepend="and" property="AND_code_NULL">
   code is null  //当对象存在时,使用<isPropertyAvailable>的实体内容
  </isPropertyAvailable>
  
<isNotPropertyAvailable>当不存在对象的变量属性时,可以使用元素的实体内容
<isNull>当对象变量为null时,可以使用元素的实体内容
<isNotNull>
<isEmpty>
<isNotEmpty>
这些元素都有prepend和property属性,

prepend指定了条件元素:
<isEqual>在比较对象与对象之间的变量属性,或对象与静态值相等的条件下,可以使用元素的实体内容
<isNotEqual>
<isGreaterThan>
<isGreaterEqual>
<isLessThan>
<isLessEqual>
这些元素都用prepend,property,compareProperty,compareValue 4个属性compareProperty指定比较的另一个对象的变量属性

8.<statement>通用的SQL执行元素,可以执行任何SQL语句,所有的SQL都可以使用<statement>来替换,有个xmlResultName返回结果生成一个XML文档
<procedure>用于执行存储过程,有个xmlResultName返回结果生成一个XML文档

9.利用<insert>元素来生成自动主键
用sequence生成主键
<insert id="insertByAutoPK" parameterClass="com.sun.Attacksolution">
 <selectKey resultClass="int" keyProperty="id">
  SELECT STOCKIDSEQUANENCE.NEXTVAL AS ID FROM DUAL
 </selectKey>
 insert into USER(USER_ID,USER_NAME) values (#id#,#userName)
</insert>

用indentity生成主键
<insert id="insertByAutoPK" parameterClass="com.sun.Attacksolution">
 <selectKey resultClass="int" keyProperty="id">
  SELECT @@IDENTITY AS ID
 </selectKey>
 insert into USER(USER_ID,USER_NAME) values (#id#,#userName)
</insert>

10.在<select><statement><procedure>中可以使用xmlResultName来将结果集写到XML文档
<select id="getUser" parameterClass="int" resultClass="xml" xmlResultName="user">
 SELECT USER_ID as id,USER_NAME as userName FROM USER WHERE USER_ID = #userId#
<select>

结果是
<user>
 <id>1</id>
 <userName>RW</userName>
</user>

xmlResultName="user" user是根元素


11.<resultMap id="get-pattackDocument-result" class="com.sun.PattackDocument">
 <result property="code" column="code">
 <result property="mean" column="mean">
 <result property="attackSoution" column="parents_attack_enent_code" select="getAttackSolution" />
 
VO为PattackDocument.java,AttackSolution.java,PattackDocument和AttackSolution是一对多关系
PattackDocument.java:
private ArrayList attackSolution;
public ArrayList getAttackSolution(){
 return attackSolution;
}

当将结果集写入VO变量属性时,得到一个JDBC无法识别的List类型,于是底层框架就会查找select属性所指定的查询SQL语句,运行这个子查询,并将子查询的结果写入List,返回到VO变量属性attackSolution

12.SqlMapClient
用来存储指定的经过解析的映射文件,SqlMapClient放在com.ibatis.sqlmap.client,是一个接口

SqlMapClient实例产生是通过com.ibatis.sqlmap.client.SqlMapClientBuilder类来完成的,SqlMapClientBuilder类提供了静态方法buildSqlMapCient来读取配置文件,并返回SqlMapClient接口类型,而此时的SqlMapClient变量实例已经过的了数据库连接的基本配置信息。
SqlMapClientBuilder类的静态方法buildSqlMapCient需要一个Reader类型的参数,这个Reader类型的参数可以由IBatis框架提供的工具类com.ibatis.common.resources.Resources来生成。Resources类专门来读取classpath下的配置文件,通过Resources类可以让SqlMapClientBuilder类读取到配置文件的所有信息

13.需要ibatis-sqlmap-2.jar 框架包
ibatis-common-2.jar  工具类包
ibatis-dao-2.jar

原创粉丝点击