1、首先是跟表对应的对象
package test;
public class Customer {
private Integer id;
private String customerName;
private String address;
private String zipCode;
private String linkMan;
private String phone;
private String mobilePhone;
private String email;
private String remark;
private int active;
public Customer() {
super();
}
@Override
public String toString() {
return "ID: "+getId()+" CustomerName: "+getCustomerName();
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getCustomerName() {
return customerName;
}
public void setCustomerName(String customerName) {
this.customerName = customerName;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public String getZipCode() {
return zipCode;
}
public void setZipCode(String zipCode) {
this.zipCode = zipCode;
}
public String getLinkMan() {
return linkMan;
}
public void setLinkMan(String linkMan) {
this.linkMan = linkMan;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
public String getMobilePhone() {
return mobilePhone;
}
public void setMobilePhone(String mobilePhone) {
this.mobilePhone = mobilePhone;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getRemark() {
return remark;
}
public void setRemark(String remark) {
this.remark = remark;
}
public int getActive() {
return active;
}
public void setActive(int active) {
this.active = active;
}
}
2、接着配置对应的Customer.cfg.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 package="test">
<class name="Customer" table="Customer">
<id name="id">
<generator class="native"></generator>
</id>
<property name="customerName" />
<property name="address" />
<property name="zipCode" />
<property name="linkMan" />
<property name="phone" />
<property name="mobilePhone" />
<property name="email" />
<property name="remark" />
<property name="active" />
</class>
</hibernate-mapping>
3、配置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>
<!--sql方言 -->
<property name="dialect">
org.hibernate.dialect.SQLServerDialect
</property>
<property name="connection.url">
jdbc:sqlserver://localhost:1433;database=test
</property>
<property name="connection.username">sa</property>
<property name="connection.password">123456</property>
<!--sql驱动 -->
<property name="connection.driver_class">
com.microsoft.sqlserver.jdbc.SQLServerDriver
</property>
<property name="show_sql">true</property>
<property name="format_sql">false</property>
<mapping resource="test/Customer.hbm.xml" />
</session-factory>
</hibernate-configuration>
4、最后写一个类,验证是否配置正确,连接到数据库
package test;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
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();
Customer customer=(Customer) s.get(Customer.class, 1);
System.out.println(customer.toString());
}
}
5、添加的hibernate需要的包如下图:
注意一点:原本添加了sqlServer的驱动jar包两个:sqljdbc.jar和sqljdbc4.jar,
结果报错: com.microsoft.sqlserver.jdbc.SQLServerException: 不支持此服务器版本。目标服务器必须是 SQL Server 2000 或更高版本。
解决方案:去掉sqljdbc.jar就好了。
运行结果如下图: