第一次搭建hibernate

3个准备,7个步骤

三个准备:准备Jar包 准备User类 准备映射和数据库

七个步骤:Configuration、SessionFactory、打开Session、开始一个事务、持久化操作save/update/delete/get、提交事务 、关闭Session

1.导入jar包

   orm包和JDBC

2.创建User.java类

保存在hibernate包下

package hibernate;

import java.io.Serializable;
import java.util.Date;

public class User implements Serializable {  
    private long id;
    private String name;
    private Date birthday;
    
    public long getId() {
        return id;
    }

    public void setId(long id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Date getBirthday() {
        return birthday;
    }

    public void setBirthday(Date birthday) {
        this.birthday = birthday;
    }

    public User() {
    }
}

2.创建hibernate映射文件 hibernate.cfg.xml

此处的mapping resource 应该写全

hibernate/User.hbm.xml

要注意文件名的大小写

<?xml version="1.0" encoding="UTF-8"?>
<hibernate-configuration>
<session-factory>   
	<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
	<property name="hibernate.connection.url">jdbc:mysql://127.0.0.1:3306/test</property>
	<property name="hibernate.connection.username">root</property>
	<property name="hibernate.connection.password"></property>
	<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
	<property name="show_sql">true</property>
	  <mapping resource="hibernate/User.hbm.xml"/>
</session-factory>
</hibernate-configuration>

3.创建User类的映射文件User.hbm.xml

    这个文件和User类放在一个包下

    映射里的column的字段名与数据库的字段名不区分大小写(也就是说这里大写数据库小写并不影响数据流通)

<?xml version="1.0" encoding="UTF-8"?>

<hibernate-mapping>
  <class name="hibernate.User" table="t_user">
    <id name="id" column="ID"  type="long">
      	<generator class="increment"/>
    </id>
    <property name="name" column="NAME" />
    <property name="birthday" column="BIRTHDAY"/>
  </class>
</hibernate-mapping>

4.添加测试类

package hibernate;

import java.util.Date;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;

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.setName("Hibernate user");
 		user.setBirthday(new Date());;
 		session.save(user);
 		tx.commit();// 6、 提交事务      
         }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、付费专栏及课程。

余额充值