Ibatis 入门

Ibatis是一个轻便的sqlMap工具,相较于ORM的hibernate学习成本低,并且可控性更强。

 

Ibatis增删该查简单例子[ibatis版本ibatis-2.3.4.726.jar]

数据模型Customer.java

package com.pojo;
public class Customer {
	private String customerId;
	private String name;

	public String getCustomerId() {
		return customerId;
	}
	public void setCustomerId(String customerId) {
		this.customerId = customerId;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}
}

 

sqlmap映射配置Customer.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="CUSTOMER">
  <!-- 定义别名 -->
  <typeAlias alias="Customer" type="com.pojo.Customer"/>

  <!-- 定义数据库列名与类属性之间的结果映射 -->
  <resultMap id="CustomerResult" class="Customer">
    <result property="customerId" column="CUSTOMER_ID"/>
    <result property="name" column="NAME"/>
  </resultMap>

  <!-- 查询所有的Customer -->
  <select id="selectAllCusotmers" resultMap="CustomerResult">
    select * from CUSTOMER
  </select>

  <!-- 通过id查询Customer -->
  <select id="selectCustomerById" parameterClass="String" resultClass="Customer">
    select
      CUSTOMER_ID as customerId,
      NAME as name
    from CUSTOMER
    where CUSTOMER_ID = #customerId#
  </select>
   
  <!-- 插入一个Customer -->
  <insert id="insertCustomer" parameterClass="Customer">
    insert into CUSTOMER (
      CUSTOMER_ID,
      NAME)
    values (
      #customerId#, #name#
    )
  </insert>

  <!-- 更新一个Customer -->
  <update id="updateCustomer" parameterClass="Customer">
    update CUSTOMER set
      NAME = #name#
    where
      CUSTOMER_ID = #customerId#
  </update>

  <!-- 删除一个Customer -->
  <delete id="deleteCustomerById" parameterClass="String">
    delete from CUSTOMER where CUSTOMER_ID = #customerId#
  </delete>

</sqlMap>

 

 sqlmap配置SqlMapConfig.xml

<?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>
      
  <!-- 默认值为false,指定各个sqlMap中的namespace是否起作用,
         如果为ture,则在指定sql语句是要加入namespace名称例如 namespace.XXX -->     
  <settings useStatementNamespaces ="true" />  
  
  <!-- 指定数据库链接信息 -->
  <transactionManager type="JDBC" commitRequired="false">
    <dataSource type="SIMPLE">
      <property name="JDBC.Driver" value="加载数据驱动"/>
      <property name="JDBC.ConnectionURL" value="数据库链接URL"/>
      <property name="JDBC.Username" value="用户名"/>
      <property name="JDBC.Password" value="密码"/>
    </dataSource>
  </transactionManager>

  <!-- 引入sqlmap -->
  <sqlMap resource="com/sqlmap/Customer.xml"/>

</sqlMapConfig>

 

测试类Main.java

package com;
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.pojo.Customer;
public class Main {
  private static SqlMapClient sqlMapper;
  static {
    try {
      Reader reader = Resources.getResourceAsReader("com/sqlmap/SqlMapConfig.xml");
      sqlMapper = SqlMapClientBuilder.buildSqlMapClient(reader);
      reader.close(); 
    } catch (IOException e) {
      throw new RuntimeException("Something bad happened while building the SqlMapClient instance." + e, e);
    }
  }

  /** 查询所有的Customer */
  @SuppressWarnings("unchecked")
public static List<Customer> selectAllCusotmers () throws SQLException {
    return sqlMapper.queryForList("CUSTOMER.selectAllCusotmers");
  }

  /** 通过id查询Customer */
  public static Customer selectCustomerById(String customerId) throws SQLException {
    return (Customer) sqlMapper.queryForObject("CUSTOMER.selectCustomerById", customerId);
  }

  /** 插入一个Customer */
  public static void insertCustomer(Customer customer) throws SQLException {
    sqlMapper.insert("CUSTOMER.insertCustomer", customer);
  }
  /**  更新一个Customer */
  public static void updateCustomer(Customer customer) throws SQLException {
    sqlMapper.update("CUSTOMER.updateCustomer", customer);
  }
  /** 删除一个Customer */
  public static void deleteCustomerById (String id) throws SQLException {
    sqlMapper.delete("CUSTOMER.deleteCustomerById", id);
  }

}

 

 

你可能感兴趣的:(sql,Web,ibatis,jdbc)