ibatis学习和实践

优点:sql语句和java分离
简单易学
开发效率高
增加移植性
缺点: 自己手动编写sql语句
传递参数的个数限定

package fat.yk.util;

import java.io.IOException;
import java.io.Reader;

import com.ibatis.common.resources.Resources;
import com.ibatis.sqlmap.client.SqlMapClient;
import com.ibatis.sqlmap.client.SqlMapClientBuilder;


public class IbatisUtil {
public static SqlMapClient sqlMapClient = null;

static {
try {
Reader reader = Resources
.getResourceAsReader("fat/yk/util/config/SqlMapConfig.xml");//获取配置文件信息

sqlMapClient = SqlMapClientBuilder.buildSqlMapClient(reader);
reader.close();
} catch (IOException e) {
// TODO Auto-generated catch block
System.out.println(e.toString());
e.printStackTrace();
}

}


主要的配置文件:
[color=cyan]SqlMapConfig.xml[/color]
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE sqlMapConfig
PUBLIC "-//iBATIS.com//DTD SQL Map Config 2.0//EN"
[color=red]"http://www.ibatis.com/dtd/sql-map-config-2.dtd[/color]">
<!-- Always ensure to use the correct XML header as above! -->
<sqlMapConfig>
<!-- The properties (name=value) in the file specified here can be used placeholders in this config file (e.g. “${driver}”. The file is relative to the classpath and is completely optional. -->
<properties resource="fat/yk/util/config/sqlMap.properties" />
<!-- These settings control SqlMapClient configuration details, primarily to do with transaction
management. They are all optional (more detail later in this document). -->
<!-- settings
cacheModelsEnabled="true"
enhancementEnabled="true"
lazyLoadingEnabled="true"
maxRequests="32"
maxSessions="10"
maxTransactions="5"
useStatementNamespaces="false"
/-->
<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="JDBC.DefaultAutoCommit" value="true" />
<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>
<!-- Identify all SQL Map XML files to be loaded by this SQL map. Notice the paths
are relative to the classpath. For now, we only have one… -->

<sqlMap resource="fat/yk/util/config/Student.xml" />
</sqlMapConfig>

}


[color=cyan]SqlMap.properties[/color]
driver=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/test
username=root
password=


[color=cyan]Student.xml[/color]
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE sqlMap
PUBLIC "-//ibatis.apache.org//DTD SQL Map 2.0//EN"
[color=red] "http://ibatis.apache.org/dtd/sql-map-2.dtd">[/color]
<sqlMap>
<typeAlias alias="Student" type="fat.yk.pojos.Student" />
<resultMap class="Student" id="stuMap">
<result property="id" column="id" />
<result property="name" column="name" />
<result property="age" column="age" />
<result property="sex" column="sex" />
</resultMap>
<select id="selectAllStudent" resultClass="Student">
select * from student
</select>
<select id="selectStudentById" resultClass="Student"
parameterClass="int">
select * from student where id=#id#
</select>
<insert id="insertStudent" parameterClass="Student">
<!-- 主键自动增长 -->
<selectKey keyProperty="id" resultClass="int">
SELECT LAST_INSERT_ID() as value
</selectKey>
insert into student(name,age,sex) values(#name#,#age#,#sex#);
</insert>

<delete id="delStudent" parameterClass="Student">
delete from student where id=#id#
</delete>

<update id="upStudent" parameterClass="Student">
update student
set name=#name#,
age=#age#,
sex=#sex#
where id=#id#
</update>
<select id="selectStudentByLike" parameterClass="String" resultMap="stuMap">
select * from student where name like '%$name$%'
</select>

<select id="getStuRole" resultClass="String" parameterClass="int">
select r.roleName from student as s,role as r,stuRole where s.id=#id# and s.id=stuRole.stuId and r.id=stuRole.roleId
</select>
</sqlMap>

红色的地方呢头标题的引用不一样,一开始我直接复制SqlMapConfig.xml的头,结果出错了。复制就是容易出错!!!

例外多表查询我怎么用怎么觉的不是很方便,当然 ibatis就是用于简单功能需求开发的,如果要实现麻烦的,就多此一举了。

原程序如下
数据库:mysql
表简单
根据fat.yk.pojos的属性字段改改就OK。

http://0101xb236de232.iteye.com/blog/848194
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值