hibernate的链接数据库的基本步骤

① 在package Hibernate新建一个sdUser的class

代码如下:

 package Hibernate;

public class sdUser {

    private String id;

    private String name;

    private char sex;

    private int age;

    public int getAge() {

        return age;

    }

    public String getId() {

        return id;

    }

    public String getName() {

        return name;

    }

    public char getSex() {

        return sex;

    }

    public void setAge(int i) {

        age = i;

    }

    public void setId(String string) {

        id = string;

    }

    public void setName(String string) {

        name = string;

    }

    public void setSex(char c) {

        sex = c;

    }

}

②     在package Hibernate下new一个sdUser.hbm.xml的xml文件。(这个xml文件的作用为告诉Hibernate如何映射至DB中相应的table的)

选中jbuilder中package Hibernate(右击)->new->file弹出create new file对话框,我们选中type : xml,name: sdUser.hbm

我们也可以在c:\Hibernate\src\Hibernate直接建一个sdUser.hbm.xml的文件。

代码如下:

<?xml version="1.0"?>

<!DOCTYPE hibernate-mapping

    PUBLIC "-//Hibernate/Hibernate Mapping DTD//EN"

    "http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">

<hibernate-mapping>

    <class name="Hibernate.sdUser" table="sdUSER">

        <id name="id" type="string" unsaved-value="null">

            <column name="user_id" sql-type="char(32)" />

            <generator class="uuid.hex"/>

        </id>

        <property name="name" type="string" not-null="true">

            <column name="name" length="16" not-null="true"/>

        </property>

        <property name="sex" type="char"/>

        <property name="age" type="int"/>

    </class>

</hibernate-mapping>

③     在package Hibernate新建一个Example的class

代码如下

package Hibernate;

import java.io.IOException;

import java.io.InputStream;

import java.util.Properties;

import org.hibernate.*;

import org.hibernate.cfg.Configuration;

public class Example{

        private static SessionFactory _sessions = null;

        private static Properties pops = new Properties();

        static{

                try {

                  //读取上面设的hibernate.properties

                        InputStream stream = Example.class.getResourceAsStream("hibernate.properties");

                        try {

                                pops.load(stream);

                        } catch (IOException e1) {

                                e1.printStackTrace();

                        }

                        Configuration cfg = new Configuration();

                        cfg.addClass(sdUser.class);

                        cfg.setProperties(pops);

                        _sessions = cfg.buildSessionFactory();

            } catch (MappingException e) {

                   e.printStackTrace();

            } catch (HibernateException e) {

                   e.printStackTrace();

            }

        }

        public static void main(String[] args) throws HibernateException {

                sdUser sduser = new sdUser();

                sduser.setName("caterpillar");

                sduser.setSex('M');

                sduser.setAge(28);

                Session session = _sessions.openSession();

                Transaction tx = null;

                try{

                        tx = session.beginTransaction();

                        session.save(sduser);

                        tx.commit();

                }catch(HibernateException he){

                        if(tx != null) tx.rollback();

                        throw he;

                }

                finally{

                        session.close();

                }

        }

}

4 接着导入hibernate所需要的包, 然后是hibernate.cfg.xml文件的配置,详细请看另一篇博客

原文地址:https://www.cnblogs.com/a892647300/p/2971246.html