J2EE学习笔记——Hibernate HQL查询
来源:互联网 发布:ubuntu ipython使用 编辑:程序博客网 时间:2024/05/22 00:45
新建一个 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;@Entitypublic 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;@Overrideprotected void setUp() throws Exception { sessionFactory=new AnnotationConfiguration().configure().buildSessionFactory();}@Overrideprotected 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 爱编程唯一网 !!
- J2EE学习笔记——Hibernate HQL查询
- hibernate学习笔记——hql查询
- Hibernate学习笔记(十) — HQL查询
- Hibernate学习笔记总结(三)——Hibernate 3.x——HQL查询语言(重点)
- Hibernate 笔记 HQL查询
- Hibernate3学习笔记(4)——Hibernate查询技术之HQL语句
- hibernate 学习笔记(2)——HQL和高级查询
- Hibernate—(HQL查询)
- hibernate学习之六——HQL查询
- J2EE学习笔记——Hibernate基础
- hibernate查询——hql查询
- Hibernate学习笔记:HQL
- hibernate HQL学习笔记
- Hibernate笔记:HQL查询总结(一)——简单属性查询和实体对象查询
- hibernate 中HQL语句查询学习笔记一
- hibernate 中HQL语句查询学习笔记一
- Hibernate学习笔记 第六章 HQL查询与缓存
- Hibernate学习笔记 -- day07 HQL、QBC查询用法
- qt鼠标事件总结(转)
- 实现类类型的重载版本swap
- 面向对象、Orcal数据库实现对数据的操作
- ACE_Task框架
- dojo笔记——持续添加中
- J2EE学习笔记——Hibernate HQL查询
- hibernate中 lazy的使用
- zoj_3367Connect them
- find学习笔记
- C/C++编译和链接过程详解 (重定向表,导出符号表,未解决符号表)
- Visual C++ 6.0工程中的项目文件都表示什么
- Junit测试
- locate学习笔记
- VirtualBox安装Fedora文件共享