Hibernate连接SQLServer数据库的配置
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
Hibernate连接SQLServer数据库的配置
主要文件有四类:
1. xx.java类文件; 放在src目录下自己创建的包中
2. xx.hbm.xml文件;放在类文件所在的包中,即与类文件在同一目录下
3. hibernate.cfg.xml文件;直接放在src目录下
4. jar包;可以build path导入,也可以直接放在lib目录下(如果是创建的web工程的话可以)
实例:
1.xx.hbm.xml文件
Customer.hbm.xml,用来将Customer类和数据库中的CUSTOMER表进行映射。
代码:
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"">
<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"
"">
<hibernate-configuration>
<session-factory>
<!--配置SQLServer连接属性-->
<property
name="dialect">org.hibernate.dialect.SQLServerDialect</prope rty>
<property
name="connection.driver_class">com.microsoft.sqlserver.jdbc.S
QLServerDriver</property>
<property
name="connection.url">jdbc:sqlserver://localhost:1433;databas eName=Test</property>
<property name="ername">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包
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){
erName=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) {
erName = 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);
mit();
session.close();
}
}。