iBATIS入门(实例代码)

J2EE

iBATIS入门(实例代码)

首先建立表t_user
CREATE TABLE public.t_user
(
  uid int4,
  name varchar(20),
  sex int4,
  age int4,
  addr varchar(50),
  zipcode varchar(6),
) WITH OIDS;

整个project的目录如下:
                                       
然后把所需要的包引入(可把iBATIS_JPetStore-4.0.5的lib目录下的包直接拿过来用,另外还要一个jdbc的包)。如下图:

                                              

新建一个sql-map-config.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="sqlmap/map/SqlMapConfigExample.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="sqlmap/map/User.xml" />
  </sqlMapConfig>


新建数据库资源文件SqlMapConfigExample.properties
driver=org.postgresql.Driver
url=jdbc:postgresql://localhost/testapp
username=zzz
password=zzz

类user.java:

package com.ibatis.my.domain;
public class User {
    public int uid;
    public String name;
    public int sex;
    public int age;
    public String addr;
    public String zipcode;   

    public String getAddr() {
        return addr;
    }
    public void setAddr(String addr) {
        this.addr = addr;
    }
    public int getAge() {
        return age;
    }
    public void setAge(int age) {
        this.age = age;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public int getSex() {
        return sex;
    }
    public void setSex(int sex) {
        this.sex = sex;
    }
    public int getUid() {
        return uid;
    }
    public void setUid(int uid) {
        this.uid = uid;
    }
    public String getZipcode() {
        return zipcode;
    }
    public void setZipcode(String zipcode) {
        this.zipcode = zipcode;
    }
}


配置user.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE sqlMap
  PUBLIC "-//iBATIS.com//DTD SQL Map 2.0//EN"
  "http://www.ibatis.com/dtd/sql-map-2.dtd">
  
 <sqlMap namespace="User">
        <select id="getUser" parameterClass="java.lang.Integer">
                  SELECT * FROM t_user WHERE uid=#uid#
        </select>
  <insert id="insertUser" parameterClass="com.ibatis.my.domain.User">
              INSERT INTO
                     t_user(uid, name, sex, age, addr, zipcode) 
              VALUES (#uid#, #name#, #sex#, #age#, #addr#, #zipcode#)
  </insert> 
 </sqlMap>

建一个myapp.java

import java.io.Reader;

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


public class Myapp {
    private static final SqlMapClient sqlMap;
    static {
        try {
            String resource = "sqlmap/map/sql-map-config.xml";
            Reader reader = Resources.getResourceAsReader (resource);
            sqlMap = SqlMapClientBuilder.buildSqlMapClient(reader);
        }
        catch (Exception e) {
         e.printStackTrace();    
        throw new RuntimeException ("Error initializing MyApp class. Cause:"+e);
        }
    }
    public static SqlMapClient getSqlMapInstance () {
        return sqlMap;
    }
}


操作类Opperates.java

package com.ibatis.my.action;

import com.ibatis.my.domain.User;
import com.ibatis.sqlmap.client.SqlMapClient;


public class Opperates {

    public static void main(String[] args) {
        User newUser= new User();
        newUser.setUid(5);
        newUser.setName("oring");
        newUser.setSex(2);
        newUser.setAge(2);
       
        newUser.setAddr("dfghg");
        newUser.setZipcode("301");
        SqlMapClient sqlMap = Myapp.getSqlMapInstance();
        try{
           
           Integer uid= new Integer(1);
        //   User user = (User) sqlMap.queryForObject ("getUser", uid);           
           sqlMap.insert ("insertUser", newUser);
        }
        catch(Exception e){
               e.printStackTrace();
        }
       
    }
}

在Eclipse中选择Run As Java Application运行Opperates.java,即可看到数据库中添加了一条记录,如下: