iBatis 入门

iBatis 简介:

iBatis 是apache 的一个开源项目,一个O/R Mapping 解决方案,iBatis 最大的特点就是小巧,上手很快。如果不需要太多复杂的功能,iBatis 是能够满足你的要求又足够灵活的最简单的解决方案,现在的iBatis 已经改名为Mybatis 了。

官网为:http://www.mybatis.org/

 

今天学习下ibatis ,要学习他,首先需要搭建ibatis需要的环境

1.导入相关的jar包

2.编写配置文件  

    jdbc连接的属性文件

    总配置文件,SqlMapConfig.xml

    关于每个实体的映射文件(Map文件)

 

下面是我学习时做的Demo:

Emp.java

[java]  view plain copy 在CODE上查看代码片 派生到我的代码片
 
  1. package com.ibatis.entity;  
  2.   
  3. import java.util.Date;  
  4.   
  5. public class Emp {  
  6.     //变量的名字必须和数据库的字段名一致,否则会映射不出来  
  7.     private int empno;  
  8.     private String ename;  
  9.     private String job;  
  10.     private Integer mgr;  
  11.     private Date hiredate;  
  12.     private int sal;  
  13.     private int comm;  
  14.     private int deptno;  
  15.       
  16.     public int getEmpno() {  
  17.         return empno;  
  18.     }  
  19.     public void setEmpno(int empno) {  
  20.         this.empno = empno;  
  21.     }  
  22.   
  23.     public String getEname() {  
  24.         return ename;  
  25.     }  
  26.     public void setEname(String ename) {  
  27.         this.ename = ename;  
  28.     }  
  29.     public String getJob() {  
  30.         return job;  
  31.     }  
  32.     public void setJob(String job) {  
  33.         this.job = job;  
  34.     }  
  35.     public Integer getMgr() {  
  36.         return mgr;  
  37.     }  
  38.     public void setMgr(Integer mgr) {  
  39.         this.mgr = mgr;  
  40.     }  
  41.     public Date getHiredate() {  
  42.         return hiredate;  
  43.     }  
  44.     public void setHiredate(Date hiredate) {  
  45.         this.hiredate = hiredate;  
  46.     }  
  47.     public int getSal() {  
  48.         return sal;  
  49.     }  
  50.     public void setSal(int sal) {  
  51.         this.sal = sal;  
  52.     }  
  53.     public int getComm() {  
  54.         return comm;  
  55.     }  
  56.     public void setComm(int comm) {  
  57.         this.comm = comm;  
  58.     }  
  59.     public int getDeptno() {  
  60.         return deptno;  
  61.     }  
  62.     public void setDeptno(int deptno) {  
  63.         this.deptno = deptno;  
  64.     }  
  65.       
  66. @Override  
  67. public String toString() {  
  68.     return "empno="+empno  
  69.             +"\tname="+ename  
  70.             +"\tjob="+job  
  71.             +"\tmgr="+mgr  
  72.             +"\thiredate="+hiredate;  
  73.  }  
  74. }  


SqlMap.properties

[java]  view plain copy 在CODE上查看代码片 派生到我的代码片
 
  1. driver=oracle.jdbc.driver.OracleDriver  
  2. url=jdbc:oracle:thin:@127.0.0.1:1521:orcl  
  3. username=scott  
  4. password=yulei123  

 

Emp.xml

[html]  view plain copy 在CODE上查看代码片 派生到我的代码片
 
  1. <?xml version="1.0" encoding="UTF-8" ?>  
  2. <!DOCTYPE sqlMap PUBLIC "-//ibatis.apache.org//DTD SQL Map 2.0//EN"  
  3.    "http://ibatis.apache.org/dtd/sql-map-2.dtd">  
  4.   
  5. <sqlMap>  
  6.       <typeAlias alias="Emp" type="com.ibatis.entity.Emp"/>  
  7.         
  8.       <!-- id表示select里的sql语句,resultClass表示返回结果的类型 -->    
  9.       <select id="selectAllEmp" resultClass="Emp">  
  10.          select * from emp  
  11.       </select>  
  12.         
  13.        <!-- parameterClass表示参数的内容 -->    
  14.        <!-- #表示这是一个外部调用的需要传进的参数,可以理解为占位符 -->    
  15.          
  16.        <select id="selectEmpById" parameterClass="int" resultClass="Emp">  
  17.          select * from emp   
  18.               where empno=#empno#  
  19.        </select>  
  20.         
  21.       <!-- 不使用主键的方式 -->  
  22.       <insert id="addEmp" parameterClass="Emp">  
  23.             insert into   
  24.               emp (empno,ename,job,mgr,hiredate,sal,comm,deptno)  
  25.               values(#empno#,#ename#,#job#,#mgr#,#hiredate#,#sal#,#comm#,#deptno#)  
  26.       </insert>  
  27.         
  28.       <delete id="deleteEmpbyId" parameterClass="int">  
  29.        <!-- #id#里的id可以随意取,但是上面的insert则会有影响,因为上面的ename会从Emp里的属性里去查找 -->    
  30.        <!-- 我们也可以这样理解,如果有#占位符,则ibatis会调用parameterClass里的属性去赋值 -->   
  31.          delete from emp where empno=#id#  
  32.       </delete>  
  33.         
  34.       <update id="updateEmpById" parameterClass="Emp">  
  35.             update emp   
  36.              set ename=#ename#  
  37.             where empno=#empno#   
  38.       </update>  
  39.         
  40.       <!-- 模糊查询没有使用#,而使用了$符号 -->  
  41.       <select id="selectEmpByEname" parameterClass="String" resultClass="Emp">  
  42.           select empno,ename,sal,hiredate  
  43.             from emp  
  44.             where ename like '%$ename$%'   
  45.       </select>  
  46.         
  47.       <!-- 主键插入方式 -->  
  48.       <insert id="insertEmp" parameterClass="Emp">  
  49.       <!-- 对于Oracle 需要将selectKey放在insert前面 -->  
  50.       <selectKey resultClass="int" keyProperty="empno">  
  51.             select empPkempno.Nextval as empno  
  52.              from dual  
  53.       </selectKey>  
  54.           insert into   
  55.               emp (empno,ename,job,mgr,hiredate,sal,comm,deptno)  
  56.               values(#empno#,#ename#,#job#,#mgr#,#hiredate#,#sal#,#comm#,#deptno#)  
  57.       </insert>  
  58.         
  59. </sqlMap>  


SqlMapConfig.xml

[html]  view plain copy 在CODE上查看代码片 派生到我的代码片
 
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <!DOCTYPE sqlMapConfig PUBLIC "-//ibatis.apache.org//DTD SQL Map Config 2.0//EN"  
  3.     "http://ibatis.apache.org/dtd/sql-map-config-2.dtd">  
  4.   
  5. <sqlMapConfig>  
  6.     <!-- 引用JDBC属性的配置文件 -->  
  7.     <properties resource="com/ibatis/SqlMap.properties" />  
  8.     <!-- 使用JDBC的事务管理 -->  
  9.     <transactionManager type="JDBC">  
  10.         <!-- 数据源 -->  
  11.         <dataSource type="SIMPLE">  
  12.             <property name="JDBC.Driver" value="${driver}" />  
  13.             <property name="JDBC.ConnectionURL" value="${url}" />  
  14.             <property name="JDBC.Username" value="${username}" />  
  15.             <property name="JDBC.Password" value="${password}" />  
  16.         </dataSource>  
  17.     </transactionManager>  
  18.     <!-- 这里可以写多个实体的映射文件 -->  
  19.     <sqlMap resource="com/ibatis/Emp.xml" />  
  20.       
  21. </sqlMapConfig>  

 

IEmpDao.java

[java]  view plain copy 在CODE上查看代码片 派生到我的代码片
 
  1. package com.ibatis.dao;  
  2.   
  3. import java.util.List;  
  4.   
  5. import com.ibatis.entity.Emp;  
  6.   
  7. public interface IEmpDao {  
  8.     /** 
  9.      * 不使用主键的方式添加数据 
  10.      * @param emp 
  11.      */  
  12.    public void addEmp(Emp emp);  
  13.    /** 
  14.     * 使用主键的方式添加数据 
  15.     * @param emp 
  16.     */  
  17.    public void addEmpBySequence(Emp emp);  
  18.    /** 
  19.     * 根据id删除指定的员工 
  20.     * @param id 
  21.     */  
  22.    public void deleteEmpById(int id);  
  23.    /** 
  24.     * 根据id修改员工数据 
  25.     * @param emp 
  26.     */  
  27.    public void updateEmpById(Emp emp);  
  28.    /** 
  29.     * 查询所有的员工数据 
  30.     * @return 
  31.     */  
  32.    public List<Emp> queryAllEmp();  
  33.    /** 
  34.     * 模糊查询 
  35.     * @param name 
  36.     * @return 
  37.     */  
  38.    public List<Emp> queryEmpByName(String name);  
  39.    /** 
  40.     * 根据ID查询指定的员工数据 
  41.     * @param id 
  42.     * @return 
  43.     */  
  44.    public Emp queryEmpById(int id);  
  45. }  


IEmpDaoImpl.java

[java]  view plain copy 在CODE上查看代码片 派生到我的代码片
 
  1. package com.ibatis.impl;  
  2.   
  3. import java.io.IOException;  
  4. import java.io.Reader;  
  5. import java.sql.SQLException;  
  6. import java.util.List;  
  7.   
  8. import com.ibatis.common.resources.Resources;  
  9. import com.ibatis.dao.IEmpDao;  
  10. import com.ibatis.entity.Emp;  
  11. import com.ibatis.sqlmap.client.SqlMapClient;  
  12. import com.ibatis.sqlmap.client.SqlMapClientBuilder;  
  13.   
  14. public class IEmpDaoImpl implements IEmpDao{  
  15.       
  16.     private static SqlMapClient sqlMapClient=null;   
  17.       
  18.     //读取配置文件start  
  19.     static{  
  20.         try {  
  21.             Reader reader=Resources.getResourceAsReader("com/ibatis/SqlMapConfig.xml");  
  22.             sqlMapClient=SqlMapClientBuilder.buildSqlMapClient(reader);  
  23.             reader.close();  
  24.         } catch (IOException e) {  
  25.             e.printStackTrace();  
  26.         }  
  27.     }  
  28.     //end  
  29.     /** 
  30.      * 不使用主键的方式添加数据 
  31.      * @param emp 
  32.      */  
  33.     @Override  
  34.     public void addEmp(Emp emp) {  
  35.         Object object=null;  
  36.       try {  
  37.             object=sqlMapClient.insert("addEmp", emp);  
  38.             System.out.println("添加员工的返回值:"+object);  
  39.     } catch (SQLException e) {  
  40.         e.printStackTrace();  
  41.     }  
  42. }  
  43.      /** 
  44.         * 使用主键的方式添加数据 
  45.         * @param emp 
  46.        */  
  47.     @Override  
  48.     public void addEmpBySequence(Emp emp) {  
  49.         try {  
  50.             //1.从数据库序列中获取主键值  
  51.             //2.往emp表中插入记录  
  52.             sqlMapClient.insert("insertEmp", emp);  
  53.             System.out.println(emp.getEmpno());  
  54.         } catch (SQLException e) {  
  55.             e.printStackTrace();  
  56.         }  
  57.     }  
  58.   
  59.     @Override  
  60.     public void deleteEmpById(int id) {  
  61.         try {  
  62.             int num=sqlMapClient.delete("deleteEmpbyId", id);  
  63.             System.out.println(num);  
  64.         } catch (SQLException e) {  
  65.             e.printStackTrace();  
  66.         }  
  67.     }  
  68.   
  69.     @Override  
  70.     public void updateEmpById(Emp emp) {  
  71.        try {  
  72.         int num=sqlMapClient.update("updateEmpById", emp);  
  73.         System.out.println(num);  
  74.     } catch (SQLException e) {  
  75.         e.printStackTrace();  
  76.     }  
  77. }  
  78.   
  79.     @Override  
  80.     public List<Emp> queryAllEmp() {  
  81.         List<Emp> emps=null;  
  82.             try {  
  83.                 emps=sqlMapClient.queryForList("selectAllEmp");  
  84.             } catch (SQLException e) {  
  85.                 e.printStackTrace();  
  86.             }  
  87.         return emps;  
  88.     }  
  89.     /** 
  90.      * 模糊查询 
  91.      */  
  92.     @Override  
  93.     public List<Emp> queryEmpByName(String name) {  
  94.         List<Emp> empList=null;  
  95.         try {  
  96.             empList=sqlMapClient.queryForList("selectEmpByEname", name);  
  97.         } catch (SQLException e) {  
  98.             e.printStackTrace();  
  99.         }  
  100.         return empList;  
  101.     }  
  102.   
  103.     /** 
  104.      * 根据id查询指定的员工值 
  105.      */  
  106.     @Override  
  107.     public Emp queryEmpById(int id) {  
  108.         Emp emp=null;  
  109.         try {  
  110.             emp=(Emp) sqlMapClient.queryForObject("selectEmpById", id);  
  111.         } catch (SQLException e) {  
  112.             e.printStackTrace();  
  113.         }  
  114.         return emp;  
  115.     }  
  116.       
  117. }  

 

TestIbatis.java

[java]  view plain copy 在CODE上查看代码片 派生到我的代码片
 
  1. package com.ibatis.test;  
  2.   
  3. import java.util.Date;  
  4. import java.util.List;  
  5.   
  6. import com.ibatis.dao.IEmpDao;  
  7. import com.ibatis.entity.Emp;  
  8. import com.ibatis.impl.IEmpDaoImpl;  
  9.   
  10. public class TestIbatis {  
  11.      public static void main(String[] args) {  
  12.            
  13.          /** 
  14.           * 查询所有员工 
  15.           */  
  16.         IEmpDao dao=new IEmpDaoImpl();  
  17.             for(Emp emp:dao.queryAllEmp()){  
  18.                 System.out.println(emp.toString());  
  19.             }  
  20.              /** 
  21.               * 根据id查找员工 
  22.               */  
  23.           System.out.println(dao.queryEmpById(7839));  
  24.               /** 
  25.                * 不使用主键的方式添加员工 
  26.                */  
  27.                 Emp emp =new Emp();  
  28.                  emp.setEmpno(2010);  
  29.                  emp.setEname("董秀锦");  
  30.                  emp.setComm(30);  
  31.                  emp.setDeptno(20);  
  32.                  emp.setMgr(7839);  
  33.                  emp.setSal(2000);  
  34.                  emp.setJob("达人");  
  35.                  emp.setHiredate(new Date());  
  36.                  dao.addEmp(emp);  
  37.                  /** 
  38.                   * 使用主键的方式添加数据 
  39.                   */  
  40.                  Emp emp2 =new Emp();  
  41.                  emp2.setEmpno(2223); //这个不其作用了,使用的是序列生成的主键值  
  42.                  emp2.setEname("董秀锦");  
  43.                  emp2.setComm(30);  
  44.                  emp2.setDeptno(20);  
  45.                  emp2.setMgr(7839);  
  46.                  emp2.setSal(2000);  
  47.                  emp2.setJob("达人");  
  48.                  emp2.setHiredate(new Date());  
  49.                  dao.addEmpBySequence(emp2) ;   
  50.             /** 
  51.              * 删除 
  52.              */  
  53.              dao.deleteEmpById(2010);  
  54.             /** 
  55.              * 修改 
  56.              */  
  57.                  Emp emp1 =new Emp();  
  58.                  emp1.setEmpno(7566);  
  59.                  emp1.setEname("董秀锦");  
  60.                  dao.updateEmpById(emp1);  
  61.                    
  62.         /** 
  63.          * 模糊查询 
  64.          */  
  65.                 List<Emp> empList= dao.queryEmpByName("秀");  
  66.                  for(Emp e:empList){  
  67.                     System.out.println(e);   
  68.                  }  
  69.               
  70.          }  
  71.        
  72.        
  73. }  

 

iBatis 的优缺点:

优点:

1、 减少代码量,简单;

2、 性能增强;

3、 Sql 语句与程序代码分离;

4、 增强了移植性;

缺点:

1、 和Hibernate 相比,sql 需要自己写;

2、 参数数量只能有一个,多个参数时不太方便;

猜你喜欢

转载自dododo1234321-163-com.iteye.com/blog/2015697
今日推荐