您的位置:首页 > 其它

ibatis跟着走

2007-11-07 19:49 190 查看
google_ad_client = "pub-5033576919944123";
google_ad_width = 728;
google_ad_height = 90;
google_ad_format = "728x90_as";
google_ad_type = "text_image";
//2007-10-24: csdn.blog
google_ad_channel = "8548491739";

第一步:加jar包: 包括ibatis的包和数据库的驱动包.

第二步:写配置,包括xml还有属性文件,如下

ibatis.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">
<sqlMapConfig>
<properties resource="ibatis.properties" />

<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}" />
</dataSource>
</transactionManager>

<sqlMap resource="wtc/ibatis/bi.xml" />

</sqlMapConfig>

bi.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="wtc.ibatis">

<select id="query" resultClass="java.util.HashMap">
select * from tableName(你的tableName)

</select>

</sqlMap>

ibatis.properties(名字可以改,但相应的地方要改,看有背景色的地方)

## Oracle
driver=oracle.jdbc.driver.OracleDriver(改成你的驱动)
url=jdbc:oracle:thin:@10.20.30.83:1521:db(改成你的)

username=dbu(改成你的)
password=dbp(改成你的)

第三步:写个类可以获取SqlMapClient的类

package wtc.ibatis;

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

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

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

/**
* <p>
* @author wujy <br>
* @date 2007-11-5 <br>
*
*/
public class SqlMapClientHolder {
private static SqlMapClient sqlMapClient = null;
private static String defaultXml = "ibatis.xml";
private static Logger logger = LoggerFactory.getLogger(SqlMapClientHolder.class);

public static SqlMapClient getSqlMapClient(String resource) {
if (sqlMapClient == null){
if (resource == null){
resource = defaultXml;
}
Reader reader;
try {
reader = Resources.getResourceAsReader (resource);
sqlMapClient = SqlMapClientBuilder.buildSqlMapClient(reader);
} catch (IOException e) {
logger.debug("读取xml错误", e);
}
}
return sqlMapClient;
}

public static SqlMapClient getSqlMapClient() {
return getSqlMapClient(defaultXml);
}

public void setSqlMapClient(SqlMapClient sqlMapClient) {
this.sqlMapClient = sqlMapClient;
}

}

完了,写个测试:

package wtc.ibatis;

import java.sql.SQLException;
import java.util.List;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;

/**
* <p>
* @author wujy <br>
* @date 2007-11-5 <br>
*/
public class IbatisTest {

/**
* @throws java.lang.Exception
*
*/
@Before
public void setUp() throws Exception {
}

@Test
public final void testStartUp() {
try {
List list = SqlMapClientHolder.getSqlMapClient().queryForList("query");
System.out.println(list);
} catch (SQLException e) {
e.printStackTrace();
}
}

/**
* @throws java.lang.Exception
*
*/
@After
public void tearDown() throws Exception {
}

}

此乃最基础的.喜欢就看看.

给些网址:

eclipse的ibatis插件:http://ibatis.apache.org/tools/abator

www.ibatis.org

google_ad_client = "pub-5033576919944123";
google_ad_width = 728;
google_ad_height = 90;
google_ad_format = "728x90_as";
google_ad_type = "text_image";
//2007-10-24: csdn.blog
google_ad_channel = "8548491739";
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: