BlogJava 联系 聚合 管理  

Blog Stats

随笔档案(17)

文章档案(1)


GaoWei

1.首先写个简单的Java Bean对象

package com;

import java.io.Serializable;

public class Account implements Serializable {

 private String usename;

 private String usepass;

 public String getUsename() {
  return usename;
 }

 public void setUsename(String usename) {
  this.usename = usename;
 }

 public String getUsepass() {
  return usepass;
 }

 public void setUsepass(String usepass) {
  this.usepass = usepass;
 }

}


2.sqlmap配置文件
<?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="com/jdbc/properties/mysql.properties"/>
 
 <settings
  cacheModelsEnabled="true"
  enhancementEnabled="true"
  lazyLoadingEnabled="true"
  maxRequests="32"
  maxSessions="10"
  useStatementNamespaces="false"/>
 
 
 <transactionManager type="JDBC">
      <dataSource type="SIMPLE">
         <property name="JDBC.Driver" value="${jdbc.driver}"/>
         <property name="JDBC.ConnectionURL" value="${jdbc.url}"/>
         <property name="JDBC.Username" value="${jdbc.user}"/>
         <property name="JDBC.Password" value="${jdbc.password}"/>
      </dataSource>
 </transactionManager>
 
 
 <sqlMap resource="com/xml/Account.xml"/>
</sqlMapConfig>

3.sqlmap映射文件
<?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="Account">


       <select id="getAccount" parameterClass="String" resultClass="com.Account">
       SELECT * from useinfo WHERE USENAME=#value#
       </select>
      
      
       <insert id="insertAccount" parameterClass="com.Account">
        INSERT INTO USEINFO
               VALUES(#usename#,#usepass#)
       </insert>
      
       <update id="updateAccount" parameterClass="com.Account">
       UPDATE USEINFO SET USEPASS=#usepass#
              WHERE USENAME=#usename#
       </update>
      
       <delete id="deleteAccount" parameterClass="com.Account">
       DELETE FROM USEINFO WHERE USENAME=#usename#
       </delete>
      
</sqlMap>



4.sqlmap,JAVA类
package com;
import java.sql.*;

import com.ibatis.common.resources.Resources;
import com.ibatis.sqlmap.client.*;

import java.io.*;
public class MySqlConfig {

 private static SqlMapClient sqlMap;
 static
 {
  try
  {
   String resource="com/xml/SqlMapConfig.xml";
   Reader reader=Resources.getResourceAsReader(resource);
   sqlMap=SqlMapClientBuilder.buildSqlMapClient(reader);
   reader.close();
  }
  catch(Exception e)
  {
   e.printStackTrace();
  }
  
 }
 
 public static SqlMapClient getSqlMapInstatce()
 {
  return sqlMap;
 }
 

}


5用于测试的Main主类

package com;

import java.sql.SQLException;

import com.ibatis.sqlmap.client.SqlMapClient;

public class Main {


 public static void main(String[] args) {
  
    SqlMapClient sqlMap=MySqlConfig.getSqlMapInstatce();
   
    try {
     Account account=new Account();
     account.setUsename("gw");
     account.setUsepass("usepass");
     sqlMap.insert("insertAccount",account);
  account=(Account) sqlMap.queryForObject("getAccount","gw");
  System.out.println(account.getUsepass());
  account.setUsepass("gggg874");
  sqlMap.update("updateAccount",account);
  account=(Account)sqlMap.queryForObject("getAccount","gw");
  System.out.println(account.getUsepass());
  
  sqlMap.delete("deleteAccount",account);
  
 } catch (SQLException e) {
  // TODO Auto-generated catch block
  e.printStackTrace();
 }
   
 }

}




jdbc.driver          =com.mysql.jdbc.Driver
jdbc.url               =jdbc:mysql://192.168.100.81:3306/my_db
jdbc.user             =gw
jdbc.password    =gao123

jdbc.initialSize     =2
jdbc.maxActive   =60
jdbc.maxIdle       =10
jdbc.maxWait     =50
jdbc.minIdle        =5





posted on 2006-09-16 15:35 gggg874 阅读(296) 评论(0)  编辑  收藏

只有注册用户登录后才能发表评论。


网站导航: