Hibernate初识
Posted MyBatis
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Hibernate初识相关的知识,希望对你有一定的参考价值。
小配置里的代码:
<?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 package="cn.happy.entity">
<class name="Dog" table="Dog" >
<id name="dogid" column="dogid">
<generator class="native"></generator>//id主键 自增
</id>
<property name="dogname" column="dogname"></property>
<property name="dogage" column="dogage" ></property>
</class>
</hibernate-mapping>
实体类:
/** * Created by lgy on 2017/9/18. */ public class Dog { private String dogname; private Integer dogage; private Integer dogid; public String getDogname() { return dogname; } public void setDogname(String dogname) { this.dogname = dogname; } public Integer getDogage() { return dogage; } public void setDogage(Integer dogage) { this.dogage = dogage; } public Integer getDogid() { return dogid; } public void setDogid(Integer dogid) { this.dogid = dogid; } }
util:
public class HibernateUntil { private static Configuration cfg=new Configuration().configure(); private static SessionFactory factory=cfg.buildSessionFactory(); //1.方法返回session 静态成员变量不能直接使用非静态成员 //Session依赖于Session工厂,工厂依赖于Configure public static Session getSession(){ return factory.openSession(); } //2.关闭session public static void closeSession(){ getSession().close();
大配置:
<?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="connection.driver_class">oracle.jdbc.driver.OracleDriver</property> <!--数据库url--> <property name="connection.url" >jdbc:oracle:thin:@localhost:1521:orcl</property> <!--数据库username--> <property name="connection.username" >test</property> <!--数据库password--> <property name="connection.password" >test</property> <!--hibernate方言--><!--和Spring整合时name属性前S必须加hibernate,单独用hibernate框架时,name属性可以直接用dialect--> <property name="hibernate.dialect" >org.hibernate.dialect.Oracle10gDialect</property> <!--输出所有语句到控制台--> <property name="hirbernate.show_sql" >true</property> <!--在log和console中打印出更新 格式化显示sql--> <property name="hibernate.format_sql" >true</property> <!--hbm2ddl就是**.hbm.xml,生成特定数据库的sql 从程序中自动建表 updata表示如果底层存在表,name更新表结构 --> <property name="hibernate.hbm2ddl.auto" >update</property> <!--指定当前session范围和上下文 thread至当前线程用来跟踪管理--> <property name="current_session_context_class" >thread</property> <mapping resource="cn/happy/entity/Dog.hbm.xml" ></mapping> </session-factory> </hibernate-configuration>
Test测试类:
@Test public void testhibernate() { //configuration Configuration cfg=new Configuration().configure(); SessionFactory factory=cfg.buildSessionFactory(); Session session=factory.openSession(); Transaction tx=session.beginTransaction(); Dog dog=new Dog(); dog.setDogname("小白"); session.save(dog); tx.commit(); System.out.println("add ok!"); session.close();
}
//用get方法查询数据库
@Test
private void findStudent() {
//02Hibernate 保存
//读取大配置文件,获取连接的数据库信息
Configuration cfg=new Configuration().configure();
//3创建SessionFactory
SessionFactory factory=cfg.buildSessionFactory();
//加工session
Session session=factory.openSession();
//开启事务
Transaction tx=session.beginTransaction();
//5Hibernate
//根据session的方法做数据操作 检索
Student student=session.get(Student.class,2);
System.out.println(student.getName());
//提交事务
tx.commit();
//关闭session
session.close();
System.out.println("success ok");
}
//用load方法查询
@Test
public void update2(){
Configuration cfg=new Configuration().configure();
SessionFactory factory=cfg.buildSessionFactory();
Session session=factory.openSession();
Transaction tx=session.beginTransaction();
Dog dog=session.load(Dog.class,1);
System.out.println("load-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=");
System.out.println(dog.getDogname());
}
load方法:
无论get方法查询,还是load方法,都要开启事务,也不要手动提交!!!
以上是关于Hibernate初识的主要内容,如果未能解决你的问题,请参考以下文章
初识Spring源码 -- doResolveDependency | findAutowireCandidates | @Order@Priority调用排序 | @Autowired注入(代码片段
初识Spring源码 -- doResolveDependency | findAutowireCandidates | @Order@Priority调用排序 | @Autowired注入(代码片段