主要文件有四类:
1.xx.java类文件; 放在src目录下自己创建的包中
2. xx.hbm.xml文件; 放在类文件所在的包中,即与类文件在同一目录下
3. hibernate.cfg.xml文件; 直接放在src目录下
4. jar包; 可以buildpath导入,也可以直接放在lib目录下(如果是创建的web工程的话可以)
实例:
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-mappingpackage="com.xiaobiao.ch05.action">
<classname="com.xiaobiao.ch05.action.Customer"table="CUSTOMER">
<!-- 主键-->
<id name="id"column="ID">
<generatorclass="native"/>
</id>
<!-- 用户名-->
<propertyname="userName" column="USERNAME" type="string"not-null="true"></property>
<!-- 密码-->
<propertyname="password" column="PASSWORD" type="string"not-null="true"></property>
<!-- 真实姓名-->
<propertyname="realName" column="REALNAME"type="string"/>
<!-- 地址-->
<propertyname="address" column="ADDRESS"type="string"/>
<!-- 手机-->
<propertyname="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连接属性-->
<propertyname="dialect">org.hibernate.dialect.SQLServerDialect</property>
<propertyname="connection.driver_class">com.microsoft.sqlserver.jdbc.SQLServerDriver</property>
<propertyname="connection.url">jdbc:sqlserver://localhost:1433;databaseName=Test</property>
<propertyname="connection.username">sa</property>
<propertyname="connection.password">123456</property>
<!--在控制台显示SQL语句-->
<property name="show_sqlserver">true</property>
<!--根据需要自动生成、更新数据表-->
<propertyname="hbm2ddl.auto">update</property>
<propertyname="myeclipse.connection.profile">sqlserver</property>
<!--注册所有ORM映射文件-->
<mappingresource="com/xiaobiao/ch05/action/Customer.hbm.xml"/>
</session-factory>
</hibernate-configuration>
3.jar包
![Hibernate连接SQLServer数据库的配置 Hibernate连接SQLServer数据库的配置](https://i-blog.csdnimg.cn/blog_migrate/31c4571ff24be6b9973831a1f52c6f7c.jpeg)
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,Stringpassword,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=newCustomer("zhangsan","1234","张三","怀化","15");
Configuration configuration=newConfiguration();
configuration.configure("/hibernate.cfg.xml");
SessionFactorysessionFactory=configuration.buildSessionFactory();
Sessionsession=sessionFactory.openSession();
Transactiontrans=session.beginTransaction();
session.save(cus);
trans.commit();
session.close();
}
}