hibernate demo查询语句
dept.java
public class dept { private int deptid; private String deptname; private Set<employee> emps= new HashSet<employee>(); public int getDeptid() { return deptid; } public void setDeptid(int deptid) { this.deptid = deptid; } public String getDeptname() { return deptname; } public void setDeptname(String deptname) { this.deptname = deptname; } public Set<employee> getEmps() { return emps; } public void setEmps(Set<employee> emps) { this.emps = emps; } }
dept.hbm.xml
<?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"> <hibernate-mapping package="cn.itcast.aquery" > <class name="dept" table="t_dept"> <id name="deptid" > <generator class="native"></generator> </id> <property name="deptname" length="20"/>
//该处 length=20 如果不加引号,myeclipse会提示无法读取xml , 错误信息 !该处错误较为难找! <set name="emps"> <key column="dept_id"></key> <one-to-many class="employee"/> </set> </class> </hibernate-mapping>
employee.java
public class employee { private int empid; private String empname; private double salary; private dept dept; public int getEmpid() { return empid; } public void setEmpid(int empid) { this.empid = empid; } public String getEmpname() { return empname; } public void setEmpname(String empname) { this.empname = empname; } public double getSalary() { return salary; } public void setSalary(double salary) { this.salary = salary; } public dept getDept() { return dept; } public void setDept(dept dept) { this.dept = dept; } }
employee.hbm.xml
<hibernate-mapping package="cn.itcast.aquery" > <class name="employee" table="t_employ"> <id name="empid" > <generator class="native"></generator> </id> <property name="empname" length="20" /> <property name="salary" type="double"></property> <many-to-one name="dept" column="dept_id " class="dept"></many-to-one> </class> </hibernate-mapping>
app.java
public class app { private static SessionFactory sf; static { sf=new Configuration().configure().addClass(dept.class).addClass(employee.class).buildSessionFactory(); } @Test public void get_load() throws Exception{ Session session=sf.openSession(); session.beginTransaction(); dept dept=(dept)session.get(dept.class,2); System.out.println(dept.getDeptname()); System.out.println(dept.getEmps()); session.getTransaction().commit(); session.close(); } }
2.HQL查询
dept.hbm.xml
<hibernate-mapping package=”cn.itcast.aquery” auto-import=”true”>
app.java
Query q=session.createQuery(“from dept”);
System.out.println(q.list());
如果auto-import =”false\’ Query q=session.createQuery(“from cn.itcast.aquery.dept“); 该处要写完整包名
===============================
3.将查询语句放入配置文件中
public class app { private static SessionFactory sf; static { sf=new Configuration().configure().addClass(dept.class).addClass(employee.class).buildSessionFactory(); } @Test public void get_load() throws Exception{ Session session=sf.openSession(); session.beginTransaction(); /* dept dept=(dept)session.get(dept.class,2); System.out.println(dept.getDeptname()); System.out.println(dept.getEmps());*/ //Query q=session.createQuery("select d.deptid,d.deptname from dept d");
Query q = session.getNamedQuery("getalldept"); //配置文件中 配置名称 getalldept
/* q.setParameter("myid", 0); q.setParameter("name", "财务部");*/
q.setParameter(0,7); System.out.println(q.list()); session.getTransaction().commit(); session.close(); } }
dept.hbm.xml
<hibernate-mapping package="cn.itcast.aquery" auto-import="true"> <class name="dept" table="t_dept"> <id name="deptid" > <generator class="native"></generator> </id> <property name="deptname" length="20"/> <set name="emps"> <key column="dept_id"></key> <one-to-many class="employee"/> </set> </class> <query name="getalldept"> <!-- from dept d where deptid=:myid or deptname=:name--> <![CDATA[ from dept d where deptid < ? ]]> </query> </hibernate-mapping>
4。 Criteria 查询, @Test public void criteria() { Session session = sf.openSession(); session.beginTransaction(); Criteria criteria = session.createCriteria(Employee.class); // 构建条件 criteria.add(Restrictions.eq("empId", 12)); // criteria.add(Restrictions.idEq(12)); // 主键查询 System.out.println(criteria.list()); session.getTransaction().commit(); session.close(); } 5。 SQLQuery, 本地SQL查询 // 不能跨数据库平台: 如果该了数据库,sql语句有肯能要改。 @Test public void sql() { Session session = sf.openSession(); session.beginTransaction(); SQLQuery q = session.createSQLQuery("SELECT * FROM t_Dept limit 5;") .addEntity(Dept.class); // 也可以自动封装 System.out.println(q.list()); session.getTransaction().commit(); session.close(); } }