day36-hibernate检索和优化 02-Hibernate检索方式:简单查询及别名查询
Hibernate:
insert
into
Customer
(cname)
values
(?)
Hibernate:
insert
into
orders
(addr, cno)
values
(?, ?)
Hibernate:
insert
into
orders
(addr, cno)
values
(?, ?)
Hibernate:
insert
into
orders
(addr, cno)
values
(?, ?)
Hibernate:
insert
into
orders
(addr, cno)
values
(?, ?)
Hibernate:
insert
into
orders
(addr, cno)
values
(?, ?)
Hibernate:
insert
into
orders
(addr, cno)
values
(?, ?)
Hibernate:
insert
into
orders
(addr, cno)
values
(?, ?)
Hibernate:
insert
into
orders
(addr, cno)
values
(?, ?)
Hibernate:
insert
into
orders
(addr, cno)
values
(?, ?)
Hibernate:
insert
into
orders
(addr, cno)
values
(?, ?)
Hibernate:
insert
into
orders
(addr, cno)
values
(?, ?)
Hibernate:
update
orders
set
cno=?
where
oid=?
Hibernate:
update
orders
set
cno=?
where
oid=?
Hibernate:
update
orders
set
cno=?
where
oid=?
Hibernate:
update
orders
set
cno=?
where
oid=?
Hibernate:
update
orders
set
cno=?
where
oid=?
Hibernate:
update
orders
set
cno=?
where
oid=?
Hibernate:
update
orders
set
cno=?
where
oid=?
Hibernate:
update
orders
set
cno=?
where
oid=?
Hibernate:
update
orders
set
cno=?
where
oid=?
Hibernate:
update
orders
set
cno=?
where
oid=?
Hibernate:
update
orders
set
cno=?
where
oid=?
Hibernate:
insert
into
Customer
(cname)
values
(?)
Hibernate:
insert
into
orders
(addr, cno)
values
(?, ?)
Hibernate:
insert
into
orders
(addr, cno)
values
(?, ?)
Hibernate:
insert
into
orders
(addr, cno)
values
(?, ?)
Hibernate:
insert
into
orders
(addr, cno)
values
(?, ?)
Hibernate:
insert
into
orders
(addr, cno)
values
(?, ?)
Hibernate:
insert
into
orders
(addr, cno)
values
(?, ?)
Hibernate:
insert
into
orders
(addr, cno)
values
(?, ?)
Hibernate:
insert
into
orders
(addr, cno)
values
(?, ?)
Hibernate:
insert
into
orders
(addr, cno)
values
(?, ?)
Hibernate:
insert
into
orders
(addr, cno)
values
(?, ?)
Hibernate:
insert
into
orders
(addr, cno)
values
(?, ?)
Hibernate:
update
orders
set
cno=?
where
oid=?
Hibernate:
update
orders
set
cno=?
where
oid=?
Hibernate:
update
orders
set
cno=?
where
oid=?
Hibernate:
update
orders
set
cno=?
where
oid=?
Hibernate:
update
orders
set
cno=?
where
oid=?
Hibernate:
update
orders
set
cno=?
where
oid=?
Hibernate:
update
orders
set
cno=?
where
oid=?
Hibernate:
update
orders
set
cno=?
where
oid=?
Hibernate:
update
orders
set
cno=?
where
oid=?
Hibernate:
update
orders
set
cno=?
where
oid=?
Hibernate:
update
orders
set
cno=?
where
oid=?
Hibernate:
insert
into
Customer
(cname)
values
(?)
Hibernate:
insert
into
orders
(addr, cno)
values
(?, ?)
Hibernate:
insert
into
orders
(addr, cno)
values
(?, ?)
Hibernate:
insert
into
orders
(addr, cno)
values
(?, ?)
Hibernate:
insert
into
orders
(addr, cno)
values
(?, ?)
Hibernate:
insert
into
orders
(addr, cno)
values
(?, ?)
Hibernate:
insert
into
orders
(addr, cno)
values
(?, ?)
Hibernate:
insert
into
orders
(addr, cno)
values
(?, ?)
Hibernate:
insert
into
orders
(addr, cno)
values
(?, ?)
Hibernate:
insert
into
orders
(addr, cno)
values
(?, ?)
Hibernate:
insert
into
orders
(addr, cno)
values
(?, ?)
Hibernate:
insert
into
orders
(addr, cno)
values
(?, ?)
Hibernate:
update
orders
set
cno=?
where
oid=?
Hibernate:
update
orders
set
cno=?
where
oid=?
Hibernate:
update
orders
set
cno=?
where
oid=?
Hibernate:
update
orders
set
cno=?
where
oid=?
Hibernate:
update
orders
set
cno=?
where
oid=?
Hibernate:
update
orders
set
cno=?
where
oid=?
Hibernate:
update
orders
set
cno=?
where
oid=?
Hibernate:
update
orders
set
cno=?
where
oid=?
Hibernate:
update
orders
set
cno=?
where
oid=?
Hibernate:
update
orders
set
cno=?
where
oid=?
Hibernate:
update
orders
set
cno=?
where
oid=?
初始化数据之后使用三种方式检索数据。
客户这里打印也打印出订单的集合,订单这边又打印客户了。这是死循环了,所以这里应该去掉一方的打印。
用SQL的时候给列起过别名。Mysql数据库用As起别名,HQL也是支持这种操作的。
package cn.itcast.test; import java.util.Arrays; import java.util.List; import org.hibernate.Query; import org.hibernate.Session; import org.hibernate.Transaction; import org.junit.Test; import cn.itcast.utils.HibernateUtils; import cn.itcast.vo.Customer; import cn.itcast.vo.Order; /** * Hibernate的检索方式 * @author zhongzh * */ public class HibernateDemo1 { @Test /* * 使用别名: * */ public void demo3(){ Session session = HibernateUtils.openSession(); Transaction tx = session.beginTransaction(); //使用别名 //List<Customer> list = session.createQuery("from Customer as c where c.cname = ?").list(); /* List<Customer> list = session.createQuery("from Customer as c").list(); System.out.println(list);*/ //使用别名:带参数 /* List<Customer> list = session.createQuery("from Customer as c where c.cname = ?").setString(0, "小沈").list(); System.out.println(list);*/ //别名as可以省略 /* List<Customer> list = session.createQuery("from Customer c").list(); System.out.println(list);*/ //不支持 select * from Customer写法.可以写成select 别名 from Customer as 别名; //List<Customer> list = session.createQuery("select * from Customer").list();//createQuery不支持select *的写法,会报语法错误 List<Customer> list = session.createQuery("select c from Customer c").list();//createQuery不支持select *的写法,会报语法错误 System.out.println(list); tx.commit(); session.close(); } @Test public void demo2(){ Session session = HibernateUtils.openSession(); Transaction tx = session.beginTransaction(); //1.使用HQL查询所有客户信息: /* Query query = session.createQuery("from Customer");//"from Customer"是一个HQL语句。返回一个Query接口 query.list(); * */ //session.createQuery("from Customer").setString("", "").list();//HQL支持方法链编程 /* List<Customer> list = session.createQuery("from Customer").list();//HQL支持方法链编程 for(Customer customer:list){ System.out.println(customer); }*/ //2.使用QBC的方式查询所有记录: /* List<Customer> list= session.createCriteria(Customer.class).list();//返回Criteria接口 for (Customer customer:list){ System.out.println(customer); }*/ //3.使用SQL语句查询所有记录: /* List<Object[]> list = session.createSQLQuery("select * from customer").list();//这里要写SQL语句 这里操作的是数据库表 //但是HQL和QBC方式操作的都是对象 这种SQL查询的方式得到的List集合不会直接装到实体对象里面。而是放到Object数组中。 for(Object[] objects:list){ System.out.println(Arrays.toString(objects));//把Object数组打印一下 }*/ //4.使用SQL语句查询所有记录:封装到实体对象中 List<Customer> list = session.createSQLQuery("select * from customer").addEntity(Customer.class).list();//这里要写SQL语句 这里操作的是数据库表 //但是HQL和QBC方式操作的都是对象 这种SQL查询的方式得到的List集合不会直接装到实体对象里面。而是放到Object数组中。这也是方法链编程 for(Customer customer:list){ System.out.println(customer);//这个时候就是打印出实体对象的方式 } tx.commit(); session.close(); } @Test /* * * 初始化数据 */ public void demo1(){ Session session = HibernateUtils.openSession(); Transaction tx = session.beginTransaction(); Customer customer = new Customer(); //customer.setCname("小金"); //customer.setCname("小明"); customer.setCname("小沈"); for(int i=0;i<=10;i++){ Order order = new Order(); //order.setAddr("西三旗"+i); order.setAddr("山东"+i); order.setCustomer(customer); customer.getOrders().add(order); } session.save(customer); //还需要设置订单,如果不想设置订单那就要配置级联才行 tx.commit(); session.close(); } }
package cn.itcast.vo; import java.util.HashSet; import java.util.Set; /** * 客户的实体: * @author 姜涛 * */ public class Customer { private Integer cid; private String cname; // 一个客户有多个订单. private Set<Order> orders = new HashSet<Order>(); public Integer getCid() { return cid; } public void setCid(Integer cid) { this.cid = cid; } public String getCname() { return cname; } public void setCname(String cname) { this.cname = cname; } public Set<Order> getOrders() { return orders; } public void setOrders(Set<Order> orders) { this.orders = orders; } @Override public String toString() {//客户这里打印也打印出订单的集合。这是死循环了,所以这里应该去掉一方的打印。 /* return "Customer [cid=" + cid + ", cname=" + cname + ", orders=" + orders + "]";*/ return "Customer [cid=" + cid + ", cname=" + cname + "]";//客户这里去掉订单集合的打印 } }
package cn.itcast.vo; /** * 订单的实体: * @author 姜涛 * */ public class Order { private Integer oid; private String addr; // 订单属于某一个客户.放置一个客户的对象. private Customer customer; public Integer getOid() { return oid; } public void setOid(Integer oid) { this.oid = oid; } public String getAddr() { return addr; } public void setAddr(String addr) { this.addr = addr; } public Customer getCustomer() { return customer; } public void setCustomer(Customer customer) { this.customer = customer; } @Override public String toString() {//订单这边又打印客户了。这是死循环了,所以这里应该去掉一方的打印。 return "Order [oid=" + oid + ", addr=" + addr + ", customer=" + customer + "]"; } }
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"> <hibernate-mapping> <!-- <class name="cn.itcast.hibernate3.demo2.Customer" table="customer"> --> <class name="cn.itcast.vo.Customer"> <!-- 配置唯一标识 --> <id name="cid" column="cid"> <generator class="native"/> </id> <!-- 配置普通属性 --> <property name="cname" column="cname" length="20"/> <!-- 建立映射 --> <!-- 配置一个集合 <set>的name Customer对象中的关联对象的属性名称. --> <!-- 这里把级联去掉 要最简短的配置 <set name="orders" cascade="save-update" inverse="true"> --> <set name="orders" cascade="save-update"> <!-- <key>标签中column:用来描述一对多多的一方的外键的名称. --> <key column="cno"></key> <!-- 配置一个<one-to-many>标签中class属性:订单的类的全路径 --> <!-- <one-to-many class="cn.itcast.hibernate3.demo2.Order"/> --> <one-to-many class="cn.itcast.vo.Order"/> </set> </class> </hibernate-mapping>
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"> <hibernate-mapping> <!-- <class name="cn.itcast.hibernate3.demo2.Order" table="orders"> --> <class name="cn.itcast.vo.Order" table="orders"> <!-- 配置唯一标识 --> <id name="oid" column="oid"> <generator class="native"/> </id> <!-- 配置普通属性 --> <property name="addr" column="addr" length="50"/> <!-- 配置映射 --> <!-- <many-to-one>标签 name :关联对象的属性的名称. column :表中的外键名称. class :关联对象类的全路径 --> <!-- <many-to-one name="customer" column="cno" class="cn.itcast.hibernate3.demo2.Customer"/> --> <many-to-one name="customer" column="cno" class="cn.itcast.vo.Customer"/> </class> </hibernate-mapping>
<?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="hibernate.connection.driver_class"> com.mysql.jdbc.Driver </property> <property name="hibernate.connection.url"><!-- 连接的数据库是hibernate3_day02 --> jdbc:mysql:///hibernate3_day03 </property> <property name="hibernate.connection.username">root</property> <property name="hibernate.connection.password"></property> <!-- Hibernate的方言 --> <!-- 生成底层SQL不同的 --> <property name="hibernate.dialect"> org.hibernate.dialect.MySQLDialect </property> <!-- 可选的属性 --> <!-- 显示SQL --> <property name="hibernate.show_sql">true</property> <!-- 格式化SQL --> <property name="hibernate.format_sql">true</property> <property name="hibernate.connection.autocommit">false</property> <!-- hbm:映射 to DDL: create drop alter --> <property name="hibernate.hbm2ddl.auto">update</property> <!-- C3P0连接池设定--> <!-- 使用c3po连接池 配置连接池提供的供应商--> <property name="connection.provider_class"> org.hibernate.connection.C3P0ConnectionProvider </property> <!--在连接池中可用的数据库连接的最少数目 --> <property name="c3p0.min_size">5</property> <!--在连接池中所有数据库连接的最大数目 --> <property name="c3p0.max_size">20</property> <!--设定数据库连接的过期时间,以秒为单位, 如果连接池中的某个数据库连接处于空闲状态的时间超过了timeout时间,就会从连接池中清除 --> <property name="c3p0.timeout">120</property> <!--每3000秒检查所有连接池中的空闲连接 以秒为单位--> <property name="c3p0.idle_test_period">3000</property> <!-- 通知Hibernate加载那些映射文件 --> <!-- <mapping resource="cn/itcast/hibernate3/demo1/Book.hbm.xml" /> <mapping resource="cn/itcast/hibernate3/demo2/Customer.hbm.xml" /> <mapping resource="cn/itcast/hibernate3/demo2/Order.hbm.xml" /> <mapping resource="cn/itcast/hibernate3/demo1/Book.hbm.xml" /> --> <!-- 把映射文件中的这几个都拿掉 重新把它引入 --> <mapping resource="cn/itcast/vo/Customer.hbm.xml" /> <mapping resource="cn/itcast/vo/Order.hbm.xml" /> </session-factory> </hibernate-configuration>