您的位置:首页 > 数据库

Hibernate连接SQLServer数据库的配置

2015-05-31 16:58 330 查看
连接2005

点击打开链接

主要文件有四类:

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"

"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包

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();

}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: