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

Ibatis 的根本用法

2012-10-28 
Ibatis 的基本用法Ibatis 的基本用法:1.需要配置Ibatis 的jar包:ibatis-sqlmap-2.jaribatis-common-2.jar2

Ibatis 的基本用法
Ibatis 的基本用法:
1.需要配置Ibatis 的jar包:
ibatis-sqlmap-2.jar
ibatis-common-2.jar
2.创建工程:
建立Vo:UserInfo 类

Copy code

package com.sunyang.stuibatis.domain;

import java.util.List;
import java.util.StringTokenizer;
import java.util.ArrayList;

/**
* Created by IntelliJ IDEA.
* User: fanhongming
* Date: 2005-1-24
* Time: 17:08:42
* To change this template use File | Settings | File Templates.
*/
public class UserInfo {
  private Integer id;
  private String userNO;
  private String userName;
  private Integer userAge;
  private List idList;

 

  public List getIdList() {
    return idList;
  }

  public void setIdList(List idList) {
    this.idList = idList;
  }

  public Integer getId() {
    return id;
  }

  public void setId(Integer id) {
    this.id = id;
  }

  public String getUserNO() {
    return userNO;
  }

  public void setUserNO(String userNO) {
    this.userNO = userNO;
  }

  public String getUserName() {
    return userName;
  }

  public void setUserName(String userName) {
    this.userName = userName;
  }

  public Integer getUserAge() {
    return userAge;
  }

  public void setUserAge(Integer userAge) {
    this.userAge = userAge;
  }

  public UserInfo() {
    this.id = new Integer(-1);
    this.userNO = "";
    this.userName = "";
    this.userAge = new Integer(-1);

  }
}

3.配置xml (以sqlserver为例)
DataAccess.xml
代码如下:

Copy code
<?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>
  <settings
    cacheModelsEnabled="true"
    enhancementEnabled="true"
    lazyLoadingEnabled="true"
    errorTracingEnabled="true"
    maxRequests="32"
    maxSessions="10"
    maxTransactions="5"
    useStatementNamespaces="false"
    />
  <transactionManager type="JDBC">
    <dataSource type="SIMPLE">
        <property name="JDBC.Driver"
          value="com.microsoft.jdbc.sqlserver.SQLServerDriver"/>
        <property name="JDBC.ConnectionURL"
          value="jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=iBatis"/>
        <property name="JDBC.Username" value="sa"/>
        <property name="JDBC.Password" value=""/>
        <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 userinfo"/>
        <property name="Pool.PingEnabled" value="false"/>
        <property name="Pool.PingConnectionsOlderThan" value="1"/>
        <property name="Pool.PingConnectionsNotUsedFor" value="1"/>
    </dataSource>
  </transactionManager>
  <sqlMap resource="com/sunyang/stuibatis/persistence/iBatis/map/sunyang.xml"/>
<!-- 此处为配置表和Vo(DTO) 的映射 -->
</sqlMapConfig>

配置表和Vo(DTO) 的映射:sunyang.xml

Copy code

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!DOCTYPE sqlMap PUBLIC "-//iBATIS.com//DTD SQL Map 2.0//EN"
    "http://www.ibatis.com/dtd/sql-map-2.dtd">
<sqlMap namespace="UserInfo">
  <cacheModel id="UserInfoCache" type="LRU">
    <flushInterval hours="24"/>
    <flushOnExecute statement="insertUserInfo"/>
    <flushOnExecute statement="updateUserInfo"/>
    <flushOnExecute statement="deleteUserInfo"/>
    <flushOnExecute statement="selectUserInfo"/>
  <property name="cache-size" value="100"/>
  </cacheModel>

  <resultMap id="userInfo" column="id" />
    <result property="userNO" column="userNO"/>
    <result property="userName" column="userName"/>
    <result property="userAge" column="userAge"/>
  </resultMap>


  <insert id="insertUserInfo">
    insert into userinfo (userNO,userName,userAge) values(#userNO#,#userName#,#userAge#)
    <selectKey resultkeyProperty="id">
        SELECT IDENT_CURRENT('userinfo') as id
    </selectKey>
  </insert>


  <update id="updateUserInfo">
    update userInfo set
    userNO=#userNO#,
    userName=#userName#,
    userAge =#userAge#
    where id=#id#

  </update>

  <delete id="deleteUserInfo">
    delete from userInfo
    where id =#id#
  </delete>

  <select id="selectUserInfo" cacheModel="UserInfoCache" resultMap="userInfo">
    select id,userNO,userName,userAge
    from userInfo
    <dynamic prepend="where">
        <isNotEqual prepend="AND" property="id" compareValue="-1">
          (id=#id#)
        </isNotEqual>
        <isNotEmpty prepend="AND" property="userNO">
          (userNO=#userNO#)
        </isNotEmpty>
        <isNotEmpty prepend="AND" property="userName">
          (userName=#userName#)
        </isNotEmpty>
    </dynamic>
  </select>

</sqlMap>


写调用类:

Copy code

public class UserInfoSqlMapDao implements UserInfoDao {

  public SqlMapClient sqlMap;
//取得sqlmap

  public boolean getSqlMapClient() {
    String resource = "com/sunyang/stuibatis/persistence/iBatis/map/DataAccess.xml";
    Reader reader = null;
    try {
        reader = Resources.getResourceAsReader(resource);
    } catch (IOException e) {
        e.printStackTrace();
    }
    XmlSqlMapClientBuilder xmlBuilder = new XmlSqlMapClientBuilder();
    sqlMap = xmlBuilder.buildSqlMap(reader);
    return false;
  }
//插入信息

  public boolean insertUserInfo(Object obj) {
    UserInfo user = (UserInfo) obj;
    try {

        sqlMap.insert("insertUserInfo", user);           //todo "insertUserinfo "   为xml中的preparestatement 的名字
    } catch (SQLException e) {
        e.printStackTrace();
    }
    return false;
  }
public static void main(String[] args) {
    UserInfoSqlMapDao dao = new UserInfoSqlMapDao();
    UserInfo user = new UserInfo();
    user.setUserName("changchun");
    user.setUserNO("ww");
    user.setUserAge(new Integer(1));

    dao.getSqlMapClient();
    dao.insertUserInfo(user);       //todo 插入
    user.setUserName("beijng");
  user.setUserNO("beijing 2008");
  dao .updataUserInfo(user);     //todo 更新*/
  dao.deleteUserInfo(user);
UserInfo uu = (UserInfo) dao.selectOne(user);   //todo 单条查询
  System.out.println("uu.username=" + uu.getUserName());
  System.out.println("uu.userno=" + uu.getUserNO());
}
}

热点排行