2008-05-16

Ibatis环境配置

 

一:网上http://www.apache.org/上下载Ibatis包

 

和Ibatis关联的几个包

 

commons-dbcp.jar

log4j.jar

cglib.jar

oscache.jar

commons-logging.jar

有时候可能各个版本不同会产生错误

 

 

二:配置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>

  <settings cacheModelsEnabled="true"<!--使用缓存-->
     enhancementEnabled="true"
     lazyLoadingEnabled="true"
     maxRequests="32"
     maxSessions="10"
     maxTransactions="10"
     useStatementNamespaces="false"
  />
  <transactionManager type="JDBC" commitRequired="false"><!--使用连接池-->
    <dataSource type="JNDI">
      <property name="DataSource" value="ibatis"/>
    </dataSource>

<!--
    <dataSource type="SIMPLE">
      <property name="JDBC.Driver" value="org.hsqldb.jdbcDriver"/>
      <property name="JDBC.ConnectionURL" value="jdbc:hsqldb:."/>
      <property name="JDBC.Username" value="sa"/>
      <property name="JDBC.Password" value="sa"/>
    </dataSource>
     -->

  </transactionManager>

  <sqlMap resource="com/myspackage/***.xml"/><!--SQL映射配置文件-->

</sqlMapConfig>

 

此主配置文件可以放到class下的任何位置,只要在得到此配置文件时候的标明URI即可。

 

com/myspackage/***.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="Account">

  <typeAlias alias="Account" type="com.mydomain.domain.Account"/>

   <resultMap id="MapResult" class="HashMap">
    <result property="id" column="ACC_ID"/>
    <result property="firstName" column="ACC_FIRST_NAME"/>
    <result property="lastName" column="ACC_LAST_NAME"/>
    <result property="emailAddress" column="ACC_EMAIL"/>
  </resultMap>
  <cacheModel id="CacheID" type="OSCACHE"><!--[OSCACHE,MEMORY,LRU,FIFO]-->
     <flushInterval hours="24"/>
  <property name="size" value="1000" />
  </cacheModel>

  <select id="selectId" parameterClass="int" resultClass="Map"><!--返回一个Map类型的VO-->
    select
      *    from SomeTable
    where SomeTable_id = #id#
  </select>

  <insert id="insertAccount" parameterClass="Account"><!--使用AccoutVO作为in parameter,也可以使用Map-->
    insert into ACCOUNT (
      ACC_ID,
      ACC_FIRST_NAME,
      ACC_LAST_NAME,
      ACC_EMAIL
    values (
      #id#, #firstName#, #lastName#, #emailAddress#
    )
  </insert>

  <update id="updateAccount" parameterClass="Account">
    update ACCOUNT set
      ACC_FIRST_NAME = #firstName#,
      ACC_LAST_NAME = #lastName#,
      ACC_EMAIL = #emailAddress#
    where
      ACC_ID = #id#
  </update>

  <delete id="deleteAccountById" parameterClass="int">
    delete from ACCOUNT where ACC_ID = #id#
  </delete>

</sqlMap>

 

三:DAO

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

import java.io.Reader;
import java.io.IOException;
import java.util.List;
import java.sql.SQLException;

public class SimpleExample {

  private static SqlMapClient sqlMapper;

  static {
    try {
      Reader reader = Resources.getResourceAsReader("SqlMapConfig.xml");
      sqlMapper = SqlMapClientBuilder.buildSqlMapClient(reader);
      reader.close();
    } catch (IOException e) {
      throw new RuntimeException("Something bad happened while building the SqlMapClient instance." + e, e);
    }
  }

  public static List selectAllAccounts (java.util.Map map) throws SQLException {
   List list=(List)sqlMapper.queryForObject("selectAllAccounts", map);
    return list;
  }

  public static Account selectAccountById  (int id) throws SQLException {
    return (Account) sqlMapper.queryForObject("selectAccountById", new Integer(id));
  }

  public static void insertAccount (Account account) throws SQLException {
    sqlMapper.insert("insertAccount", account);
  }

  public static void updateAccount (Account account) throws SQLException {
    sqlMapper.update("updateAccount", account);
  }

  public static void deleteAccount (int id) throws SQLException {
    sqlMapper.delete("deleteAccount", new Integer(id));
  }

}

 

四:使用此DAO

 

Map map=new HashMap();
map.put("id",request.getParameter("id"));
List list=SimpleExample .queryForList("selectAllAccounts",map);

 

 

 

追加几个包

 

评论
发表评论

您还没有登录,请登录后发表评论