Hibernate配置

来源:互联网 发布:火车票照片制作软件 编辑:程序博客网 时间:2024/06/04 19:12

Hibernate配置

         首先,Hhibernate是一种让关系模型变成对象模型的,我们在来看看是如何配置的,首先我们在http://www.hibernate.org网站上行下载下来hibrnate使用的文件包!

         我们在下载的目录中的/hibernate3.jar和/lib文件下的jar包全部引入道classpath中

就是这个图片中的hibernate文件和lib下的所有jar文件。引入过来。

         再次,我们就应该配置hibernate.cfg.xml文件或者hibernate.properties,在现在我们最流行的就是xml的了。

         Xml中的内容是:数据库的URL、用户名、密码、JDBC驱动类、方言等。启动时Hibernate会在CLASSPATH里找这个配置文件。映射文件(hbm.xml,对象模型和关系模型的映射)。在/eg目录下有完整的hibernate示例。

         我们来下一个hibernate.cfg.xml文件我们看看

         首先我们来描述一下这个,对了这个文件放的位置应该在src的子文件夹下

<!DOCTYPE hibernate-configuration PUBLIC

              "-//Hibernate/HibernateConfiguration DTD 3.0//EN"

              "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">

 

<hibernate-configuration>

              <session-factoryname="foo">       <!-- 数据库名字 -->

                            <propertyname="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>   <!-- 连接数据库的驱动 -->

                            <propertyname="connection.url">jdbc:mysql:///my</property>   <!-- 连接数据库的地址 -->

                            <propertyname="connection.username">root</property>                  =<!-- 连接数据库的名字 -->

                            <property name="connection.password">qiaoyu.</property>            <!-- 连接数据库的密码 -->

                            <propertyname="dialect">org.hibernate.dialect.MySQLDialect</property>   <!-- 方言 -->

                            <propertyname="hbm2ddl.auto">create</property>                <!-- 数据库是否自己创建 -->

                            <propertyname="show_sql">true</property>                                 <!-- 显示数据库的执行语句 -->

                            <mappingresource="domain/User.hbm.xml"/>                                <!-- 实体所对应的实体的xml文件 -->

              </session-factory>

</hibernate-configuration>

              上面这个就是关于这个xml文件的一些说法

         在下面我们,我们就要在写一个文件就是      实体名.hbm.xml文件,这个文件是放在和实体一个文件夹一面的。

我们来看一下、

<?xml version="1.0"?>

<!DOCTYPE hibernate-mapping PUBLIC

              "-//Hibernate/HibernateMapping DTD 3.0//EN"

              "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">

<!--  每个实体都有一个所对应的这个xml文件 -->

<hibernate-mappingpackage="domain"><!-- 包明子就是实体类在那个包里面 -->

 

              <classname="User" table="user">       <!--包里面的那个实体  -->

                            <idname="id" >                   <!--实体对应的主键 如果主键和对应的名字就加一个column -->

                                          <generatorclass="native"/>

                            </id>

                            <propertyname="name" not-null="true"/>            <!-- 实体中所对应的所有的字段 -->

                            <propertyname="birthday"/>                                           <!--试题中所对应的所有的字段-->

              </class>

</hibernate-mapping>

这就是内容了。

   下面我们创建一个实体文件名字是User.java

package domain;

 

import java.util.Date;

 

public class User {

              privateint id;

              privateString name;

              privateDate birthday;

              publicint getId() {

                            returnid;

              }

              publicvoid setId(int id) {

                            this.id= id;

              }

              publicString getName() {

                            returnname;

              }

              publicvoid setName(String name) {

                            this.name= name;

              }

              publicDate getBirthday() {

                            returnbirthday;

              }

              publicvoid setBirthday(Date birthday) {

                            this.birthday= birthday;

              }

}

从中我们知道了,是一个只有id name 和birthday的文件

然后,我们在创建一个测试类名字叫TestHibernate.java

package Testing;

 

import java.util.Date;

 

import org.hibernate.Session;

import org.hibernate.SessionFactory;

import org.hibernate.Transaction;

import org.hibernate.cfg.Configuration;

 

import domain.User;

 

public class TestHibernate {

 

              publicstatic void main(String[] args) {

                            Configurationcfg = new Configuration();

                            cfg.configure();                                                                                                       //用来完成hibernate的初始化---读取配置文件

                                                                                                                                                                                                    //得到sessionFactory对象相当于----DriverManager对象

                            SessionFactorysf = cfg.buildSessionFactory();

                                                                                                                                                                       

                            Sessionsession=sf.openSession();                                             //通过sessionFactory得到session对象

                            Transactiontx=session.beginTransaction();

                            Useruser=new User();

                            user.setName("tom");

                            user.setBirthday(newDate());

                            session.save(user);//就能保存到数据库中

                            tx.commit();///////////////////提交

                            session.close();

                            System.out.println("end");

              }

 

}

里面有详细的解释,然后我们就懂了!这就是今天讲的内容

  

 

 

 

 


原创粉丝点击