iBATIS基本配置
?
相对于Hibernate提供了全面的数据库封装机制的“全自动化”ORM实现而言,iBATIS则是一种“半自动”的ORM实现。
所谓“全自动化”ORM实现是指实现了POJO(Plain Old Java Object 简单的Java对象)和数据库表之间的映射,以及SQL的自动生成和执行。而IBatis则不会为程序员在运行期间自动生成SQL语句,需要程序员自己编写,然后通过映射配置文件,将SQL所需的参数,以及返回的结果字段映射到指定的POJO中。
iBATIS的下载与配置:
iBATIS的官方下载地址:http://labs.renren.com/apache-mirror/ibatis/binaries/ibatis.java/
MYBATIS的官方下载地址:http://code.google.com/p/mybatis/downloads/list
注:iBATIS 是 3.0以前的版本的名字,3.0(包括3.0)以后的版本叫MyBatis。MyBatis 和 iBatis有很多的区别,包括包的结构和类名。而我们今天所说的iBatis是指3.0以前的版本。
注:所有的持久类属性的类型都不要声明为基本类型(int、long、float、double),应该声明为对应的封装类型(Integer、Long、Float、Double),因为IBatis是面向对象的解决方案。因此所有的属性、参数和结果必须是对象。
把下载的iBatis文件(jar文件)和数据库驱动文件(jar文件)添加到当前项目的lib文件夹中并添加到当前项目的classpath中。
配置文件样例(sqlMapConfig.xml):
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE sqlMapConfig??????
??? PUBLIC "-//ibatis.apache.org//DTD SQL Map Config 2.0//EN" "http://ibatis.apache.org/dtd/sql-map-config-2.dtd">
<sqlMapConfig>
?????????? <properties?resource="SqlMapConfig.properties" />??? /*引用属性文件*/
?????????? <settings?
?????????????????????enhancementEnabled="true"????? /*全局性地启用运行时字节码增强*/
?????????????????????useStatementNamespaces="true"??? /*使用全限定名来应用Mapped statement*/
???????????????????? cacheModelsEnabled="true"??? /*全局性地启用缓存模式*/
?????????????????????lazyLoadingEnabled="true"???? /*全局性地使用所有延迟加载*/
??????????? />
??????????? <transactionManager?type="JDBC">
?????????????????? <dataSource?type="SIMPLE">
??????????????????????????<property?name="JDBC.Driver" value="${driver}" />
??????????????????????????<property?name="JDBC.ConnectionURL" value="${url}" />
??????????????????????????<property?name="JDBC.Username" value="${username}" /
??????????????????????????<property?name="JDBC.Password" value="${password}" />
??????????????????????????<property?name="Pool.MaximumActiveConnections" value="10" />?? /*数据库连接池可维持的最大容量*/
????????????????????????? <property?name="Pool.MaximumIdleConnections" value="5" />?? /*数据库连接池可挂起的连接数*/
??????????????????????????<property?name="Pool.MaximumCheckoutTime" value="120000" /> /*连接某个任务占用的最大时间(ms)*/
??????????????????????????<property?name="Pool.TimeToWait" value="500" /> /*线程等待连接的最大时间(ms)*/
??????????????????????????<property?name="Pool.PingQuery" value="select 1 from users" /> /*数据库连接状态检测语句*/
??????????????????????????<property?name="Pool.PingEnabled" value="false" />? /*是否允许检测连接状态*/
??????????????????????????<property?name="Pool.PingConnectionsOlderThan" value="1" /> /*对连接时间超过设定值将进行连接检测*/
??????????????????????????<property?name="Pool.PingConnectionsNotUsedFor" value="1" /> /*对空闲超过设定值的连接进行检测*/
???????????????????</dataSource>
??????????? </transactionManager>
??????????? <sqlMap?resource="com/demo/ibatis/beans/users_SqlMap.xml" />
</sqlMapConfig>
SqlMapConfig.properties?属性文件:
driver=com.microsoft.jdbc.sqlserver.SQLServerDriver
url=jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=demo;SelectMethod=cursor(SQL Server数据库必须有)
username=sa
password=joe
注:配置文件和属性文件均在src文件夹和bin文件夹中。
users_SqlMap.xml映射文件样例:
<?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="users">
????????? <cacheModel?id="userCache" type="LRU">
?????????????????? <flushInterval?hours="24"/>
?????????????????? <property?name="size" value="1000"/>
????????? </cacheModel>
??????????<typeAlias?alias="User" type="com.demo.ibatis.beans.User"/>?? /*使用别名*/
????????? <resultMap?id="userResult" column="ID" jdbcType="VARCHAR"/>
?????????????????? <result?property="username" column="username" jdbcType="VARCHAR"/>
?????????????????? <result?property="password" column="password" jdbcType="VARCHAR"/>
???????????????????<result?property="email" column="email" jdbcType="VARCHAR"/>
????????? </resultMap>
?????????? <select?id="getUser"?cacheModel="userCache"?resultMap="userResult"?parameterClass="User">
????????????????????????? select * from user where?id = #id#
????????? ?</select>
?????????? <select?id="getUserList"?cacheModel="userCache"?resultMap="userResult"?parameterClass="User">
????????????????????????? select * from user
???????????</select>
??????????? <insert?id="insertUser"?parameterClass="User">
????????????????????? insert into user(ID,username,password,email)?
????????????????????? values(#id:NUMERIC#,#username:VARCHAR#,#password:VARCHAR#,#email:VARCHAR#);
??????????? </insert>
??????????? <update?id="updateUser"?parameterClass="User">
???????????????????? update user set username=#username:VARCHAR#,password=#password:VARCHAR#,email=#email:VARCHAR#?
???????????????????? where ID=#id:NUMERIC#;
??????????? </update>
??????????? <delete?id="deleteUser"?parameterClass="User">
???????????????????? delete from user where ID="id:NUMERIC";
??????????? </delete>
</sqlMap>
?