Hibernate投影查询

【例】雇员信息表

雇员表结构


雇员信息


第一步:创建雇员类
package com.dwx.models;
import java.util.Date;
public class Employee {
	private int id;
	private String emp_id;
	private String emp_name;
	private String emp_sex;
	private Date emp_birth;
	private String emp_job;
	private double emp_salary;
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public String getEmp_id() {
		return emp_id;
	}
	public void setEmp_id(String emp_id) {
		this.emp_id = emp_id;
	}
	public String getEmp_name() {
		return emp_name;
	}
	public void setEmp_name(String emp_name) {
		this.emp_name = emp_name;
	}
	public String getEmp_sex() {
		return emp_sex;
	}
	public void setEmp_sex(String emp_sex) {
		this.emp_sex = emp_sex;
	}
	public Date getEmp_birth() {
		return emp_birth;
	}
	public void setEmp_birth(Date emp_birth) {
		this.emp_birth = emp_birth;
	}
	public String getEmp_job() {
		return emp_job;
	}
	public void setEmp_job(String emp_job) {
		this.emp_job = emp_job;
	}
	public double getEmp_salary() {
		return emp_salary;
	}
	public void setEmp_salary(double emp_salary) {
		this.emp_salary = emp_salary;
	}
}
第二步:配置Employee.hbm.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
	<class name="com.dwx.models.Employee" table="employee">
		<id name="id"><generator class="native"></generator></id>
		<property name="emp_id"></property>
		<property name="emp_name"></property>
		<property name="emp_sex"></property>
		<property name="emp_birth"></property>
		<property name="emp_job"></property>
		<property name="emp_salary"></property>
	</class>
</hibernate-mapping>
第三步:配置hibernate.cfg.xml
<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
          "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
          "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<!-- Generated by MyEclipse Hibernate Tools.                   -->
<hibernate-configuration>

<session-factory>
	<property name="myeclipse.connection.profile">test</property>
	<property name="dialect">
		org.hibernate.dialect.MySQLDialect
	</property>
	<property name="connection.password">root</property>
	<property name="connection.username">root</property>
	<property name="connection.url">
		jdbc:mysql://localhost:3306/student
	</property>
	<property name="connection.driver_class">
		com.mysql.jdbc.Driver
	</property>
	<property name="show_sql">true</property>
	<mapping resource="com/dwx/models/Employee.hbm.xml" />

</session-factory>

</hibernate-configuration>
第四步:创建测试类
package com.dwx.models;
import java.util.Iterator;
import java.util.List;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;
public class Test {
	public static void main(String[] args) {
		Session session=null;
		Transaction tx=null;
		try{
			session=HibernateSessionFactory.getSession();
			String hql="from Employee";
			Query query=session.createQuery(hql);
			List list=query.list();
			Iterator it=list.iterator();
			while(it.hasNext()){
				Employee emp=(Employee)it.next();
				System.out.println(emp.getEmp_id()+" "+emp.getEmp_name()+" "+emp.getEmp_sex()+
						emp.getEmp_birth()+" "+emp.getEmp_job()+" "+emp.getEmp_salary());
			}
		}catch(Exception e){
			e.printStackTrace();
		}finally{
			HibernateSessionFactory.closeSession();
		}
	}

}

查询所有雇员信息

session=HibernateSessionFactory.getSession();
			String hql="from Employee";
			Query query=session.createQuery(hql);
			List list=query.list();
			Iterator it=list.iterator();
			while(it.hasNext()){
				Employee emp=(Employee)it.next();
				System.out.println(emp.getEmp_id()+" "+emp.getEmp_name()+" "+emp.getEmp_sex()+
						emp.getEmp_birth()+" "+emp.getEmp_job()+" "+emp.getEmp_salary());
			}

运行结果:


查询雇员部分信息

session=HibernateSessionFactory.getSession();
			String hql="select emp.emp_id,emp.emp_name from Employee emp";
			Query query=session.createQuery(hql);
			List list=query.list();
			Iterator it=list.iterator();
			while(it.hasNext()){
				Object [] object=(Object[])it.next();
				System.out.println(object[0]+" "+object[1]);
			}

运行结果:


对查询结果进行排序

session=HibernateSessionFactory.getSession();
			String hql="from Employee emp order by emp.id desc";
			Query query=session.createQuery(hql);
			List list=query.list();
			Iterator it=list.iterator();
			while(it.hasNext()){
				Employee emp=(Employee)it.next();
				System.out.println(emp.getId()+" "+emp.getEmp_id()+" "+emp.getEmp_name()+" "+emp.getEmp_sex()+
						emp.getEmp_birth()+" "+emp.getEmp_job()+" "+emp.getEmp_salary());
			}

运行结果:



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

云淡风轻58

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值