Eclipse中Hibernate的引入

Eclipse中hibernate的引入

阅读此篇文章,如有eclipse操作不会,可以参考: Eclipse中struts的引入

  1. 打开链接http://hibernate.org/orm/downloads/下载相应版本的zip文件,我下载的是4.3.11.Final.zip
    Eclipse中Hibernate的引入_第1张图片

Eclipse中Hibernate的引入_第2张图片

2.下载完成后,解压zip文件,把目录\hibernate-release-4.3.11.Final\lib\required\的所有jar包复制到一个喜欢的目录下;

Eclipse中Hibernate的引入_第3张图片


3.由于hibernate底层使用到JDBC,所有还要去下载mysql-connector-java-5.1.40-bin.jar驱动,官网下载地址为https://dev.mysql.com/downloads/connector/j/ ,下载完成后,解压复制其中的jar包到一个喜欢的目录下;
Eclipse中Hibernate的引入_第4张图片

4.将刚刚保存的两个目录在Eclipse中添加为user library;

Eclipse中Hibernate的引入_第5张图片

完成上面操作后,就可以在eclipse中使用hibernate了;


0.新建一个动态网站,配置build path,引入hibernate和mysql user library,并把依赖库的所有jar包复制到工程目录WebContent/WEB-INF/lib目录下

1.新建实体类News.java

package org.sdw.app;

public class News {


    private int id;
    private String title;
    private String content;
    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    public String getTitle() {
        return title;
    }
    public void setTitle(String title) {
        this.title = title;
    }
    public String getContent() {
        return content;
    }
    public void setContent(String content) {
        this.content = content;
    }

}



2.建立实体类和数据库表的映射对应关系,创建News.hbm.xml文件




<hibernate-mapping>

    <class name="org.sdw.app.News" table="NEWS">
        <id name="id" type="int">
            <column name="ID" />
            <generator class="assigned" />
        id>
        <property name="title" type="java.lang.String">
            <column name="TITLE" />
        property>
        <property name="content" type="java.lang.String">
            <column name="CONTENT" />
        property>
    class>
hibernate-mapping>



3.创建hibernate与底层MySql连接的配置文件hibernate.cfg.xml



<hibernate-configuration>
    <session-factory>
        <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driverproperty>
        <property name="hibernate.connection.password">sdongwanproperty>
        
        <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/sdongwanproperty>
        <property name="hibernate.connection.username">sdongwanproperty>
        <property name="hibernate.dialect">org.hibernate.dialect.MySQL5InnoDBDialect property>
        <property name="hibernate.hbm2ddl.auto">createproperty>
        
        <mapping resource="org/sdw/app/News.hbm.xml"/>

    session-factory>
hibernate-configuration>



4.创建测试主类NewsManager.java


import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;

public class NewsManager {

    public static void main(String[] args) {
        Configuration config = new Configuration().configure();
        ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder().applySettings(config.getProperties())
                .build();

        SessionFactory sessionFactory = config.buildSessionFactory(serviceRegistry);
        Session session = sessionFactory.openSession();
        Transaction transaction = session.beginTransaction();
        News news = new News();
        news.setContent("sdongwan_content");
        news.setId(2);
        news.setTitle("sdongwan_title");
        session.save(news);
        transaction.commit();

        sessionFactory.close();

    }
}

如果顺利的话就可以在自己的数据库中,创建一个news表,并且插入了一条数据
Eclipse中Hibernate的引入_第6张图片



如果出现错误,检查各个文件所在的目录是否正确

Eclipse中Hibernate的引入_第7张图片


对了,上面创建hibernate映射文件和配置文件,可以使用hibernate tools工具来创建,如何安装工具和使用自行百度

你可能感兴趣的:(javaee)