`
horseroad
  • 浏览: 11224 次
  • 性别: Icon_minigender_1
  • 来自: 新疆
最近访客 更多访客>>
社区版块
存档分类
最新评论

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数据库包。

 

分享到:
评论

相关推荐

    ibatis入门级示例

    ibatis入门级完整示例,包含ibatis-2.3.4.726.jar、commons-logging-1.0.4.jar、mysql-connector-java-3.1.10-bin.jar和建表语句。

    ibatis入门开发指南pdf 程序代码示例 jar包

    ibatis入门开发指南pdf 程序代码示例 jar包

    最简单的iBatis入门例子.rar

    iBatis入门好东东,最简单的iBatis入门例子.rar 有源码示例

    ibatis3__发布_入门示例

    ibatis3__发布_入门示例,多多了解一下。

    iBatis入门与精通

    iBatis资料,iBatis的初步和入门与精通,使用示例

    MyBatis(iBatis 3)入门示例 及 整合Mybatis与Spring3

    NULL 博文链接:https://xiajs.iteye.com/blog/1180059

    IBatis框架简单例子

    自己写的ibatis例子,可以用来入门,也可以学习一下配置文件的配置方法,程序中对可以Student表进行增删改查,自己也可以根据需要添加新的方法,里面有源码和数据库脚本。有需要的,可以下过来看看。

    ibatis快速入门

    里面有三个文件。有一个ibatis的帮助文档,详细介绍了各方法和类。 另两个是pdf格式的,详细讲解了ibatis的入门。 和集成Flex的开发示例

    ibatis一个小示例

    ibatis一个小示例,注意里面的配置文件,里面有详细的注释,方便初学者入门。

    ibatis入门教程

    SqlMapClient 基本操作示例.......................................................... 16 OR 映射............................................................................................................

    ibatis doc

    iBatis示例程序.rar iBATIS-SqlMaps-入门.pdf iBATIS-开发指南.pdf iBatis依赖包说明.png iBATIS_DAO-2.2.0.638.zip iBatis示例程序.rar

    iBATIS实战

    附录A iBATIS.NET快速入门 264 A.1 比较iBATIS和iBATIS.NET 264 A.1.1 为何Java开发人员应该关心iBATIS.NET 264 A.1.2 为何.NET开发人员应该关心iBATIS.NET 265 A.1.3 主要区别是什么 265 A.1.4 相似之处又在哪里 ...

    MyIbatis3.0入门实例

    MyIbatis3.0入门+进阶实例,直接把资源工程导入到MyEclise里就可以运行,导入到...---ibatis3__发布_入门示例.pdf 可以带你熟练使用MyIbatis3.0,实例代码对MyIbatis3.0各个功能实例都做有说明,让你更好嘀使用MyIbatis3.0

    SpringBatch批处理 刘相编

    《Spring Batch 批处理框架》全面、系统地介绍了批处理框架Spring Batch,通过详尽的实战示例向读者展示了Spring Batch框架对大数据批处理的基本开发能力,并对框架的架构设计、源码做了特定的剖析;在帮助读者掌握...

    Struts2入门教程。包括jquery集成等。入门必看

    第 1章 STRUTS2入门................................................................................................................................................3 第 2章STRUTS2晋级.....................

    MyBatis example

    基于MyBatis3.0.3操作mySql数据库的简单示例。综合了各种xml文件的配置。和以往的ibatis配置有所不同。非常适合初学者入门。

    ssi框架总结及实例

    ssi框架很好的入门实例,前端使用的是extjs

    springmybatis

    (读者注:其实这个应该叫做很基础的入门一下下,如果你看过Hibernate了那这个就非常的简单) (再加一条,其实大家可以看官方的教程更好些:http://mybatis.github.io/mybatis-3/,而且如果英文不是很好的那就看...

Global site tag (gtag.js) - Google Analytics