Hibernate連接SQLServer數據庫的配置

主要文件有四類:

1. xx.java類文件;  放在src目錄下自己創建的包中

2. xx.hbm.xml文件; 放在類文件所在的包中,即與類文件在同一目錄下

3. hibernate.cfg.xml文件;  直接放在src目錄下

4. jar包;  可以build path導入,也可以直接放在lib目錄下(如果是創建的web工程的話可以)

Hibernate連接SQLServer數據庫的配置



實例:

1.xx.hbm.xml文件

   Customer.hbm.xml,用來將Customer類和數據庫中的CUSTOMER表進行映射。

代碼:

<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="com.xiaobiao.ch05.action">
 <class name="com.xiaobiao.ch05.action.Customer" table="CUSTOMER">
  <!-- 主鍵 -->
  <id name="id" column="ID">
   <generator class="native"/>
  </id>
  <!-- 用戶名 -->
  <property name="userName" column="USERNAME" type="string" not-null="true"></property>
  <!-- 密碼 -->
  <property name="password" column="PASSWORD" type="string" not-null="true"></property>
  <!-- 真實姓名 -->
  <property name="realName" column="REALNAME" type="string"/>
  <!-- 地址 -->
  <property name="address" column="ADDRESS" type="string"/>
  <!-- 手機 -->
  <property name="mobile" column="MOBILE" type="string"/>
 </class>
</hibernate-mapping>

 

2.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">
<hibernate-configuration>
<session-factory>

<!--配置SQLServer連接屬性-->
 <property name="dialect">org.hibernate.dialect.SQLServerDialect</property>
 <property name="connection.driver_class">com.microsoft.sqlserver.jdbc.SQLServerDriver</property>
 <property name="connection.url">jdbc:sqlserver://localhost:1433;databaseName=Test</property>
 <property name="connection.username">sa</property>
 <property name="connection.password">123456</property>

<!--在控制檯顯示SQL語句-->
 <property name="show_sqlserver">true</property>

<!--根據需要自動生成、更新數據表-->
 <property name="hbm2ddl.auto">update</property>
 <property name="myeclipse.connection.profile">sqlserver</property>

<!--註冊所有ORM映射文件-->
 <mapping resource="com/xiaobiao/ch05/action/Customer.hbm.xml" />
</session-factory>
</hibernate-configuration>

 

3.jar包

Hibernate連接SQLServer數據庫的配置

4..java類文件

   1.Customer.javam 創建客戶類Customer

代碼:

public class Customer {
 private Integer id;
 private String userName;
 private String password;
 private String realName;
 private String address;
 private String mobile;
 public Customer(String userName,String password,String realName,String address,String mobile){
  this.userName=userName;
  this.password=password;
  this.realName=realName;
  this.address=address;
  this.mobile=mobile;
 }
 public Customer(){
  
 }
 public Integer getId() {
  return id;
 }
 public void setId(Integer id) {
  this.id = id;
 }
 public String getUserName() {
  return userName;
 }
 public void setUserName(String userName) {
  this.userName = userName;
 }
 public String getPassword() {
  return password;
 }
 public void setPassword(String password) {
  this.password = password;
 }
 public String getRealName() {
  return realName;
 }
 public void setRealName(String realName) {
  this.realName = realName;
 }
 public String getAddress() {
  return address;
 }
 public void setAddress(String address) {
  this.address = address;
 }
 public String getMobile() {
  return mobile;
 }
 public void setMobile(String mobile) {
  this.mobile = mobile;
 }
}

 

2.CustomerTest.java 測試,先實力換一個Customer對象,在使用hibernate將此對象保存到數據庫中。

代碼:

public class CustomerTest {
 public static void main(String[] args){
  Customer cus=new Customer("zhangsan","1234","張三","懷化","15");
  Configuration configuration=new Configuration();
  configuration.configure("/hibernate.cfg.xml");
  
  
  SessionFactory sessionFactory=configuration.buildSessionFactory();
  Session session=sessionFactory.openSession();
  Transaction trans=session.beginTransaction();
  session.save(cus);
  trans.commit();
  session.close();
 }
}

發佈了31 篇原創文章 · 獲贊 2 · 訪問量 10萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章