使用Hibernate連接SQL數據庫(環境:eclipse3.1, tomecat 5.0.28)
need: Hibernate-3.1.zip,msjdbc.tar
1、在eclipse中建立一個tomcat新工程,如:HibernateForMSSql。 <hibernate-mapping> 8、編寫Hibernate配置文件,hibernate.cfg.xml放到WEB-INF/classes根目錄下。(注:把它放到src下eclipse會把它拷貝過去, ? <session-factory>
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"
??? "
??? <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>
直接放到classes下可能被IDE刪除。)
<!DOCTYPE hibernate-configuration PUBLIC
??? "-//Hibernate/Hibernate Configuration DTD//EN"
??? "???
<hibernate-configuration>
??? <!--
??? <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條記錄哦。