hibernate3 动态查询,关联关系

来源:互联网 发布:linux vi 模式切换 编辑:程序博客网 时间:2024/05/18 19:20
大配置:
<?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"><!--根节点--><hibernate-configuration>    <session-factory>    <property name="connection.driver_class">oracle.jdbc.driver.OracleDriver</property>    <property name="connection.url">jdbc:oracle:thin:@localhost:1521:orcl</property>    <property name="connection.username">scott</property>    <property name="connection.password">1234</property>    <!--SQL dialect SQL方言 缺少hibernate不行-->    <property name="dialect">org.hibernate.dialect.Oracle10gDialect</property>        <!--在控制台打印sql-->    <property name="show_sql">true</property>    <!-- 自动构建表结构 如果是creat 先delete在创建 如果是update 自动更新表结构 -->    <property name="hbm2ddl.auto">update</property>       <!-- 关联小配置-->        <!--<mapping resource="bdqn/entity/Cat.xml"/>        <mapping resource="bdqn/entity/Dept.xml"/>-->        <mapping resource="bdqn/entity/dynamic/Emp.xml"></mapping>    </session-factory></hibernate-configuration>
案例要求:
小配置:
<?xml version="1.0"?><!DOCTYPE hibernate-mapping PUBLIC        "-//Hibernate/Hibernate Mapping DTD 3.0//EN"        "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"        >
<!--native根据底层数据库做选着--><hibernate-mapping package="bdqn.entity.dynamic" >    <class name="Emp" table="Emp" schema="scott" >        <id name="empno" column="empno">            <generator class="native"/>        </id>        <property name="ename"/>        <property name="sal"/>        <property name="job"/>        <property name="mgr"/>        <property name="deptno"/>        <property name="comm"/>        <property name="hiredate"/>    </class></hibernate-mapping>





编写单测:

import bdqn.entity.dynamic.Emp;import bdqn.entity.dynamic.viewmodel.Empcondition;import bdqn.util.hibernateutil;import org.hibernate.Hibernate;import org.hibernate.Session;import org.hibernate.SessionFactory;import org.hibernate.Transaction;import org.hibernate.cfg.Configuration;import org.hibernate.query.Query;import org.junit.Before;import org.junit.Test;import java.text.SimpleDateFormat;import java.util.Date;import java.util.List;public class test03 {    Configuration cfg;    SessionFactory factory;    Session session;     Transaction tx;     @Before    public void show(){         cfg=new Configuration().configure();         factory=cfg.buildSessionFactory();         session=factory.openSession();         tx=session.beginTransaction();     }动态查询:     @Test    public void show2() throws Exception{     String sql="from Emp where1==1";         Empcondition conditon=new Empcondition();         conditon.setSal(1000);         conditon.setJob("CLERK");         SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");         Date formdate=sdf.parse("1984-04-01");         conditon.setFromhiredate(formdate);         Date todate=sdf.parse("1985-09-09");         conditon.setEndhiredate(todate);         StringBuffer buffer=new StringBuffer();         if (conditon.getJob()!=null){             buffer.append(" and job=:job");         }         if (conditon.getSal()!=null){             buffer.append(" and sal >:sal");         }         if (conditon.getFromhiredate()!=null){             buffer.append(" and hiredate>=:hiredate");         }         if (conditon.getEndhiredate()!=null){             buffer.append(" and hiredate<=:hiredate");         }         Query query=session.createQuery(buffer.toString());         query.setProperties(conditon);         List<Emp> list=query.list();         for (Emp emp:list){             System.out.println(emp.getEname());         }    }    @Test    public void show3(){        String sql="from Emp order by empno";        Query query=session.createQuery(sql);        int pageIndex=2;        int pageSize=3;        query.setFirstResult((pageIndex-1)*pageSize);        query.setMaxResults(pageSize);        List<Emp> list=query.list();        for (Emp emp:list){            System.out.println(emp.getEname());        }    }
//多对一 关联关系 准备Emp,Dept   小配置@Testpublic void show4(){    Emp emp=session.get(Emp.class,1);    System.out.println(emp.getEmpname());    System.out.println(emp.getDept().getDeptname());}
<?xml version="1.0"?><!DOCTYPE hibernate-mapping PUBLIC        "-//Hibernate/Hibernate Mapping DTD 3.0//EN"        "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"        ><!--native根据底层数据库做选着--><hibernate-mapping package="bdqn.manytoone" >    <class name="Dept" table="Dept"  schema="scott">        <id name="deptno" column="deptno">            <generator class="native"/>        </id>        <property name="deptname"/>    </class></hibernate-mapping>
<?xml version="1.0"?><!DOCTYPE hibernate-mapping PUBLIC        "-//Hibernate/Hibernate Mapping DTD 3.0//EN"        "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"        ><!--native根据底层数据库做选着--><hibernate-mapping package="bdqn.manytoone" >    <class name="Emp" table="Emp"  schema="scott">        <id name="empno" column="empno">            <generator class="native"/>        </id>        <property name="empname" column="EMPNO"/>        <many-to-one name="dept" column="deptno" class="Dept"></many-to-one>    </class></hibernate-mapping>



}


编写hibernate工具类:
package bdqn.util;import org.hibernate.Session;import org.hibernate.SessionFactory;import org.hibernate.cfg.Configuration;/** * hibernate工具类 */public class hibernateutil {    private static ThreadLocal<Session> tl;    private  static Configuration cfg;    private static SessionFactory factory;    static{        tl=new ThreadLocal<Session>();        cfg=new Configuration().configure();        factory=cfg.buildSessionFactory();    }    public Session getSession(){        //是否存在环境变量        Session session=tl.get();       //是否存在session       if (session==null){           session=factory.openSession();           tl.set(session);       }        return session;    }}

原创粉丝点击