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