`

ibatis 开始

阅读更多

参考了官方的 iBATIS-SqlMaps-2-Tutorial_cn.pdf 

注意 6中一定要加上try catch 否则会报Unhandled exception type SQLException错误

1.Person.java

根据自己的数据库表制作Person.java,他也可能是User.java,Company.java等等

 

2.Person.java

Code
<!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />--> 1<?xml version="1.0" encoding="UTF-8"?>
 2
 3<!DOCTYPE sqlMap
 4    PUBLIC "-//iBATIS.com//DTD SQL Map 2.0//EN"
 5     "http://www.ibatis.com/dtd/sql-map-2.dtd">
 6     
 7<sqlMap namespace="Person">
 8
 9 <select id="getPerson" parameterClass="int" resultClass="com.xc.ibatis.Person">
10  <![CDATA[  
11      select 
12          *
13      from Person 
14      WHERE id = #value#
15      ]]>
16 </select>
17 
18</sqlMap>

 

3.SqlMapConfig.xml

Code
<!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />--> 1<?xml version="1.0" encoding="UTF-8" standalone="no"?>
 2<!DOCTYPE sqlMapConfig PUBLIC "-//iBATIS.com//DTD SQL Map Config 2.0//EN" "http://www.ibatis.com/dtd/sql-map-config-2.dtd">
 3
 4<sqlMapConfig>
 5
 6 <properties resource="SqlMapConfig.properties" />
 7 <settings 
 8     cacheModelsEnabled="true" 
 9     lazyLoadingEnabled="true"
10      enhancementEnabled="true" 
11     maxSessions="64" 
12      maxTransactions="8"
13     maxRequests="128" />
14
15 <transactionManager type="JDBC">
16  <dataSource type="SIMPLE">
17   <property value="${driver}" name="JDBC.Driver" />
18   <property value="${url}" name="JDBC.ConnectionURL" />
19   <property value="${username}" name="JDBC.Username" />
20   <property value="${password}" name="JDBC.Password" />
21   <property value="15" name="Pool.MaximumActiveConnections" />
22   <property value="15" name="Pool.MaximumIdleConnections" />
23   <property value="1000" name="Pool.MaximumWait" />
24  </dataSource>
25 </transactionManager>
26
27 <sqlMap resource="com/xc/ibatis/Person.xml" />
28
29</sqlMapConfig>
30

 

4.SqlMapConfig.properties

<!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />-->username=user

password=pass
url=jdbc:oracle:thin:@192.168.0.78:1521:ora9i
driver=oracle.jdbc.driver.OracleDriver

 

 

 

5.MyAppSqlConfig.java

Code
<!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />--> 1package com.xc.ibatis;
 2
 3import java.io.Reader;
 4
 5import com.ibatis.common.resources.Resources;
 6import com.ibatis.sqlmap.client.SqlMapClient;
 7import com.ibatis.sqlmap.client.SqlMapClientBuilder;
 8
 9public class MyAppSqlConfig {
10    private static final SqlMapClient sqlMap;
11    
12    static{
13        try{
14            String resource = "SqlMapConfig.xml";
15            Reader reader = Resources.getResourceAsReader(resource);
16            sqlMap = SqlMapClientBuilder.buildSqlMapClient(reader);
17            reader.close();
18        }
catch(Exception e){
19            e.printStackTrace();
20            throw new RuntimeException("Error initializing MyAppSqlConfig class. Cause:"+ e);
21        }

22    }

23    
24    public static SqlMapClient getSqlMapInstance(){
25        return sqlMap;
26    }

27}

28

 

6.testibatis.java

Code
<!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />--> 1package Test;
 2
 3import com.ibatis.sqlmap.client.SqlMapClient;
 4import com.xc.ibatis.*;
 5
 6public class testibatis {
 7
 8    /** *//**
 9     * @param args
10     */

11    public static void main(String[] args) {
12        // TODO Auto-generated method stub
13        SqlMapClient sqlMap = MyAppSqlConfig.getSqlMapInstance();
14        try{
15            
16        Integer Orgid = new Integer(3);
17        Person person = (Person)sqlMap.queryForObject("getPerson", id);
18        System.out.println(person.getName()+ " " + person.getAge());
19        }
catch(Exception e){
20            e.printStackTrace();
21            throw new RuntimeException("Error testibatis Cause:"+ e);
22        }

23    }

24
25}

26

 

 

分享到:
评论

相关推荐

    ibatis开始流程

    ibatis开始流程

    ibatis应对批量update

    最近遇到需要批量update数据的问题,一开始用了一个for循环去update,数据量大的时候效率很低。原因是for循环每次update一条语句,都是一次连接过程。遇到大批数据更新的时候,效率就可想而知了。在google上找了一遍...

    springboot+mysql+ibatis完整整合案例

    springboot+mysql+ibatis完整整合案例,代码可以直接运行,适合新手入门学习,加油

    Ibatis(中文文档,还有一个调好的例子)

    下面我们看一个最简单的入门例子,是《ibatis 开发指南》上的例子改的,不过上面讲的不仔细,我开始学的时候搞了一个晚上才把那个例子跑起来的,相信一些朋友也和我一样,在入门的时候有一点小郁闷,我把整个工程...

    对IBatis分页的改进,使ibatis支持hibernate式的物理分页

    公司的大部分项目都开始使用IBatis作为O/R Mapping了,但是在使用的过程中也发现了很多不方便和存在争议的地方,其中一个不方便的地方就是分页,目前的处理方式都是在sqlMap中写针对特定数据库的物理分页Sql语句,对于...

    iBATIS实战

    本书是讲述iBATIS框架的权威著作。书中既详实地介绍了iBATIS的设计理念和基础知识,也讨论了动态SQL、高速缓存、DAD框架等高级主题,还讲解了iBATIS在实际开发中的应用。书的最后给出了一个设计优雅、层次清晰的示例...

    ibatis 帮助文档

    对刚开始学习ibatis 的人真的很有用,希望能帮到您

    iBatis介绍文档.rar

    从现在开始我们只对 for Java 版的SQL Maps 展开讨论。 你可以在 http://www.ibatis.com 下载 iBATIS for Java,目前最新版本是2.0.9,压缩包里已经包含了 SQL Maps(ibatis-sqlmap-2.jar) 和 DAO 框架(ibatis-dao-2....

    Struts2.0+Springframework2.5+ibatis2.3完美整合用户登录及增删改查

    本演示示例主要使用目前最新,最流行技术Struts2.1 +Spring 2.5.1+ibatis2.3整合开发而成,这与我以前发布的版本中最大特色是整合了Spring2.5.1中的注解功能和半自动化工具ibatis技术,这是本示例的两大特色,简化了配置...

    ibatis 开发指南(pdf)

    ibatis 开发指南 ibatis Quick Start............................................................................................ 5 准备工作...........................................................

    MyIbatIS中文版电子书

    轻松入门ibatis....从基础开始ibatIS..

    struts+iBATIS+spring整合例子

    开始入门iBATIS,spring,struts2的同志们可以去看看

    Spring 整合 iBATIS 文档 详解 加 源代码

    开始学习iBATIS一段时间了,这是第一次做iBATIS和spring整合的例子,测试运行成功,含源代码,代码中有规范的注释,希望能帮助读者。

    ibatis-2.3.0.677 jar 包 ibatis

    一开始学习的时候看才300多k没敢用,最后下了好多大的,其实这个300多k的才是真的,解压导入项目就行了,亲测可用

    ibatis in action

    英文版的ibatis in action,可以通过这本书开始学起!

    Spring+Struts+ibatis实现的简单student表

    这个是自己在刚开始接触java时写的一个Spring+Struts+ibatis结构的web工程,实现了对一张表的增删查改

    ibatis相关问题

    学习ibatis从这里开始 博文链接:https://imticg.iteye.com/blog/215856

    spring mvc3.0+ibatis3.0(基于oracle) Demo

    折腾了好几天,终于从零开始把spring mvc 3.0 + ibatis 3.0 (基于oracle)的Demo完全跑通了。Demo中有不少注释,使用的jar包基本是最精简的了。留着做个备用,也给大家一个参考!

Global site tag (gtag.js) - Google Analytics