日期:2014-05-16  浏览次数:20512 次

ibatis快速操作Oracle数据库
1. 首先导入ibatis的jar包(ibatis-2.3.0.677.jar,ibatis-dao-2.jar)和oracle连接驱动的jar包(classes12.jar)
2. 新建oracle实例oral,用户root,密码root,表visitlog
          --访问日志表--
     CREATE TABLE VisitLog (
     IndexId NUMBER(10) NOT NULL,
     OperateCode VARCHAR2(4) NOT NULL,
     TableName VARCHAR2(10),
     OperatorDesp VARCHAR2(100) NOT NULL,
     OperatorAccount VARCHAR2(20),
     OperatorTime DATE,
     UserIp VARCHAR2(20) NOT NULL
     )NOLOGGING;
    ALTER TABLE VisitLog ADD CONSTRAINT PK_VisitLog_IndexId PRIMARY KEY (IndexId);
  
3   在d:目录下新建连接数据库的连接文件database.properties
     ####################################
     # Database Connectivity Properties
     ####################################

     driver=oracle.jdbc.driver.OracleDriver
     url=jdbc:oracle:thin:@localhost:1521:oral
     username=root
     password=root
     MaximumActiveConnections=20
     MaximumIdleConnections=5
     MaximumWait=60000
     MaximumCheckoutTime=12000000
     TimeToWait=5000
     PingEnabled=false
     PingConnectionsOlderThan=30000
     PingConnectionsNotUsedFor=60000

4   新建webprobject项目ibatistest,新建包com.test
    新建读取加载驱动类BatisManager.class
    package com.test;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.io.Reader;
import java.util.Properties;
import com.ibatis.common.resources.Resources;
import com.ibatis.dao.client.DaoManager;
import com.ibatis.dao.client.DaoManagerBuilder;
import com.ibatis.sqlmap.client.SqlMapClient;
import com.ibatis.sqlmap.client.SqlMapClientBuilder;

public class BatisManager {
private static final String DAO_RESOURCE = "sqlmap/dao.xml";
private static final String SQL_RESOURCE = "sqlmap/sql-map-config.xml";
private static DaoManager daoManager;
private static SqlMapClient sqlMap;
private static String db_conf_path = null;
static {
}

public static DaoManager getDaomanager() {
return daoManager;
}

public static SqlMapClient getSqlMapInstance() {
return sqlMap;
}

public static Object getDaoObject(Class cls) {
return daoManager.getDao(cls);
}

public static Properties getDataBaseProp(String path) throws Exception {
Properties prop = null;
InputStream in = null;
try {
File f = new File(path);
in = new FileInputStream(f);
if (f.isFile())
prop = new Properties();
prop.load(in);
in.close();
} catch (Exception ex) {
System.out.println("The DataBase Config File Is Null Or False");
throw new Exception(
"Dont Read Correctly DataBase Config File,Please Check The File Path Or Content");
} finally {
in = null;
}
return prop;
}

public static void init(String db_conf_path) {
BatisManager.setDb_conf_path(db_conf_path);
try {
System.out.println("INFO: Starting To Load Dao Config File "
+ DAO_RESOURCE);

Reader r