Hibernate自动创表&&Hibernate的四种查询方式
来源:互联网 发布:打谱软件什么意思 编辑:程序博客网 时间:2024/06/11 03:59
1.SQL 本地Sql查询 ------> createSQLQuery( "sql查询语句").addEntity(实体类);
2.HQL Hibernate 查询语言------> createQuery("from 实体类")
3.QBC(Query by cretirea)标准查询 ------> createCriteria(实体类)
4.get,load方式查询 ------> get(实体类,查询id值(即序列号));
代码如下
package Hibernate;
import java.util.List;
import org.hibernate.Criteria;
import org.hibernate.Query;
import org.hibernate.SQLQuery;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.hibernate.classic.Session;
import org.hibernate.criterion.Restrictions;
import org.hibernate.tool.hbm2ddl.SchemaExport;
import org.junit.Test;
public class test {
private static SessionFactory sf;
static{
Configuration conn = new Configuration().configure();
sf = conn.buildSessionFactory();
}
// 自动建表
//@Test
public void testCreate() throws Exception {
// 创建配置管理类对象
Configuration config = new Configuration();
// 加载主配置文件
config.configure();
// 创建工具类对象
SchemaExport export = new SchemaExport(config);
// 建表
// 第一个参数: 是否在控制台打印建表语句
// 第二个参数: 是否执行脚本
export.create(true, true);
}
//QBC完全面向对象查询
// @Test
public void query_qbc() {
Configuration conn = new Configuration().configure();
SessionFactory sf = conn.buildSessionFactory();
Session session = sf.openSession();
Criteria cc = session.createCriteria(Employee_bean.class);
cc.add(Restrictions.eq("empId", 1));
List list = cc.list();
System.out.println(list);
}
//
//@Test
public void query_hql() {
Session session = sf.openSession();
Query cq = session.createQuery("from Employee_bean");
List list = cq.list();
System.out.println(list);
}
//@Test
public void query_sql() {
Session session = sf.openSession();
SQLQuery sq = session.createSQLQuery( "select * from Employee where id=1").addEntity(Employee_bean.class);
List list = sq.list();
System.out.println(list);
}
@Test
public void query_get_load() {
Session session = sf.openSession();
Employee_bean emp1 = (Employee_bean) session.get(Employee_bean.class, 1);
Employee_bean emp2 = (Employee_bean) session.load(Employee_bean.class, 1);
System.out.println(emp2);
//另外可以使用get或者load方式,直接获取数据某个字段的元组值
System.out.println(emp2.getEmpName());
//总结:其实load和get效果差不多但是 ,get和load方法有区别---->get是直接输出sql语句;load是懒加载,load采用代理,等使用该对象的时候才输出结果!
}
}
- Hibernate自动创表&&Hibernate的四种查询方式
- Hibernate的查询方式
- hibernate的查询方式
- hibernate的查询方式
- Hibernate的查询方式
- Hibernate的查询方式
- Hibernate的查询方式
- hibernate的查询方式
- hibernate的查询方式
- hibernate的查询方式
- hibernate的查询方式
- Hibernate的查询方式
- Hibernate的查询方式
- Hibernate的四种查询方式(主键查询,HQL查询,Criteria查询,本地sql查询)和修改和添加
- Hibernate 查询MatchMode的四种模式
- Hibernate 查询MatchMode的四种模式
- Hibernate 查询MatchMode的四种模式
- Hibernate创建数据库的四种方式
- gulp的gulpfile.js文件配置(2)
- Flask+gevent 异步 WEB 架构
- JSP中有哪些重定向方法,它们的区别?
- 解决Ubuntu16.04时间不对的问题
- DP-01背包
- Hibernate自动创表&&Hibernate的四种查询方式
- verilog学习笔记——8位带置位信号的计数器
- Java中的泛型方法
- React Native边学边做(二)登录页面
- PAT 1069. 微博转发抽奖(20)
- java初学求素数小程序
- hihoCoder [Offer收割]编程练习赛8 小Ho的强迫症 (裴蜀定理)
- 第一章 开始Lua
- Win64 驱动内核编程-8.内核里的其他常用