|
该帖已经被评为良好帖
|
|
|---|---|
| 作者 | 正文 |
|
最后更新时间:2008-05-05 关键字: hibernate
说明篇
快速构建篇
一. 何谓hibernate? package org.py.hib.quickstart;
/**
* User entity.
* @author MyEclipse Persistence Tools
*/
@SuppressWarnings("serial")
public class User implements java.io.Serializable
{
private String id;
private String name;
public User()
{
}
public String getId()
{
return this.id;
}
public void setId(String id)
{
this.id = id;
}
public String getName()
{
return this.name;
}
public void setName(String name)
{
this.name = name;
}
}
根据实体,创建数据表。sql如下: use HibernateQuickUse; drop table if exists User; create table user ( id varchar(32) primary key, name varchar(32) ); 这里的id,我没有采用Integer auto_increment, 原因是为了数据库的数据能方便的导入到另外一种数据库里面,比方说:oracle。当然,这个是以牺牲部分效率为前提的。因为id是integer的,能更加快速查询。不过,从数据库会自动为 primary key 建立 index来看,效率也不会相差太多。 要想通过hibernate访问数据库。首先要建立描述数据库的文件:hibernate.cfg.xml。放到src下面。内容如下: <?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">
<hibernate-configuration>
<session-factory>
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="connection.url">jdbc:mysql://localhost:3306/hibernatequickUse</property>
<property name="connection.username">root</property>
<property name="connection.password">root</property>
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="show_sql">true</property>
<mapping resource="org/py/hib/quickstart/User.hbm.xml" />
</session-factory>
</hibernate-configuration>
说说上面的 "dialect", 这个对应着hibernate生成哪种数据库的sql。 然后是:"show_sql", 这个是为了调试时候输出sql语句到屏幕用的。 注意"mapping resource"部分。这个部分表示你的 实体- 数据库 映射文件的位置。(什么是实体-数据库 映射文件,看下面。)
实体-数据库映射文件 -- 主要是告诉hibernate,这个User类,对应着哪个table,User类里面的那个属性对应着table里面的哪个字段。 我们可以建立 实体-数据库 的xml映射文件,也可以采用Annotations映射,但是目前只说xml映射方式。如下: <?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">
<hibernate-mapping>
<class name="org.py.hib.quickstart.User" table="user">
<id name="id" type="java.lang.String" column="id" length="32">
<generator class="uuid" />
</id>
<property name="name" type="java.lang.String" column="name" length="32" />
</class>
</hibernate-mapping>
上面的xml还是很好理解的。注意一个generator中的class,他可以有很多。我们这用的是uuid。什么是uuid。这个可以google一下。呵呵。google是最好的教科书。还能有很多其他的,比方说:native。具体的同样请教google。 有了上面的准备,那么我们开始来测试一下。这里我们用junit。具体怎么使用junit,呵呵。答案我想大家都知道了,也不用我说了。其实我对test 和 使用它也不熟练。 我把测试用力放到了test/org.py.hib.quickstart下面。代码如下: package org.py.hib.quickstart;
import junit.framework.Assert;
import junit.framework.TestCase;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.junit.After;
import org.junit.Before;
public class QuickStartTest extends TestCase
{
SessionFactory factory;
String m_name = "ryanpoy";
String m_name2 = "ryanpoy2";
@Before
public void setUp() throws Exception
{
Configuration conf = new Configuration().configure();
factory = conf.buildSessionFactory();
}
/**
* 测试添加
* @throws Exception
*/
public void testSave() throws Exception
{
System.out.println("\n=== test save ===");
User u = new User();
u.setName(m_name); // 设置用户名 = m_name
Session session = null;
Transaction tran = null;
try
{
session = factory.openSession();
tran = session.beginTransaction();
session.save(u);
tran.commit();
Assert.assertEquals(u.getId() != null, true);
} catch (Exception ex)
{
tran.rollback();
throw ex;
} finally
{
if (session != null)
{
try
{
session.close();
} catch (Exception ex)
{
// nothing to do
} finally
{
if (session != null)
session = null;
}
}
}
}
/**
* 测试查询
* @throws Exception
*/
public void testFind() throws Exception
{
System.out.println("\n=== test find ===");
Session session = null;
try
{
session = factory.openSession();
User u = (User) session.createQuery("from User").list().get(0);
Assert.assertEquals(true, u.getId() != null);
Assert.assertEquals(m_name, u.getName());
} catch (Exception ex)
{
throw ex;
} finally
{
if (session != null)
{
try
{
session.close();
} catch (Exception ex)
{
// nothing to do
} finally
{
if (session != null)
session = null;
}
}
}
}
/**
* 测试修改
* @throws Exception
*/
public void testModify() throws Exception
{
System.out.println("\n=== test modify ===");
Session session = null;
Transaction tran = null;
try
{
session = factory.openSession();
tran = session.beginTransaction();
User u = (User) session.createQuery("from User").list().get(0);
u.setName(m_name2); // 修改用户名 = m_name2.(原来用户名= m_name)
tran.commit();
} catch (Exception ex)
{
throw ex;
} finally
{
if (session != null)
{
try
{
session.close();
} catch (Exception ex)
{
// nothing to do
} finally
{
if (session != null)
session = null;
}
}
}
/*
* 修改后再查询
*/
System.out.println("\n=== test find after modify ===");
try
{
session = factory.openSession();
User u = (User) session.createQuery("from User").list().get(0);
Assert.assertEquals(true, u.getId() != null);
Assert.assertEquals(m_name2, u.getName());
} catch (Exception ex)
{
throw ex;
} finally
{
if (session != null)
{
try
{
session.close();
} catch (Exception ex)
{
// nothing to do
} finally
{
if (session != null)
session = null;
}
}
}
}
/**
* 测试删除
* @throws Exception
*/
public void testDelete() throws Exception
{
System.out.println("\n=== test delete ===");
Session session = null;
Transaction tran = null;
try
{
session = factory.openSession();
tran = session.beginTransaction();
User u = (User) session.createQuery("from User").list().get(0);
session.delete(u);
tran.commit();
} catch (Exception ex)
{
throw ex;
} finally
{
if (session != null)
{
try
{
session.close();
} catch (Exception ex)
{
// nothing to do
} finally
{
if (session != null)
session = null;
}
}
}
/*
* 删除后再查询
*/
System.out.println("\n=== test find after delete ===");
try
{
session = factory.openSession();
Integer num = (Integer) session.createQuery("from User").list().size();
Assert.assertEquals(0, num.intValue());
} catch (Exception ex)
{
throw ex;
} finally
{
if (session != null)
{
try
{
session.close();
} catch (Exception ex)
{
// nothing to do
} finally
{
if (session != null)
session = null;
}
}
}
}
/**
*
*/
@After
public void tearDown() throws Exception
{
factory.close();
}
}
运行后,我们很欣慰的看到一路绿灯,全部通过了。那么测试没有问题。呵呵(这里的测试可能还不完善。请大家指出。前面说过,我对测试这块也不熟)。 看控制台,会输出如下信息: === test save === Hibernate: insert into hibernatequickuse.user (name, id) values (?, ?) === test find === Hibernate: select user0_.id as id2_, user0_.name as name2_ from hibernatequickuse.user user0_ === test modify === Hibernate: select user0_.id as id4_, user0_.name as name4_ from hibernatequickuse.user user0_ Hibernate: update hibernatequickuse.user set name=? where id=? === test find after modify === Hibernate: select user0_.id as id4_, user0_.name as name4_ from hibernatequickuse.user user0_ === test delete === Hibernate: select user0_.id as id6_, user0_.name as name6_ from hibernatequickuse.user user0_ Hibernate: delete from hibernatequickuse.user where id=? === test find after delete === Hibernate: select user0_.id as id6_, user0_.name as name6_ from hibernatequickuse.user user0_ 这些,就是hibernte自动生成的。仔细看看,其实就是标准sql。呵呵。懂jdbc的都能看懂。 好了,第一部分就到这。附件中的zip包是原代码。 语言的组织,举例的细节似乎像记流水账,看来还是没有文学细胞啊。忘了上次是哪位了,他写的标题就像是看 《圣斗士星矢》一项,激情洋溢啊。 声明:JavaEye文章版权属于作者,受法律保护。没有作者书面许可不得转载。
|
|
| 返回顶楼 | |
|
最后更新时间:2008-05-05
Assert.assertEquals(true, u.getId() != null) 改成assertNotNull(u.getId()) 更直接吧
|
|
| 返回顶楼 | |
|
最后更新时间:2008-05-08
wolf2008 写道 Assert.assertEquals(true, u.getId() != null) 改成assertNotNull(u.getId()) 更直接吧
呵呵。说得是。不过,我的写法问题到不大。其实还有很多其他方法可以用。 |
|
| 返回顶楼 | |
|
最后更新时间:2008-05-28
你能不能写一篇junit的入门?
Assert.assertEquals 这些方法我都不知道 |
|
| 返回顶楼 | |
|
最后更新时间:2008-05-28
忘了上次是哪位了,他写的标题就像是看 《圣斗士星矢》一样,激情洋溢啊。
这句比较有文学细胞。 |
|
| 返回顶楼 | |
|
最后更新时间:2008-05-28
没有发现那里能快速构建,应该是完全的说明篇
|
|
| 返回顶楼 | |
|
最后更新时间:2008-05-28
我用myeclipse5.5运行你的例子
one2one的时候,老报junit找不到,可我已经吧junit4放进目录了呀 那位碰到过同样的问题的兄弟 体系一下呀 |
|
| 返回顶楼 | |
|
最后更新时间:2008-05-28
你的junit case从那弄过来的,还是import org.junit.After;
import org.junit.Before;包 编码也硬的很,死活改不过来 |
|
| 返回顶楼 | |
|
最后更新时间:2008-06-03
小弟没用过多少junit,觉得我们要测试的是自己写的方法,但感觉楼主测的似乎是hibernate的操作
|
|
| 返回顶楼 | |
|
最后更新时间:2008-06-17
james0438 写道 小弟没用过多少junit,觉得我们要测试的是自己写的方法,但感觉楼主测的似乎是hibernate的操作
我理解的"自己的方法"。就是业务逻辑。 在这个测试里面,我的业务就是实现增删改查。所以,是没有问题的。 |
|
| 返回顶楼 | |





