ibatis入门示例

      由于某种原因我需要研究下ibatis,看了一下文件些了个入门示例共没有入门的同仁学习也算是自己做个笔记日后没准还能用上。

      数据库:mysql5.5

      ide:eclipse

      首先在eclips的某个库下建个表

建标语句 写道
CREATE TABLE Account (

ACC_ID int ( 11 ) NOT NULL ,

ACC_FIRST_NAME varchar ( 50 ) default NULL ,

ACC_LAST_NAME varchar ( 50 ) default NULL ,

ACC_EMAIL varchar ( 80 ) default NULL

) ENGINE=InnoDB DEFAULT CHARSET=utf8;

 其次domai对象

package com.mydomain.domain;

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

  <typeAlias alias="Account" type="com.mydomain.domain.Account"/>

  <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 id="selectAllAccounts" resultMap="AccountResult">
    select * from ACCOUNT
  </select>
  <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>
  <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 id="updateAccount" parameterClass="Account">
    update ACCOUNT set
      ACC_FIRST_NAME = #firstName#,
      ACC_LAST_NAME = #lastName#,
      ACC_EMAIL = #emailAddress#
    where
      ACC_ID = #id#
  </update>
  <delete id="deleteAccountById" parameterClass="int">
    delete from ACCOUNT where ACC_ID = #id#
  </delete>

</sqlMap>

 

这个配置文件里边建立了Accout对象和数据库Account的关联关系,已经对象属性和表的字段之间的对应关系,同时定义了各种操作。如果之前接触过hibernate的话这个就不难理解了。

还有另外一个就是配置文件:

主要是设置连接的数据库的配置

<?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> 
  <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:3306/eredg4?useUnicode=true&amp;characterEncoding=UTF-8"/>
      <property name="JDBC.Username" value="root"/>
      <property name="JDBC.Password" value="admin"/>
    </dataSource>
  </transactionManager>
  <sqlMap resource="com/mydomain/data/Account.xml"/>
</sqlMapConfig>

 

 和Account.xml对应的各个操作的方法:

package com.mydomain.data;

import com.ibatis.sqlmap.client.SqlMapClient;
import com.ibatis.sqlmap.client.SqlMapClientBuilder;
import com.ibatis.common.resources.Resources;
import com.mydomain.domain.Account;

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


public class SimpleExample {

  private static SqlMapClient sqlMapper;

  static {
    try {
      Reader reader = Resources.getResourceAsReader("com/mydomain/data/SqlMapConfig.xml");
      sqlMapper = SqlMapClientBuilder.buildSqlMapClient(reader);
      reader.close(); 
    } catch (IOException e) {
      // Fail fast.
      throw new RuntimeException("Something bad happened while building the SqlMapClient instance." + e, e);
    }
  }

  public static List selectAllAccounts () throws SQLException {
    return sqlMapper.queryForList("selectAllAccounts");
  }

  public static Account selectAccountById  (int id) throws SQLException {
    return (Account) sqlMapper.queryForObject("selectAccountById", id);
  }

  public static void insertAccount (Account account) throws SQLException {
    sqlMapper.insert("insertAccount", account);
  }

  public static void updateAccount (Account account) throws SQLException {
    sqlMapper.update("updateAccount", account);
  }

  public static void deleteAccount (int id) throws SQLException {
    sqlMapper.delete("deleteAccount", id);
  }

}

 

现在就剩下测试文件了:

package com.mydomain.test;

import com.mydomain.data.SimpleExample;
import com.mydomain.domain.*;

public class AccountTest {

	/**
	 * @param args
	 */
	public static void main(String[] args) throws Exception{
		/**
		 * 增
		 */
		/**
		Account account =new Account();
		account.setId(1);
		account.setFirstName("Road");
		account.setLastName("Horse");
		account.setEmailAddress("hlh-ml@163.com");
		SimpleExample.insertAccount(account);*/
		/**
		 * 查
		 */
		Account account =new Account();
		account=SimpleExample.selectAccountById(1);
		System.out.println("LastName=="+account.getLastName());

	}

}

 

到此ok,我把项目也传上来还没学过的可以当作入门的例子来学习。需要导入ibatis包和mysql数据库包。

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值