利用 Hibernate 创建表

在hibernate.cfg.xml文件中配置一段这样的话: 

<?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">com.mysql.jdbc.Driver</property>  
        <!-- 数据库链接 -->  
        <property name="connection.url">jdbc:mysql://localhost/hibernate</property>  
        <!-- 数据库用户 -->       
        <property name="connection.username">root</property>  
        <!-- 数据库密码 -->  
        <property name="connection.password">root</property>           
        <!-- 改成MySqlDialect(mysql的sql语句) -->  
        <property name="dialect">org.hibernate.dialect.MySQLDialect</property>  
        <!-- 打印出所有的sql -->  
        <property name="show_sql">true</property>  
       
        <!-- 创建表 -->
        <property name="hbm2ddl.auto">create</property>  
        <mapping resource="cn/domain/User.hbm.xml"/>  
    </session-factory>    
</hibernate-configuration> 
<property name="hbm2ddl.auto">create</property>    用于创建表

根据实体类,向表中添加列和属性值:

映射文件:

<?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>  
  <class name="cn.domain.User" table="user" catalog="hibernate">  
    <id name="Id" column="ID">  
        <generator class="increment"/>  
    </id>  
    <property name="Username" column="Username" type="string"/>  
    <property name="Password" column="Password" type="string"/>
    <property name="birthday" column="birthday" type="date"/>  
  </class>  
</hibernate-mapping>  
测试类:
package cn.test;   
import java.util.Date;
import org.hibernate.Session;  
import org.hibernate.SessionFactory;  
import org.hibernate.Transaction;  
import org.hibernate.cfg.Configuration;  
import org.junit.experimental.theories.suppliers.TestedOn;
import cn.domain.User;   
public class Test {  
    public static void main(String[] args) {  
         Configuration conf = new Configuration().configure();//1、读取配置文件  
         SessionFactory sf = conf.buildSessionFactory();// 2、创建SessionFactory  
         Session session = sf.openSession();// 3、打开Session  
         Transaction tx = null;  
         try{  
        tx = session.beginTransaction();// 4、开始一个事务  
        // 5、持久化操作  
        User user = new User();  
        user.setUsername("lisi");
        user.setPassword("123456");
        user.setBirthday(new Date());
        session.save(user);  
        tx.commit();// 6、 提交事务
        System.out.println("End");
          }catch(Exception e){  
        if (null!=tx){tx.rollback();}  
        e.printStackTrace();        
          }finally{  
        session.close();// 7、关闭Session  
          }  
    }  
}  


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值