首页 > 学院 > 开发设计 > 正文

Hibernate总结

2019-11-11 05:19:40
字体:
来源:转载
供稿:网友

**

hibernate设计及ORM(摘录)

** ORM:Object–Relation–Mapping对象关系映射

对象指的就是java的实体对象; 关系指的是关系型数据库。(Oracle、DB2、MySQL、SqlServer) ORM的主要思想就是将程序中的对象和数据库中的数据实现自动映射 转换。利用ORM工具,在查询时,可以自动将记录封装成Java对象返 回。在更新、插入操作时,可以将对象自动写入数据表。对于中间的 SQL+JDBC操作细节,完全封装在工具底层

hibernate几个架构 1)POJO类

public class hibernate_test { public String name; public int age; public String sex; public hibernate_test() { } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public String getSex() { return sex; } public void setSex(String sex) { this.sex = sex; } @Override public String toString() { return "hibernate_test [name=" + name + ", age=" + age + ", sex=" + sex + "]"; }}

2)hibernate_cfg.xml

<?xml version="1.0"?><!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN""http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"><hibernate-mapping package="hibernate_001"> <class name="hibernate_test" table="HIBERNATE_TEST"> <id name="name" type="java.lang.String"> <column name="NAME" /> </id> <PRoperty name="age" type="int"> <column name="AGE" /> </property> <property name="sex" type="java.lang.String"> <column name="SEX" /> </property> </class> </hibernate-mapping>

3)*.hbm.xml

<hibernate-configuration><session-factory> <!--指定连接数据库用的驱动--> <property name="connection.driver_class"></property> <property name="connection.url"></property> <!--指定连接数据库的用户名--> <property name="connection.username">用户名</property> <property name="connection.passWord">密码</property> <!-- SQL dialect oracle方言--> <property name="dialect">org.hibernate.dialect.Oracle10gDialect</property> <!-- Echo all executed SQL to stdout --> <property name="show_sql">true</property> <!-- Drop and re-create the database schema on startup --> <property name="hbm2ddl.auto">create</property> <!-- 配置文件地址 --> <mapping resource="xxx"/></session-factory></hibernate-configuration>

使用hibernate的优势 :不再需要大量的sql语句 ,实体类跟数据库之间实现自动映射转换

hibernate调用API **1.创建 Configuration对象 2.创建session工厂 3.通过工厂创建会话对象 4.获取事物对象 5.进行操作,提交,结束及关闭Session**

Configuration cfg=new Configuration();SessionFactory sf=cfg.configure().buildSessionFactory();Session session=sf.openSession();session.beginTransaction();

常用的ID generator 1.identity 递增 2.sequence 只限oracle 3.native 跨数据库时使用 4.assigned 用户自定义id

其他如foreign,sequence,Hilo

hibernate7大关联映射 不多作介绍,详见:

http://blog.csdn.net/t1012665655/article/details/54574441

hibernate缓存机制

“session缓存”即hibernate的一级缓存,sessionfactory即二级缓存,同时二级缓存适合存放 :常量数据,几乎不再修改的数据,非并发访问的数据

详见:

http://www.cnblogs.com/wean/archive/2012/05/16/2502724.html

Hibernate查询(摘录)

*a.HQL查询

Hibernate Query LanguageHQL与SQL语句结构相似,SQL语句是面向数据表和字段进行查询,而HQL语句是面向Hibernate映射过来的对象进行查询,因此HQL被称为是一种面向对象查询语言HQL和SQL共同点: --都支持select,from,where,order by,having,group by等子句。 --都支持运算符表达式,例如+,-,*,/,>,<等 --都支持in,not in,between and,like等过滤条件关键字 --都支持分组函max,min,sum,avg,countHQL和SQL不同点: --HQL是大小写敏感的,类名和属性名严格区分大小写 --HQL不支持select * 写法 --HQL不支持join...on...中的on子句,因为join...on发生在多表操作,而 Hibernate中对于有关系的多张表已将将关联映射写在了xxx.hbm.xml 中,在查询时会自动加上join..on --HQL不支持表名和字段名public class TestHQL { Configuration cfg=new Configuration(); SessionFactory sFactory=cfg.configure().buildSessionFactory(); //@Test public void save(){ Session session=sFactory.openSession(); session.beginTransaction(); //设置10个板块 for (int i = 0; i < 10; i++) { Categroy categroy=new Categroy(); categroy.setName("c"+i); session.save(categroy); } //设10个主题 for (int i = 0; i <10; i++) { Categroy c=new Categroy(); c.setId(1); Topic topic=new Topic(); topic.setCategroy(c); topic.setName("t"+i); session.save(topic); } //设10个对应回复 /*for (int i = 0; i < 10; i++) { Topic t=new Topic(); t.setId(1); Msg msg=new Msg(); msg.setMess("m"+i); msg.setTopic(t); session.save(msg); }*/ session.getTransaction().commit(); session.close(); } //@Test public void Test01(){ Session session=sFactory.openSession(); session.beginTransaction(); Query query=session.createQuery("from Categroy"); List<Categroy> categroys=query.list(); for(Categroy c:categroys){ System.out.println(c.getName()); } session.getTransaction().commit(); session.close(); } /** *查询多个属性,动态构建一个对象,必须要有有参,无参构造器 * */ // @Test public void Test02(){ Session session=sFactory.openSession(); session.beginTransaction(); String sql="select new Topic(id,name,categroy) from Topic"; Query query=session.createQuery(sql); List<Topic> topics=query.list(); for(Topic c:topics){ System.out.println(c); } session.getTransaction().commit(); session.close(); } /** * 对象查询,条件查询 * 参数的顺序从0开始 */ // @Test public void test03(){ Session session = sFactory.openSession(); String hql = "select * from Dept where name=?"; Query query = session.createQuery(hql); query.setString(0,"hfz"); List<Topic> topics = query.list(); for (Topic d :topics) { System.out.println(d); } session.getTransaction().commit(); session.close(); } /** * 对象查询,条件查询 */ //@Test public void test04(){ Session session = sFactory.openSession(); String hql = "select * from Dept where name=? and id=?"; Query query = session.createQuery(hql); query.setString(0,"hfz").setInteger(1,1).list(); List<Topic> topics = query.list(); for (Topic d :topics) { System.out.println(d); } session.getTransaction().commit(); session.close(); } /** * 查询所有板块信息 * 分页查询 * setFirstResult();确定查询的起点 * setMaxResult();确定查询的条数 */ @Test public void test05(){ Session session = sFactory.openSession(); int currentPage = 5; int pageSize = 5; List<Categroy> categroys = session.createQuery("from Categroy"). setFirstResult((currentPage-1)*pageSize). setMaxResults(pageSize).list(); for (Categroy c : categroys) { System.out.println(c.getName()); } session.getTransaction().commit(); session.close(); } }

当然,现在与之类似的mybatis也用的越来越多,俩者的对比也是没玩没了,到最后无非还是那句话,能抓老鼠的猫就是一只好猫 引用mybatis作者的话即:


If you are starting a new project and you’re in full control of your object model and database design, Hibernate is a good choice of O/R tool. If you are accessing any 3rd party databases (e.g. vendor supplied), or you’re working with a legacy database, or even just a really poorly designed database, then an O/R mapper might not be capable of handling the situation. That’s were an SQL Mapper comes in handy



发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表