ibatis实现数据和数据查询
目通过用
软件环境
MYSQL
Ibatis2.3.0.667
mysql-connector-java-5.0.5
MYSQL中数据库test中创建t_user表(create table(id int,name varchar(10),sex int);)
Field | Type | Null | Key | Default | Extra |
id | Int(11) | YES |
| Null |
|
Name | Varchar(10) | YES |
| Null |
|
Sex | Int(11) | YES |
| Null |
|
创建ibatis配置文件
ibatis配置文件描述和数据库相关的配置信息
<?
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
>
<!--
用传统的JDBC实现事务处理
-->
<
transactionManager
type
="JDBC"
>
<!--
ibatis内置的dataSource实现实现的是一个简单的数据库连接池
-->
<
dataSource
type
="SIMPLE"
>
<!
—提供JDBC的驱动类--
>
<
property
name
="JDBC.Driver"
value
="org.gjt.mm.mysql.Driver"
/>
<!
—数据库位置,用户名密码--
>
<
property
name
="JDBC.ConnectionURL"
value
="jdbc:mysql://localhost/test"
/>
<
property
name
="JDBC.Username"
value
="root"
/>
<
property
name
="JDBC.Password"
value
="12201220"
/>
<!
—数据库连接池用可维护的最大容量--
>
<
property
name
="Pool.MaximumActiveConnections"
value
="10"
/>
<!
—数据库连接池中允许挂起的连接数--
>
<
property
name
="Pool.MaximumIdleConnections"
value
="5"
/>
<!
—某个任务允许占用连接的最大时间超过这个时间连接强制收回(毫秒)--
>
<
property
name
="Pool.MaximumCheckoutTime"
value
="120000"
/>
<!
—当连接池中无空闲连接线程等待空闲连接出现的最长时间(毫秒)--
>
<
property
name
="Pool.TimeToWait"
value
="500"
/>
<!
—数据库连接状态检查语句--
>
<
property
name
="Pool.PingQuery"
value
="select 1 from ACCOUNT"
/>
<!
—-是否允许检查连接状态--
>
<
property
name
="Pool.PingEnabled"
value
="false"
/>
<!
—-对连接时间超过设置的值则进行检测--
>
<
property
name
="Pool.PingConnectionsOlderThan"
value
="1"
/>
<!
—-对空闲超过设定值进行检测--
>

<
property
name
="Pool.PingConnectionsNotUsedFor"
value
="1"
/>
</
dataSource
>
</
transactionManager
>
<!--
设置映射文件的位置
-->
<
sqlMap
resource
="com/ibatis/sample/User.xml"
/>
</
sqlMapConfig
>
创建映射文件
<!--
定义了本映射文件中的别名,以避免过长变量值的反复书写
-->
<
typeAlias
alias
="user"
type
="com.ibatis.sample.User"
/>
<!--
这里申明了一个名为"userCache"的 cacheModel
-->
<
cacheModel
id
="userCache"
type
="LRU"
>
<!--
设定缓存有效期,如果超过此设定值,则将此CacheModel的缓存清空
-->
<
flushInterval
hours
="24"
/>
<!
—当执行updateUser的时候本cacheModel清空--
>
<
flushOnExecute
statement
=" updateUser"
/>
<!--
本CacheModel中最大容纳的数据对象数量
-->
<
property
name
="size"
value
="1000"
/>
</
cacheModel
>
<!
—Statement名称--
>
<
select
id
="getUser"
<!—statement的参数类型--
>
parameterClass="java.lang.String"
<!
—statement的返回值类型--
>
resultClass="user"
<!
—使用之前定义的那个catcheModel--
>
cacheModel="userCache" >
<!
—对应的SQL语句--
>
<![CDATA[
select name, sex from t_user where name = #name#
]]>
</
select
>
<!
—update的statement--
>
<
update
id
="updateUser"
parameterClass
="user"
>
UPDATE t_user
SET
name=#name#,
sex=#sex#
WHERE id = #id#
</
update
>
<!
—insert的statement--
>
<
insert
id
="insertUser"
paramterClass
="user"
>
Insert into t_user (id,name,sex) values(#id#,#name#,#sex#)
</
insert
>
</
sqlMap
>
程序代码
String resource
=
"
com/ibatis/sample/SqlMapConfig.xml
"
;
Reader reader;
reader
=
Resources.getResourceAsReader(resource);
SqlMapClient sqlMap
=
SqlMapClientBuilder.buildSqlMapClient(reader);
User user
=
new
User();
user.setId(
new
Integer(
1
));
user.setName(
"
Erica
"
);
user.setSex(
new
Integer(
1
));
//
sqlMap系统初始化完毕,开始事务
sqlMap.startTransaction();
sqlMap.insert(
"
insertUser
"
, user);
sqlMap.update(
"
updateUser
"
, user);
User use1r
=
new
User();
List list
=
(List) sqlMap.queryForList(
"
getUser
"
,
"
Erica
"
);
//
提交事务
sqlMap.commitTransaction();
以上是一个最简单的ibatis的实践