Java天空

 

2007年8月19日

ibatis eclipse plugin Abator配置使用简单说明

安装:
         eclipse自动安装url: http://ibatis.apache.org/tools/abator

配置:
         安装好后将在New菜单中看到一个新的文件类型Abator for iBATIS Configuration File,这个是Abator的配置文件,new一个并修改配置文件,配置样式如下所示:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE abatorConfiguration PUBLIC "-//Apache Software Foundation//DTD Abator for iBATIS Configuration 1.0//EN"
  "http://ibatis.apache.org/dtd/abator-config_1_0.dtd"
>

<abatorConfiguration>
  
<abatorContext generatorSet="Java5">    <!-- TODO: Add Database Connection Information -->
    
<jdbcConnection driverClass="oracle.jdbc.driver.OracleDriver"
        connectionURL
="jdbc:oracle:thin:@127.0.0.1:1521:orcl"
        userId
="user"
        password
="password">
      
<!-- 注意:以下的classPathEntry一定要设置,否则会出现Exception JDBC Driver的错误 -->
      
<classPathEntry location="D:/workspace/lib/ojdbc14_g.jar" />
    
</jdbcConnection>

    
<javaModelGenerator targetPackage="com.iamyy.test.ibatis.vo" targetProject="AbatorTest" />
    
<sqlMapGenerator targetPackage="com.iamyy.test.ibatis.dao" targetProject="AbatorTest" />
    
<daoGenerator type="IBATIS" targetPackage="com.iamyy.test.ibatis.dao" targetProject="AbatorTest" />

    
<table tableName="T_Role" domainObjectName="Role">
      
<generatedKey column="role_id" identity="true" sqlStatement="select s_role.nextval from dual"/>
      
<columnOverride column="role_id" property="id" javaType="java.lang.String" jdbcType="VARCHAR"/>
      
<columnOverride column="role_name" property="name" javaType="java.lang.String" jdbcType="VARCHAR" />
    
</table>

  
</abatorContext>
</abatorConfiguration>


   
abatorContext的一个属性generatorSet有3个选项Legacy、Java2、Java5,一般用Java5;

         Legacy:如果没有generatorSet属性的话,默认是Legacy。但并不推荐使用Legacy因为它在生成Example类(用于查询条件)的时候有很多限制,他将查询条件写在sqlMap配置文件中,将查询值写在Example中,这样就对修改产生一些困难。

         Java2和Java5:他们只支持iBATIS 2.20以上的版本。在以这个模式成生的Example文件中包含了查询条件和查询值。这样修改就方便多了,对于用join的select时的查询就更方 便了,自己可以定义查询条件,自由度高了很多(对于join的and查询可能还得自己修改一下Example代码)。对于or和and的应用Legacy 的限制就比较大了。

         <javaModelGenerator>、 <sqlMapGenerator>、 <daoGenerator>描述了个自生成的位置。<daoGenerator>中type属性则用来告诉abator生成的DAO是用于iBATIS还是SPRING等容器。

         <table>告诉abator生成那个table的sqlMap。

         <generatedKey>来告诉abator那些列需要自动返回值(当插入的时候可以返回插入记录的主键,这对有外键的数据库极其有用)identity默认为false,则在sqlMap配置文件中<selectKey>的位置在sql命令前面,所以identity应该改为true。根据数据库类型的不同sqlStatement属性取值也不同。


运行:

         右键单击配置文件,点击Generate iBatis Artifacts,生成文件。

         ps: 如果<daoGenerator>中type属性是ibatis的话,由于Abator生成的DAOImpl文件继承自com.ibatis.dao.client.template.SqlMapDaoTemplate,在ibatis ver. 2.3.0.677中会有错误提示,需酌情修改。
         ps的ps: 不建议使用自动生成的Example去操作数据,一是因为Example中生成的方法有拼写SQL之嫌不符合OO思想,二是拼写后SQL效率有待进一步考证,没仔细看,但至少看到not in语句,如果要使用的话至少要进行一些修改。(^-^)个人愚见。
         
         参考:Abator document: http://ibatis.apache.org/docs/tools/abator/
                       iBATIS的Eclipse插件Abator使用方法

posted @ 2007-08-19 19:09 YY 阅读(3191) | 评论 (2)编辑 收藏

2007年8月16日

ibatis操作oracle数据库时,空值(null)异常的不完全解决方法

ibatis操作oracle数据库时,如果出现空值,ibatis不是插入NULL,而是出现异常,异常信息大致如下:

Caused by: com.ibatis.common.jdbc.exception.NestedSQLException:   
--- The error occurred in [someSqlMapFile.xml].  
--- The error occurred while applying a parameter map.  
--- Check the insertUser-InlineParameterMap.  
--- Check the parameter mapping for the '[someProperty]' property.  
--- Cause: java.sql.SQLException: 无效的列类型
    at com.ibatis.sqlmap.engine.mapping.statement.GeneralStatement.executeUpdate(GeneralStatement.java:
91)
    at com.ibatis.sqlmap.engine.impl.SqlMapExecutorDelegate.insert(SqlMapExecutorDelegate.java:
447)
    at com.ibatis.sqlmap.engine.impl.SqlMapSessionImpl.insert(SqlMapSessionImpl.java:
82)
    at com.ibatis.sqlmap.engine.impl.SqlMapClientImpl.insert(SqlMapClientImpl.java:
59)


如果你先插入空值,必须告诉IBatis当该字段出现NULL值该用什么值来替代,方法有2种,如下:

方法一(parameterClass):
1 INSERT INTO TEST(ID, NAME, PASSWD) VALUES (#id#,#name#, #passwd:VARCHAR:NULL#)

支持的类型在 java.sql.Types 中列示出来了
注意:DATA 类型默认不能为NULL

方法二(parameterMap):
使用这个方法时,首先应保证你的Oracle jdbc driver是10G以上版本,不然也是白搭。

 1 <parameterMap id="insert-person-paraMap" class="com.unmi.Person" >
 2     <parameter property="id"/>
 3     <parameter property="name"/>
 4     <parameter property="passwd" jdbcType="VARCHAR"/>
 5 </parameterMap>
 6     
 7 <!-- 插入一条Person对应的记录到数据库中 -->
 8 <insert id="insertPerson" parameterMap="insert-person-paraMap">
 9     INSERT INTO PERSON (ID, NAME, PASSWD) VALUES (?,?,?)
10 </insert>
11 

参考:http://www.blogjava.net/fatbear/archive/2007/06/07/122607.html 
            iBatis 应用程序向 Oralce 数据表字段插入 NULL 值
            作者:肥熊熊

posted @ 2007-08-16 16:18 YY 阅读(1805) | 评论 (1)编辑 收藏

2006年9月16日

Delphi 7使用DBExpress中的SQLConnection连接SQLSERVER数据库出错原因及解决

安装完Delphi 7后,使用DBExpress中的SQLConnection连接MS SQLSERVER时,如果数据库的sa用户没有设置密码的话,会提示出错。这是因为缺少一个补丁程序,可以去网上搜索:dbExpress Driver Patch1,下载这个补丁并安装,问题应该可以解决。
ps:如果Delphi是安装在除C盘外其它盘符,使用SQLConnection会找不到数据库驱动信息,需要修改注册表,将安装过补丁后的C:\Program Files\Common FIles\Borland*改成你自己的盘符。

    有问题可以跟我联系:yy-man@163.com

 

posted @ 2006-09-16 17:17 YY 阅读(746) | 评论 (0)编辑 收藏

2006年4月20日

commons DBCP 配置参数简要说明

  前段时间因为项目原因,要在修改数据库连接池到DBCP上,折腾了半天,有一点收获,不敢藏私,特在这里与朋友们共享。
  在配置时,主要难以理解的主要有:removeAbandoned 、logAbandoned、removeAbandonedTimeout、maxWait这四个参数,设置了rmoveAbandoned=true那么在getNumActive()快要到getMaxActive()的时候,系统会进行无效的Connection的回收,回收的Connection为removeAbandonedTimeout(默认300秒)中设置的秒数后没有使用的Connection,激活回收机制好像是getNumActive()=getMaxActive()-2。 :) 有点忘了。
  logAbandoned=true的话,将会在回收事件后,在log中打印出回收Connection的错误信息,包括在哪个地方用了Connection却忘记关闭了,在调试的时候很有用。
  在这里私人建议maxWait的时间不要设得太长,maxWait如果设置太长那么客户端会等待很久才激发回收事件。
  以下是我的配置的properties文件:
#连接设置
jdbc.driverClassName=oracle.jdbc.driver.OracleDriver
jdbc.url=jdbc:oracle:thin:@127.0.0.1:1521:DBSERVER
jdbc.username=user
jdbc.password=pass

#<!-- 初始化连接 -->
dataSource.initialSize=10

#<!-- 最大空闲连接 -->
dataSource.maxIdle=20

#<!-- 最小空闲连接 -->
dataSource.minIdle=5

#最大连接数量
dataSource.maxActive=50

#是否在自动回收超时连接的时候打印连接的超时错误
dataSource.logAbandoned=true

#是否自动回收超时连接
dataSource.removeAbandoned=true

#超时时间(以秒数为单位)
dataSource.removeAbandonedTimeout=180

#<!-- 超时等待时间以毫秒为单位 6000毫秒/1000等于60秒 -->
dataSource.maxWait=1000

  以下是我在连接控制中调用的方法:

        Properties  dbProps=null;
  //下面的读取配置文件可以根据实际的不同修改
        dbProps = ConfigProperties.getInstance().getProperties("jdbc.properties");
        try {
         String driveClassName = dbProps.getProperty("jdbc.driverClassName");
         String url = dbProps.getProperty("jdbc.url");
         String username = dbProps.getProperty("jdbc.username");
         String password = dbProps.getProperty("jdbc.password");
         
         String initialSize = dbProps.getProperty("dataSource.initialSize");
         String minIdle = dbProps.getProperty("dataSource.minIdle");
         String maxIdle = dbProps.getProperty("dataSource.maxIdle");
         String maxWait = dbProps.getProperty("dataSource.maxWait");
         String maxActive = dbProps.getProperty("dataSource.maxActive");
           //是否在自动回收超时连接的时候打印连接的超时错误
          boolean logAbandoned = (Boolean.valueOf(dbProps.getProperty("dataSource.logAbandoned","false"))).booleanValue();

          //是否自动回收超时连接
          boolean removeAbandoned = (Boolean.valueOf(dbProps.getProperty("dataSource.removeAbandoned","false"))).booleanValue();

          //超时时间(以秒数为单位)
          int removeAbandonedTimeout = Integer.parseInt(dbProps.getProperty("dataSource.removeAbandonedTimeout","300"));
        
         dataSource = new BasicDataSource();
         dataSource.setDriverClassName(driveClassName);
         dataSource.setUrl(url);
         dataSource.setUsername(username);
         dataSource.setPassword(password);

         //初始化连接数
         if(initialSize!=null)
          dataSource.setInitialSize(Integer.parseInt(initialSize));
         
         //最小空闲连接
         if(minIdle!=null)
          dataSource.setMinIdle(Integer.parseInt(minIdle));

         //最大空闲连接
         if(maxIdle!=null)
          dataSource.setMaxIdle(Integer.parseInt(maxIdle));
         
         //超时回收时间(以毫秒为单位)
         if(maxWait!=null)
          dataSource.setMaxWait(Long.parseLong(maxWait));
         
         //最大连接数
         if(maxActive!=null){
          if(!maxActive.trim().equals("0"))
           dataSource.setMaxActive(Integer.parseInt(maxActive));
         }

         System.out.println("logAbandoned="+logAbandoned);
            dataSource.setLogAbandoned(logAbandoned);
         dataSource.setRemoveAbandoned(removeAbandoned);
         dataSource.setRemoveAbandonedTimeout(removeAbandonedTimeout);
         
         Connection conn = dataSource.getConnection();
         if(conn==null){
          log("创建连接池时,无法取得连接!检查设置!!!");
         }else{
          conn.close();
         }
         System.out.println("连接池创建成功!!!");
        }
        catch (Exception e) {
         e.printStackTrace();
            System.out.println("创建连接池失败!请检查设置!!!");
        }

  有使用问题或建议可与我联系:yy-man@163.com
      
         2006-04-20   By: 小土

posted @ 2006-04-20 11:49 YY 阅读(1290) | 评论 (1)编辑 收藏

仅列出标题  

导航

统计

常用链接

留言簿(1)

随笔分类

随笔档案

搜索

最新评论

阅读排行榜

评论排行榜