Hibernate入门级demo 配置连接 Sql Server 2005 (Hibernate in Action)
1 在SQL Server中数据库hbm中设计一个MESSAGES表,其中MESSAGE_ID是主键,自动编号.
2 编写一个数据库的Bean类
package com.hibernateinaction.hello;
public class Message {
private long id;
private String text;
public Message() {
}
public void setId(long id) {
this.id = id;
}
public long getId() {
return id;
}
public void setText(String text) {
this.text = text;
}
public String getText() {
return text;
}
}
3 编写映射文件Message.hbm.xml,其中<generator class="native" />表示是自动编号,这个文件和类文件放在同一目录
<?xml version="1.0" encoding='UTF-8'?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="com.hibernateinaction.hello.Message" table="MESSAGES">
<id name="id" column="MESSAGE_ID">
<generator class="native" />
</id>
<property name="text" column="MESSAGE_TEXT" />
</class>
</hibernate-mapping>
4 编写hibernate配置文件hibernate.cfg.xml, 这个文件放在源码的根目录,注意文件名一定要是这个,因为我们后面要使用函数Configuration configure() ,配置好数据库命,用户名,密码等,还要注意数据库的tcp/ip协议时允许的,SQL Server服务已经启动.最后面有一个映射的引用,引用到我们刚刚写的mapping文件.注意路径要正确.
文档中写道:
Use the mappings and properties specified in an application resource named 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">
<!-- Generated by MyEclipse Hibernate Tools. -->
<hibernate-configuration>
<session-factory>
<property name="dialect">org.hibernate.dialect.SQLServerDialect</property>
<property name="connection.url">jdbc:sqlserver://127.0.0.1:1433;database=hbm</property>
<property name="connection.username">sa</property>
<property name="connection.password">sa</property>
<property name="connection.driver_class">com.microsoft.sqlserver.jdbc.SQLServerDriver</property>
<mapping resource="com/hibernateinaction/hello/Message.hbm.xml" />
</session-factory>
</hibernate-configuration>
5. log4j的配置文件,由于hibernate是依赖于log4j的,所以需要有这个配置文件,否则会有warning.
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">
<appender name="console" class="org.apache.log4j.ConsoleAppender">
<param name="Target" value="System.out" />
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%-5p %c{1} - %m%n" />
</layout>
</appender>
<root>
<priority value="debug" />
<appender-ref ref="console" />
</root>
</log4j:configuration>
添加依赖的JAR包
包括Hibernate的所有jar包.另外还需要而外的两个jar包
slf4j-log4j12-1.5.6.jar
log4j-1.2.14.jar
还有就是sql server的驱动jar包,这个在微软的网站上有:
sqljdbc.jar sqljdbc4.jar
所依赖的JAR包如下图所示:
未必全部用到,但是这样肯定是没有问题的
使用的编译环境是JRE1.6 Hibernate版本是3.5.6 数据库是SQL Server 2005 Developer Edition
最后编写一个测试用的主函数
package com.hibernateinaction.hello;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.hibernate.classic.Session;
public class App {
public static void main(String[] args) {
Configuration cfg = new Configuration();
SessionFactory sf = cfg.configure().buildSessionFactory();
Session s = sf.openSession();
Transaction tx = s.beginTransaction();
Message msg = new Message();
msg.setId(1);
msg.setText("new message");
s.save(msg);
tx.commit();
s.close();
System.out.println("end");
}
}
成功之后会在数据库的MESSAGES表中生成一条记录,OK,hibernate Demo配置完成.