现在的位置: 首页 > 综合 > 正文

iBatis 学习资料

2013年02月19日 ⁄ 综合 ⁄ 共 6619字 ⁄ 字号 评论关闭
iBatis

/**/ /* 
SQLyog 企业版 - MySQL GUI v4.1
主机 - 5.0.7-beta-nt : 数据库 - sample
*********************************************************************
服务器版本 : 5.0.7-beta-nt
create database if not exists `sample`;
USE `sample`;
/* 数据表 `t_user` 的表结构 */ 
 drop   table   if   exists  `t_user`;
 CREATE   TABLE  `t_user` (
  `id`  int  ( 11 )  NOT   NULL  auto_increment,
  ` name `  varchar  ( 50 )  default   NULL  ,
  `sex`  int  ( 11 )  default   NULL  ,
   PRIMARY   KEY    (`id`)
) ENGINE = InnoDB  DEFAULT  CHARSET = latin1;
 /**/ /*  数据表 `t_user` 的数据 */ 
 insert   into  `t_user`  values  ( 1 ,  ' zhupan '  , 1 ),( 2 ,  ' zhupan '  , 2 ),( 3 ,  ' 3 '  , 3 ),( 4 ,  ' 4 '  , 4 ),( 5 ,  ' 5 '  , 5 ); 

编写 iBatis

xml 代码
  1. <!--sp-->xml version="1.0" encoding="UTF-8" ?>  
  2. <!--CTYPE sqlMapConfig    </sp-->
  3.     PUBLIC "-//iBATIS.com//DTD SQL Map Config 2.0//EN"    
  4.     "http://www.ibatis.com/dtd/sql-map-config-2.dtd">  
  5.   
  6. <sqlMapConfig>  
  7.   
  8.     <settings cacheModelsEnabled="true" enhancementEnabled="true"  
  9.         lazyLoadingEnabled="true" errorTracingEnabled="true" maxRequests="32"  
  10.         maxSessions="10" maxTransactions="5" useStatementNamespaces="false" />  
  11.   
  12.     <transactionManager type="JDBC">  
  13.         <dataSource type="SIMPLE">  
  14.             <property name="JDBC.Driver" value="com.mysql.jdbc.Driver" />  
  15.             <property name="JDBC.ConnectionURL"  
  16.                 value="jdbc:mysql://localhost/sample" />  
  17.             <property name="JDBC.Username" value="root" />  
  18.             <property name="JDBC.Password" value="" />  
  19.             <property name="Pool.MaximumActiveConnections" value="10" />  
  20.             <property name="Pool.MaximumIdleConnections" value="5" />  
  21.             <property name="Pool.MaximumCheckoutTime" value="120000" />  
  22.             <property name="Pool.TimeToWait" value="500" />  
  23.             <property name="Pool.PingQuery"  
  24.                 value="select 1 from sample" />  
  25.             <property name="Pool.PingEnabled" value="false" />  
  26.             <property name="Pool.PingConnectionsOlderThan" value="1" />  
  27.             <property name="Pool.PingConnectionsNotUsedFor" value="1" />  
  28.         dataSource>  
  29.     transactionManager>  
  30.   
  31.     <sqlMap resource="com/ctgusec/zhupan/maps/User.xml" />  
  32. sqlMapConfig>  

如果不用 mysql

  •   
  • import java.io.Serializable;   
  •   
  • public class User implements Serializable {   
  •   
  •     /** */  
  •     /**  
  •      *   
  •      * @author zhupan  
  •      *   
  •      */  
  •   
  •     private static final long serialVersionUID = 1L;   
  •   
  •     private Integer id;   
  •   
  •     private String name;   
  •   
  •     private Integer sex;   
  •   
  •     public User() {   
  •   
  •     }   
  •   
  •     public Integer getId() {   
  •   
  •         return this.id;   
  •   
  •     }   
  •   
  •     public void setId(Integer id) {   
  •   
  •         this.id = id;   
  •   
  •     }   
  •   
  •     public String getName() {   
  •   
  •         return this.name;   
  •   
  •     }   
  •   
  •     public void setName(String name) {   
  •   
  •         this.name = name;   
  •   
  •     }   
  •   
  •     public Integer getSex() {   
  •   
  •         return this.sex;   
  •   
  •     }   
  •   
  •     public void setSex(Integer sex) {   
  •   
  •         this.sex = sex;   
  •   
  •     }   
  •   
  • }   
    1.  

    xml 代码
    1. <!--sp-->xml version="1.0" encoding="UTF-8"?>  
    2.   
    3. <!--CTYPE sqlMap    </sp-->
    4.     PUBLIC "-//iBATIS.com//DTD SQL Map 2.0//EN"    
    5.     "http://www.ibatis.com/dtd/sql-map-2.dtd">  
    6.   
    7. <sqlMap namespace="User">  
    8.   
    9.     <typeAlias alias="user" type="com.ctgusec.zhupan.model.User" />  
    10.     <select id="getUser" parameterClass="java.lang.String"  
    11.         resultClass="user">  
    12.         <!--DATA[  </sp--> 
    13.     select   
    14.       name,   
    15.       sex   
    16.     from t_user   
    17.     where name = #name#   
    18.     ]]>  
    19.     select>  
    20.   
    21.     <select id="getAllUser" resultClass="user">  
    22.         <!--DATA[  </sp--> 
    23.     select   
    24.       name,   
    25.       sex   
    26.     from t_user   where name = #name#    
    27.     ]]>  
    28.     select>  
    29.   
    30.     <update id="updateUser" parameterClass="user">  
    31.         <!--DATA[  </sp--> 
    32.     UPDATE t_user   
    33.     SET    
    34.       name=#name#,   
    35.       sex=#sex#   
    36.   WHERE id = #id#   
    37. ]]>  
    38.     update>  
    39.   
    40.     <insert id="insertUser" parameterClass="user">  
    41.         INSERT INTO t_user ( name, sex) VALUES ( #name#, #sex# )   
    42.     insert>  
    43.   
    44.     <delete id="deleteUser" parameterClass="java.lang.String">  
    45.         delete from t_user where id=#value#   
    46.     delete>  
    47.   
    48. sqlMap>  

     

       ID
    java 代码 
    1. package com.ctgusec.zhupan;   
    2.   
    3. import java.sql.SQLException;   
    4. import java.util.List;   
    5.   
    6. import com.ctgusec.zhupan.model.User;   
    7. import com.ibatis.sqlmap.client.SqlMapClientBuilder;   
    8.   
    9. /**  
    10.  *   
    11.  * @author zhupan  
    12.  */  
    13. public class ExampleMain {   
    14.   
    15.     public static void update() {   
    16.         //首先初始化iBatis获得一个SqlMapClient对象   
    17.         String resource = "com/ctgusec/zhupan/maps/SqlMapConfig.xml";   
    18.         com.ibatis.sqlmap.client.SqlMapClient sqlMap = null;   
    19.         try {   
    20.             java.io.Reader reader = com.ibatis.common.resources.Resources   
    21.                     .getResourceAsReader(resource);   
    22.             sqlMap = SqlMapClientBuilder.buildSqlMapClient(reader);   
    23.         } catch (Exception e) {   
    24.             e.printStackTrace();   
    25.         }   
    26.         // sqlMap系统初始化完毕,开始执行update操作   
    27.         try {   
    28.             sqlMap.startTransaction();   
    29.             User user = new User();   
    30.             user.setId(new Integer(1));   
    31.             user.setName("zhupan");   
    32.             user.setSex(new Integer(1));   
    33.             sqlMap.update("updateUser", user);   
    34.             sqlMap.commitTransaction();   
    35.         } catch (SQLException e) {   
    36.             System.out.println(e.getMessage());   
    37.         } finally {   
    38.             try {   
    39.                 sqlMap.endTransaction();   
    40.             } catch (SQLException e) {   
    41.                 e.printStackTrace();   
    42.             }   
    43.         }   
    44.     }   
    45.     public static List getUser() {   
    46. //      首先初始化iBatis获得一个SqlMapClient对象   
    47.         String resource = "com/ctgusec/zhupan/maps/SqlMapConfig.xml";   
    48.         com.ibatis.sqlmap.client.SqlMapClient sqlMap = null;   
    49.         List user=null;   
    50.         try {   
    51.             java.io.Reader reader = com.ibatis.common.resources.Resources   
    52.                     .getResourceAsReader(resource);   
    53.             sqlMap = SqlMapClientBuilder.buildSqlMapClient(reader);   
    54.         } catch (Exception e) {   
    55.             e.printStackTrace();   
    56.         }   
    57.         // sqlMap系统初始化完毕,开始执行getAllUser操作   
    58.         try {   
    59.             sqlMap.startTransaction();                 
    60.             user=sqlMap.queryForList("getAllUser", null);   
    61.             sqlMap.commitTransaction();   
    62.         } catch (SQLException e) {   
    63.             System.out.println(e.getMessage());   
    64.         } finally {   
    65.             try {   
    66.                 sqlMap.endTransaction();   
    67.             } catch (SQLException e) {   
    68.                 e.printStackTrace();   
    69.             }   
    70.         }   
    71.         return user;   
    72.     }   
    73.     public static void main(String[] args) {   
    74.         update();   
    75.         List user=getUser();       
    76.         for(int i=0;i
    77.         {   
    78.             System.out.println(((User)user.get(i)).getName());   
    79.         }   
    80.   
    81.     }   
    82. }  

     

    抱歉!评论已关闭.