Java hibernate HQL查询(全部查询,参数查询,模糊查询,投影查询)

Student.java类

package cn.com.edu;


import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.ManyToOne;

@Entity
public class Student {
	@Id
	@GeneratedValue(strategy=GenerationType.AUTO)
	private int id;
	private String name;
	private String sex;
	@ManyToOne(cascade={CascadeType.MERGE},fetch=FetchType.EAGER)
	private Project pro;
	public Student() {
		super();
	}
	public Student(String name, String sex) {
		super();
		this.name = name;
		this.sex = sex;
	}
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getSex() {
		return sex;
	}
	public void setSex(String sex) {
		this.sex = sex;
	}
	public Project getPro() {
		return pro;
	}
	public void setPro(Project pro) {
		this.pro = pro;
	}
	@Override
	public String toString() {
		return "Student [id=" + id + ", name=" + name + ", sex=" + sex + "]";
	}

}

Project.java类

package cn.com.edu;

import java.util.HashSet;
import java.util.Set;

import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.OneToMany;



@Entity
public class Project {
	@Id
	@GeneratedValue(strategy=GenerationType.AUTO)
	private int id;
	private String name;
	@OneToMany(cascade={CascadeType.ALL},mappedBy="pro",fetch=FetchType.LAZY)
	private Set<Student> students=new HashSet<Student>();
	public Project() {
		super();
		// TODO Auto-generated constructor stub
	}
	public Project(String name) {
		super();
		this.name = name;
	}
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public Set<Student> getStudents() {
		return students;
	}
	public void setStudents(Set<Student> students) {
		this.students = students;
	}
}

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>
        <property name="dialect">org.hibernate.dialect.MySQLDialect</property>
        <property name="connection.url">jdbc:mysql:///test?characterEncoding=utf8</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="myeclipse.connection.profile">firstTest</property>
    	<property name="show_sql">true</property>
    	<property name="format_sql">true</property>
<!--     	<property name="hbm2ddl.auto">create</property> -->
    	
    	<mapping class="cn.com.edu.Student"/>
    	<mapping class="cn.com.edu.Project" />
    </session-factory>

</hibernate-configuration>

hiberTest.java  测试类

package cn.com.edu;

import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;


public class hiberTest {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Configuration con=new Configuration().configure();//读取解析配置文件
		SessionFactory sf=con.buildSessionFactory(); //创建工厂,读取映射信息
		
		Session session=sf.openSession();//打开Session

		Transaction tr=session.beginTransaction(); //创建并开始一个事务
//		function1(session);//全部查询
//		function2(session);//参数绑定
//		function3(session);//模糊查询
		function4(session);//查询某些属性 投影查询
		tr.commit();
		session.close();
		sf.close();
	}

	private static void function4(Session session) {
		String hql="select s.name,s.sex from Student s";
		Query query =session.createQuery(hql);
		List<Object[]> list=query.list();
		for(Object[] student:list)
		System.out.println(student[0]+"-----"+student[1]);
	}

	private static void function3(Session session) {
		String hql="from Student s where s.name like ? and sex=?";
		Query query=session.createQuery(hql);
		query.setString(0, "%s%").setString(1, "男");
		List<Student> list =query.list();
		for(Student student:list){
			System.out.println(student);
		}
		
	}

	private static void function2(Session session) {
		String hql="from Student s where s.name=?";
		Query query = session.createQuery(hql);
		query.setString(0, "w");//0->第一个问号,"w"->问号的值
		List<Student> list=query.list();
		for(Student student:list){
			System.out.println(student);
		}
		
	}

	private static void function1(Session session) {
		String hql="from Student";
		Query query =session.createQuery(hql);
		List<Student> student=query.list();
		for(Student student2:student)
		System.out.println(student2);
	}
	
}

 

Hibernate HQL查询 分页查询 模糊查询.docxHibernate HQL查询 分页查询 模糊查询.docxHibernate HQL查询 分页查询 模糊查询.docxHibernate HQL查询 分页查询 模糊查询.docxHibernate HQL查询 分页查询 模糊查询.docxHibernate HQL查询 分页查询 模糊查询.docxHibernate HQL查询 分页查询 模糊查询.docxHibernate HQL查询 分页查询 模糊查询.docxHibernate HQL查询 分页查询 模糊查询.docxHibernate HQL查询 分页查询 模糊查询.docxHibernate HQL查询 分页查询 模糊查询.docxHibernate HQL查询 分页查询 模糊查询.docxHibernate HQL查询 分页查询 模糊查询.docxHibernate HQL查询 分页查询 模糊查询.docxHibernate HQL查询 分页查询 模糊查询.docxHibernate HQL查询 分页查询 模糊查询.docxHibernate HQL查询 分页查询 模糊查询.docxHibernate HQL查询 分页查询 模糊查询.docxHibernate HQL查询 分页查询 模糊查询.docxHibernate HQL查询 分页查询 模糊查询.docxHibernate HQL查询 分页查询 模糊查询.docxHibernate HQL查询 分页查询 模糊查询.docxHibernate HQL查询 分页查询 模糊查询.docxHibernate HQL查询 分页查询 模糊查询.docxHibernate HQL查询 分页查询 模糊查询.docxHibernate HQL查询 分页查询 模糊查询.docxHibernate HQL查询 分页查询 模糊查询.docxHibernate HQL查询 分页查询 模糊查询.docxHibernate HQL查询 分页查询 模糊查询.docxHibernate HQL查询 分页查询 模糊查询.docxHibernate HQL查询 分页查询 模糊查询.docx
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值