Use Ibatis to manage ORACLE database!

three config file:config.propertis,SqlMapConfig_SE.xml,SqlMap_MDF.xml

config.properties: this file is config the propertis of the oracle database like port, password.......

oracle_port=1521
oracle_password=test
oracle_sid=orcl
oracle_user=test
oracle_server=172.16.113.228
timesten_dsn=bms_datafeedtest
reconnect_times=5

mail_smtp_host=mail.longtop.com
mail_smtp_port=25
mail_authenticator_username =xgdu
mail_authenticator_password =Dxg12345
[email protected]

[email protected]

default_serveraddr=127.0.0.1
default_port=9999

backup_serveraddr=127.0.0.1
backup_port=9999

SqlMapConfig_SE.xml: this file is setting the Ibatis ,this file have use config .properties to config some propertis

<?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">

<sqlMapConfig>
 <properties resource="config.properties" />
 <settings
  cacheModelsEnabled="true"
  enhancementEnabled="true"
  lazyLoadingEnabled="true"
  maxRequests="1024"
  maxSessions="100"
  maxTransactions="100"
  useStatementNamespaces="false"
 />

 <transactionManager type="JDBC" >
 <dataSource type="SIMPLE">
  <property name="JDBC.Driver" value="oracle.jdbc.driver.OracleDriver"/>
  <property name="JDBC.ConnectionURL" value="jdbc:oracle:thin:@${oracle_server}:${oracle_port}:${oracle_sid}"/>
  <property name="JDBC.Username" value="${oracle_user}"/>
  <property name="JDBC.Password" value="${oracle_password}"/>
  <property name="JDBC.Password.Encrypted" value="false"/>
  <property name="JDBC.DefaultAutoCommit" value="true"/>
  <property name="Pool.MaximumIdleConnections" value="5"/>
  <property name="Pool.MaximumActiveConnections" value="5"/>
 </dataSource>
 </transactionManager>

<sqlMap resource="SqlMap_MDF.xml" />
</sqlMapConfig>

SqlMap_MDF.xml:this file the SQL operating language

<?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="Person">
 <select id="queryPerson" resultClass="java.util.HashMap" >
        <![CDATA[
           select NO,NAME,ADDR FROM FIRSTTABLE
            where NO=#values#
        ]]>
    </select>
    <insert id="insertPerson" parameterClass="java.util.HashMap">
      <![CDATA[insert into FIRSTTABLE(NO,NAME,ADDR) values(#NO#,#NAME#,#ADDR#)]]>
    </insert>
    <update id="updatePerson" parameterClass="java.util.HashMap">
     <![CDATA[update FIRSTTABLE set NAME=#NAME#,ADDR=#ADDR# where NO=#NO#]]>
    </update>
    <delete id="deletePerson" parameterClass="java.lang.Integer">
     <![CDATA[delete from FIRSTTABLE where NO=#values#]]>
    </delete>
</sqlMap> 

the follow is java file :MyConfig.java DBManager.java

MyConfig.java:this file is use java to config Ibatis

package org.lti;

import java.io.Reader;
import com.ibatis.common.resources.*;
import com.ibatis.sqlmap.client.SqlMapClient;
import com.ibatis.sqlmap.client.SqlMapClientBuilder;
public class MyConfig
{
 private static  SqlMapClient sqlMap;
 static
 {
  try
  {
   String resource="SqlMapConfig_SE.xml";
   Reader reader=Resources.getResourceAsReader(resource);
   sqlMap=SqlMapClientBuilder.buildSqlMapClient(reader);
  }
  catch(Exception e)
  {
   e.printStackTrace();
  }
 }
 public static SqlMapClient getSqlMapInstance()
 {
  return sqlMap;
 }
}

DBManager.java:this file is doing database operating

package org.lti;

import java.util.HashMap;
import com.ibatis.sqlmap.client.SqlMapClient;
public class DBManager
{
 public static void main(String[] args) throws Exception
 {
  // TODO Auto-generated method stub
  queryDB(3);
  updateDB(2,"lucy","lti");
  insertDB(8,"talent","guangzhou");
  deleteDB(8);
 }
 //query the table
 public static void queryDB(int i)throws Exception
 {
  SqlMapClient sqlMap=MyConfig.getSqlMapInstance();
  HashMap p=new HashMap();
  p=(HashMap)sqlMap.queryForObject("queryPerson",i);
  System.out.println("the NO   is "+p.get("NO"));
  System.out.println("the NAME is "+p.get("NAME"));
  System.out.println("the ADDR is "+p.get("ADDR"));
 }
 //update the table
 public static void updateDB(int no,String name,String addr)throws Exception
 {
  SqlMapClient sqlMap=MyConfig.getSqlMapInstance();
  HashMap p=new HashMap();
  p.put("NO",no);
  p.put("NAME",name);
  p.put("ADDR",addr);
  sqlMap.update("updatePerson",p);
 }
 //insert the table
 public static void insertDB(int no,String name,String addr)throws Exception
 {
  SqlMapClient sqlMap=MyConfig.getSqlMapInstance();
  HashMap p=new HashMap();
  p.put("NO", no);
  p.put("NAME",name);
  p.put("ADDR",addr);
  sqlMap.update("insertPerson",p);
 }
 //delete the table
 public static void deleteDB(int i)throws Exception
 {
  SqlMapClient sqlMap=MyConfig.getSqlMapInstance();
  sqlMap.delete("deletePerson",i);
 }
}
 

you can find the Ibatis's document at www.ibatis.com

你可能感兴趣的:(Use Ibatis to manage ORACLE database!)