最近入门学习Hibernate框架,想分享总结一下踩过的坑和成功后的案例。java
所用软件:sql
eclipse数据库
Oracle数据库 (若是你使用的不是Oracle数据库能够修改hibernate.cfg.xml里面的配置)session
如今,咱们来看一下咱们的包结构和数据库表结构oracle
第一步:创建eclipse项目app
1.eclipse里面新建一个Dynamic Web Project项目框架
2.在src中建一个名叫hibernate.cfg.xml的xml文件和两个分别叫com.Sakura.maya.model和com.Sakura.maya.test的包。eclipse
3.在com.Sakura.maya.model包中建两个类Hibernate.java和Hibernate.hbm.xml的文件ide
4.在com.Sakura.maya.test包中创建一个叫HibernateAction的类学习
结构以下:
第二步:引入jar包
包名以下:
第三步:建立数据库
create table Table_wcc_text(
stu_id number(10) primary key,
stu_name varchar2(20) not null,
stu_sex varchar2(20) default 'men' check(stu_sex in('men','women'))
)
结构以下:
第四步:加入代码
1.Hibernate.java
package com.Sakura.maya.model; public class Hibernate { private int stu_id; private String stu_name; private String stu_sex; public int getStu_id() { return stu_id; } public void setStu_id(int stu_id) { this.stu_id = stu_id; } public String getStu_name() { return stu_name; } public void setStu_name(String stu_name) { this.stu_name = stu_name; } public String getStu_sex() { return stu_sex; } public void setStu_sex(String stu_sex) { this.stu_sex = stu_sex; } @Override public String toString() { return "HibernateAction [stu_id=" + stu_id + ", stu_name=" + stu_name + ", stu_sex=" + stu_sex + "]"; } }
2.Hibernate.hbm.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> <property name="hibernate.dialect">org.hibernate.dialect.OracleDialect</property> <!-- 若是不是Oracle数据库请修改--> <!-- <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property> --> <!-- 数据库方言 若是不是Oracle数据库请修改--> <property name="hibernate.connection.driver_class">oracle.jdbc.driver.OracleDriver</property><!-- 引用jdbc包 若是不是Oracle数据库请修改--> <property name="hibernate.connection.username">用户名</property> <property name="hibernate.connection.password">密码</property> <property name="hibernate.connection.url">jdbc:oracle:数据库连接</property> <!-- 数据库连接 --> <property name="show_sql">true</property> <!-- true 执行完在控制台打印SQL语句 --> <!-- 表映射加载 --> <mapping resource="com/Sakura/maya/model/Hibernate.hbm.xml"/> </session-factory> </hibernate-configuration>
3.hibernate.cfg.xml
<?xml version='1.0' encoding='UTF-8'?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/hibernate-Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> <hibernate-mapping default-lazy="false" package="com.Sakura.maya.model"> <!-- 导入包 --> <class name="Hibernate" table="Table_wcc_text"> <!-- 表名 --> <id name="stu_id"> <!-- 主键 --> <generator class="assigned"/><!-- 若是主键是自增加改成class="native" --> </id> <!-- 对应的各个字段名 --> <property name="stu_name"/> <property name="stu_sex"/> </class> </hibernate-mapping>
4.HibernateAction.java
package com.Sakura.maya.test; import java.util.List; import javax.security.auth.kerberos.DelegationPermission; import org.hibernate.*; import org.hibernate.cfg.*; import com.Sakura.maya.model.Hibernate; public class HibernateAction { //读取hibernate.cfg.xml的配置,加载Hiberna的类库 Configuration config=new Configuration().configure(); //根据配置,生成session工厂 SessionFactory factory= config.buildSessionFactory(); //用工厂生成session Session session =factory.openSession(); public static void main(String[] args) { HibernateAction dl = new HibernateAction(); dl.SelectAll(); } public void Select(Integer stu_id) { // 查询 Hibernate data = (Hibernate) session.get(Hibernate.class, stu_id); System.out.println(data.getStu_id() + data.getStu_name() + data.getStu_sex()); } public void SelectAll() { // 查询全部 String hqlString = "from Hibernate"; Query query = session.createQuery(hqlString); List<Hibernate> list = query.list(); for (Hibernate ll : list) { System.out.print(ll); } session.close(); } public void Insert() { //添加 //1.造对象 Hibernate data = new Hibernate(); data.setStu_id('1'); data.setStu_name("Sakura"); data.setStu_sex("men"); //2.用session.save()保存到数据库了 try { session.beginTransaction(); session.save(data); session.getTransaction().commit(); System.out.print("添加成功"); } catch (Exception ex) { session.getTransaction().rollback(); } session.close(); } //删除 public void Delete(Integer stu_id) { //先查出 Hibernate data = (Hibernate) session.get(Hibernate.class, stu_id);// get?�load?�能够查 if (data != null) { session.beginTransaction(); //删除提交数据库 session.delete(data); System.out.print("删除成功"); session.getTransaction().commit(); } session.close(); } public void update(Integer stu_id) { //修改 //1.查 Hibernate data = (Hibernate) session.load(Hibernate.class, stu_id); if (data != null) { session.beginTransaction(); //2.改 data.setStu_name("Sakura1"); data.setStu_sex("women"); //3.提交 session.update(data); session.getTransaction().commit(); } session.close(); System.out.print("修改为功"); } }
OK!就搭建成功了!
在搭建之中碰见的小问题:
1.显示链接不到数据库
解决方法:多是你不是oracle数据库在hibernate.cfg.xml中修改链接方法。
2.报错:javax/transaction/Synchronization
解决方法:你须要下载一个jta.jar包
3.报错:找不到hibernate.cfg.xml文件
解决方法:缺乏jar包或者放的位置不对,放在src