Posted on 2006-09-25 17:26
HotJava 阅读(3569)
评论(0) 编辑 收藏
使用Hibernate连接SQL数据库(环境:eclipse3.1, tomecat 5.0.28)
need: Hibernate-3.1.zip,msjdbc.tar
1、在eclipse中建立一个tomcat新工程,如:HibernateForMSSql。
2、解压得到hibernate-3.1目录,把目录下hibernate3.jar以及/lib中的所有*.jar拷贝到HibernateForMSSql/WEB-INF/lib中。
3、将msjdbc.tar解压得到的msjdbc/lib中的三个jar文件拷贝到HibernateForMSSql/WEB-INF/lib中;将etc/目录下的log4j.properties文件放入到WEB-INF/classes下面
4、在eclipse中,选中项目,在弹出菜单中->Build Path->Add Library->User Library,新建User Library取名为hibernate1,将
刚才加入到HibernateForMSSql/WEB-INF/lib中的包导入。
5、在WEB-INF/src中,编写ADO对象,本例为Customer.java:
public class Customer {
private int id;
private String username;
private String password;
public int getId() {
return id;
}
public String getPassword() {
return password;
}
public String getUsername() {
return username;
}
public void setId(int id) {
this.id = id;
}
public void setPassword(String password) {
this.password = password;
}
public void setUsername(String username) {
this.username = username;
}
}
6、在SQL数据库加入对应的数据表,本例新建数据库hibernate_test
CREATE TABLE CUSTOMER
(
CID INTEGER NOT NULL PRIMARY KEY,
USERNAME VARCHAR(12) NOT NULL,
PASSWORD VARCHAR(12)
);
7、在WEB-INF/src中加入数据映射定义Customer.hbm.xml (以hbm.xml作为后缀是约定的名称)
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="Customer" table="CUSTOMER">
<id name="id" column="CID">
<generator class="uuid" />
</id>
<property name="username" column="USERNAME" />
<property name="password" column="PASSWORD" />
</class>
</hibernate-mapping>
8、编写Hibernate配置文件,hibernate.cfg.xml放到WEB-INF/classes根目录下。(注:把它放到src下eclipse会把它拷贝过去,
直接放到classes下可能被IDE删除。)
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd" >
<hibernate-configuration>
<session-factory>
<!--
<property name="connection.datasource">java:comp/env/jdbc/mydb</property>
-->
<property name="generate_statistics">true</property>
<property name="dialect">org.hibernate.dialect.SQLServerDialect</property>
<property name="connection.driver_class">com.microsoft.jdbc.sqlserver.SQLServerDriver</property>
<property name="connection.pool_size">150</property>
<property name="connection.url">jdbc:microsoft:sqlserver://localhost:1433;database=hibernate_test</property>
<property name="connection.username">sa</property>
<property name="connection.password">123</property>
<property name="hibernate.default_catalog">hibernate_test</property>
<property name="hibernate.default_schema">dbo</property>
<property name="show_sql">false</property>
<mapping resource="Customer.hbm.xml"/>
</session-factory>
</hibernate-configuration>
9、加入测试代码到WEB-INF/src中,Test.java
import org.hibernate.*;
import org.hibernate.cfg.*;
public class Test {
public static void main(String[] args) {
try {
SessionFactory sf = new Configuration().configure().buildSessionFactory();
Session session = sf.openSession();
Transaction tx = session.beginTransaction();
for (int i = 0; i < 200; i++) {
Customer customer = new Customer();
customer.setUsername("customer" + i);
customer.setPassword("customer");
session.save(customer);
}
tx.commit();
session.close();
} catch (HibernateException e) {
e.printStackTrace();
}
}
}
10、去数据库里面看看,加入了200条记录哦。