eclipse配置hibernate

hibernate相对于spring配置还是略显繁琐的:
对于Eclipse要求:
EclipseEE
数据库,我选用的是
mysql

新件项目:需要建成:dynamic web project

在WEB-INF/lib下需要的jar包:
[img]http://dl.iteye.com/upload/attachment/542231/ac84f937-c4bb-313a-ad03-83663c361185.png[/img]

整个项目的目录结构:

[img]http://dl.iteye.com/upload/attachment/542233/5b086e1d-ba3b-3e5e-9374-4c876d6fead2.png[/img]

源码:
HelloWorld.java

package com.yy.hello;

import org.hibernate.Session;
import org.hibernate.Transaction;

import persistence.*;

public class HelloWorld {
public static void main(String[] args) {
Session session = HibernateUtil.getSessionFactory().openSession();
Transaction tx = session.beginTransaction();

Message message = new Message("Hello World");
Long msgId = (Long) session.save(message);

tx.commit();
session.close();

HibernateUtil.shutdown();
}
}


Message.java

package com.yy.hello;

public class Message {
private Long id;
private String text;
private Message nextMessage;

public Message(){}

public Message(String text) {
this.text = text;
}

public Long getId() {
return id;
}

public void setId(Long id) {
this.id = id;
}

public String getText() {
return text;
}

public void setText(String text) {
this.text = text;
}

public Message getNextMessage() {
return nextMessage;
}

public void setNextMessage(Message nextMessage) {
this.nextMessage = nextMessage;
}

}


Message.hbm.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="com.yy.hello.Message" table="MESSAGES" >
<id
name="id"
column="MESSAGE_ID">
<generator class="increment"></generator>
</id>

<property name="text" column="MESSAGE_TEXT"></property>

<many-to-one
name="nextMessage"
cascade="all"
column="NEXT_MESSAGE_ID"
foreign-key="FK_NEXT_MESSAGE" />

</class>
</hibernate-mapping>


HibernateUtil.java

package persistence;

import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;


public class HibernateUtil {

private static SessionFactory sessionFactory;

static{
try{
sessionFactory = new Configuration()
.configure()
.buildSessionFactory();
} catch (Throwable ex) {
throw new ExceptionInInitializerError(ex);
}
}

public static SessionFactory getSessionFactory(){
return sessionFactory;
}

public static void shutdown(){
getSessionFactory().close();
}
}



hibernate.cfg.xml

<!DOCTYPE hibernate-configuration SYSTEM
"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.url">jdbc:mysql://localhost:3306/hibernate_text</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">root</property>
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<!-- 让hibernate自动生成表 -->
<property name="hibernate.hbm2ddl.auto">create</property>

<!-- use the C3P0 connection pool provider -->
<property name="hibernate.c3p0.min_size">5</property>
<property name="hibernate.c3p0.max_size">20</property>
<property name="hibernate.c3p0.timeout">300</property>
<property name="hibernate.c3p0.max_statements">50</property>
<property name="hibernate.c3p0.idle_test_period">3000</property>

<!-- show and print nice SQL on stdout -->
<property name="show_sql">true</property>
<property name="format_sql">true</property>

<!-- List of XML mapping files -->
<mapping resource="com/yy/hello/Message.hbm.xml" />
</session-factory>
</hibernate-configuration>


log4j.properties

log4j.rootLogger=DEBUG,stdout,R

log4j.logger.org=ERROR, A1

log4j.logger.com.gc.action=DEBUG,A2

log4j.appender.A1=org.apache.log4j.RollingFileAppender
log4j.appender.A1.File=org.log
log4j.appender.A1.MaxFileSize=500KB
log4j.appender.A1.MaxBackupIndex=50
log4j.appender.A1.Append=true
log4j.appender.A1.layout=org.apache.log4j.PatternLayout
log4j.appender.A1.layout.ConversionPattern=%d{ISO8601} - [%p] [%C{1}] - %m%n


log4j.appender.A2=org.apache.log4j.RollingFileAppender
log4j.appender.A2.File=gc.log
log4j.appender.A2.MaxFileSize=500KB
log4j.appender.A2.MaxBackupIndex=50
log4j.appender.A2.Append=true
log4j.appender.A2.layout=org.apache.log4j.PatternLayout
log4j.appender.A2.layout.ConversionPattern=%d{ISO8601} - [%p] [%C{1}] - %m%n

#--------------------stdout--------------------------------
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout

# Pattern to output the caller's file name and line number.
log4j.appender.stdout.layout.ConversionPattern=[%-5p] %d{yyyy-MM-dd HH:mm:ss} %c - %m%n

#--------------------R--------------------------------
#log4j.appender.R=org.apache.log4j.RollingFileAppender
log4j.appender.R=org.apache.log4j.DailyRollingFileAppender
#this log file will be stored in web server's /bin directory,modify to your path which want to store.
log4j.appender.R.File=gf.log
#log4j.appender.R.datePattern='.'yyyy-MM-dd-HH-mm
log4j.appender.R.datePattern='.'yyyy-MM-dd
log4j.appender.R.append=true
## Keep one backup file
log4j.appender.R.layout=org.apache.log4j.PatternLayout
log4j.appender.R.layout.ConversionPattern=[%-5p] %d{yyyy-MM-dd HH:mm:ss} %c - %m%n
#[%-5p] %d{yyyy-MM-dd HH:mm:ss,SSS} method:%l%n%m%n


注意事项:
hibernate.cfg.xml放在源代码目录中,但是处在任何包之外的。
log4j.properties是放在WEB-INF/classes文件夹下的。
编译的时候,编译HelloWorld.java文件,用Java Application运行。
你需要在mysql中先建立好数据库(但在此不需要预先建立好表)。

至此,登录mysql,便可以发现表已经自动声称,并已经存入了数据。


补充:
将上面的改为注解,在此我将需要修改,并且改后的源文件贴出,读者可自行比较其中的不同之处:
Message.java

package com.yy.hello;

import javax.persistence.*;

@Entity
@Table(name = "TAB_AN")
public class Message {

@Id
@GeneratedValue
@Column(name = "MESSAGE_ID")
private Long id;

@Column(name = "MESSAGE_TEXT")
private String text;

@ManyToOne(cascade = CascadeType.ALL)
@JoinColumn(name = "NEXT_TAB_AN")
private Message nextMessage;

public Message(){}

public Message(String text) {
this.text = text;
}

public Long getId() {
return id;
}

public void setId(Long id) {
this.id = id;
}

public String getText() {
return text;
}

public void setText(String text) {
this.text = text;
}

public Message getNextMessage() {
return nextMessage;
}

public void setNextMessage(Message nextMessage) {
this.nextMessage = nextMessage;
}

}



hibernate.cfg.xml

<!DOCTYPE hibernate-configuration SYSTEM
"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.url">jdbc:mysql://localhost:3306/HBDB</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">root</property>
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<!-- 让hibernate自动生成表 -->
<property name="hibernate.hbm2ddl.auto">create</property>

<!-- use the C3P0 connection pool provider -->
<property name="hibernate.c3p0.min_size">5</property>
<property name="hibernate.c3p0.max_size">20</property>
<property name="hibernate.c3p0.timeout">300</property>
<property name="hibernate.c3p0.max_statements">50</property>
<property name="hibernate.c3p0.idle_test_period">3000</property>

<!-- show and print nice SQL on stdout -->
<property name="show_sql">true</property>
<property name="format_sql">true</property>

<!-- List of XML mapping files -->
<mapping class="com.yy.hello.Message" />
<mapping package="com/yy/hello" />
</session-factory>
</hibernate-configuration>


还需要手动删除Message.hbm.xml文件,不需要导入新的包,编译,运行,在数据库里面会发现已经有了新的TAB_AN。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值