写hibernate的基本方法

1、新建一个普通的java项目,按照上面的步骤引入相关的jar包和配置文件

2、建立User实体类
[java]  view plain  copy
  1. import java.util.Date;  
  2.   
  3. public class User {  
  4.     private String id;  
  5.     private String username;  
  6.     private String password;  
  7.     private Date createTime;  
  8.     private Date expireTime;  
  9.       
  10.     public String getId() {  
  11.         return id;  
  12.     }  
  13.     public void setId(String id) {  
  14.         this.id = id;  
  15.     }  
  16.     public String getUsername() {  
  17.         return username;  
  18.     }  
  19.     public void setUsername(String userName) {  
  20.         this.username = userName;  
  21.     }  
  22.     public String getPassword() {  
  23.         return password;  
  24.     }  
  25.     public void setPassword(String password) {  
  26.         this.password = password;  
  27.     }  
  28.     public Date getCreateTime() {  
  29.         return createTime;  
  30.     }  
  31.     public void setCreateTime(Date createTime) {  
  32.         this.createTime = createTime;  
  33.     }  
  34.     public Date getExpireTime() {  
  35.         return expireTime;  
  36.     }  
  37.     public void setExpireTime(Date expireTime) {  
  38.         this.expireTime = expireTime;  
  39.     }  
  40. }  

2、提供User.hbm.xml文件,完成实体类的映射
[html]  view plain  copy
  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. <hibernate-mapping>  
  7.     <class name="com.example.hibernate.User">  
  8.         <id name="id">  
  9.             <generator class="uuid"/>  
  10.         </id>  
  11.         <property name="username"/>  
  12.         <property name="password"/>  
  13.         <property name="createTime"/>  
  14.         <property name="expireTime"/>  
  15.     </class>  
  16. </hibernate-mapping>  
其中的property标签是将要生成是数据库表中的字段,在这里不用关心各个字段是什么类型的。因为Hibernate会根据上面的实体类中属性的类型来决定将来表中字段的类型

3、配置hibernate.cfg.xml文件
[html]  view plain  copy
  1. <hibernate-configuration>  
  2.     <session-factory >  
  3.         <!-- mysql数据库驱动 -->  
  4.         <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>  
  5.         <!-- mysql数据库名称 -->  
  6.         <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/hibernate_first</property>  
  7.         <!-- 数据库的登陆用户名 -->  
  8.         <property name="hibernate.connection.username">root</property>  
  9.         <!-- 数据库的登陆密码 -->  
  10.         <property name="hibernate.connection.password">root</property>  
  11.         <!-- 方言:为每一种数据库提供适配器,方便转换 -->  
  12.         <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>  
  13.           
  14.         <mapping resource="com/example/hibernate/User.hbm.xml"/>
  15.     </session-factory>  
  16. </hibernate-configuration>  
注意:必须是“/”而不能是“.”。
4、生成表:编写工具类ExoprtDB.java,将hbm生成ddl
[java]  view plain  copy
  1. import org.hibernate.cfg.Configuration;  
  2. import org.hibernate.tool.hbm2ddl.SchemaExport;  
  3. /** 
  4.  * 将hbm生成ddl 
  5.  * @author BCH 
  6.  * 
  7.  */  
  8. public class ExoprtDB {  
  9.   
  10.     public static void main(String[] args) {  
  11.         //默认读取hibernate.cfg.xml文件  
  12.         Configuration cfr = new Configuration().configure();  
  13.           
  14.         SchemaExport export = new SchemaExport(cfr);  
  15.         export.create(truetrue);  
  16.     }  
  17. }  
到这里就可以生成User表了,但是如果直接运行ExoprtDB.java文件是不能生成User表的。因为在mysql数据中还没有建立数据库Hibernate-first。所以在mysql控制台中通过create database hibernate-first; use hibernate-first;之后再执行ExoprtDB.java文件就可以生成表了。

5、向表中添加数据
[java]  view plain  copy
  1. import java.util.Date;  
  2.   
  3. import org.hibernate.Session;  
  4. import org.hibernate.SessionFactory;  
  5. import org.hibernate.cfg.Configuration;  
  6.   
  7. public class Client {  
  8.     public static void main(String[] args) {  
  9.         //读取配置文件  
  10.         Configuration cfg = new Configuration().configure();  
  11.           
  12.         SessionFactory factory = cfg.buildSessionFactory();  
  13.           
  14.         Session session = null;  
  15.         try{  
  16.             session = factory.openSession();  
  17.             //开启事务  
  18.             session.beginTransaction();  
  19.               
  20.             User user = new User();  
  21.             user.setUsername("用户名");  
  22.             user.setPassword("123");  
  23.             user.setCreateTime(new Date());  
  24.             user.setExpireTime(new Date());  
  25.               
  26.             session.save(user);  
  27.             //提交事务  
  28.             session.getTransaction().commit();  
  29.               
  30.         }catch(Exception e){  
  31.             e.printStackTrace();  
  32.             //回滚事务  
  33.             session.getTransaction().rollback();  
  34.         }finally{  
  35.             if(session != null){  
  36.                 if(session.isOpen()){  
  37.                     //关闭session  
  38.                     session.close();  
  39.                 }  
  40.             }  
  41.         }  
  42.     }  
  43. }  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值