JavaWeb项目配置Hibernate

来源:互联网 发布:js是什么文件格式 编辑:程序博客网 时间:2024/06/05 20:22

1.下载Hibernate

2.将lib目录下的所有jar包拷贝到WEB-INF的lib目录下

3.新建一个测试实体

/** *  */package com.example.entity.Test;import java.util.Date;import javax.persistence.Column;import javax.persistence.Entity;import javax.persistence.GeneratedValue;import javax.persistence.GenerationType;import javax.persistence.Id;import javax.persistence.Table;/** * @author pantao * */@Entity@Table(name = "test")public class Test {    /**     *      */    @Id    @GeneratedValue(strategy = GenerationType.IDENTITY)    @Column(name = "id")    private int id;    @Column(name = "create_time")    private Date ceateTime = new Date();    public Test() {    }    public int getId() {        return id;    }    public void setId(int id) {        this.id = id;    }    public Date getCeateTime() {        return ceateTime;    }    public void setCeateTime(Date ceateTime) {        this.ceateTime = ceateTime;    }}

4.在src文件夹下新建hibernate.cfg.xml,并添加如下代码

<?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="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>        <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>        <property name="hibernate.connection.url">jdbc:mysql://127.0.0.1/databasename</property>        <property name="hibernate.connection.username">username</property>        <property name="hibernate.connection.password">password</property>        <property name="current_session_context_class">thread</property>        <property name="hibernate.show_sql">true</property>        <property name="hibernate.hbm2ddl.auto">update</property>        <property name="c3p0.max_size">100</property>        <property name="c3p0.min_size">10</property>        <property name="c3p0.timeout">5000</property>        <property name="c3p0.max_statements">100</property>        <property name="c3p0.idle_test_period">3000</property>        <property name="c3p0.acquire_increment">10</property>        <property name="c3p0.validate">false</property>        <!-- 映射实体 -->        <mapping class="com.example.entity.Test" />    </session-factory></hibernate-configuration>

注意修改连数据库的url(ip地址和域名均可)和数据库名,以及用户名和密码