新建一个 java project .
1、加入 hibernate 支持:
2、选择hibernate 3.3
3、建立hibernate.cfg.xml文件
4、 不选择数据库选项
5、建立 sessionfactory
===============================================================================================
项目总体结构:
====================================================================
建立 Student.java
package xuyan.com.vo;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
@Entity
public class Student {
@Id
@GeneratedValue
public int getID() {
return ID;
}
public void setID(int iD) {
ID = iD;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
private int ID;
private String name;
private int age;
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
hibernate.cfg.xml 文件:
<?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 name="mysql">
<property name="hibernate.connection.driver_class">
com.microsoft.sqlserver.jdbc.SQLServerDriver
</property>
<property name="hibernate.connection.url">
jdbc:sqlserver://localhost:1433;DatabaseName=HibernateHQL
</property>
<property name="hibernate.connection.username">sa</property>
<property name="hibernate.connection.password">1234</property>
<property name="hibernate.dialect">
org.hibernate.dialect.SQLServerDialect
</property>
<property name="hibernate.show_sql">true</property>
<property name="hibernate.hbm2ddl.auto">update</property>
<property name="current_session_context_class">thread</property>
<mapping class="xuyan.com.vo.Student" />
</session-factory>
</hibernate-configuration>
===========================================================
TestStudent.java
package xuyan.com.vo;
import java.util.List;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.AnnotationConfiguration;
import org.hibernate.tool.hbm2ddl.SchemaExport;
import junit.framework.TestCase;
public class TestStudent extends TestCase{
private static SessionFactory sessionFactory;
@Override
protected void setUp() throws Exception {
sessionFactory=new AnnotationConfiguration().configure().buildSessionFactory();
}
@Override
protected void tearDown() throws Exception {
sessionFactory.close();
}
public void testSchemaExport()
{
SchemaExport sch=new SchemaExport( new AnnotationConfiguration().configure());
sch.create(true, true);
}
public void testSave()
{
Session session=sessionFactory.getCurrentSession();
Transaction tx=session.beginTransaction();
try {
Student student=new Student();
//student.setID(1);
student.setName("sky99");
student.setAge(28);
session.save(student);
tx.commit();
} catch (Exception e) {
tx.rollback();
e.printStackTrace();
}
}
public void testQuery()
{
Session session=sessionFactory.getCurrentSession();
Transaction tx=session.beginTransaction();
try {
String hql="from Student";
Query query=session.createQuery(hql);
List<Student> list = query.list();
for(Student s :list)
{
System.out.println(s.getID()+"=="+s.getName());
}
tx.commit();
} catch (Exception e) {
tx.rollback();
e.printStackTrace();
}
}
public void testQueryOne()
{
Session session=sessionFactory.getCurrentSession();
Transaction tx=session.beginTransaction();
try {
String hql="from Student where age>:age ";
Query query=session.createQuery(hql);
query.setParameter("age", 20);
List<Student> list = query.list();
for(Student s :list)
{
System.out.println(s.getID()+"=="+s.getName());
}
tx.commit();
} catch (Exception e) {
tx.rollback();
e.printStackTrace();
}
}
public void testSelectQuery()
{
Session session=sessionFactory.getCurrentSession();
Transaction tx=session.beginTransaction();
try {
String hql=" select ID,name,age from Student ";
Query query=session.createQuery(hql);
List list = query.list();
for(int i=0;i<list.size();i++)
{
Object[] obj=(Object[]) list.get(i);
System.out.println("ID:"+obj[0]+"名字:"+obj[1]+"年龄:"+obj[2]);
}
tx.commit();
} catch (Exception e) {
tx.rollback();
e.printStackTrace();
}
}
}
可以在测试方法中 进行测试
注意:
1、 String hql="from Student"; 语句中 数据库表名 区分大小写
2、 String hql=" select ID,name,age from Student "; 当hql 语句为select 中
必须用
for(int i=0;i<list.size();i++)
{
Object[] obj=(Object[]) list.get(i);
System.out.println("ID:"+obj[0]+"名字:"+obj[1]+"年龄:"+obj[2]);
}
方法进行输出
=================================================================
更多编程信息 请访问 www.ibcve.com 爱编程唯一网 !!