zoukankan      html  css  js  c++  java
  • hibernate的参数查询

     大配置文件: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">
    <hibernate-configuration>
    <session-factory>
    <!-- 指定数据库所用到的驱动 -->
    <property name="connection.driver_class">oracle.jdbc.driver.OracleDriver</property>
    <!-- 指定数据库链接的url,hibernate链接的数据库名 -->
    <property name="connection.url">jdbc:oracle:thin:@localhost:1521:orcl</property>
    <!-- 指定连接数据库的用户名 -->
    <property name="connection.username">root</property>
    <!-- 指定连接数据库的用户口令 -->
    <property name="connection.password">root</property>
    <!-- Enable Hibernate's automatic session context management -->
    <property name="current_session_context_class">thread</property>

    <!--格式化sql -->
    <property name="format_sql ">true</property>
    <!-- 打印sql 控制台-->
    <property name="show_sql">true</property>
    <!-- 指定数据库方言 -->
    <property name="dialect">org.hibernate.dialect.Oracle10gDialect</property>
    <!-- 根据需要自动创建数据库表 -->
    <property name="hbm2ddl.auto">update</property>

    <!--关联小配置文件-->
    <mapping resource="cn/day01/entity/School.hbm.xml"></mapping>
    <mapping resource="cn/day03hql/hql/entity/Dept.hbm.xml"></mapping>
    </session-factory>
    </hibernate-configuration>

    实体类
    public class Dept {
    private int deptid;
    private String deptname;

    public Dept() {
    }

    public Dept(int deptid, String deptname) {
    this.deptid = deptid;
    this.deptname = deptname;
    }

    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;
    }
    }



    小配置文件
    <?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.day03hql.hql.entity">
    <!--表名称-->
    <class name="cn.day03hql.hql.entity.Dept" table="DEPT" schema="root">
    <!--列名-->
    <id name="deptid" column="DEPTID">
    <!--主键生成的策略 native:自动生成主键字段-->
    <generator class="native"></generator>
    </id>
    <property name="deptname" column="DEPTNAME"></property>
    </class>
    </hibernate-mapping>

    测试类

    添加条件可以使用名称占位符 :name,也可以使用匿名占位符 ?

     01.匿名占位符

        from Dept where name = ?  

    给占位符设置值 query.setParameter(0,);  注意,参数从0开始

     

    //参数查询   方法一  匿名占位符  ?占位符的索引下标从0开始
    @Test
    public void test05(){
    //读取大配置文件
    Configuration cfg=new Configuration().configure();
    //session工厂
    SessionFactory factory=cfg.buildSessionFactory();
    //session对象
    Session session = factory.openSession();
    String hql=" from Dept where deptname=?";
    Query query = session.createQuery(hql);
    query.setParameter(0,"开发部");
    List<Dept> list = query.list();
    for (Dept dept:list){
    System.out.println(dept.getDeptname());
    }
    }

     

     

     

    02.,名称占位符

     

           from Customer where ntitle= :title

     

           给命名参数设置值 query.setParameter("title",);

    //参数查询   名称占位符 方法二  参数名称绑定 :name
    @Test
    public void test06(){
    //读取大配置文件
    Configuration cfg=new Configuration().configure();
    //session工厂
    SessionFactory factory=cfg.buildSessionFactory();
    //session对象
    Session session = factory.openSession();
    String hql=" from Dept where deptname=:deptname";
    Query query = session.createQuery(hql);
    query.setParameter("deptname","开发部");
    List<Dept> list = query.list();
    for (Dept dept:list){
    System.out.println(dept.getDeptname());
    }
    }

      

     03.绑定命名参数与一个对象的属性值

    //参数查询  对象类型 方法三  参数名称绑定+++对象属性 :name
    @Test
    public void test07(){
    //读取大配置文件
    Configuration cfg=new Configuration().configure();
    //session工厂
    SessionFactory factory=cfg.buildSessionFactory();
    //session对象
    Session session = factory.openSession();
    String hql=" from Dept where deptname=:deptname";
    Dept depts=new Dept();
    depts.setDeptname("开发部");

    Query query = session.createQuery(hql);
    query.setProperties(depts);
    List<Dept> list = query.list();
    for (Dept dept:list){
    System.out.println(dept.getDeptname());
    }
    }



    多个参数查询类似,所以这里就不说了。

     

  • 相关阅读:
    配置HDFS HttpFS和WebHDFS
    编译hbase-1.2.3源代码
    Yarn application has already exited with state FINISHED
    一种基于Redis的10行代码实现IP频率控制方法
    PRId64的正确用法
    cmake检测g++编译器是否支持c++11
    定时取指定进程内存脚本
    C++常见gcc编译链接错误解决方法
    Congestion Avoidance in TCP
    Studying TCP's Throughput and Goodput using NS
  • 原文地址:https://www.cnblogs.com/sujulin/p/8119225.html
Copyright © 2011-2022 走看看