Hibernate框架连接数据库
Hibernate的优点:
1、Hibernate对JDBC访问数据库的代码做了封装,大大简化了数据访问层繁琐的重复性代码。
2、Hibernate是一个基于jdbc的主流持久化框架,是一个优秀的orm实现,它很大程度的简化了dao层编码工作。
3、Hibernate使用java的反射机制,而不是字节码增强程序类实现透明性
4、Hibernate的性能非常好,因为它是一个轻量级框架。映射的灵 活性很出色。它支持很多关系型数据库,从一对一到多对多的各种复杂关系。
5、Hibernate使数据库的操作完全面向对象。而不是从前的面向关系进行操作。
classpath下的hibernate.cfg.xml
<?xml version='1.0' encoding='utf-8'?> <!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"> <hibernate-configuration> <session-factory> <!-- 数据库连接信息 --> <property name="connection.driver_class">com.mysql.jdbc.Driver</property> <property name="connection.url">jdbc:mysql://localhost:3306/hibernatedb</property> <property name="connection.username">root</property> <property name="connection.password">bdqn</property> <!-- 数据库方言 --> <property name="dialect"> org.hibernate.dialect.MySQL5Dialect </property> <!-- update 没有表自动建立表 validate 验证表结构,如有改动,更新表结构 create 每次运行都创建 create-drop 每次创建,运行完销毁 --> <property name="hbm2ddl.auto">update</property> <!-- 显示sql --> <property name="show_sql">true</property> <!-- 格式化sql语句 --> <property name="format_sql">true</property> <!-- 添加映射文件 --> <mapping resource="cn/bdqn/domain/Person.hbm.xml"/> </session-factory> </hibernate-configuration>
例:新建一个Person类(cn.bdqn.domain.Person.java)
package cn.bdqn.domain; public class Person { private Integer pid; private String pname; private Integer age; private String location; public Person() { } public Person(String pname, Integer age, String location) { this.pname = pname; this.age = age; this.location = location; } public Integer getPid() { return pid; } public void setPid(Integer pid) { this.pid = pid; } public String getPname() { return pname; } public void setPname(String pname) { this.pname = pname; } public Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; } public String getLocation() { return location; } public void setLocation(String location) { this.location = location; } @Override public String toString() { return "Person [pid=" + pid + ", pname=" + pname + ", age=" + age + ", location=" + location + "]"; } }
cn.bdqn.domain.Person.hbm.xml
<?xml version="1.0" encoding="utf-8"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> <hibernate-mapping> <!-- table 表名 --> <class name="cn.bdqn.domain.Person" table="person"> <!-- 主键--> <id name="pid" column="pid" type="integer" length="11"> <!-- 主键生成机制 Increment 每次增量为1 identity 需要数据库支持自动增长 Sequence Oracle数据库 uuid 主键是string类型 128位的UUID算法来生成标识符 assigned 手动设置主键 --> <generator class="identity"></generator> </id> <!--一般属性 --> <property name="pname" type="string" length="50"></property> <property name="age" type="integer" length="20"></property> <property name="location" type="string"></property> </class> </hibernate-mapping>
cn.bdqn.test.hbTest.java
package cn.bdqn.test; import java.util.List; import org.hibernate.SessionFactory; import org.hibernate.Transaction; import org.hibernate.cfg.Configuration; import org.hibernate.classic.Session; import org.junit.Before; import org.junit.Test; import cn.bdqn.domain.Person; public class hbTest { private SessionFactory factory; @Before public void getSessionFactory(){ //1.加载配置文件 Configuration config = new Configuration(); config.configure(); //默认加载classpath下的hibernate.cfg.xml //2.获取sessionFactory factory = config.buildSessionFactory(); } @Test public void test1(){ //System.out.println(factory); //添加一条数据 //3.获取session Session session = factory.openSession(); //4.开启事务 Transaction ts = session.beginTransaction(); Person p1 = new Person("小王", 12, "天津市"); session.save(p1); //临时状态 变为持久状态 //session.clear(); //持久状态变为游离状态 ts.commit(); session.close(); } //查询 @Test public void testFind(){ //1.获取session Session session = factory.openSession(); Person per = (Person)session.get(Person.class, 2);//持久状态 System.out.println(per); session.close(); } //删除 @Test public void testDelete(){ //1.获取session Session session = factory.openSession(); Transaction ts = session.beginTransaction(); Person p = new Person(); p.setPid(1); session.delete(p); ts.commit(); session.close(); } //更新 @Test public void testUpdate(){ //1.获取session Session session = factory.openSession(); Transaction ts = session.beginTransaction(); Person p = new Person(); p.setPid(2); p.setAge(48); p.setLocation("北京市"); p.setPname("老张"); session.update(p); ts.commit(); session.close(); } //查询所有 @Test public void testFindPersons(){ Session session = factory.openSession(); List<Person> plist = session.createQuery("from Person").list(); System.out.println(plist); session.close(); } }
hibernate中对象的状态
1.瞬时状态(临时状态)
不处于 Session 的缓存中,在数据库中没有对应的记录
2.持久状态
调用save(),get(),load(),update(),saveOrUpdate() 将java对象的状态变成持久化状态
3.游离状态
clear(),close(),evict(); 不在session缓存中 一般由持久化状态转变,数据库中可能有对应的记录
长路漫漫,键盘作伴~