搭建hibernate框架

一、

  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.看效果
执行结果

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值