Chinaunix首页 | 论坛 | 博客
  • 博客访问: 2538482
  • 博文数量: 245
  • 博客积分: 4125
  • 博客等级: 上校
  • 技术积分: 3113
  • 用 户 组: 普通用户
  • 注册时间: 2009-03-25 23:56
文章分类

全部博文(245)

文章存档

2015年(2)

2014年(26)

2013年(41)

2012年(40)

2011年(134)

2010年(2)

分类: Java

2012-05-20 20:01:31

虽然apache已经不再维护iBATIS了。由于工作需要,我还是得学习它。这篇文章主要记录iBATIS的基本知识。这里我们,使用java + ibatis + mysql实现增删改查。
 
本文开发环境:
windows 7
springsource 2.9.1(eclipse 3.7)
jdk 1.7
mysql 5.5
-------------jar ------------
iBATIS 2.3.4.726
mysql-connector-java-5.1.20

最后项目的结构:

step 0:下载ibatis 和mysql驱动
  
 


step 1:在mysql test数据库中,创建表EMPLOYEE:

点击(此处)折叠或打开

  1. mysql> CREATE TABLE EMPLOYEE (
  2.             id INT NOT NULL auto_increment,
  3.             first_name VARCHAR(20) default NULL,
  4.             last_name VARCHAR(20) default NULL,
  5.             salary INT default NULL,
  6.             PRIMARY KEY (id)
  7.         );

step 2:创建SqlMapConfig.xml
在ibatis的主要配置文件中,我们:
使用jdbc访问数据库test;指定了访问数据库的用户名和密码信息;以及指定Employee.xml(在这个xml中定义所有与Employee相关的sql语句映射。)

点击(此处)折叠或打开

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!DOCTYPE sqlMapConfig
  3. PUBLIC "-//ibatis.apache.org//DTD SQL Map Config 2.0//EN"
  4. "">


  5. <sqlMapConfig>
  6.     <settings enhancementEnabled="true" useStatementNamespaces="true"/>
  7.     <transactionManager type="JDBC">
  8.         <dataSource type="SIMPLE">
  9.             <property name="JDBC.Driver" value="com.mysql.jdbc.Driver" />
  10.             <property name="JDBC.ConnectionURL" value="jdbc:mysql://localhost:3306/test" />
  11.             <property name="JDBC.Username" value="root" />
  12.             <property name="JDBC.Password" value="root" />
  13.             <property name="JDBC.AutoCommit" value="true" />
  14.         </dataSource>
  15.     </transactionManager>

  16.     <sqlMap resource="com/phpcode8/ibatis/domain/Employee.xml"/>
  17. </sqlMapConfig>

step 3:创建Employee POJO类

点击(此处)折叠或打开

  1. package com.phpcode8.ibatis.domain;

  2. public class Employee {
  3.     private int id;
  4.     private String first_name;
  5.     private String last_name;
  6.     private int salary;
  7.     
  8.      public Employee() {}
  9.     
  10.      public Employee(String fname, String lname, int salary) {
  11.      this.first_name = fname;
  12.      this.last_name = lname;
  13.      this.salary = salary;
  14.      }
  15.     
  16.     
  17.     public int getId() {
  18.         return id;
  19.     }
  20.     public void setId(int id) {
  21.         this.id = id;
  22.     }
  23.     public String getFirst_name() {
  24.         return first_name;
  25.     }
  26.     public void setFirst_name(String first_name) {
  27.         this.first_name = first_name;
  28.     }
  29.     public String getLast_name() {
  30.         return last_name;
  31.     }
  32.     public void setLast_name(String last_name) {
  33.         this.last_name = last_name;
  34.     }
  35.     public int getSalary() {
  36.         return salary;
  37.     }
  38.     public void setSalary(int salary) {
  39.         this.salary = salary;
  40.     }

  41. }
step 4:创建Employee.xml

点击(此处)折叠或打开

  1. <?xml version="1.0" encoding="UTF-8" ?>
  2. <!DOCTYPE sqlMap
  3.     PUBLIC "-//ibatis.apache.org//DTD SQL Map 2.0//EN"
  4.     "">


  5. <sqlMap namespace="Employee">
  6. <!-- Use type aliases to avoid typing the full classname every time. -->
  7. <typeAlias alias="Employee" type="com.phpcode8.ibatis.domain.Employee"/>

  8. <insert id="insert" parameterClass="Employee">
  9.           
  10.    insert into EMPLOYEE(first_name, last_name, salary)
  11.    values (#first_name#, #last_name#, #salary#)

  12.    <selectKey resultClass="int" keyProperty="id">
  13.       select last_insert_id() as id
  14.    </selectKey>

  15. </insert>

  16. <select id="getAll" resultClass="Employee">
  17.    SELECT * FROM EMPLOYEE
  18. </select>

  19. <update id="update" parameterClass="Employee">
  20.    UPDATE EMPLOYEE
  21.    SET first_name = #first_name#
  22.    WHERE id = #id#
  23. </update>

  24. <delete id="delete" parameterClass="int">
  25.    DELETE FROM EMPLOYEE
  26.    WHERE id = #id#
  27. </delete>
  28. </sqlMap>

在上面的xml中我们为了使用iBATIS定义sql mapping语句,我们使用了标签,在insert标签中定义了一个id,这个id将会在HelloiBATIS.java中使用,它将用于向数据库中执行INSERT查询。
同理,使用