第一个ibatis demo

第一个ibatis demo
今天下午项目需要,看了下ibatie
开始接触并要求熟练掌握
我的第一个ibati demo

-----------------------------------
/**
 * IbatisTest.java
 * com.isoftstone.cry.ibatisTest
 *
 * Function: TODO 
 *
 *   ver     date        author
 * ──────────────────────────────────
 *      Mar 10, 2011   程
 *
 * Copyright (c) 2011,  All Rights Reserved.
*/

package com.isoftstone.cry.ibatis;

import java.io.IOException;
import java.io.Reader;
import java.sql.SQLException;
import java.util.List;

import com.ibatis.common.resources.Resources;
import com.ibatis.sqlmap.client.SqlMapClient;
import com.ibatis.sqlmap.client.SqlMapClientBuilder;
import com.isoftstone.cry.pojo.pageTest;

/**
 * ClassName:IbatisTest
 * Project: IbatisTest
 * Company: isoftStone
 * @author   程
 * @version  
 * @since    Ver 1.1
 * @Date  Mar 10, 2011  8:06:15 AM
 * @see   
 */
public class IbatisTest {
 public static void main(String[] args) throws IOException,SQLException{
  Reader reader = Resources.getResourceAsReader("SqlMapConfig.xml");
  SqlMapClient sqlMap = SqlMapClientBuilder.buildSqlMapClient(reader);
  
//  @SuppressWarnings("unchecked")  
//  根据 - namespace="pageTest" 
//   select * from pagetest
//   List<pageTest> ptList = (List<pageTest>)sqlMap.queryForList("pageTest.getAll");
//   for(pageTest pt:ptList){
//    System.out.println(pt.getId()+" - "+pt.getName());
//   }
  
//     select * from pagetest where id = #id# ($id$)
//  pageTest pt = (pageTest)sqlMap.queryForObject("pageTest.getAll",new Long(1));
//  System.out.println("pageTest . name = " +pt.getName());
  
  /**
   * ..... where name like '%$name$%'
   */
//  List<pageTest> ptList = (List<pageTest>)sqlMap.queryForList("pageTest.getAll",new String("程"));
//  for(pageTest pt:ptList){
//   System.out.println(pt.getId()+" "+pt.getName());
//  }
 }
}

 

-----------------------------------
/**
 * Student.java
 * com.isoftstone.cry.pojo
 *
 * Function: TODO 
 *
 *   ver     date        author
 * ──────────────────────────────────
 *      Mar 10, 2011   程
 *
 * Copyright (c) 2011,  All Rights Reserved.
*/

package com.isoftstone.cry.pojo;

import java.io.Serializable;

/**
 * ClassName:Student
 * Project: 
 * Company: isoftStone
 *
 * @author   程
 * @version  
 * @since    Ver 1.1
 * @Date  Mar 10, 2011  6:52:24 AM
 * @see   
 */
public class pageTest implements Serializable{
 private long id ;
 private String name ;
 private long age ;
 
 public long getId() {
  return id;
 }
 public void setId(long id) {
  this.id = id;
 }
 public String getName() {
  return name;
 }
 public void setName(String name) {
  this.name = name;
 }
 public long getAge() {
  return age;
 }
 public void setAge(long age) {
  this.age = age;
 }
}

-------------------------------------------
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE sqlMap 
PUBLIC "-//ibatis.apache.org//DTD SQL Map 2.0//EN"
"http://ibatis.apache.org/dtd/sql-map-2.dtd">

<sqlMap namespace="pageTest">
<select id="getAll" resultClass="com.isoftstone.cry.pojo.pageTest">
          select * from pagetest
</select>
</sqlMap>
-------------------------------------------------
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE sqlMapConfig
PUBLIC "-//ibatis.apache.org//DTD SQL Map Config 2.0//EN"
"http://ibatis.apache.org/dtd/sql-map-config-2.dtd">

<sqlMapConfig>
 <settings cacheModelsEnabled="true" enhancementEnabled="true" 
  lazyLoadingEnabled="true" maxRequests="32" maxSessions="10"
  maxTransactions="5" useStatementNamespaces="true" />
  
    <transactionManager type="JDBC" commitRequired="false">
       <dataSource type="SIMPLE">
         <property name="JDBC.Driver" value="oracle.jdbc.OracleDriver"/>
         <property name="JDBC.ConnectionURL"
              value="jdbc:oracle:thin:@127.0.0.1:1521:ORCL"/>
         <property name="JDBC.Username" value="SYSTEM"/>
         <property name="JDBC.Password" value="suypower"/>
       </dataSource>
    </transactionManager>
    
    <sqlMap resource="com/isoftstone/cry/pojo/pageTest.xml"/> 
</sqlMapConfig>

你可能感兴趣的:(java,apache,sql,xml,ibatis)