学习Hibernate

来源:互联网 发布:java 国内书籍 编辑:程序博客网 时间:2024/06/18 09:26

项目结构

 

News.java和NewsTest.java是另一个小项目,不用理会。

 

1、建立UserName.java类

package com.cfc.hibernate;public class UserName {private int id;private String name;private String password;public int getId() {return this.id;}public void setId(int id) {this.id = id;}public String getName() {return this.name;}public void setName(String name) {this.name = name;}public String getPassword() {return this.password;}public void setPassword(String password) {this.password = password;}}

2、建立UserName.hbm.xml


 

<?xml version="1.0"?><!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN""http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"><hibernate-mapping package="com.cfc.hibernate"><class name="UserName"><id name="id"><generator class="native"></generator></id><property name="name" /><property name="password" /></class></hibernate-mapping>


3、建立:hibernate.cfc.xml

<!DOCTYPE hibernate-configuration PUBLIC          "-//Hibernate/Hibernate Configuration DTD 3.0//EN"          "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">            <hibernate-configuration>          <session-factory>              <!-- 数据库驱动 -->              <property name="hibernate.connection.driver_class">net.sourceforge.jtds.jdbc.Driver</property>              <!-- 数据库名称地址 -->              <property name="hibernate.connection.url">jdbc:jtds:sqlserver://localhost:1433;DatabaseName=hibernatedemo01</property>              <!-- 数据库用户名 -->              <property name="hibernate.connection.username">sa</property>              <!-- 数据库密码 -->              <property name="hibernate.connection.password">1234</property>              <!-- 配置数据库适配器.可以直接使用相关数据库的相关特点-->              <property name="hibernate.dialect">org.hibernate.dialect.SQLServerDialect</property>                          <!-- 根据需要自动创建数据表 -->              <property name="hibernate.hbm2ddl.auto">update</property>                        <!-- 打印sql语句到控制台 -->             <property name="hibernate.show_sql">true</property>              <!-- 对打印的Sql语句进行格式化 -->              <property name="hibernate.format_sql">true</property>                                         <!-- 对资源进行映射 -->              <mapping resource="com/cfc/hibernate/UserName.hbm.xml"/>          </session-factory>      </hibernate-configuration>  


4、测试类:Test.java

package com.cfc.hibernate.demo;import org.hibernate.Session;import org.hibernate.SessionFactory;import org.hibernate.Transaction;import org.hibernate.cfg.Configuration;import com.cfc.hibernate.UserName;public class Test {public static void main(String[] args){try{Configuration conf=new Configuration().configure();SessionFactory sf=conf.buildSessionFactory();Session sess=sf.openSession();Transaction tx=sess.beginTransaction();UserName user=new UserName();user.setName("ss");user.setPassword("22");sess.save(user);tx.commit();sess.close();System.out.print("end");}catch(Exception e){e.printStackTrace();}}}


结果是:

 

总结:

1、之前的UserName.java曾用User.java。因为之前建立过,后又删了。出现了问题(总说:Caused by: java.sql.SQLException: 在关键字 'User' 附近有语法错误,后在网上查到相关资料,换名就行了)。

2、原先使用的是驱动是微软的。但出现了问题,上网找一些资料。说是微软提供的驱动有bug。于是改了用了jtds-1.2.2.jar。问题解决

hibernate相关的包已经上传。不是完整版。http://download.csdn.net/detail/caikule/5112687

原创粉丝点击