ibatis入门1

1 篇文章 0 订阅

iBATIS一词来源于“internet”和“abatis”的组合,是一个由Clinton Begin在2001年发起的开放源代码项目。最初侧重于密码软件的开发,现在是一个基于Java持久层框架。

相对HibernateApache OJB等“一站式”ORM解决方案而言,ibatis 是一种“半自动化”的ORM实现。

这里的“半自动化”,是相对Hibernate等提供了全面的数据库封装机制的“全自动化”
ORM 实现而言,“全自动”ORM 实现了 POJO 和数据库表之间的映射,以及 SQL 的自动
生成和执行。而ibatis 的着力点,则在于POJO 与 SQL之间的映射关系。也就是说,ibatis
并不会为程序员在运行期自动生成 SQL 执行。具体的 SQL 需要程序员编写,然后通过映
配置文件,将SQL所需的参数,以及返回的结果字段映射到指定 POJO。
大体框架是:

1 一个POJO类

2 一个针对pojo类的xml配置文件,其中配置数据库增删改等sql

3 主配置文件SqlMapConfig,配置数据源,配置项目中的pojo.xml


例子如下:

SqlMapConfig

<?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>   
  <!-- 
  <properties resource="SqlMapConfig.properties"/>
  <transactionManager type="JDBC" commitRequired="false">   
    <dataSource type="SIMPLE">   
      <property name="JDBC.Driver" value="${driver}"/>   
      <property name="JDBC.ConnectionURL" value="${url}"/>   
      <property name="JDBC.Username" value="${username}"/>   
      <property name="JDBC.Password" value="${password}"/>   
    </dataSource>   
  </transactionManager>    -->
  
  <!-- Configure a built-in transaction manager.  If you're using an    
       app server, you probably want to use its transaction manager    
       and a managed datasource -->   
  <transactionManager type="JDBC" commitRequired="false">   
    <dataSource type="SIMPLE">   
      <property name="JDBC.Driver" value="com.mysql.jdbc.Driver"/>   
      <property name="JDBC.ConnectionURL" value="jdbc:mysql://localhost/simple"/>   
      <property name="JDBC.Username" value="flb"/>   
      <property name="JDBC.Password" value="flbflb"/>   
    </dataSource>   
  </transactionManager>   
  
  <!-- List the SQL Map XML files. They can be loaded from the    
       classpath, as they are here (com.domain.data...) -->   
  <sqlMap resource="com/XML/Account.xml"/>   
  <!-- List more here...   
  <sqlMap resource="com/mydomain/data/Order.xml"/>   
  <sqlMap resource="com/mydomain/data/Documents.xml"/>   
  -->   
  
</sqlMapConfig>   
  

新建一个account的表后,pojo和xml如下

package bean;
public class Account {   
	  
	  private int id;   
	  private String firstName;   
	  private String lastName;   
	  private String emailAddress;   
	  
	  public int getId() {   
	    return id;   
	  }   
	  
	  public void setId(int id) {   
	    this.id = id;   
	  }   
	  
	  public String getFirstName() {   
	    return firstName;   
	  }   
	  
	  public void setFirstName(String firstName) {   
	    this.firstName = firstName;   
	  }   
	  
	  public String getLastName() {   
	    return lastName;   
	  }   
	  
	  public void setLastName(String lastName) {   
	    this.lastName = lastName;   
	  }   
	  
	  public String getEmailAddress() {   
	    return emailAddress;   
	  }   
	  
	  public void setEmailAddress(String emailAddress) {   
	    this.emailAddress = emailAddress;   
	  }   
	  
	} 
xml:

<?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="Account">   
  
  <!-- Use type aliases to avoid typing the full classname every time. -->   
  <typeAlias alias="Account" type="bean.Account"/>   
  
  <!-- Result maps describe the mapping between the columns returned   
       from a query, and the class properties.  A result map isn't   
       necessary if the columns (or aliases) match to the properties    
       exactly. -->   
  <resultMap id="AccountResult" class="Account">   
    <result property="id" column="ACC_ID"/>   
    <result property="firstName" column="ACC_FIRST_NAME"/>   
    <result property="lastName" column="ACC_LAST_NAME"/>   
    <result property="emailAddress" column="ACC_EMAIL"/>   
  </resultMap>   
  
  <!-- Select with no parameters using the result map for Account class. -->   
  <select id="selectAllAccounts" resultMap="AccountResult">   
    select * from account   
  </select>   
  
  <!-- A simpler select example without the result map.  Note the    
       aliases to match the properties of the target result class. -->   
  <select id="selectAccountById" parameterClass="int" resultClass="Account">   
    select   
      ACC_ID as id,   
      ACC_FIRST_NAME as firstName,   
      ACC_LAST_NAME as lastName,   
      ACC_EMAIL as emailAddress   
    from ACCOUNT   
    where ACC_ID = #id#   
  </select>   
  <select id="selectAccountById2" parameterClass="java.lang.String" resultClass="Account">   
    select   
      ACC_ID as id,   
      ACC_FIRST_NAME as firstName,   
      ACC_LAST_NAME as lastName,   
      ACC_EMAIL as emailAddress   
    from ACCOUNT   
    where ACC_EMAIL like '$ACC_EMAIL$'  
  </select>   
      
  <!-- Insert example, using the Account parameter class -->   
  <insert id="insertAccount" parameterClass="Account">   
    insert into ACCOUNT (   
      ACC_ID,   
      ACC_FIRST_NAME,   
      ACC_LAST_NAME,   
      ACC_EMAIL   
    )values (   
      #id#, #firstName#, #lastName#, #emailAddress#   
    )   
  </insert>   
  
  <!-- Update example, using the Account parameter class -->   
  <update id="updateAccount" parameterClass="Account">   
    update ACCOUNT set   
      ACC_FIRST_NAME = #firstName#,   
      ACC_LAST_NAME = #lastName#,   
      ACC_EMAIL = #emailAddress#   
    where   
      ACC_ID = #id#   
  </update>   
  
  <!-- Delete example, using an integer as the parameter class -->   
  <delete id="deleteAccountById" parameterClass="int">   
    delete from ACCOUNT where ACC_ID = #id#   
  </delete>   
  
</sqlMap>   
  
读取主配置文件:

package com.Main;
import java.io.Reader;   
import java.sql.Connection;   
import java.sql.DriverManager;   
import java.sql.ResultSet;   
import java.sql.ResultSetMetaData;   
import java.sql.Statement;    
import com.ibatis.common.resources.Resources;   
import com.ibatis.sqlmap.client.SqlMapClient;   
import com.ibatis.sqlmap.client.SqlMapClientBuilder;    
  
public class Util {   
  Statement st;   
     
  public Util() throws Exception{   

  }   
  public SqlMapClient getSqlMapClient() throws Exception{   
    Reader reader = Resources.getResourceAsReader("SqlMapConfig.xml");   
    SqlMapClient sqlMapper = SqlMapClientBuilder.buildSqlMapClient(reader);   
    reader.close();    
    return sqlMapper;   
  }   
  
}   
下面就可以进行操作啦:

package com.Main;

import java.sql.ResultSetMetaData;
import java.util.List;

import bean.Account;

import com.ibatis.sqlmap.client.SqlMapClient;   

public class Main{   
     
  public static void main(String[] a) throws Exception{   
    Util util = new Util();   

//    使用sql语句创建表
//    util.executeSQLCommand("create table ACCOUNT(ACC_ID int primary key, ACC_FIRST_NAME varchar(20),ACC_LAST_NAME varchar(20),ACC_EMAIL varchar(20))");     
    SqlMapClient sqlMapper = util.getSqlMapClient();      
//    Account account  = new Account(); 
//    for(int i=0;i<5;i++){
//	    account.setId(i);   
//	    account.setEmailAddress("w");   
//	    account.setFirstName("first");   
//	    account.setLastName("last");   
//	       
//	    sqlMapper.insert("insertAccount", account);   
//    }
//     util.checkData("select * from account");   \
    //遍历所有的数据
//    List<Account> result=sqlMapper.queryForList("selectAllAccounts", null);
//    for(Account a1:result){
//    int id = a1.getId();
//    String em=a1.getEmailAddress();
//    System.out.println(id+"   "+em);
//    }
    
//    Account at=new Account();
//    at.setId(1);
//    at.setFirstName("liuhong");
//    at.setLastName("wei");
    at.setEmailAddress("weiliuhong1@163.com");
    sqlMapper.insert("insertAccount", at);  //向数据库中添加一条数据
//    at.setEmailAddress("wei.pt@taobao.com");
    sqlMapper.update("updateAccount", at);//根据对象的来修改数据库中一条记录
//    sqlMapper.delete("deleteAccountById", 1);//根据ID来对数据库,进行删除操作
    
    
    //指定查询
    List<Account> result=sqlMapper.queryForList("selectAccountById2", "w%");
    for(Account a1:result){
      String em=a1.getEmailAddress();
      System.out.println(em);
      }
  }    
}   






评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值