Hibernate入门案例及增删改查

一、Hibernate入门案例剖析:

①创建实体类Student 并重写toString方法

复制代码
public class Student {

    private Integer sid;
    private Integer age; private String name; public Integer getSid() { return sid; } public void setSid(Integer sid) { this.sid = sid; } public Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; } public String getName() { return name; } public void setName(String name) { this.name = name; } @Override public String toString() { return "Student [sid=" + sid + ", age=" + age + ", name=" + name + "]"; } }
复制代码

② 创建学生对象 并赋值

 

③引入jar包

④ 构建大配置<hibernate.cfg.xml>

可分为以下步骤:

1.连接数据库的语句

2.sql方言

3.可省的配置(show_sql、format_sql 取值为true)

4.让程序生成底层数据表(hbm2ddl.auto) update/create。create是每次将数据表删除后,重新创建

5.关联小配置

<mapping resource="cn/happy/entity/Student.hbm.xml" />

关键代码如下:

复制代码
<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
        "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
        "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">

<hibernate-configuration>

    <session-factory>

        <!-- Database connection settings 数据库连接设置-->
        <!-- 驱动类 -->
        <property name="connection.driver_class">oracle.jdbc.OracleDriver</property>
        <!-- url地址 -->
        <property name="connection.url">jdbc:oracle:thin:@localhost:1521:orcl3</property>
        <property name="connection.username">wj</property>
        <property name="connection.password">9090</property>

        <!-- SQL dialect  (SQL 方言) -->
        <property name="dialect">org.hibernate.dialect.Oracle10gDialect</property>


        <!--在控制台打印后台的SQL语句 -->
        <property name="show_sql">true</property>
        
        
        <!-- 格式化显示SQL -->
        <!-- <property name="format_sql">true</property> -->
        

        
        <!-- 自动生成student表 -->
         <property name="hbm2ddl.auto">update</property>  
        <!-- 关联小配置 -->
        <mapping resource="cn/happy/entity/Student.hbm.xml" />
        <!-- <mapping class="cn.happy.entity.Grade"/> -->
        
    </session-factory>

</hibernate-configuration>
复制代码

⑤ 构建小配置(Student.hbm.xml)

复制代码
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
        "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
        "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">

<hibernate-mapping package="cn.happy.entity">
        <class name="Student" table="STUDENT">
        <id name="sid" column="SID">
           <!-- 主键生成策略:native:
           native:如果后台是Oracle 后台是MySQL,自动应用自增 assigned:程序员给主键赋值 uuid:32位的16进制数 sequence native --> <generator class="assigned"> <param name="sequence">SEQ_NUM</param> </generator> </id> <!-- <version name="version"></version> --> <property name="name" type="string" column="NAME"/> <property name="age"/> </class> </hibernate-mapping>
复制代码

⑥ 工具类HibernateUtil、构建私有静态的Configuration、SessionFactory对象、定义返回session以及关闭session的方法

复制代码
private static Configuration cf=new Configuration().configure();
    private static SessionFactory sf=cf.buildSessionFactory(); //方法返回session public static Session getSession(){ return sf.openSession(); } //关闭Session public static void CloseSession(){ getSession().close(); }
复制代码

⑦测试类【增删改查】 使用标记After、Before可简化代码

复制代码
package cn.happy.test;
import org.hibernate.Session;
import org.hibernate.SessionFactory; import org.hibernate.Transaction; import org.hibernate.cfg.Configuration; import org.junit.After; import org.junit.Before; import org.junit.Test; import cn.happy.entity.Student; import cn.happy.util.HibernateUtil; public class Test1 { Session session; Transaction tx; @After public void afterTest(){ tx.commit(); HibernateUtil.CloseSession(); } @Before public void initData(){ session=HibernateUtil.getSession(); tx=session.beginTransaction(); } /* * get方法查询 */ @Test public void getData(){ Student stu=(Student)session.get(Student.class, 3); System.out.println(stu); } /* * 增加 */ @Test public void addData(){ Student stu=new Student(); stu.setSid(12); stu.setAge(11); stu.setName("李小龙1"); //读取大配置文件 获取连接信息 Configuration cfg=new Configuration().configure(); //创建SessionFactory SessionFactory fa=cfg.buildSessionFactory(); //加工Session Session se=fa.openSession(); Transaction tx = se.beginTransaction(); //保存  se.save(stu); //事务提交  tx.commit(); se.close(); System.out.println("Save ok!"); } /* * 删除 */ @Test public void delData(){ Session session=HibernateUtil.getSession(); Student stu=new Student(); stu.setSid(2); Transaction tx=session.beginTransaction(); session.delete(stu); tx.commit(); HibernateUtil.CloseSession(); System.out.println("del ok!"); } /* * 修改 */ @Test public void updateData(){ Session session=HibernateUtil.getSession(); Student stu=(Student)session.load(Student.class,3); stu.setName("呵呵"); Transaction tx=session.beginTransaction(); session.update(stu); tx.commit(); HibernateUtil.CloseSession(); System.out.println("update ok!"); } }
复制代码

 

转载于:https://www.cnblogs.com/WuXuanKun/p/5818004.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值