Eclipse中Hibernate的引入

    xiaoxiao2021-03-25  137

    Eclipse中hibernate的引入

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

    打开链接http://hibernate.org/orm/downloads/下载相应版本的zip文件,我下载的是4.3.11.Final.zip

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

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

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

    完成上面操作后,就可以在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文件

    <?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> <!-- Generated 2017-3-6 16:03:59 by Hibernate Tools 3.4.0.CR1 --> <hibernate-mapping> <!-- 映射到数据库news表--> <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

    <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"> <hibernate-configuration> <session-factory> <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property> <property name="hibernate.connection.password">sdongwan</property> <!-- 连接到sdongwan数据库--> <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/sdongwan</property> <property name="hibernate.connection.username">sdongwan</property> <property name="hibernate.dialect">org.hibernate.dialect.MySQL5InnoDBDialect </property> <property name="hibernate.hbm2ddl.auto">create</property> <!-- 注意这里,我的包名是org.sdw.app,而要写成下面的形式 --> <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表,并且插入了一条数据

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

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

    转载请注明原文地址: https://ju.6miu.com/read-2056.html

    最新回复(0)