【Hibernate学习杂记】最简单的Hibernate配置实用

下面写的东西是博主学习的时候的笔记,如果有错漏或者看不懂,请留言!能互相进步是件不错的事情,谢谢!

************************************************************

1、导入我们的jar包

window->perferences->java->build path->user libraries

在这里new新建一个library

如下图


2、新建java project

2.1、导入library

导入相应的数据库驱动程序

2.2、写配置文件

在src目录下面新建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>

        <!-- Database connection settings -->
        <property name="connection.driver_class">com.mysql.jdbc.Driver</property>	<!-- 这里是数据库的驱动程序 -->
        <property name="connection.url">jdbc:mysql://localhost/hibernate</property>	<!-- 这里是数据库的链接   hibernate填响应的数据库名
        <property name="connection.username">root</property>	<!--这里是数据库的用户名 -->
        <property name="connection.password">password</property>	<!--这里是数据库的密码 -->


        <!-- JDBC connection pool (use the built-in) -->
        <!-- <property name="connection.pool_size">1</property> -->

        <!-- SQL dialect -->
        <property name="dialect">org.hibernate.dialect.MySQLDialect</property>	<!-- 数据库方言 在文档可以找到响应的参数 -->

        <!-- Enable Hibernate's automatic session context management -->
        <!-- <property name="current_session_context_class">thread</property> -->

        <!-- Disable the second-level cache  -->
        <property name="cache.provider_class">org.hibernate.cache.internal.NoCacheProvider</property>

        <!-- Echo all executed SQL to stdout -->
        <property name="show_sql">true</property>	<!-- 勾上了 -->

        <!-- Drop and re-create the database schema on startup -->
        <!-- <property name="hbm2ddl.auto">update</property> -->

        <mapping class="bat.test.model.Student" /> <!-- 使用annotation 这里直接写上类名即可-->  
        

    </session-factory>

</hibernate-configuration>
这个文件一定要记住在文档里面拷贝

3、新建类

这个项目有两个类

一个是model类

model代码

package bat.test.model;

import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;


@Entity
//@Table(name="Student", catalog="hibernate")
public class Student {
	private int id;
	private String name;
	private int age;
	
	@Id
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public int getAge() {
		return age;
	}
	public void setAge(int age) {
		this.age = age;
	}

}

另一个用于测试的

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.hibernate.metamodel.source.internal.MetadataImpl;
import org.hibernate.service.ServiceRegistry;
import org.hibernate.service.ServiceRegistryBuilder;

import bat.test.model.Student;


public class StudentTest {

	public static void main(String[] args) {
		Student s = new Student();
		//s.setId(1);
		s.setName("s1");
		s.setAge(1);
		
		Configuration cfg = new Configuration();  
        cfg.configure();//读取配置文件
        
        ServiceRegistry serviceRegistry =new ServiceRegistryBuilder().  
        applySettings(cfg.getProperties()).buildServiceRegistry();  
          
        SessionFactory factory = cfg.buildSessionFactory(serviceRegistry); 
          
        Session session = factory.openSession();  
        session.beginTransaction();  
        session.save(s);  
        session.getTransaction().commit();  
        session.close();  
        factory.close(); 
		
		  
	}
	
}

建相应的数据库和数据表

CREATE TABLE `Student` (
`id`  int(11)  ,
`name`  varchar(20) ,
`age`  int(11) 
)


现在可以运行测试了


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值