学习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
- Hibernate学习:初识hibernate
- Hibernate学习:(Hibernate)cascade
- hibernate学习 hibernate概述
- 【hibernate】 深度学习hibernate
- 学习HIBERNATE
- Hibernate学习
- hibernate学习
- Hibernate学习
- hibernate学习:
- Hibernate学习
- hibernate学习
- Hibernate 学习
- Hibernate学习
- 学习Hibernate
- hibernate学习
- hibernate学习
- hibernate 学习
- Hibernate学习
- C#制作Windows service服务系列一:制作一个可安装、可启动、可停止、可卸载的Windows service
- poj 2823 线段树求区间最值
- 递归
- C#制作Windows service服务系列二--演示一个定期执行的windows服务及调试
- Posting only possible in periods 2013/02 and 2013/01 in company code 0001
- 学习Hibernate
- C#制作Windows service服务系列三--制作可控制界面的Windows服务(windows service)
- Tomcat Remote Debug
- ffmpeg开发指南
- c++智能指针auto_ptr和shared_ptr
- SQL Server 2005数据表导出到Oracle的方案 .
- javascript 小技巧
- Chartboost-x新鲜出炉: C++ Wrapper of Chartboost for Cocos2d-x
- 常用网址