搭建hibernate框架

来源:互联网 发布:3m 异味抑制剂 知乎 编辑:程序博客网 时间:2024/05/22 12:59

一、

  1. 下载hibernate3.6.0 解压缩后将hibernate3.jar和lib下的required、jpa子目录下所有JAR包添加到应用类加载路径中
  2. 下载SLF4J日志工具需要包slf4j-1.6.1.zip将压缩包中的slf4j-nop-1.6.1.jar添加到系统的类加载路径下
  3. 下载mysql数据库连接驱动 mysql-connector-java-5.1.28-bin.jar
  4. 如果用C3P0连接池,则将hibernate-distribution-3.6.0.Final/lib的optional子目录下的c3p0目录下的JAR包添加到系统的类加载路径下

所有需要的JAR包截图

二、

  1. 实体类
package org.crazyit.app.domain;public class News {    //消息类的标识属性    private Integer id;    private String title;    private String content;    public News() {        super();    }    public Integer getId() {        return id;    }    public void setId(Integer 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" encoding="UTF-8"?><!DOCTYPE hibernate-mapping SYSTEM "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd" ><hibernate-mapping package="org.crazyit.app.domain">    <class name="News" table="news_table">        <id name="id">            <generator class="identity"/>        </id>        <property name="title"/>        <property name="content"/>    </class></hibernate-mapping>

3.hibernate配置文件

<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE hibernate-configuration SYSTEM "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd" ><hibernate-configuration>  <session-factory>    <property name="connection.driver_class">com.mysql.jdbc.Driver</property>    <property name="connection.url">jdbc:mysql://localhost/hibernate</property>    <property name="connection.username">root</property>    <property name="connection.password">root</property>    <property name="hibernate.c3p0.max_size">20</property>    <property name="hibernate.c3p0.min_size">1</property>    <property name="hibernate.c3p0.timeout">5000</property>    <property name="hibernate.c3p0.max_statements">100</property>    <property name="hibernate.c3p0.idle_text_period">3000</property>    <property name="hibernate.c3p0.acquire_increment">2</property>    <property name="hibernate.c3p0.validate">true</property>    <!-- 指定数据库方言 -->    <property name="dialect">org.hibernate.dialect.MySQLInnoDBDialect</property>    <!-- 根据需要自动创建数据表 -->    <property name="hbm2ddl.auto">update</property>    <!-- 罗列所有的映射文件 -->    <mapping resource="org/crazyit/app/domain/News.hbm.xml"/>  </session-factory></hibernate-configuration>

4.测试类

package lee;import org.crazyit.app.domain.News;import org.hibernate.Session;import org.hibernate.SessionFactory;import org.hibernate.Transaction;import org.hibernate.cfg.Configuration;public class NewsManager {    public static void main(String[] args) {        Configuration conf = new Configuration().configure();        SessionFactory sf = conf.buildSessionFactory();        Session sess = sf.openSession();        Transaction tx = sess.beginTransaction();        News n = new News();        n.setTitle("中国");        n.setContent("成立了!");        sess.save(n);        tx.commit();        sess.close();        sf.close();    }}

5.看效果
执行结果

原创粉丝点击