当前位置:  编程技术>java/j2ee

基于Java ORM框架的使用详解

    来源: 互联网  发布时间:2014-10-25

    本文导语:  ORM框架不是一个新话题,它已经流传了很多年。它的优点在于提供了概念性的、易于理解的数据模型,将数据库中的表和内存中的对象建立了很好的映射关系。我们在这里主要关注Java中常用的两个ORM框架:Hibernate和iBatis。下...

ORM框架不是一个新话题,它已经流传了很多年。它的优点在于提供了概念性的、易于理解的数据模型,将数据库中的表和内存中的对象建立了很好的映射关系。
我们在这里主要关注Java中常用的两个ORM框架:Hibernate和iBatis。下面来介绍这两个框架简单的使用方法,如果将来有时间,我会深入的写一些更有意思的相关文章。
Hibernate
Hibernate是一个持久化框架和ORM框架,持久化和ORM是两个有区别的概念,持久化注重对象的存储方法是否随着程序的退出而消亡,ORM关注的是如何在数据库表和内存对象之间建立关联。
Hibernate使用POJO来表示Model,使用XML配置文件来配置对象和表之间的关系,它提供了一系列API来通过对对象的操作而改变数据库中的过程。
Hibernate更强调如何对单条记录进行操作,对于更复杂的操作,它提供了一种新的面向对象的查询语言:HQL。
我们先来定义一个关于Hibernate中Session管理的类,这里的Session类似于JDBC中的Connection。
代码如下:

Hibernate的Session管理类
 public class HibernateSessionManager {

     private static SessionFactory sessionFactory;

     static
     {
         try
         {
             sessionFactory = new Configuration().configure("sample/orm/hibernate/hibernate.cfg.xml").buildSessionFactory();
         }
         catch(Exception ex)
         {
             ex.printStackTrace();
         }
     }

     public static final ThreadLocal tl = new ThreadLocal();

     public static Session currentSession()
     {
         Session s = (Session)tl.get();
         if (s == null)
         {
             s = sessionFactory.openSession();
             tl.set(s);
         }

         return s;
     }

     public static void closeSession()
     {
         Session s = (Session)tl.get();
         tl.set(null);
         if (s != null)
         {
             s.close();
         }
     }
 }

基于单张表进行操作
下面我们来看一个简单的示例,它沿用了中的数据库,使用MySQL的test数据库中的user表。
首先,我们来定义VO对象:
代码如下:

定义User对象
 public class User implements Serializable
 {
     private static final long serialVersionUID = 1L;
     private int userID;
     private String userName;
     public void setUserID(int userID) {
         this.userID = userID;
     }
     public int getUserID() {
         return userID;
     }
     public void setUserName(String userName) {
         this.userName = userName;
     }
     public String getUserName() {
         return userName;
     }
 }

然后,我们定义User对象和数据库中user表之间的关联,user表中只有两列:id和name。
代码如下:


 

 
    
        
            
            
        
        
            
        
    
 

将上述内容存储为User.hbm.xml。
接下来,我们需要定义一个关于Hibernate的全局配置文件,这里文件名是hibernate.cfg.xml。
代码如下:


 

 
    
         com.mysql.jdbc.Driver
         jdbc:mysql://localhost/test
         root
         123   
         org.hibernate.dialect.MySQLDialect
         true
         50
         25

                
    
 

可以看到,上述配置文件中包含了数据库连接的信息,诸如driver信息、数据库url、用户名、密码等等,还包括了我们上面定义的User.hbm.xml。
最后,我们编写测试代码,来对user表进行增、删、查、改的操作:
代码如下:

使用Hibernate对user表进行操作
 private static void getUser(int id)
 {
     Session session = HibernateSessionManager.currentSession();
     System.out.println("=====Query test=====");
     User user = (User)session.get(User.class, new Integer(id));
     if (user != null)
     {
         System.out.println("ID:" + user.getUserID() + "; Name:" + user.getUserName());
     }
     HibernateSessionManager.closeSession();
 }

 private static void insertUser()
 {
     Session session = HibernateSessionManager.currentSession();
     System.out.println("=====Insert test=====");
     Transaction transaction = session.beginTransaction();
     User user = new User();
     user.setUserID(6);
     user.setUserName("Zhang Fei");
     session.save(user);
     session.flush();
     transaction.commit();
     HibernateSessionManager.closeSession();
     getUser(6);
 }

 private static void updateUser(int id)
 {
     Session session = HibernateSessionManager.currentSession();
     System.out.println("=====Update test=====");
     Transaction transaction = session.beginTransaction();
     User user = (User)session.get(User.class, new Integer(id));
     System.out.println("=====Before Update=====");
     if (user != null)
     {
         System.out.println("ID:" + user.getUserID() + "; Name:" + user.getUserName());
     }
     user.setUserName("Devil");
     session.save(user);
     session.flush();
     transaction.commit();
     user = (User)session.get(User.class, new Integer(id));
     System.out.println("=====After Update=====");
     if (user != null)
     {
         System.out.println("ID:" + user.getUserID() + "; Name:" + user.getUserName());
     }
     HibernateSessionManager.closeSession();
 }

 private static void deleteUser(int id)
 {
     Session session = HibernateSessionManager.currentSession();
     System.out.println("=====Delete test=====");
     Transaction transaction = session.beginTransaction();
     User user = (User)session.get(User.class, new Integer(id));
     System.out.println("=====Before Delte=====");
     if (user != null)
     {
         System.out.println("ID:" + user.getUserID() + "; Name:" + user.getUserName());
     }
     session.delete(user);
     transaction.commit();
     user = (User)session.get(User.class, new Integer(id));
     System.out.println("=====After Update=====");
     if (user != null)
     {
         System.out.println("ID:" + user.getUserID() + "; Name:" + user.getUserName());
     }
     else
     {
         System.out.println("Delete successfully.");
     }
     HibernateSessionManager.closeSession();
 }

我们按照如下顺序调用测试代码:
代码如下:

insertUser();
updateUser(6);
deleteUser(6);

可以看到如下结果:
代码如下:

=====Insert test=====
Hibernate: insert into test.user (name, id) values (?, ?)
=====Query test=====
Hibernate: select user0_.id as id0_, user0_.name as name0_0_ from test.user user0_ where user0_.id=?
ID:6; Name:Zhang Fei
=====Update test=====
Hibernate: select user0_.id as id0_, user0_.name as name0_0_ from test.user user0_ where user0_.id=?
=====Before Update=====
ID:6; Name:Zhang Fei
Hibernate: update test.user set name=? where id=?
=====After Update=====
ID:6; Name:Devil
=====Delete test=====
Hibernate: select user0_.id as id0_, user0_.name as name0_0_ from test.user user0_ where user0_.id=?
=====Before Delte=====
ID:6; Name:Devil
Hibernate: delete from test.user where id=?
Hibernate: select user0_.id as id0_, user0_.name as name0_0_ from test.user user0_ where user0_.id=?
=====After Delete=====
Delete successfully.

请注意,上面的结果中,输出了每次数据库操作时的SQL语句,这是因为在配置文件中有如下配置:
代码如下:

true

我们可以在开发调试阶段将其打开,在部署到客户方时,将其关闭。
基于多表关联的操作
Hibernate在建立多表关联时,根据主外键的设置,表之间的关联可以分为三种:一对一、一对多和多对多。这些关联会体现在表的配置文件以及VO中。
下面我们来看一个经典的多表关联示例:排课表。数据库中建立如下四张表:Grade/Class/ClassRoom/Schedule。刚发现,使用MySQL自带的管理器导出表定义基本是一件不可能的任务。。。。
上述各表除ID以及必要外键外,只有Name一列。
然后看各个VO的定义:
代码如下:

定义Grade对象
 package sample.orm.hibernate;

 import java.io.Serializable;
 import java.util.Set;

 public class Grade implements Serializable
 {
     private static final long serialVersionUID = 1L;
     private int gradeID;
     private String gradeName;
     private Set classes;
     public void setGradeID(int gradeID) {
         this.gradeID = gradeID;
     }
     public int getGradeID() {
         return gradeID;
     }
     public void setGradeName(String gradeName) {
         this.gradeName = gradeName;
     }
     public String getGradeName() {
         return gradeName;
     }
     public void setClasses(Set classes) {
         this.classes = classes;
     }
     public Set getClasses() {
         return classes;
     }
 }

代码如下:

定义Class对象
 package sample.orm.hibernate;

 import java.io.Serializable;
 import java.util.Set;

 public class Class implements Serializable
 {
     private static final long serialVersionUID = 1L;
     private int classID;
     private Grade grade;
     private Set classrooms;
     private String className;
     public void setClassID(int classID) {
         this.classID = classID;
     }
     public int getClassID() {
         return classID;
     }
     public void setClassName(String className) {
         this.className = className;
     }
     public String getClassName() {
         return className;
     }
     public void setGrade(Grade grade) {
         this.grade = grade;
     }
     public Grade getGrade() {
         return grade;
     }
     public void setClassrooms(Set classrooms) {
         this.classrooms = classrooms;
     }
     public Set getClassrooms() {
         return classrooms;
     }
 }

代码如下:

定义ClassRoom对象
 package sample.orm.hibernate;

 import java.io.Serializable;
 import java.util.Set;

 public class ClassRoom implements Serializable
 {
     private static final long serialVersionUID = 1L;
     private int classRoomID;
     private String classRoomName;
     private Set classes;
     public void setClassRoomID(int classRoomID) {
         this.classRoomID = classRoomID;
     }
     public int getClassRoomID() {
         return classRoomID;
     }
     public void setClassRoomName(String classRoomName) {
         this.classRoomName = classRoomName;
     }
     public String getClassRoomName() {
         return classRoomName;
     }
     public void setClasses(Set classes) {
         this.classes = classes;
     }
     public Set getClasses() {
         return classes;
     }
 }

代码如下:

定义Schedule对象
 package sample.orm.hibernate;

 import java.io.Serializable;
 import java.util.Set;

 public class Schedule implements Serializable
 {
     private static final long serialVersionUID = 1L;
     private int scheduleID;
     private int classRoomID;
     private int classID;
     private Set classes;
     public void setClassRoomID(int classRoomID) {
         this.classRoomID = classRoomID;
     }
     public int getClassRoomID() {
         return classRoomID;
     }
     public void setClassID(int classID) {
         this.classID = classID;
     }
     public int getClassID() {
         return classID;
     }
     public void setClasses(Set classes) {
         this.classes = classes;
     }
     public Set getClasses() {
         return classes;
     }
     public void setScheduleID(int scheduleID) {
         this.scheduleID = scheduleID;
     }
     public int getScheduleID() {
         return scheduleID;
     }
 }

接着是各个表的关联配置文件:
1)Grade.hbm.xml
代码如下:


 

 
    
        
            
            
        
        
            
        

        
            
                
            
            
        
    
 

注意上面的配置,里面的节点说明了Grade和Class之间一对多的关系。
2)Class.hbm.xml
代码如下:


 

 
    
        
            
            
        
        
            
        

        
            
        

        
            
            
        
    
 

注意它定义两个关联:一个是和Grade之间多对一的关系,一个适合ClassRoom之间多对多的关系。
3)ClassRoom.hbm.xml
代码如下:


 

 
    
        
            
            
        
        
            
        

        
            
            
        
    
 

它只定义了一个关联:和Class之间的多对多关联。
4)Schedule.hbm.xml
代码如下:


 

 
    
        
            
            
        
        
            
        
        
            
        
    
 

这里就不需要再定义关联了。
我们需要在Hibernate全局配置文件中添加如下内容:
代码如下:






下面是各种测试方法,在有关联的情况下,Hibernate提供了下面几个特性:
•延迟加载
•级联添加
•级联修改
•级联删除
代码如下:

多表关联情况下的一些测试方法
 private static void getClass(int gradeid)
 {
     Session session = HibernateSessionManager.currentSession();
     System.out.println("=====Get Class info=====");
     Transaction transaction = session.beginTransaction();
     Grade grade = (Grade)session.get(Grade.class, new Integer(gradeid));

     Hibernate.initialize(grade);
     Iterator iterator = grade.getClasses().iterator();
     System.out.println("年级:" + grade.getGradeName() + "包括以下班级:");
     while(iterator.hasNext())
     {
         System.out.println(grade.getGradeName() + ((Class)iterator.next()).getClassName());
     }
     HibernateSessionManager.closeSession();
 }

 private static void getSchedule(int gradeid)
 {
     Session session = HibernateSessionManager.currentSession();
     Transaction transaction = session.beginTransaction();
     Grade grade = (Grade)session.get(Grade.class, new Integer(gradeid));
     if (grade != null)
     {
         System.out.println("ID:" + grade.getGradeID() + "; Name:" + grade.getGradeName());
     }

     Hibernate.initialize(grade.getClasses());

     Iterator iterator = grade.getClasses().iterator();
     while(iterator.hasNext())
     {
         Class c = (Class)iterator.next();
         System.out.println(grade.getGradeName() + c.getClassName() + "使用以下教室:");
         Hibernate.initialize(c.getClassrooms());
         Iterator iterator1 = c.getClassrooms().iterator();
         while(iterator1.hasNext())
         {
             System.out.println(((ClassRoom)iterator1.next()).getClassRoomName());
         }
     }
     HibernateSessionManager.closeSession();
 }

 private static void insertGrade()
 {
     Session session = HibernateSessionManager.currentSession();
     Transaction transaction = session.beginTransaction();
     Grade grade = new Grade();
     grade.setGradeID(4);
     grade.setGradeName("四年级");

     Class c1 = new Class();
     c1.setClassID(7);
     c1.setGrade(grade);
     c1.setClassName("一班");
     Class c2 = new Class();
     c2.setClassID(8);
     c2.setGrade(grade);
     c2.setClassName("二班");

     Set set = new HashSet();
     set.add(c1);
     set.add(c2);

     grade.setClasses(set);

     session.save(grade);
     session.flush();
     transaction.commit();
     HibernateSessionManager.closeSession();
     getClass(4);
 }

 private static void deleteGrade(int gradeid)
 {
     Session session = HibernateSessionManager.currentSession();
     Transaction transaction = session.beginTransaction();
     Grade grade = (Grade)session.get(Grade.class, new Integer(gradeid));
     if (grade != null)
     {
         session.delete(grade);
         session.flush();
     }

     transaction.commit();

     grade = (Grade)session.get(Grade.class, new Integer(gradeid));
     if (grade == null)
     {
         System.out.println("删除成功");
     }
     HibernateSessionManager.closeSession();
 }

 private static void updateGrade1(int gradeid)
 {
     Session session = HibernateSessionManager.currentSession();
     Transaction transaction = session.beginTransaction();
     Grade grade = (Grade)session.get(Grade.class, new Integer(gradeid));
     if (grade != null)
     {
         System.out.println("ID:" + grade.getGradeID() + "; Name:" + grade.getGradeName());
     }
     grade.setGradeName("Grade " + gradeid);
     session.save(grade);
     session.flush();
     transaction.commit();
     HibernateSessionManager.closeSession();
     getClass(gradeid);
 }

 private static void updateGrade2(int gradeid)
 {
     Session session = HibernateSessionManager.currentSession();
     Transaction transaction = session.beginTransaction();
     Grade grade = (Grade)session.get(Grade.class, new Integer(gradeid));
     if (grade != null)
     {
         System.out.println("ID:" + grade.getGradeID() + "; Name:" + grade.getGradeName());
     }

     Grade newGrade = new Grade();
     newGrade.setGradeID(10);
     newGrade.setGradeName(grade.getGradeName());
     Set set = grade.getClasses();
     Set newSet = new HashSet();
     Iterator iterator = set.iterator();
     while(iterator.hasNext())
     {
         Class c = (Class)iterator.next();
         Class temp = new Class();
         temp.setClassID(c.getClassID());
         temp.setClassName(c.getClassName());
         temp.setGrade(newGrade);
         newSet.add(temp);
     }
     newGrade.setClasses(newSet);
     session.delete(grade);
     session.flush();
     session.save(newGrade);
     session.flush();
     transaction.commit();
     grade = (Grade)session.get(Grade.class, new Integer(gradeid));
     if (grade == null)
     {
         System.out.println("删除成功");
     }
     HibernateSessionManager.closeSession();
     getClass(10);
 }

按顺序调用上面的方法:
代码如下:

getClass(1);
 getSchedule(1);
 insertGrade();
 updateGrade1(4);
 updateGrade2(4);
 deleteGrade(10);

执行结果如下:
代码如下:

=====Get Class info=====
Hibernate: select grade0_.gradeid as gradeid0_, grade0_.gradename as gradename1_0_ from test.grade grade0_ where grade0_.gradeid=?
Hibernate: select classes0_.gradeid as gradeid1_, classes0_.classid as classid1_, classes0_.classid as classid0_, classes0_.classname as classname2_0_, classes0_.gradeid as gradeid2_0_ from test.class classes0_ where classes0_.gradeid=?
年级:一年级包括以下班级:
一年级二班
一年级一班
Hibernate: select grade0_.gradeid as gradeid0_, grade0_.gradename as gradename1_0_ from test.grade grade0_ where grade0_.gradeid=?
ID:1; Name:一年级
Hibernate: select classes0_.gradeid as gradeid1_, classes0_.classid as classid1_, classes0_.classid as classid0_, classes0_.classname as classname2_0_, classes0_.gradeid as gradeid2_0_ from test.class classes0_ where classes0_.gradeid=?
一年级一班使用以下教室:
Hibernate: select classrooms0_.classid as classid1_, classrooms0_.classroomid as classroo2_1_, classroom1_.classroomid as classroo1_0_, classroom1_.classroomname as classroo2_4_0_ from schedule classrooms0_ inner join test.classroom classroom1_ on classrooms0_.classroomid=classroom1_.classroomid where classrooms0_.classid=?
教室二
教室五
教室一
一年级二班使用以下教室:
Hibernate: select classrooms0_.classid as classid1_, classrooms0_.classroomid as classroo2_1_, classroom1_.classroomid as classroo1_0_, classroom1_.classroomname as classroo2_4_0_ from schedule classrooms0_ inner join test.classroom classroom1_ on classrooms0_.classroomid=classroom1_.classroomid where classrooms0_.classid=?
教室四
教室二
教室六
Hibernate: select class_.classid, class_.classname as classname2_, class_.gradeid as gradeid2_ from test.class class_ where class_.classid=?
Hibernate: select class_.classid, class_.classname as classname2_, class_.gradeid as gradeid2_ from test.class class_ where class_.classid=?
Hibernate: insert into test.grade (gradename, gradeid) values (?, ?)
Hibernate: insert into test.class (classname, gradeid, classid) values (?, ?, ?)
Hibernate: insert into test.class (classname, gradeid, classid) values (?, ?, ?)
=====Get Class info=====
Hibernate: select grade0_.gradeid as gradeid0_, grade0_.gradename as gradename1_0_ from test.grade grade0_ where grade0_.gradeid=?
Hibernate: select classes0_.gradeid as gradeid1_, classes0_.classid as classid1_, classes0_.classid as classid0_, classes0_.classname as classname2_0_, classes0_.gradeid as gradeid2_0_ from test.class classes0_ where classes0_.gradeid=?
年级:四年级包括以下班级:
四年级二班
四年级一班
Hibernate: select grade0_.gradeid as gradeid0_, grade0_.gradename as gradename1_0_ from test.grade grade0_ where grade0_.gradeid=?
ID:4; Name:四年级
Hibernate: update test.grade set gradename=? where gradeid=?
=====Get Class info=====
Hibernate: select grade0_.gradeid as gradeid0_, grade0_.gradename as gradename1_0_ from test.grade grade0_ where grade0_.gradeid=?
Hibernate: select classes0_.gradeid as gradeid1_, classes0_.classid as classid1_, classes0_.classid as classid0_, classes0_.classname as classname2_0_, classes0_.gradeid as gradeid2_0_ from test.class classes0_ where classes0_.gradeid=?
年级:Grade 4包括以下班级:
Grade 4二班
Grade 4一班
Hibernate: select grade0_.gradeid as gradeid0_, grade0_.gradename as gradename1_0_ from test.grade grade0_ where grade0_.gradeid=?
ID:4; Name:Grade 4
Hibernate: select classes0_.gradeid as gradeid1_, classes0_.classid as classid1_, classes0_.classid as classid0_, classes0_.classname as classname2_0_, classes0_.gradeid as gradeid2_0_ from test.class classes0_ where classes0_.gradeid=?
Hibernate: select classrooms0_.classid as classid1_, classrooms0_.classroomid as classroo2_1_, classroom1_.classroomid as classroo1_0_, classroom1_.classroomname as classroo2_4_0_ from schedule classrooms0_ inner join test.classroom classroom1_ on classrooms0_.classroomid=classroom1_.classroomid where classrooms0_.classid=?
Hibernate: select classrooms0_.classid as classid1_, classrooms0_.classroomid as classroo2_1_, classroom1_.classroomid as classroo1_0_, classroom1_.classroomname as classroo2_4_0_ from schedule classrooms0_ inner join test.classroom classroom1_ on classrooms0_.classroomid=classroom1_.classroomid where classrooms0_.classid=?
Hibernate: delete from test.class where classid=?
Hibernate: delete from test.class where classid=?
Hibernate: delete from test.grade where gradeid=?
Hibernate: select class_.classid, class_.classname as classname2_, class_.gradeid as gradeid2_ from test.class class_ where class_.classid=?
Hibernate: select class_.classid, class_.classname as classname2_, class_.gradeid as gradeid2_ from test.class class_ where class_.classid=?
Hibernate: insert into test.grade (gradename, gradeid) values (?, ?)
Hibernate: insert into test.class (classname, gradeid, classid) values (?, ?, ?)
Hibernate: insert into test.class (classname, gradeid, classid) values (?, ?, ?)
Hibernate: select grade0_.gradeid as gradeid0_, grade0_.gradename as gradename1_0_ from test.grade grade0_ where grade0_.gradeid=?
删除成功
=====Get Class info=====
Hibernate: select grade0_.gradeid as gradeid0_, grade0_.gradename as gradename1_0_ from test.grade grade0_ where grade0_.gradeid=?
Hibernate: select classes0_.gradeid as gradeid1_, classes0_.classid as classid1_, classes0_.classid as classid0_, classes0_.classname as classname2_0_, classes0_.gradeid as gradeid2_0_ from test.class classes0_ where classes0_.gradeid=?
年级:Grade 4包括以下班级:
Grade 4一班
Grade 4二班
Hibernate: select grade0_.gradeid as gradeid0_, grade0_.gradename as gradename1_0_ from test.grade grade0_ where grade0_.gradeid=?
Hibernate: select classes0_.gradeid as gradeid1_, classes0_.classid as classid1_, classes0_.classid as classid0_, classes0_.classname as classname2_0_, classes0_.gradeid as gradeid2_0_ from test.class classes0_ where classes0_.gradeid=?
Hibernate: select classrooms0_.classid as classid1_, classrooms0_.classroomid as classroo2_1_, classroom1_.classroomid as classroo1_0_, classroom1_.classroomname as classroo2_4_0_ from schedule classrooms0_ inner join test.classroom classroom1_ on classrooms0_.classroomid=classroom1_.classroomid where classrooms0_.classid=?
Hibernate: select classrooms0_.classid as classid1_, classrooms0_.classroomid as classroo2_1_, classroom1_.classroomid as classroo1_0_, classroom1_.classroomname as classroo2_4_0_ from schedule classrooms0_ inner join test.classroom classroom1_ on classrooms0_.classroomid=classroom1_.classroomid where classrooms0_.classid=?
Hibernate: delete from test.class where classid=?
Hibernate: delete from test.class where classid=?
Hibernate: delete from test.grade where gradeid=?
Hibernate: select grade0_.gradeid as gradeid0_, grade0_.gradename as gradename1_0_ from test.grade grade0_ where grade0_.gradeid=?
删除成功

同样,执行结果中包含了各个SQL语句。
iBatis
iBatis是另外一种ORM框架,和Hibernate擅长操作单条记录不同,iBatis是基于SQL模板的,可以说,iBatis每次和数据库进行操作时,都有明确的SQL语句,而这些SQL语句,就是我们定义在配置文件中的。
我们还是以test数据库中的user表为例,简单说明iBatis的操作流程:
首先,我们还是需要定义VO对象,这里还是使用和Hibernate讲解时相同的User:
代码如下:

定义User对象
 package sample.orm.ibatis;

 import java.io.Serializable;

 public class User implements Serializable
 {
     private static final long serialVersionUID = 1L;
     private int userID;
     private String userName;
     public void setUserID(int userID) {
         this.userID = userID;
     }
     public int getUserID() {
         return userID;
     }
     public void setUserName(String userName) {
         this.userName = userName;
     }
     public String getUserName() {
         return userName;
     }

 }

然后需要针对这个VO,定义一个独立的配置文件:User.xml
代码如下:


 

 

    

    
    
        
        
        
        
        
        
     

    

    
    
         select id as userID,name as userName from user where id = #userID#
    
    
         select id as userID,name as userName from user
    
    
         update user SET name=#userName# WHERE id = #userID#
    
    
         insert into user ( id, name ) VALUES ( #userID#,#userName#)
    
    
         delete from user where id=#userID#
    

 

这个配置文件主要包括三部分:
1)缓存的配置
2)对象属性和表字段之间的关联
3)针对表的各种CRUD操作
然后是关于iBatis的全局配置文件SqlMapConfig.xml:
代码如下:


 

 

    

    
        
           
           
           
           
           
           
           
           
           
           
        
    

    

 

和Hibernate全局配置文件类似,它也包含了数据库连接的信息、数据库连接池的信息以及我们定义的User.xml。
下面是测试方法:
代码如下:

iBatis测试方法
 public class Sample {

     private SqlMapClient sqlMap = null;

     private void buildMap() throws IOException
     {
         String resource = "sample/orm/ibatis/SqlMapConfig.xml";         
         Reader reader = Resources.getResourceAsReader(resource);
         this.sqlMap = SqlMapClientBuilder.buildSqlMapClient(reader);
     }

     private void insertUser() throws IOException, SQLException
     {
         System.out.println("=====Insert test=====");
         if (this.sqlMap == null)
         {
             this.buildMap();
         }
         this.sqlMap.startTransaction();
         User user = new User();
         user.setUserID(10);
         user.setUserName("Angel");

         this.sqlMap.insert("insertUser", user);
         this.sqlMap.commitTransaction();

         user = getUser(10);
         printUserInfo(user);
     }

     private void updateUser() throws IOException, SQLException, InterruptedException
     {
         System.out.println("=====Update test=====");
         if (this.sqlMap == null)
         {
             this.buildMap();
         }
         this.sqlMap.startTransaction();
         User user = new User();
         user.setUserID(10);
         user.setUserName("Devil");
         this.sqlMap.update("updateUser", user);
         this.sqlMap.commitTransaction();
         this.sqlMap.flushDataCache();
 //        Thread.sleep(3000);
         user = getUser(10);
         printUserInfo(user);
     }

     private void deleteUser() throws IOException, SQLException
     {
         System.out.println("=====Delete test=====");
         if (this.sqlMap == null)
         {
             this.buildMap();
         }
         sqlMap.flushDataCache();
         this.sqlMap.startTransaction();
         this.sqlMap.delete("deleteUser", 10);
         this.sqlMap.commitTransaction();
         getAllUser();
     }

     private User getUser(int id) throws IOException, SQLException
     {
         if (this.sqlMap == null)
         {
             this.buildMap();
         }
         User user = (User)this.sqlMap.openSession().queryForObject("getUser", id);

         return user;
     }

      private List getAllUser() throws IOException, SQLException
      {
             if(this.sqlMap==null)
                this.buildMap();

          List userList=null; 
          userList=this.sqlMap.openSession().queryForList("getAllUser");
          printUserInfo(userList);
          return userList;
      }

      private void printUserInfo(User user)
      {
          System.out.println("=====user info=====");
          System.out.println("ID:" + user.getUserID() + ";Name:" + user.getUserName());
      }

      private void printUserInfo(List users)
      {
          System.out.println("=====user info=====");
          for(User user:users)
          {
              System.out.println("ID:" + user.getUserID() + ";Name:" + user.getUserName());
          }
      }

      public static void main(String[] args) throws IOException, SQLException, InterruptedException
      {
          Sample sample = new Sample();
          sample.getAllUser();
          sample.insertUser();
          sample.updateUser();
          sample.deleteUser();
      }
 }

它的执行结果如下:
代码如下:

=====user info=====
ID:1;Name:Zhang San
ID:2;Name:TEST
=====Insert test=====
=====user info=====
ID:10;Name:Angel
=====Update test=====
=====user info=====
ID:10;Name:Devil
=====Delete test=====
=====user info=====
ID:1;Name:Zhang San
ID:2;Name:TEST

这篇文章只是简单介绍了Hibernate和iBatis的用法,并没有涉及全部,例如Hibernate的事务、拦截、HQL、iBatis的缓存等等。这里主要是为了描述ORM框架的基本轮廓,以及在使用方式上它和JDBC的区别。

    
 
 

您可能感兴趣的文章:

  • java map(HashMap TreeMap)用法:初始化,遍历和排序详解
  • 哪位java同门师兄有《java2编程详解》电子文档,注意不是影印版
  • 请问哪儿有java2编程详解的电子书下载??本人急需!!跟贴有分!!!
  • 谁有JAVA的类库详解或下载地址?
  • 请问那里有《JAVA2编程详解》可以下载?
  • 《Java 2 编程详解》程序清单14.12中的一个问题。
  • 谁有电子版的《Java编程思想第二版(Thinking in java second)》和《Java2编程详解(special edition java2)》?得到给分
  • 我非常想知道JAVA跟C/C++对于硬件控制的能力孰强孰弱.(菜鸟问题,要详解,在线等待)
  • Java中的随机数详解
  • 深入分析Java内存区域的使用详解
  • Java加载JDBC驱动程序实例详解
  • Java代码重构的几种模式详解
  • JAVA中list,set,数组之间的转换详解
  • Java中关于int和Integer的区别详解
  • java equals函数用法详解
  • Java I/O技术之文件操作详解
  • java this super使用方法详解
  • java全角、半角字符的关系以及转换详解
  • 深入JAVA对象深度克隆的详解
  • 深入Java不可变类型的详解
  • 浅谈java中静态方法的重写问题详解
  • java中Spring框架介绍及如何实现对Bean的管理
  • Java批量作业框架 Java Batch Job Framework
  • java 框架Hibernate的generator属性的七种class介绍
  • Java神经网络框架 Encog for Java
  • Java简易MVC框架 etmvc
  • Java Web开发框架 Chrysalis
  • Java神经网络框架 Neuroph
  • Java图像处理框架 Marvin
  • Java企业应用开发框架 Portofino
  • Java媒体框架 JMF
  •  
    本站(WWW.)旨在分享和传播互联网科技相关的资讯和技术,将尽最大努力为读者提供更好的信息聚合和浏览方式。
    本站(WWW.)站内文章除注明原创外,均为转载、整理或搜集自网络。欢迎任何形式的转载,转载请注明出处。












  • 相关文章推荐
  • 使用java jdk中的LinkedHashMap实现简单的LRU算法
  • MySocketServer.java 使用或覆盖一个不鼓励使用的API???
  • java将类序列化并存储到mysql(使用hibernate)
  • JAVA中不赞成使用(Deprecated)的方法是否可以使用
  • 各位使用过JAVA的朋友们!JAVA好用吗?它有向VC那样的集成开发环境吗?
  • java 可以使用 可是javac不可以使用。老兄帮帮忙
  • 哪位知道如何用JAVA进行图形文件的缩放? 是使用JAVA2D 或是有第三方的软件?
  • java堆栈类使用实例(java中stack的使用方法)
  • env查看环境变量,JAVA_HOME明明在里面,但使用nutch时还是提示JAVA_HOME not set?
  • 如何使用linux下的java编译器????
  • 如何使用java这个命令?
  • 为什么使用cat输出的文本文件是中文的,使用java从文件读取出来时显示的是乱码?
  • linux 远程上使用java
  • UNIX下使用java运行class的问题
  • java:sun公司的联机帮助如何使用?
  • 请教如何使用Java编写的Applet程序关闭浏览器??
  • 怎么使用 JAVA 的包呀???
  • 针对使用java进行硬件编程
  • 使用editplus编写java如何编译成字节码文件,如何解释
  • 谁能告诉我哪里能找到java包内部类及方法使用介绍
  • 使用java时间的调查,谢谢大家
  • java命名空间java.sql类types的类成员方法: java_object定义及介绍
  • 我想学JAVA ,是买THINK IN JAVA 还是JAVA2核心技术:卷1 好???
  • java命名空间java.awt.datatransfer类dataflavor的类成员方法: imageflavor定义及介绍
  • 请问Java高手,Java的优势在那里??,Java主要适合于开发哪类应用程序
  • java命名空间java.lang.management类managementfactory的类成员方法: getcompilationmxbean定义及介绍
  • 如何将java.util.Date转化为java.sql.Date?数据库中Date类型对应于java的哪个Date呢
  • java命名空间java.lang.management接口runtimemxbean的类成员方法: getlibrarypath定义及介绍
  • 本人想学java,请问java程序员的待遇如何,和java主要有几个比较强的方向
  • java命名空间java.lang.management接口runtimemxbean的类成员方法: getstarttime定义及介绍
  • 我对JAVA一窍不通,可惜别人却给我一个Java的project,要我做一个安装程序,请问哪里有JAVA INSTALLER下载,而且我要不要安装java的sdk才能完成此项任务?


  • 站内导航:


    特别声明:169IT网站部分信息来自互联网,如果侵犯您的权利,请及时告知,本站将立即删除!

    ©2012-2021,,E-mail:www_#163.com(请将#改为@)

    浙ICP备11055608号-3