DAO+Factory+Hibernate的学习。。

看了很多的书了,想着实践一下,就参考了书上的例子写了一点,呵呵刚入门很简单的东西,只有后台写的测试类而已,还望各位指教。

我的开发环境 Eclipse3.3+Myeclipse6.0,因为Myeclipse集成了Hibernate,而且6.0最高的版本是Hibernate3.1..

数据库是MySql5.0

1.首先建库: 

 `login`.CREATE DATABASE `login` /*!40100 DEFAULT CHARACTER SET utf8*/;

 DROP TABLE IF EXISTS `login`.`person`;
CREATE TABLE  `login`.`person` (
  `id` varchar(20) character set utf8NOT NULL default '',
  `name` varchar(45) character set utf8NOT NULL default '',
  `password` varchar(45) character set utf8NOT NULL default '',
  PRIMARY KEY  (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

插入两条测试数据。

2.打开Myeclipse6.0,添加下载的MySql的驱动,建立数据库的一个连接。

3.新建WEB工程,为工程加入Hibernate支持,注意不建立SessionFactory我们可以自己建立一个,

配置文件如下:

java 代码
  1. <?xml version='1.0' encoding='UTF-8'?>   
  2. <!DOCTYPE hibernate-configuration PUBLIC   
  3.           "-//Hibernate/Hibernate Configuration DTD 3.0//EN"  
  4.           "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">   
  5.   
  6. <!-- Generated by MyEclipse Hibernate Tools.                   -->   
  7. <hibernate-configuration>   
  8.   
  9. <session-factory>   
  10.     <property name="connection.username">root</property>   
  11.     <property name="connection.url">   
  12.         jdbc:mysql://localhost:3306/login   
  13.     </property>   
  14.     <property name="dialect">   
  15.         org.hibernate.dialect.MySQLDialect   
  16.     </property>   
  17.     <property name="myeclipse.connection.profile">jnetstore</property>   
  18.     <property name="connection.password">sa</property>   
  19.     <property name="connection.driver_class">   
  20.         com.mysql.jdbc.Driver   
  21.     </property>   
  22.     <property name="show_sql">true</property>   
  23.     <mapping resource="cn/will/vo/Person.hbm.xml" />   
  24.   
  25. </session-factory>   
  26.   
  27. </hibernate-configuration>  

然后新建POJO类Person.java

代码如下:

java 代码
  1. package cn.will.vo;   
  2.   
  3. public class Person {   
  4.     private String id;   
  5.     private String name;   
  6.     private String password;   
  7.     public String getId() {   
  8.         return id;   
  9.     }   
  10.     public void setId(String id) {   
  11.         this.id = id;   
  12.     }   
  13.     public String getName() {   
  14.         return name;   
  15.     }   
  16.     public void setName(String name) {   
  17.         this.name = name;   
  18.     }   
  19.     public String getPassword() {   
  20.         return password;   
  21.     }   
  22.     public void setPassword(String password) {   
  23.         this.password = password;   
  24.     }   
  25.   
  26. }   

3.新建DAO接口类PersonOperate.java

java 代码
  1. package cn.will.dao;   
  2.   
  3. import java.util.List;   
  4.   
  5. import cn.will.vo.Person;   
  6.   
  7. public interface PersonOperate {   
  8.     public void insert(Person p) throws Exception;   
  9.     public void update(Person p) throws Exception;   
  10.     public Person queryById(String id) throws Exception;   
  11.     public void delete(Person p) throws Exception;   
  12.     public void delete(String id) throws Exception;   
  13.     public List queryAll()throws Exception;   
  14.     public List queryByLike(String cond)throws Exception;   
  15.        
  16.   
  17. }   

4.建立工厂类,这里使用了两个工厂,一个是Session工厂SessionFactory.java,一个是操作的工厂DAOFactory.java

java 代码
  1. package cn.will.factory;   
  2.   
  3. import org.hibernate.Session;   
  4. import org.hibernate.cfg.Configuration;   
  5.   
  6. public class SessionFactory {   
  7.     public static Session getSession(){   
  8.         Session session=null;   
  9.         session =new Configuration().configure().buildSessionFactory().openSession();   
  10.         return session;   
  11.     }   
  12.   
  13. }   
java 代码
  1. package cn.will.factory;   
  2.   
  3. import cn.will.dao.PersonOperate;   
  4. import cn.will.impl.PersonOperateImpl;   
  5.   
  6. public class DAOFactory {   
  7.     public static PersonOperate getOperate(){   
  8.         return new  PersonOperateImpl();       
  9.     }   
  10.   
  11. }   

5.DAO的实现类PersonOperateImpl.java

 

java 代码
  1. package cn.will.impl;   
  2.   
  3. import java.util.Iterator;   
  4. import java.util.List;   
  5.   
  6. import org.hibernate.Query;   
  7. import org.hibernate.Session;   
  8. import org.hibernate.Transaction;   
  9.   
  10. import cn.will.dao.PersonOperate;   
  11. import cn.will.factory.SessionFactory;   
  12. import cn.will.vo.Person;   
  13.   
  14. public class PersonOperateImpl implements PersonOperate {   
  15.     private Session session=null;   
  16.   
  17.     public PersonOperateImpl() {   
  18.         this.session = SessionFactory.getSession();   
  19.     }   
  20.   
  21.     public void delete(Person p) throws Exception {   
  22.         Transaction tran = this.session.beginTransaction() ;   
  23.         // 执行语句   
  24.         this.session.delete(p) ;   
  25.         // 提交事务   
  26.         tran.commit() ;   
  27.     }   
  28.   
  29.     public void delete(String id) throws Exception {   
  30.         String hql = "DELETE Person WHERE id=?" ;   
  31.         Query q = this.session.createQuery(hql) ;   
  32.         // 把参数设置   
  33.         q.setString(0,id) ;   
  34.         // 执行更新语句   
  35.         q.executeUpdate() ;   
  36.         // 进行事务处理   
  37.         this.session.beginTransaction().commit() ;   
  38.     }   
  39.   
  40.     public void insert(Person p) throws Exception {   
  41.         // 开始事务   
  42.         Transaction tran = this.session.beginTransaction() ;   
  43.         // 执行语句   
  44.         this.session.save(p) ;   
  45.         // 提交事务   
  46.         tran.commit() ;   
  47.         // 关闭Session   
  48.         this.session.close() ;   
  49.     }   
  50.   
  51.     public List queryAll() throws Exception {   
  52.         List l = null ;   
  53.         String hql = "FROM Person as p" ;   
  54.         Query q = this.session.createQuery(hql) ;   
  55.         l = q.list() ;   
  56.         return l ;   
  57.     }   
  58.   
  59.     public Person queryById(String id) throws Exception {   
  60.         Person p = null ;   
  61.         // 使用Hibernate查询语言   
  62.         String hql = "FROM Person as p WHERE p.id=?" ;   
  63.         // 通过Query接口查询   
  64.         Query q = this.session.createQuery(hql) ;   
  65.         q.setString(0,id) ;   
  66.         List l = q.list() ;   
  67.         Iterator iter = l.iterator() ;   
  68.         if(iter.hasNext())   
  69.         {   
  70.             p = (Person)iter.next() ;   
  71.         }   
  72.         return p ;   
  73.     }   
  74.   
  75.     public List queryByLike(String cond) throws Exception {   
  76.         List l = null ;   
  77.         String hql = "FROM Person as p WHERE p.name like ?" ;   
  78.         Query q = this.session.createQuery(hql) ;   
  79.         q.setString(0,"%"+cond+"%") ;   
  80.         l = q.list() ;   
  81.         return l ;   
  82.     }   
  83.   
  84.     public void update(Person p) throws Exception {   
  85.         // 开始事务   
  86.         Transaction tran = this.session.beginTransaction() ;   
  87.         // 执行语句   
  88.         this.session.update(p) ;   
  89.         // 提交事务   
  90.         tran.commit() ;   
  91.     }   
  92.   
  93. }   

6.打开DB Browser窗口,找到第二步建立的数据库连接,找到login数据库,找到person表,右键选择Hibernate Reverse Engineering,在Hibernate Mapping file(*.hbm.xml) for each database table前打勾,在java package选择存放路径,这里选择cn.will.vo,下一步,ID generater选择assigned,下一步,完成.得到person.hbm.xml的代码如下:

java 代码
  1. <?xml version="1.0" encoding="utf-8"?>   
  2. <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"  
  3. "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">   
  4. <!--    
  5.     Mapping file autogenerated by MyEclipse Persistence Tools   
  6. -->   
  7. <hibernate-mapping>   
  8.     <class name="cn.will.vo.Person" table="person" catalog="login">   
  9.         <id name="id" type="java.lang.String">   
  10.             <column name="id" length="20" />   
  11.             <generator class="assigned" />   
  12.         </id>   
  13.         <property name="name" type="java.lang.String">   
  14.             <column name="name" length="45" not-null="true" />   
  15.         </property>   
  16.         <property name="password" type="java.lang.String">   
  17.             <column name="password" length="45" not-null="true" />   
  18.         </property>   
  19.     </class>   
  20. </hibernate-mapping>   

7.加入测试类TestPersonOperate.java,代码如下:

java 代码
  1. package cn.will.test;   
  2.   
  3. import java.util.Iterator;   
  4. import java.util.List;   
  5.   
  6. import cn.will.factory.DAOFactory;   
  7. import cn.will.vo.Person;   
  8.   
  9. public class TestPersonOperate {   
  10.   
  11.     /**  
  12.      * @param args  
  13.      */  
  14.     public static void main(String[] args)throws Exception {   
  15.         // TODO Auto-generated method stub   
  16.         /*按ID查询  
  17.         Person p = new Person() ;  
  18.         p=DAOFactory.getOperate().queryById("will");  
  19.         System.out.println(p.getName()) ;  
  20.         */  
  21.         /*插入数据  
  22.         Person p=new Person();  
  23.         p.setId("aaa");  
  24.         p.setName("aaa");  
  25.         p.setPassword("aaa");  
  26.         DAOFactory.getOperate().insert(p);  
  27.         */  
  28.         /*查询所有  
  29.         Person p=new Person();  
  30.         List list=null;  
  31.         list=DAOFactory.getOperate().queryAll();  
  32.         Iterator iter=list.iterator();  
  33.         if(iter.hasNext())  
  34.         {  
  35.             p=(Person)iter.next();  
  36.             System.out.println(p.getName());  
  37.         }  
  38.         */  
  39.         /*模糊查询  
  40.         Person p=new Person();  
  41.         List list=null;  
  42.         list=DAOFactory.getOperate().queryByLike("wi");  
  43.         Iterator iter=list.iterator();  
  44.         if(iter.hasNext())  
  45.         {  
  46.             p=(Person)iter.next();  
  47.             System.out.println(p.getName());  
  48.         }  
  49.         */  
  50.     }   
  51.   
  52. }   

8.运行..进行如下查询时可以正确执行得到预期结果.

总结:本人因为是初学所以难免又不对的地方,这是我按照自己的理解写的,希望和大家共同学习,希望..

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值