首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 软件管理 > 软件架构设计 >

ibatis学习跟实践

2012-11-01 
ibatis学习和实践优点:sql语句和java分离简单易学开发效率高增加移植性缺点: 自己手动编写sql语句传递参数

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();
}

}


主要的配置文件:
SqlMapConfig.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE sqlMapConfig
PUBLIC "-//iBATIS.com//DTD SQL Map Config 2.0//EN"
"http://www.ibatis.com/dtd/sql-map-config-2.dtd">
<!-- 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>

}


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


Student.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>
<typeAlias alias="Student" type="fat.yk.pojos.Student" />
<resultMap 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" resultresultparameterresultparameterparameterparameterresultMap="stuMap">
select * from student where name like '%$name$%'
</select>

<select id="getStuRole" resultparameterClass="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

热点排行