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



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

     

  • 相关阅读:
    年末反思
    Flink运行时架构
    Phoenix 启动报错:Error: ERROR 726 (43M10): Inconsistent namespace mapping properties. Cannot initiate connection as SYSTEM:CATALOG is found but client does not have phoenix.schema.
    Clickhouse学习
    Flink简单认识
    IDEA无法pull代码到本地,Can't Update No tracked branch configured for branch master or the branch doesn't exist.
    第1章 计算机系统漫游
    简单的 Shell 脚本入门教程
    开源≠免费 常见开源协议介绍
    MySQL 视图
  • 原文地址:https://www.cnblogs.com/sujulin/p/8119225.html
Copyright © 2011-2022 走看看