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

创新互联公司成都企业网站建设服务,提供成都网站设计、做网站网站开发,网站定制,建网站,网站搭建,网站设计,成都响应式网站建设,网页设计师打造企业风格网站,提供周到的售前咨询和贴心的售后服务。欢迎咨询做网站需要多少钱:18982081108
- 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入门程序就向你介绍到这里,希望对于你了解iBATIS有所帮助。
【编辑推荐】
                分享名称:iBATIS入门程序六大步详解
                
                网页URL:http://www.csdahua.cn/qtweb/news40/254040.html
            
网站建设、网络推广公司-快上网,是专注品牌与效果的网站制作,网络营销seo公司;服务项目有等
声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 快上网