温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

iBATIS入门程序的示例分析

发布时间:2021-12-27 09:24:09 来源:亿速云 阅读:145 作者:小新 栏目:编程语言

小编给大家分享一下iBATIS入门程序的示例分析,希望大家阅读完这篇文章之后都有所收获,下面让我们一起去探讨吧!

iBATIS入门程序第一步:author.java

package com.ibatis;   public class Author {       private int id;       private String name;       public int getId() {          return id;       }       public void setId(int id) {          this.id = id;       }       public String getName() {          return name;       }       public void setName(String name) {          this.name = name;       }   }

iBATIS入门程序第二步:author.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="Author"﹥   ﹤!--模块配置--﹥   ﹤!--设置本映射文件中的别名--﹥   ﹤typeAlias alias="author" type="com.ibatis.Author" /﹥   ﹤!--   ﹤cacheModel type="LRU" ﹥    设置缓存有效期,如果超出这个时间,则会清空缓存    ﹤flushInterval hours="24"﹥﹤/flushInterval﹥     指定执行特定的statement时,清空缓存    ﹤flushOnExecute statement="updateAuthor"/﹥    SIZE:本cacheModel***容纳数据对象的数量    ﹤property value="1000"/﹥   ﹤/cacheModel﹥   需要使用模块配置,如:﹤select resultClass="author" cacheModel="authorCache"﹥   把记录使用cacheModel"authorCache"进行缓存,以后程序再使用statement进行数据查询,就直接   去缓存中取数据,而不是去数据库中取数据   --﹥   ﹤!--Statement配置--﹥       ﹤select resultClass="author"﹥    ﹤![CDATA[SELECT * FROM author]]﹥   ﹤/select﹥       ﹤update parameterClass="author"﹥    ﹤![CDATA[UPDATE author SET WHERE ﹥    ﹤/update﹥       ﹤delete parameterClass="author"﹥      delete from author WHERE    ﹤/delete﹥        ﹤insert parameterClass="author"﹥    ﹤![CDATA[INSERT INTO author(id,name) VALUES(#id#,#name#)]]﹥   ﹤/insert﹥   ﹤/sqlMap﹥

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

driver=com.microsoft.jdbc.sqlserver.SQLServerDriver   url=jdbc:microsoft:sqlserver://127.0.0.1:1433;DatabaseName=ibatis   username=sa   password=sa

iBATIS入门程序第四步:SqlMapConfig.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"﹥   ﹤!-- Ibatis配置文件--﹥   ﹤sqlMapConfig﹥   ﹤!-- 加载连接数据库属性文件 --﹥   ﹤properties resource="com/ibatis/SqlMapConfig.properties"/﹥   ﹤!--   cacheModelsEnabled:是否启动SqlMapClient的缓存机制。   enhancementEnabled:是否针对POJO启用字节码增加机制以提升geter/seter的调用效用,为延迟加载带来了及大的性能提升。   lazyLoadingEnabled:是否启用延迟加载机制。   maxRequests:***并大请求数。   maxSessions:***Session数,即当前***允许的开发SqlMapClient数   maxTransactions:***并发事务数。      --﹥   ﹤settings   cacheModelsEnabled="true"  enhancementEnabled="true"  lazyLoadingEnabled="true"  maxRequests="32"  maxSessions="10"  maxTransactions="5"  useStatementNamespaces="false"  /﹥   ﹤!-- datasource --﹥   ﹤transactionManager type="JDBC" ﹥   ﹤dataSource type="SIMPLE"﹥   ﹤!--JDBC驱动--﹥   ﹤property name=JDBC.Driver value="${driver}"/﹥   ﹤!--数据库URL--﹥   ﹤property value="${url}"/﹥   ﹤!--数据库用户名--﹥   ﹤property value="${username}"/﹥   ﹤!--数据库密码--﹥   ﹤property value="${password}"/﹥   ﹤!--不知道,在网站上查不出来,有时间再研究--﹥   ﹤property value="true" /﹥   ﹤!--数据库连接池可维持的***容量--﹥   ﹤property value="10"/﹥   ﹤!--数据库连接池中允许的可挂起连接数--﹥   ﹤property value="5"/﹥   ﹤!--数据库连接池中,连接被某个任务所占用的***时间--﹥   ﹤property value="120000"/﹥   ﹤!--当线程想从连接池中获取连接时,连接池中无可用连接,该参数设置线程所允许等待的最长时间--﹥   ﹤property value="500"/﹥   ﹤!--数据库连接状态检查语句--﹥   ﹤property value="select 1 from author"/﹥   ﹤!--是否允许检查连接状态--﹥   ﹤property value="false"/﹥   ﹤!--对持续连接超过设定值的连接进行检查--﹥   ﹤property value="1"/﹥   ﹤!--对空闲超过设定值的连接进行检查--﹥   ﹤property value="1"/﹥   ﹤/dataSource﹥   ﹤/transactionManager﹥   ﹤!--加载SqlMap文件--﹥   ﹤sqlMap resource="com/ibatis/author.xml" /﹥   ﹤/sqlMapConfig﹥

iBATIS入门程序第五步:

package com.ibatis;   import java.io.IOException;   import java.io.Reader;   import com.ibatis.common.resources.Resources;   import com.ibatis.sqlmap.client.SqlMapClient;   import com.ibatis.sqlmap.client.SqlMapClientBuilder;   public class SqlMapConf {    //初始化SqlMapClient    private static SqlMapClient sqlmapclient;    static{     //定义ibatis配置文件的路径     String resource="com/ibatis/SqlMapConfig.xml";     try {      //读取ibatis配置文件      Reader reader=Resources.getResourceAsReader(resource);      //通过SqlMapClientBuilder创建SqlMapClient      sqlmapclient=SqlMapClientBuilder.buildSqlMapClient(reader);     } catch (IOException e) {      // TODO Auto-generated catch block      System.out.println("找不到SqlMapConfig.xml文件~~");     }    }    public static SqlMapClient getInstance(){     //返回sqlmapclient,SqlMapClient是ibatis的核心主建,提供数据操作的基础平台          return sqlmapclient;    }    /**     * SqlMapClient的另一种创建方式     * XmlSqlMapClientBuilder xmlbuilder=new XmlSqlMapClientBuilder();     * SqlMapClient sqlmapclient=xmlbuilder.builderSqlMap(reader);     * XmlSqlMapClientBuilder是ibatis2.0之后版本新引入的组件,用以取代1.X版本中的     * XmlSqlMapBuilder,其作用就是创建SqlMapClient。     */   }

iBATIS入门程序第六步:

package com.ibatis;   import java.sql.SQLException;   import java.util.List;   import java.util.*;   import com.ibatis.sqlmap.client.SqlMapClient;   /**    * ibatis的事务管理器,目前只支持三种:JDBC,JTA,EXTERNAL    * JDBC:通过传统的JDBC CONNECTION.COMIT/rollback实现事务支持    * JTA:使用容器提供的JTA服务实现全局事务管理    * EXTERNAL:外部事务管理,如EJB中使用IBATIS,通过EJB的部署配置即可实现自动的事务管理机制    * 。此时IBATIS将把所有的事务委托给外部容器进行管理    */  public class IbatisClient {    private static SqlMapClient sqlmapclient=SqlMapConf.getInstance();    //根据主健ID修改NAME    public static void updateAuthor(int id,String name){     Author author=new Author();        author.setId(id);        author.setName(name);        try {         //事务开始,用的是JDBC的事务管理         sqlmapclient.startTransaction();      sqlmapclient.update("updateAuthor",author);      sqlmapclient.commitTransaction();     } catch (SQLException e) {      // TODO Auto-generated catch block      e.printStackTrace();      System.out.println("修改错误~~");     }     finally{      try {       sqlmapclient.endTransaction();      } catch (SQLException e) {       // TODO Auto-generated catch block       e.printStackTrace();      }     }    }    //查询所有的记录,返回一个集合    public static List findAll(){     List list=null;     try {      sqlmapclient.startTransaction();      //0:设置从第几条记录开始      //1:设置显示记录记录      //list=sqlmapclient.queryForList("getAllAuthor",null,0,1);      list=sqlmapclient.queryForList("getAllAuthor",null);      sqlmapclient.commitTransaction();     } catch (SQLException e) {      // TODO Auto-generated catch block      e.printStackTrace();      System.out.println("查询错误~~");     }     finally{      try {       sqlmapclient.endTransaction();      } catch (SQLException e) {       // TODO Auto-generated catch block       e.printStackTrace();      }     }     return list;    }    //添加操作    public static boolean insert(int id,String name){     boolean bool=false;     Author author=new Author();     author.setId(id);     author.setName(name);     try {      sqlmapclient.startTransaction();      sqlmapclient.insert("insertAuthor",author);      bool=true;      sqlmapclient.commitTransaction();     } catch (SQLException e) {      // TODO Auto-generated catch block      bool=false;      e.printStackTrace();      System.out.println("添加错误~~");     }     finally{      try {       sqlmapclient.endTransaction();      } catch (SQLException e) {       // TODO Auto-generated catch block       e.printStackTrace();      }     }     return bool;    }    //删除操作    public static boolean delete(int id){     boolean bool=false;     Author author=new Author();     author.setId(id);     try {      sqlmapclient.commitTransaction();      sqlmapclient.delete("deleteAuthor",author);      bool=true;      sqlmapclient.startTransaction();     } catch (SQLException e) {      // TODO Auto-generated catch block      bool=false;      e.printStackTrace();      System.out.println("删除错误~~");     }     finally{      try {       sqlmapclient.endTransaction();      } catch (SQLException e) {       // TODO Auto-generated catch block       e.printStackTrace();      }     }     return bool;    }       public static void main(String str[]){        //删除        //boolean bool=IbatisClient.delete(3);        //添加        //boolean bool=IbatisClient.insert(3,"wanwu");        //修改        //IbatisClient.updateAuthor(3,"jj");        //查询所有的记录        List list=IbatisClient.findAll();        Iterator iterator=list.iterator();        while(iterator.hasNext()){         Author author=(Author)iterator.next();         System.out.println("﹥          System.out.println("﹥         }       }   }

看完了这篇文章,相信你对“iBATIS入门程序的示例分析”有了一定的了解,如果想了解更多相关知识,欢迎关注亿速云行业资讯频道,感谢各位的阅读!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI