看了很多的书了,想着实践一下,就参考了书上的例子写了一点,呵呵刚入门很简单的东西,只有后台写的测试类而已,还望各位指教。
我的开发环境 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 代码
- <?xml version='1.0' encoding='UTF-8'?>
- <!DOCTYPE hibernate-configuration PUBLIC
- "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
- "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
-
- <!-- Generated by MyEclipse Hibernate Tools. -->
- <hibernate-configuration>
-
- <session-factory>
- <property name="connection.username">root</property>
- <property name="connection.url">
- jdbc:mysql:
- </property>
- <property name="dialect">
- org.hibernate.dialect.MySQLDialect
- </property>
- <property name="myeclipse.connection.profile">jnetstore</property>
- <property name="connection.password">sa</property>
- <property name="connection.driver_class">
- com.mysql.jdbc.Driver
- </property>
- <property name="show_sql">true</property>
- <mapping resource="cn/will/vo/Person.hbm.xml" />
-
- </session-factory>
-
- </hibernate-configuration>
然后新建POJO类Person.java
代码如下:
java 代码
- package cn.will.vo;
-
- public class Person {
- private String id;
- private String name;
- private String password;
- public String getId() {
- return id;
- }
- public void setId(String id) {
- this.id = id;
- }
- public String getName() {
- return name;
- }
- public void setName(String name) {
- this.name = name;
- }
- public String getPassword() {
- return password;
- }
- public void setPassword(String password) {
- this.password = password;
- }
-
- }
3.新建DAO接口类PersonOperate.java
java 代码
- package cn.will.dao;
-
- import java.util.List;
-
- import cn.will.vo.Person;
-
- public interface PersonOperate {
- public void insert(Person p) throws Exception;
- public void update(Person p) throws Exception;
- public Person queryById(String id) throws Exception;
- public void delete(Person p) throws Exception;
- public void delete(String id) throws Exception;
- public List queryAll()throws Exception;
- public List queryByLike(String cond)throws Exception;
-
-
- }
4.建立工厂类,这里使用了两个工厂,一个是Session工厂SessionFactory.java,一个是操作的工厂DAOFactory.java
java 代码
- package cn.will.factory;
-
- import org.hibernate.Session;
- import org.hibernate.cfg.Configuration;
-
- public class SessionFactory {
- public static Session getSession(){
- Session session=null;
- session =new Configuration().configure().buildSessionFactory().openSession();
- return session;
- }
-
- }
java 代码
- package cn.will.factory;
-
- import cn.will.dao.PersonOperate;
- import cn.will.impl.PersonOperateImpl;
-
- public class DAOFactory {
- public static PersonOperate getOperate(){
- return new PersonOperateImpl();
- }
-
- }
5.DAO的实现类PersonOperateImpl.java
java 代码
- package cn.will.impl;
-
- import java.util.Iterator;
- import java.util.List;
-
- import org.hibernate.Query;
- import org.hibernate.Session;
- import org.hibernate.Transaction;
-
- import cn.will.dao.PersonOperate;
- import cn.will.factory.SessionFactory;
- import cn.will.vo.Person;
-
- public class PersonOperateImpl implements PersonOperate {
- private Session session=null;
-
- public PersonOperateImpl() {
- this.session = SessionFactory.getSession();
- }
-
- public void delete(Person p) throws Exception {
- Transaction tran = this.session.beginTransaction() ;
-
- this.session.delete(p) ;
-
- tran.commit() ;
- }
-
- public void delete(String id) throws Exception {
- String hql = "DELETE Person WHERE id=?" ;
- Query q = this.session.createQuery(hql) ;
-
- q.setString(0,id) ;
-
- q.executeUpdate() ;
-
- this.session.beginTransaction().commit() ;
- }
-
- public void insert(Person p) throws Exception {
-
- Transaction tran = this.session.beginTransaction() ;
-
- this.session.save(p) ;
-
- tran.commit() ;
-
- this.session.close() ;
- }
-
- public List queryAll() throws Exception {
- List l = null ;
- String hql = "FROM Person as p" ;
- Query q = this.session.createQuery(hql) ;
- l = q.list() ;
- return l ;
- }
-
- public Person queryById(String id) throws Exception {
- Person p = null ;
-
- String hql = "FROM Person as p WHERE p.id=?" ;
-
- Query q = this.session.createQuery(hql) ;
- q.setString(0,id) ;
- List l = q.list() ;
- Iterator iter = l.iterator() ;
- if(iter.hasNext())
- {
- p = (Person)iter.next() ;
- }
- return p ;
- }
-
- public List queryByLike(String cond) throws Exception {
- List l = null ;
- String hql = "FROM Person as p WHERE p.name like ?" ;
- Query q = this.session.createQuery(hql) ;
- q.setString(0,"%"+cond+"%") ;
- l = q.list() ;
- return l ;
- }
-
- public void update(Person p) throws Exception {
-
- Transaction tran = this.session.beginTransaction() ;
-
- this.session.update(p) ;
-
- tran.commit() ;
- }
-
- }
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 代码
- <?xml version="1.0" encoding="utf-8"?>
- <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
- "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
- <!--
- Mapping file autogenerated by MyEclipse Persistence Tools
- -->
- <hibernate-mapping>
- <class name="cn.will.vo.Person" table="person" catalog="login">
- <id name="id" type="java.lang.String">
- <column name="id" length="20" />
- <generator class="assigned" />
- </id>
- <property name="name" type="java.lang.String">
- <column name="name" length="45" not-null="true" />
- </property>
- <property name="password" type="java.lang.String">
- <column name="password" length="45" not-null="true" />
- </property>
- </class>
- </hibernate-mapping>
7.加入测试类TestPersonOperate.java,代码如下:
java 代码
- package cn.will.test;
-
- import java.util.Iterator;
- import java.util.List;
-
- import cn.will.factory.DAOFactory;
- import cn.will.vo.Person;
-
- public class TestPersonOperate {
-
-
-
-
- public static void main(String[] args)throws Exception {
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- }
-
- }
8.运行..进行如下查询时可以正确执行得到预期结果.
总结:本人因为是初学所以难免又不对的地方,这是我按照自己的理解写的,希望和大家共同学习,希望..