• Hibernate之hql


    Street实体类

    public class Street {
    
        public Street() {
        }
    
        public Street(String name) {
            this.name = name;
        }
    
        private Integer id;
        private String name;
    
        public Integer getId() {
            return id;
        }
    
        public void setId(Integer id) {
            this.id = id;
        }
    
        public String getName() {
            return name;
        }
    
        public void setName(String name) {
            this.name = name;
        }
    
        public String getDistrict_id() {
            return district_id;
        }
    
        public void setDistrict_id(String district_id) {
            this.district_id = district_id;
        }
    
        private String district_id;
    }
    

     Street.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.hibernate.day01.entity">
        <!--实体 name=实体端的内容   column=DB端的内容-->
        <class name="Street" table="STREET" dynamic-update="true">
            <!--和底层数据表对应的主键   业务意义-->
            <id name="id" column="ID">
                <!--主键生成策略 :assigned:程序员手动给值-->
                <generator class="native"/>
            </id>
            <property name="name" column="NAME"></property>
            <property name="district_id" column="DISTRICT_ID"></property>
        </class>
    
    </hibernate-mapping>
    

      hibernate.cfg.xml大配置

    <?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的基础配置-->
        <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">zyx</property>
            <property name="connection.password">zyx</property>
            <!-- SQL dialect (方言)-->
            <property name="dialect">org.hibernate.dialect.Oracle10gDialect</property>
            <!-- Echo   all executed SQL to stdout 在控制台打印sql -->
            <property name="show_sql">true</property>
            <!--格式化SQL-->
            <property name="format_sql">true</property>
            <!-- Drop and re-create (重新创建)the database schema(架构) on startup (启动)
             是否根据hbm.xml自动建表 建表的策略 update create
            -->
            <property name="hbm2ddl.auto">update</property>
             <!--关联小配置 -->
            <mapping resource="cn/hibernate/day01/entity/Street.hbm.xml"/>
    
        </session-factory>
    
    </hibernate-configuration>
    

      

    1,获取部分列查询

    public void t5(){
            Configuration cfg=new Configuration().configure();
            //1.Session
            SessionFactory factory=cfg.buildSessionFactory();
            //2.session
            Session session=factory.openSession();  //从连接池中随机取出链接
            //3.操作
            Transaction tx=session.beginTransaction();
            String hql="select new Street(d.name)from Street d";
            Query query=session.createQuery(hql);
            List<Street> list=query.list();
            for (Street street:list) {
                System.out.println(street.getName());
            }
    
            tx.commit();
            session.close();
    
    }
    

     2,参数查询,匿名占位符

    public void t6(){
            Configuration cfg=new Configuration().configure();
            //1.Session
            SessionFactory factory=cfg.buildSessionFactory();
            //2.session
            Session session=factory.openSession();  //从连接池中随机取出链接
            //3.操作
            Transaction tx=session.beginTransaction();
            String hql="from Street d where d.name=?";
            Query query=session.createQuery(hql);
            query.setParameter(0,"李四1");
            List<Street> list=query.list();
            for (Street street:list
            ) {
            System.out.println(street.getName());
            }
    
            tx.commit();
            session.close();
    
            }
    

     3,参数查询,参数名绑定

      public void t7(){
            Configuration cfg=new Configuration().configure();
            //1.Session
            SessionFactory factory=cfg.buildSessionFactory();
            //2.session
            Session session=factory.openSession();  //从连接池中随机取出链接
            //3.操作
            Transaction tx=session.beginTransaction();
            String hql="from Street d where d.name=:name";
            Query query=session.createQuery(hql);
            query.setParameter("name","李四1");
            List<Street> list=query.list();
            for (Street street:list
                    ) {
                System.out.println(street.getName());
            }
    
            tx.commit();
            session.close();
    
        }
    

      

    4,参数查询,参数名绑定加对象属性

    public void t8(){
            Configuration cfg=new Configuration().configure();
            //1.Session
            SessionFactory factory=cfg.buildSessionFactory();
            //2.session
            Session session=factory.openSession();  //从连接池中随机取出链接
            //3.操作
            Transaction tx=session.beginTransaction();
            String hql="from Street d where d.name=:name";
            Query query=session.createQuery(hql);
            Street street=new Street();
            street.setName("李四1");
            query.setProperties(street);
            List<Street> list=query.list();
            for (Street item:list
                    ) {
                System.out.println(item.getName());
            }
    
            tx.commit();
            session.close();
    
        }
    

      5,动态sql

     public void t9(){
            Configuration cfg=new Configuration().configure();
            //1.Session
            SessionFactory factory=cfg.buildSessionFactory();
            //2.session
            Session session=factory.openSession();  //从连接池中随机取出链接
            //3.操作
            Transaction tx=session.beginTransaction();
    
            Street street=new Street();
            street.setName("李四1");
            street.setId(1);
    
           StringBuilder sb=new StringBuilder("from Street e where 1=1 ");
    
           if(street.getName()!=null){
             sb.append("and e.name=:name ");
           }
            if(street.getId()!=null){
              sb.append("and e.id>:id");
            }
    
            Query query=session.createQuery(sb.toString());
    
            query.setProperties(street);
            List<Street> list=query.list();
            for (Street item:list
                    ) {
                System.out.println(item.getName());
            }
    
            tx.commit();
            session.close();
    
        }
    

      

  • 相关阅读:
    b_lc_带阈值的图连通性(反向思维+并查集)
    b_lc_无矛盾的最佳球队(排序+LIS)
    b_lq_子串分值(记录当前字符的出现的前一个位置+组合数学)
    多测师讲解python _课堂练习题梳理_高级讲师肖sir
    多测师讲解python _常见的正则表达式_高级讲师肖sir
    多测师讲解 _python常见的加密方式_高级讲师肖sir
    多测师讲解python _100道题_高级讲师肖sir
    前端 CSS 一些标签默认有padding
    前端 CSS 盒子模型
    Linux ulimit 命令 限制系统用户对 shell 资源的访问
  • 原文地址:https://www.cnblogs.com/spghs/p/8119267.html
Copyright © 2020-2023  润新知