hibernate------初识
1.创建db_hibernate数据库,在创建的数据库中创建一个students表
2.创建一个Java项目(项目中添加hibernate所需类库)
3.项目中添加hibernate.cfg.xml配置文件,完善配置信息
1 <?xml version="1.0" encoding="UTF-8"?> 2 <!DOCTYPE hibernate-configuration PUBLIC 3 "-//Hibernate/Hibernate Configuration DTD 3.0//EN" 4 "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd"> 5 <hibernate-configuration> 6 <session-factory> 7 <!-- 数据库连接文件 --> 8 <property name="connection.driver_class">com.mysql.jdbc.Driver</property> 9 <property name="connection.url">jdbc:mysql://localhost:3306/db_hibernate</property> 10 <property name="connection.username">root</property> 11 <property name="connection.password">775297</property> 12 <!-- 数据库连接池 --> 13 <property name="connection.pool_size">1</property> 14 <!-- sql方言 --> 15 <property name="dialect">org.hibernate.dialect.MySQL5Dialect</property> 16 <!-- session环境 --> 17 <property name="current_session_context_class">thread</property> 18 <!-- 打印sql语句 --> 19 <property name="show_sql">true</property> 20 <!-- 格式化SQL语句 --> 21 <property name="format_sql">true</property> 22 <!-- 加载配置文件 --> 23 <mapping resource="com/zdsofe/hibernate/Student.hbm.xml"/> 24 </session-factory> 25 </hibernate-configuration> 26
4.创建Student实体类,为该类提供私有属性和get/set方法
1 package com.zdsofe.hibernate; 2 3 import java.util.Date; 4 5 public class Student { 6 7 private int id; 8 private String name; 9 private String sex; 10 private Date birthday; 11 12 public int getId() { 13 return id; 14 } 15 public void setId(int id) { 16 this.id = id; 17 } 18 public String getName() { 19 return name; 20 } 21 public void setName(String name) { 22 this.name = name; 23 } 24 public String getSex() { 25 return sex; 26 } 27 public void setSex(String sex) { 28 this.sex = sex; 29 } 30 public Date getBirthday() { 31 return birthday; 32 } 33 public void setBirthday(Date birthday) { 34 this.birthday = birthday; 35 } 36 37 38 }
5.在Student类的包下添加一个映射文件Student.hbm.xml
1 <?xml version="1.0" encoding="UTF-8"?> 2 <!DOCTYPE hibernate-mapping PUBLIC 3 "-//Hibernate/Hibernate Mapping DTD 3.0//EN" 4 "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"> 5 6 <hibernate-mapping package="com.zdsofe.hibernate"> 7 <!-- 定义实体类与数据库之间的映射 --> 8 <class name="Student" table="students"> 9 <!--定义表主键与属性之间的映射关系 --> 10 <id name="id" column="id"> 11 <!-- 指定主键的生产方式 --> 12 <generator class="native"></generator> 13 </id> 14 <!-- 普通属性与表的映射关系,名字相同可以写一个 --> 15 <property name="name" column="name"></property> 16 <property name="sex"></property> 17 <property name="birthday" type="date"></property> 18 </class> 19 20 </hibernate-mapping>
6.创建SessionFactory对象,获取Session对象 通过Session对象以面向对象形式操作关系型数据库
1 package com.zdsofe.hibernate; 2 3 import java.util.Date; 4 5 import org.hibernate.Session; 6 import org.hibernate.SessionFactory; 7 import org.hibernate.boot.registry.StandardServiceRegistryBuilder; 8 import org.hibernate.cfg.Configuration; 9 import org.hibernate.service.ServiceRegistry; 10 11 public class Test { 12 13 public static void main(String[] args) { 14 //通过配置文件对象获得配置文件 15 Configuration cfg = new Configuration().configure(); 16 //获取服务注册器对象 17 StandardServiceRegistryBuilder ssrb = new StandardServiceRegistryBuilder(); 18 //获得注册器对象 19 ServiceRegistry serviceRegistry = ssrb.applySettings(cfg.getProperties()).build(); 20 //创建SessionFactory 21 SessionFactory sessionFactory = cfg.buildSessionFactory(serviceRegistry); 22 //通过session工厂创建session 23 Session session = sessionFactory.getCurrentSession(); 24 //开启事务 25 session.beginTransaction(); 26 //进行数据库操作 27 Student stu = new Student(); //(瞬时状态,随时可能被销毁,被垃圾回收器回收) 28 stu.setName("饶鹏"); 29 stu.setSex("女"); 30 stu.setBirthday(new Date()); 31 32 //保存数据 33 Integer i = (Integer)session.save(stu);//当stu对象和session关联,并且有个唯一的标识(主键),那么此时的状态称为持久化 34 System.out.println(i); 35 stu.setSex("男");//处于持久化状态时会直接修改,不用调用save、update等 36 //查询 37 /* Student stu2 = (Student) session.get(Student.class, 4);*/ 38 //删除 39 /* session.delete(stu2);*/ //此时stu属于删除态,不建议使用 40 //提交事务 41 session.getTransaction().commit();//此时的状态称为脱管状态(此时的session已经销毁) 42 43 //重新获取session 44 session=sessionFactory.getCurrentSession(); 45 //开启事务 46 session.beginTransaction(); 47 session.update(stu);//在此与session关联,那么此时的状态称为持久化 48 //提交事务 49 session.getTransaction().commit(); 50 51 52 } 53 54 }
以编程的方式获得配置文件
1 package com.zdsofe.hibernate; 2 3 import org.hibernate.Session; 4 import org.hibernate.SessionFactory; 5 import org.hibernate.boot.registry.StandardServiceRegistryBuilder; 6 import org.hibernate.cfg.Configuration; 7 import org.hibernate.service.ServiceRegistry; 8 9 public class Test1 { 10 11 public static void main(String[] args) { 12 //以编程的方式获得配置文件 13 Configuration cfg=new Configuration(); 14 cfg.setProperty("hibernate.connection.driver_class","com.mysql.jdbc.Driver" ); 15 cfg.setProperty("hibernate.connection.url", "jdbc:mysql://localhost:3306/db_hibernate"); 16 cfg.setProperty("hibernate.connection.username", "root"); 17 cfg.setProperty("hibernate.connection.password", "775297"); 18 cfg.setProperty("hibernate.dialect", "org.hibernate.dialect.MySQL5Dialect"); 19 cfg.setProperty("hibernate.current_session_context_class", "thread"); 20 cfg.addResource("com/zdsofe/hibernate/Student.hbm.xml"); 21 //获得注册器对象 22 ServiceRegistry serviceRegistry=new StandardServiceRegistryBuilder().applySettings(cfg.getProperties()).build(); 23 //创建session工厂 24 SessionFactory sessionFactory=cfg.buildSessionFactory(serviceRegistry); 25 //创建session 26 Session session=sessionFactory.getCurrentSession(); 27 //开启事务 28 session.beginTransaction(); 29 //提交事务 30 session.getTransaction().commit(); 31 32 33 34 } 35 }