iBATIS入门程序六大步详解

开发 后端
iBATIS入门程序六大步主要是在六个程序基础上的,那么本文就将向你介绍iBATIS入门程序的具体操作。

iBATIS入门程序***步:author.java

  1. package com.ibatis;  
  2.  
  3. public class Author {  
  4.  
  5.     private int id;  
  6.  
  7.     private String name;  
  8.  
  9.     public int getId() {  
  10.  
  11.        return id;  
  12.  
  13.     }  
  14.  
  15.     public void setId(int id) {  
  16.  
  17.        this.id = id;  
  18.  
  19.     }  
  20.  
  21.     public String getName() {  
  22.  
  23.        return name;  
  24.  
  25.     }  
  26.  
  27.     public void setName(String name) {  
  28.  
  29.        this.name = name;  
  30.  
  31.     }  
  32.  

iBATIS入门程序第二步:author.xml

  1. ﹤?xml version="1.0" encoding="UTF-8" ?﹥  
  2.  
  3. ﹤!DOCTYPE sqlMap  
  4.  
  5. PUBLIC "-//iBATIS.com//DTD SQL Map 2.0//EN"  
  6.  
  7. "http://www.ibatis.com/dtd/sql-map-2.dtd"﹥  
  8.  
  9. ﹤sqlMap namespace="Author"﹥  
  10.  
  11. ﹤!--模块配置--﹥  
  12.  
  13. ﹤!--设置本映射文件中的别名--﹥  
  14.  
  15. ﹤typeAlias alias="author" type="com.ibatis.Author" /﹥  
  16.  
  17. ﹤!--  
  18.  
  19. ﹤cacheModel type="LRU" ﹥  
  20.  
  21.  设置缓存有效期,如果超出这个时间,则会清空缓存  
  22.  
  23.  ﹤flushInterval hours="24"﹥﹤/flushInterval﹥   
  24.  
  25.  指定执行特定的statement时,清空缓存  
  26.  
  27.  ﹤flushOnExecute statement="updateAuthor"/﹥  
  28.  
  29.  SIZE:本cacheModel***容纳数据对象的数量  
  30.  
  31.  ﹤property value="1000"/﹥  
  32.  
  33. ﹤/cacheModel﹥  
  34.  
  35. 需要使用模块配置,如:﹤select resultClass="author" cacheModel="authorCache"﹥  
  36.  
  37. 把记录使用cacheModel"authorCache"进行缓存,以后程序再使用statement进行数据查询,就直接  
  38.  
  39. 去缓存中取数据,而不是去数据库中取数据  
  40.  
  41. --﹥  
  42.  
  43. ﹤!--Statement配置--﹥  
  44.  
  45.    
  46.  
  47. ﹤select resultClass="author"﹥  
  48.  
  49.  ﹤![CDATA[SELECT * FROM author]]﹥  
  50.  
  51. ﹤/select﹥  
  52.  
  53.    
  54.  
  55. ﹤update parameterClass="author"﹥  
  56.  
  57.  ﹤![CDATA[UPDATE author SET WHERE ﹥   
  58.  
  59. ﹤/update﹥  
  60.  
  61.    
  62.  
  63. ﹤delete parameterClass="author"﹥   
  64.  
  65.   delete from author WHERE   
  66.  
  67. ﹤/delete﹥   
  68.  
  69.    
  70.  
  71. ﹤insert parameterClass="author"﹥  
  72.  
  73.  ﹤![CDATA[INSERT INTO author(id,name) VALUES(#id#,#name#)]]﹥  
  74.  
  75. ﹤/insert﹥  
  76.  
  77. ﹤/sqlMap﹥ 

iBATIS入门程序第三步:SqlMapConfig.properties

  1. driver=com.microsoft.jdbc.sqlserver.SQLServerDriver  
  2.  
  3. url=jdbc:microsoft:sqlserver://127.0.0.1:1433;DatabaseName=ibatis  
  4.  
  5. username=sa  
  6.  
  7. password=sa 

iBATIS入门程序第四步:SqlMapConfig.xml

  1. ﹤?xml version="1.0" encoding="UTF-8" ?﹥  
  2.  
  3. ﹤!DOCTYPE sqlMapConfig  
  4.  
  5. PUBLIC "-//iBATIS.com//DTD SQL Map Config 2.0//EN"  
  6.  
  7. "http://www.ibatis.com/dtd/sql-map-config-2.dtd"﹥  
  8.  
  9. ﹤!-- Ibatis配置文件--﹥  
  10.  
  11. ﹤sqlMapConfig﹥  
  12.  
  13. ﹤!-- 加载连接数据库属性文件 --﹥  
  14.  
  15. ﹤properties resource="com/ibatis/SqlMapConfig.properties"/﹥  
  16.  
  17. ﹤!--  
  18.  
  19. cacheModelsEnabled:是否启动SqlMapClient的缓存机制。  
  20.  
  21. enhancementEnabled:是否针对POJO启用字节码增加机制以提升geter/seter的调用效用,为延迟加载带来了及大的性能提升。  
  22.  
  23. lazyLoadingEnabled:是否启用延迟加载机制。  
  24.  
  25. maxRequests:***并大请求数。  
  26.  
  27. maxSessions:***Session数,即当前***允许的开发SqlMapClient数  
  28.  
  29. maxTransactions:***并发事务数。     
  30.  
  31. --﹥  
  32.  
  33. ﹤settings  
  34.  
  35. cacheModelsEnabled="true" 
  36.  
  37. enhancementEnabled="true" 
  38.  
  39. lazyLoadingEnabled="true" 
  40.  
  41. maxRequests="32" 
  42.  
  43. maxSessions="10" 
  44.  
  45. maxTransactions="5" 
  46.  
  47. useStatementNamespaces="false" 
  48.  
  49. /﹥  
  50.  
  51. ﹤!-- datasource --﹥  
  52.  
  53. ﹤transactionManager type="JDBC" ﹥  
  54.  
  55. ﹤dataSource type="SIMPLE"﹥  
  56.  
  57. ﹤!--JDBC驱动--﹥  
  58.  
  59. ﹤property name=JDBC.Driver value="${driver}"/﹥  
  60.  
  61. ﹤!--数据库URL--﹥  
  62.  
  63. ﹤property value="${url}"/﹥  
  64.  
  65. ﹤!--数据库用户名--﹥  
  66.  
  67. ﹤property value="${username}"/﹥  
  68.  
  69. ﹤!--数据库密码--﹥  
  70.  
  71. ﹤property value="${password}"/﹥  
  72.  
  73. ﹤!--不知道,在网站上查不出来,有时间再研究--﹥  
  74.  
  75. ﹤property value="true" /﹥  
  76.  
  77. ﹤!--数据库连接池可维持的***容量--﹥  
  78.  
  79. ﹤property value="10"/﹥  
  80.  
  81. ﹤!--数据库连接池中允许的可挂起连接数--﹥  
  82.  
  83. ﹤property value="5"/﹥  
  84.  
  85. ﹤!--数据库连接池中,连接被某个任务所占用的***时间--﹥  
  86.  
  87. ﹤property value="120000"/﹥  
  88.  
  89. ﹤!--当线程想从连接池中获取连接时,连接池中无可用连接,该参数设置线程所允许等待的最长时间--﹥  
  90.  
  91. ﹤property value="500"/﹥  
  92.  
  93. ﹤!--数据库连接状态检查语句--﹥  
  94.  
  95. ﹤property value="select 1 from author"/﹥  
  96.  
  97. ﹤!--是否允许检查连接状态--﹥  
  98.  
  99. ﹤property value="false"/﹥  
  100.  
  101. ﹤!--对持续连接超过设定值的连接进行检查--﹥  
  102.  
  103. ﹤property value="1"/﹥  
  104.  
  105. ﹤!--对空闲超过设定值的连接进行检查--﹥  
  106.  
  107. ﹤property value="1"/﹥  
  108.  
  109. ﹤/dataSource﹥  
  110.  
  111. ﹤/transactionManager﹥  
  112.  
  113. ﹤!--加载SqlMap文件--﹥  
  114.  
  115. ﹤sqlMap resource="com/ibatis/author.xml" /﹥  
  116.  
  117. ﹤/sqlMapConfig﹥ 

iBATIS入门程序第五步:

  1. package com.ibatis;  
  2.  
  3. import java.io.IOException;  
  4.  
  5. import java.io.Reader;  
  6.  
  7. import com.ibatis.common.resources.Resources;  
  8.  
  9. import com.ibatis.sqlmap.client.SqlMapClient;  
  10.  
  11. import com.ibatis.sqlmap.client.SqlMapClientBuilder;  
  12.  
  13. public class SqlMapConf {  
  14.  
  15.  //初始化SqlMapClient  
  16.  
  17.  private static SqlMapClient sqlmapclient;  
  18.  
  19.  static{  
  20.  
  21.   //定义ibatis配置文件的路径  
  22.  
  23.   String resource="com/ibatis/SqlMapConfig.xml";  
  24.  
  25.   try {  
  26.  
  27.    //读取ibatis配置文件  
  28.  
  29.    Reader reader=Resources.getResourceAsReader(resource);  
  30.  
  31.    //通过SqlMapClientBuilder创建SqlMapClient  
  32.  
  33.    sqlmapclient=SqlMapClientBuilder.buildSqlMapClient(reader);  
  34.  
  35.   } catch (IOException e) {  
  36.  
  37.    // TODO Auto-generated catch block  
  38.  
  39.    System.out.println("找不到SqlMapConfig.xml文件~~");  
  40.  
  41.   }  
  42.  
  43.  }  
  44.  
  45.  public static SqlMapClient getInstance(){  
  46.  
  47.   //返回sqlmapclient,SqlMapClient是ibatis的核心主建,提供数据操作的基础平台  
  48.  
  49.     
  50.  
  51.   return sqlmapclient;  
  52.  
  53.  }  
  54.  
  55.  /**  
  56.  
  57.   * SqlMapClient的另一种创建方式  
  58.  
  59.   * XmlSqlMapClientBuilder xmlbuilder=new XmlSqlMapClientBuilder();  
  60.  
  61.   * SqlMapClient sqlmapclient=xmlbuilder.builderSqlMap(reader);  
  62.  
  63.   * XmlSqlMapClientBuilder是ibatis2.0之后版本新引入的组件,用以取代1.X版本中的  
  64.  
  65.   * XmlSqlMapBuilder,其作用就是创建SqlMapClient。  
  66.  
  67.   */  
  68.  

iBATIS入门程序第六步:

  1. package com.ibatis;  
  2.  
  3. import java.sql.SQLException;  
  4.  
  5. import java.util.List;  
  6.  
  7. import java.util.*;  
  8.  
  9. import com.ibatis.sqlmap.client.SqlMapClient;  
  10.  
  11. /**  
  12.  
  13.  * ibatis的事务管理器,目前只支持三种:JDBC,JTA,EXTERNAL  
  14.  
  15.  * JDBC:通过传统的JDBC CONNECTION.COMIT/rollback实现事务支持  
  16.  
  17.  * JTA:使用容器提供的JTA服务实现全局事务管理  
  18.  
  19.  * EXTERNAL:外部事务管理,如EJB中使用IBATIS,通过EJB的部署配置即可实现自动的事务管理机制  
  20.  
  21.  * 。此时IBATIS将把所有的事务委托给外部容器进行管理  
  22.  
  23.  */ 
  24.  
  25. public class IbatisClient {  
  26.  
  27.  private static SqlMapClient sqlmapclient=SqlMapConf.getInstance();  
  28.  
  29.  //根据主健ID修改NAME  
  30.  
  31.  public static void updateAuthor(int id,String name){  
  32.  
  33.   Author author=new Author();  
  34.  
  35.      author.setId(id);  
  36.  
  37.      author.setName(name);  
  38.  
  39.      try {  
  40.  
  41.       //事务开始,用的是JDBC的事务管理  
  42.  
  43.       sqlmapclient.startTransaction();  
  44.  
  45.    sqlmapclient.update("updateAuthor",author);  
  46.  
  47.    sqlmapclient.commitTransaction();  
  48.  
  49.   } catch (SQLException e) {  
  50.  
  51.    // TODO Auto-generated catch block  
  52.  
  53.    e.printStackTrace();  
  54.  
  55.    System.out.println("修改错误~~");  
  56.  
  57.   }  
  58.  
  59.   finally{  
  60.  
  61.    try {  
  62.  
  63.     sqlmapclient.endTransaction();  
  64.  
  65.    } catch (SQLException e) {  
  66.  
  67.     // TODO Auto-generated catch block  
  68.  
  69.     e.printStackTrace();  
  70.  
  71.    }  
  72.  
  73.   }  
  74.  
  75.  }  
  76.  
  77.  //查询所有的记录,返回一个集合  
  78.  
  79.  public static List findAll(){  
  80.  
  81.   List list=null;  
  82.  
  83.   try {  
  84.  
  85.    sqlmapclient.startTransaction();  
  86.  
  87.    //0:设置从第几条记录开始  
  88.  
  89.    //1:设置显示记录记录  
  90.  
  91.    //list=sqlmapclient.queryForList("getAllAuthor",null,0,1);  
  92.  
  93.    list=sqlmapclient.queryForList("getAllAuthor",null);  
  94.  
  95.    sqlmapclient.commitTransaction();  
  96.  
  97.   } catch (SQLException e) {  
  98.  
  99.    // TODO Auto-generated catch block  
  100.  
  101.    e.printStackTrace();  
  102.  
  103.    System.out.println("查询错误~~");  
  104.  
  105.   }  
  106.  
  107.   finally{  
  108.  
  109.    try {  
  110.  
  111.     sqlmapclient.endTransaction();  
  112.  
  113.    } catch (SQLException e) {  
  114.  
  115.     // TODO Auto-generated catch block  
  116.  
  117.     e.printStackTrace();  
  118.  
  119.    }  
  120.  
  121.   }  
  122.  
  123.   return list;  
  124.  
  125.  }  
  126.  
  127.  //添加操作  
  128.  
  129.  public static boolean insert(int id,String name){  
  130.  
  131.   boolean bool=false;  
  132.  
  133.   Author author=new Author();  
  134.  
  135.   author.setId(id);  
  136.  
  137.   author.setName(name);  
  138.  
  139.   try {  
  140.  
  141.    sqlmapclient.startTransaction();  
  142.  
  143.    sqlmapclient.insert("insertAuthor",author);  
  144.  
  145.    bool=true;  
  146.  
  147.    sqlmapclient.commitTransaction();  
  148.  
  149.   } catch (SQLException e) {  
  150.  
  151.    // TODO Auto-generated catch block  
  152.  
  153.    bool=false;  
  154.  
  155.    e.printStackTrace();  
  156.  
  157.    System.out.println("添加错误~~");  
  158.  
  159.   }  
  160.  
  161.   finally{  
  162.  
  163.    try {  
  164.  
  165.     sqlmapclient.endTransaction();  
  166.  
  167.    } catch (SQLException e) {  
  168.  
  169.     // TODO Auto-generated catch block  
  170.  
  171.     e.printStackTrace();  
  172.  
  173.    }  
  174.  
  175.   }  
  176.  
  177.   return bool;  
  178.  
  179.  }  
  180.  
  181.  //删除操作  
  182.  
  183.  public static boolean delete(int id){  
  184.  
  185.   boolean bool=false;  
  186.  
  187.   Author author=new Author();  
  188.  
  189.   author.setId(id);  
  190.  
  191.   try {  
  192.  
  193.    sqlmapclient.commitTransaction();  
  194.  
  195.    sqlmapclient.delete("deleteAuthor",author);  
  196.  
  197.    bool=true;  
  198.  
  199.    sqlmapclient.startTransaction();  
  200.  
  201.   } catch (SQLException e) {  
  202.  
  203.    // TODO Auto-generated catch block  
  204.  
  205.    bool=false;  
  206.  
  207.    e.printStackTrace();  
  208.  
  209.    System.out.println("删除错误~~");  
  210.  
  211.   }  
  212.  
  213.   finally{  
  214.  
  215.    try {  
  216.  
  217.     sqlmapclient.endTransaction();  
  218.  
  219.    } catch (SQLException e) {  
  220.  
  221.     // TODO Auto-generated catch block  
  222.  
  223.     e.printStackTrace();  
  224.  
  225.    }  
  226.  
  227.   }  
  228.  
  229.   return bool;  
  230.  
  231.  }  
  232.  
  233.     public static void main(String str[]){  
  234.  
  235.      //删除  
  236.  
  237.      //boolean bool=IbatisClient.delete(3);  
  238.  
  239.      //添加  
  240.  
  241.      //boolean bool=IbatisClient.insert(3,"wanwu");  
  242.  
  243.      //修改  
  244.  
  245.      //IbatisClient.updateAuthor(3,"jj");  
  246.  
  247.      //查询所有的记录  
  248.  
  249.      List list=IbatisClient.findAll();  
  250.  
  251.      Iterator iterator=list.iterator();  
  252.  
  253.      while(iterator.hasNext()){  
  254.  
  255.       Author author=(Author)iterator.next();  
  256.  
  257.       System.out.println("﹥   
  258.  
  259.       System.out.println("﹥   
  260.  
  261.      }  
  262.  
  263.     }  
  264.  

iBATIS入门程序就向你介绍到这里,希望对于你了解iBATIS有所帮助。

【编辑推荐】

  1. 在iBATIS.NET中调用存储过程浅析
  2. Struts2.0+Springframework2.5+ibatis2.3***整合实例
  3. Struts2.0+ibatis2.3***整合实例之映射浅析
  4. Struts2.0+ibatis2.3整合实例实现服务层及Struts
  5. Struts2.0+ibatis2.3整合实例实现UI层浅析
责任编辑:仲衡 来源: 百度空间
相关推荐

2009-08-05 15:29:54

IP连接故障交换机连接

2009-07-17 10:20:24

iBATIS实例

2012-04-18 13:29:14

Web过滤

2021-08-16 08:44:56

UPS电池寿命

2010-05-31 10:32:38

面试简历

2023-06-09 13:35:00

数字化转型工业

2022-05-15 23:32:00

元宇宙虚拟世界科技

2022-02-25 20:44:28

框架深度学习架构

2013-12-12 13:02:01

2012-02-20 10:11:53

PhoneGap

2017-01-19 09:45:53

红帽

2009-09-15 16:53:50

2011-05-26 16:04:17

java

2019-07-30 08:30:40

Python主流数据库

2010-08-16 10:14:23

云计算误区

2010-09-25 15:22:19

DHCP故障处理

2009-05-08 09:20:56

IT创业员工就业

2012-12-04 11:07:29

专家惠普转型

2015-01-10 01:10:29

2012-06-07 09:18:16

ibmdw
点赞
收藏

51CTO技术栈公众号