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();
}
}
点击打开链接
主要文件有四类:
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();
}
}
相关文章推荐
- (3)mysql优化之sql语句优化
- Oracle中给表添加主键、外键
- sqlite字符反向模糊查找
- C#连接access数据库,无法启动应用程序。工作组信息文件丢失,或是已被其它用户以独占方式打开。
- mysql 查询执行的流程
- Linux Oracle10g安装
- redis一个比较大的改变,redis3.0.1 稳定版本发布,支持集群。redis 集群配置 reids Cluster
- 在VMware安装Centos再安装Oracle数据库(个人学习使用)
- mysql 优化
- mysql 逻辑架构
- sqlserver:游标
- redis集群&主从部署
- redis集群配置
- windows 下 mysql 5.6 免安装版配置
- redis主从配置
- mysql 并发控制
- MYSQL--事务处理
- JSP 基于Oracle分页
- mysql 解决 ERROR 2003 (HY000): Can't connect to MySQL server on 'localhost' (10061)
- mysql笔记01