【hibernate实例】赵雅智_第一例配置及增加

新建java项目hibernate


在hibernate项目下,新建文件夹lib,并添加jar包



新建数据库hibernate


新建表user


新建domain包,封装类


User.java

  1. package www.hbsi.com.domain;  
  2.   
  3. import java.sql.Date;  
  4.   
  5. public class User {  
  6.     private int id;  
  7.     private String name;  
  8.     private Date birthday;  
  9.     public int getId() {  
  10.         return id;  
  11.     }  
  12.     public void setId(int id) {  
  13.         this.id = id;  
  14.     }  
  15.     public String getName() {  
  16.         return name;  
  17.     }  
  18.     public void setName(String name) {  
  19.         this.name = name;  
  20.     }  
  21.     public Date getBirthday() {  
  22.         return birthday;  
  23.     }  
  24.     public void setBirthday(Date birthday) {  
  25.         this.birthday = birthday;  
  26.     }  
  27.     @Override  
  28.     public String toString() {  
  29.         return "User [birthday=" + birthday + ", id=" + id + ", name=" + name  
  30.                 + "]";  
  31.     }  
  32. }  

创建配置文件

Hibernate.cfg.xml

  1. <!DOCTYPE hibernate-configuration PUBLIC  
  2.     "-//Hibernate/Hibernate Configuration DTD 3.0//EN"  
  3.     "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">  
  4.   
  5. <!-- 根标签,根元素 -->  
  6. <hibernate-configuration>  
  7.     <!-- session工厂 -->  
  8.     <session-factory>  
  9.         <property name="connection.driver_class">com.mysql.jdbc.Driver</property>  
  10.         <property name="connection.url">jdbc:mysql:///hibernate</property>  
  11.         <property name="connection.username">root</property>  
  12.         <property name="connection.password">123456</property>  
  13.         <!-- 方言,连接的哪个数据库 -->  
  14.         <property name="dialect">org.hibernate.dialect.MySQLDialect</property>  
  15.         <!-- 执行的时候是否输出sql句 -->  
  16.         <property name="show_sql">true</property>  
  17.         <!-- 数据定义语言 -->  
  18.         <property name="hbm2ddl.auto">update</property>  
  19.     </session-factory>  
  20. </hibernate-configuration>  

持久化类的映射文件


User.hbm.xml

  1. <?xml version="1.0"?>  
  2. <!DOCTYPE hibernate-mapping PUBLIC  
  3.       "-//Hibernate/Hibernate Mapping DTD 3.0//EN"  
  4.           "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">  
  5.   
  6.   
  7. <hibernate-mapping package="com.hbsi.domain">  
  8.     <!-- 主键 -->  
  9.     <class name="User" >  
  10.         <id name="id">  
  11.             <generator class="increment" />  
  12.         </id>  
  13.         <!-- 属性 -->  
  14.         <property name="name" />  
  15.         <property name="birthday" />  
  16.     </class>  
  17.   
  18.       
  19.   
  20. </hibernate-mapping>  

让hibernate.cfg.xml知道持久化类,修改hibernate.cfg.xml

hibernate.cfg.xml

  1. <!DOCTYPE hibernate-configuration PUBLIC  
  2.     "-//Hibernate/Hibernate Configuration DTD 3.0//EN"  
  3.     "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">  
  4.   
  5. <!-- 根标签,根元素 -->  
  6. <hibernate-configuration>  
  7.     <!-- session工厂 -->  
  8.     <session-factory>  
  9.         <property name="connection.driver_class">com.mysql.jdbc.Driver</property>  
  10.         <property name="connection.url">jdbc:mysql:///hibernate</property>  
  11.         <property name="connection.username">root</property>  
  12.         <property name="connection.password">123456</property>  
  13.         <!-- 方言,连接的哪个数据库 -->  
  14.         <property name="dialect">org.hibernate.dialect.MySQLDialect</property>  
  15.         <!-- 执行的时候是否输出sql句 -->  
  16.         <property name="show_sql">true</property>  
  17.         <!-- 数据定义语言 -->  
  18.         <property name="hbm2ddl.auto">update</property>  
  19.         <!-- 加载持久化类文件 -->  
  20.         <mapping resource="www/hbsi/com/domain/User.hbm.xml"/>  
  21.       
  22.     </session-factory>  
  23. </hibernate-configuration>  


做一个测试类



Demo1.java

  1. package www.hbsi.com.demo;  
  2. import java.util.Date;  
  3.   
  4. import org.hibernate.Session;  
  5. import org.hibernate.SessionFactory;  
  6. import org.hibernate.cfg.Configuration;  
  7.   
  8. import www.hbsi.com.domain.User;  
  9.   
  10. public class Demo1 {  
  11.     public static void main(String args[]){  
  12.         User user = new User();  
  13.         user.setName("Nacy");  
  14.         user.setBirthday(new Date());  
  15.           
  16.         //新建一个配置对象,通过此对象加载hibernate.cfg.xml信息  
  17.         Configuration cfg = new Configuration().configure();  
  18.           
  19.         //创建session工厂  
  20.         SessionFactory sessionFactory = cfg.buildSessionFactory();  
  21.         //得到session对象  相当于JDBC的Connection  
  22.         Session session = sessionFactory.openSession();  
  23.           
  24.         //hibernate下要开启事务进行保存  
  25.         session.beginTransaction();  
  26.         //保存  
  27.         session.save(user);  
  28.         //提交事务  
  29.         session.getTransaction().commit();  
  30.         //关闭事务  
  31.         session.close();  
  32.     }  
  33. }  




  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值