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; }}
阅读全文
0 0
- hibernate3 动态查询,关联关系
- hibernate3映射关联关系举例
- Spring+Hibernate3的关联查询
- Hibernate3.3.2_关联关系的CRUD_Cascade_Fetch_1
- MyBatis--关联关系查询
- MyBatis--关联关系查询--自关联
- Hibernate3的动态条件查询DetachedCriteria
- Hibernate3的动态条件查询DetachedCriteria
- Hibernate3的动态条件查询DetachedCriteria
- Spring+Hibernate3的框架中用到了关联查询
- crm2011查询表的关联关系
- hibernate利用关联关系查询对象
- [NHibernate]一对多关系(关联查询)
- [NHibernate]一对多关系(关联查询)
- Mybatis学习笔记---day02关联关系查询
- dapper关联关系查询小测试
- Hibernate(基础一) Hibernate3的动态条件查询DetachedCriteria用法
- MyBatis--关联关系查询--一对多--多表连接查询
- Linux(Centos)之安装Redis及注意事项(转载)
- oracle nupivot函数实现行转列和pivot实现列转行
- VMware ESXI 5.1安装图解教程
- 提高执行力第一章:为啥领导总是检查呢,对业绩好不好
- web分布式系统的设计原则
- hibernate3 动态查询,关联关系
- 【C】 通过指针修改const变量的值【两种思路,关于指针】
- Selenium 用法和实例
- centos7安装mongodb
- ab压测工具学习与使用
- 代码详解のThread.join()实现多个线程顺序执行
- Java List的remove()方法陷阱
- Java:String,StringBuffer,StringBuilder区别
- 面经-京东秋招