ibatis简单使用

 

 

      iBATIS与和hibernate不同,它不是直接把类映射为数据库表或者说把类的字段映射为数据库列,而是把SQL语句的参数与结果(也即输入和输出)映射为类

 

一、

 

SqlMapConfig.xml是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>

 

  <transactionManager type="JDBC" commitRequired="true">

<dataSource type="DBCP">    

<property name="JDBC.Driver"

value="oracle.jdbc.driver.OracleDriver" />

<property name="JDBC.ConnectionURL"

value="jdbc:oracle:thin:@localhost:1521:test" />

<property name="JDBC.Username" value="scott" />

<property name="JDBC.Password" value="tiger" />

<property   name ="Pool.MaximumActiveConnections"   value ="30" /> 

  <property   name ="Pool.MaximumIdleConnections"   value ="10" /> 

<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/mydomain/data/Account.xml"/>

 

</sqlMapConfig>

 

transactionManager元素用于配置数据源的相关信息,sqlMap用于配置POJO类的映射文件,要放到resource属性指定的目录下,如下

 

所示:

 

<?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="AccountResult" class="Account">

    <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>

 

  <select id="selectAllAccounts" resultMap="AccountResult">

    select * from ACCOUNT

  </select>

 

  <select id="selectAccountById" parameterClass="int" resultClass="Account">

    select

      ACC_ID as id,

      ACC_FIRST_NAME as firstName,

      ACC_LAST_NAME as lastName,

      ACC_EMAIL as emailAddress

    from ACCOUNT

    where ACC_ID = #id#

  </select>

 

注意:在使用查询语句的时候要注意,查询的列一定要与声明resultClass和resultMap中的项一一对应,否则会报invalid column name错误,插入的时候不需要,因为没有result属性。

 

  <insert id="insertAccount" parameterClass="Account">

    insert into ACCOUNT

    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>

 

resultMap用于定义表字段和类属性直接的映射关系,下面就是sql语句的定义

 

 

二、

 

配置文件编写完成后,就可以通过SqlMapClient类去操作数据库了,如下所示:

 

 

public class SimpleExample {

 

  private static SqlMapClient sqlMapper;

 

  static {

    try {

      Reader reader = Resources.getResourceAsReader("com/mydomain/data/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<Account> selectAllAccounts () throws SQLException {

    return sqlMapper.queryForList("selectAllAccounts");

  }

 

  public static Account selectAccountById  (int id) throws SQLException {

    return (Account) sqlMapper.queryForObject("selectAccountById", 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", id);

  }

 

  public static void main(String args[]){

 System.out.println("==start==");

 try {

Account ac=new Account();

ac.setId(1);

ac.setFirstName("ss");

ac.setLastName("hhaha");

ac.setEmailAddress("ss@163.com");

SimpleExample.insertAccount(ac);

List list=SimpleExample.selectAllAccounts();

if(list != null){

System.out.println(list.size());

}

} catch (SQLException e) {

e.printStackTrace();

}

  }

 

}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值