Hibernate 框架的详细搭建与简单使用(一)

今天我就写一下怎么搭建Hibernate的框架的~

步骤如下:

1. 获取hibernate发布包

可以在github或者官网找找~

2.将hibernate依赖的jar集成到项目中

一共要9个jar包~

到这个目录hibernate\hibernate-distribution-3.6.10.Final\lib\required找,有6个jar包,如图:


有这些包还不够,还需要到hibernate\hibernate-distribution-3.6.10.Final\lib\jpa这个目录下,看图:


还需要俩个jar包,一个是连接数据库的包,一个是Hibernate包,如图:


到这里jar包全部找到,可以导入到项目里面了~

在项目中新建一个jar文件,把要依赖的jar包放进去,如图所示:


jar包包括驱动包和hibernate3.jar和一些别的包,都是一些必须要的包~

3.将hibernate依赖的配置文件和映射文件加入到项目中

hibernate.cfg.xml 配置文件

***.hbm.xml     映射文件

这时候就要把dtd文件放入到项目中了~

把hibernate3.jar包解压开来,进入到hibernate\hibernate-distribution-3.6.10.Final\hibernate3\org\hibernate,可以找到俩个dtd文件,如图:


在项目中新建一个dtd文件,把必要的dtd放入到dtd文件中,如图:


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://127.0.0.1:3306/ssh</property>
		<property name="connection.username">root</property>
		<property name="connection.password">root</property>

		<!-- SQL dialect -->
		<property name="dialect">org.hibernate.dialect.MySQLDialect</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>

	</session-factory>

</hibernate-configuration>
<!DOCTYPE hibernate-configuration PUBLIC
	"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
	"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
这几句可以从dtd文件中hibernate-configuration-3.0.dtd找~,这几句是必须要的

<property name="connection.url">jdbc:mysql://127.0.0.1:3306/ssh</property>中的ssh是在Native MySql中创建的一个数据库的名字~

<hibernate-configuration>这行代码出不现的话,就要配置了,在Window的Preferences做如图操作:



上面的location就是这个项目中dtd文件的所在目录,而key就是http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd这个链接~

这样配置文件就配置好了
***.hbm.xml映射文件的写法:

<?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 package="com.xu.day4.basic">

</hibernate-mapping>

<hibernate-mapping package="com.xu.day4.basic">是指要映射到哪个包下的哪个类~

同样

<!DOCTYPE hibernate-mapping PUBLIC 
    "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
    "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
这几行代码也是必须的~可以从hibernate-mapping-3.0.dtd文件中找~
<hibernate-mapping>如果这行代码也出不现的话,和配置文件的配置步骤是一样的~

4.将映射文件集成到配置文件中

看下图:



只需要这样集成就行~

那框架就搭配完成了~

现在就可以写写一个简单的例子:

要做一个用户的存储~

User类:

public class User implements Serializable{

	private static final long serialVersionUID = 1L;
	Long id;
	String name;
	Double account;
	Date birthday;
	
	public User() {
		super();
		// TODO Auto-generated constructor stub
	}
	public User(Long id, String name, Double account, Date birthday) {
		super();
		this.id = id;
		this.name = name;
		this.account = account;
		this.birthday = 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 Double getAccount() {
		return account;
	}
	public void setAccount(Double account) {
		this.account = account;
	}
	public Date getBirthday() {
		return birthday;
	}
	public void setBirthday(Date birthday) {
		this.birthday = birthday;
	}
	@Override
	public String toString() {
		return "User [id=" + id + ", name=" + name + ", account=" + account + ", birthday=" + birthday + "]";
	}
}

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://127.0.0.1:3306/ssh</property>
		<property name="connection.username">root</property>
		<property name="connection.password">root</property>

		<!-- SQL dialect -->
		<property name="dialect">org.hibernate.dialect.MySQLDialect</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 resource="com/xu/day4/basic/user.hbm.xml" />

	</session-factory>

</hibernate-configuration>
映射文件user.hbm.xml:

<?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 package="com.xu.day4.basic">

	<class name="User" table="tb_user">
		<id name="id" column="id">
			<generator class="increment" />
		</id>
		<property name="name" />
		<property name="account" />
		<property name="birthday" />
	</class>

</hibernate-mapping>

在Test类中代码运行入口:

public class Test {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		//1.创建配置对象
		Configuration configuration=new Configuration();
		//2.读取配置对象
		configuration.configure();
		//3.根据配置文件信息创建一级缓存,SessionFactory
		/*
		 * Connection
		 * 映射文件,映射对象
		 * HttpSession 用户与服务器之间的会话
		 * Session  代码与数据库之间的会话
		 */
		SessionFactory ss=configuration.buildSessionFactory();
		//4.创建Session,打开会话
		Session session=ss.openSession();
		//5.开启事务
		Transaction tran=session.beginTransaction();
		session.save(new User(null, "张二", 100.0, new Date()));
		//6.事务的提交
		tran.commit();
		//7.session关闭
		session.close();
		System.out.println("1111111111111111111");
	}
}
注释已经写的很清楚,我就不一一解释了~

下一篇我也讲个例子,可以把部分代码封装起来~

效果图我就不贴了,有什么需要的可以和我交流啊~吐舌头

源码:下载
































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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值