Oracle 增删改查
Oracle入門(mén)案例:
1.創(chuàng)建實(shí)體類(lèi)Student 并重寫(xiě)ToString方法
package cn.happy.entity;public class Student {public Integer getId() {return id;}public void setId(Integer id) {this.id = id;}public String getName() {return name;}public void setName(String name) {this.name = name;}public Integer getAge() {return age;}public void setAge(Integer age) {this.age = age;}@Overridepublic String toString() {return "Student [name=" + name + ", age=" + age + ", id=" + id + "]";}private String name;private Integer age;private Integer id;}
2.導(dǎo)入jar包
3.創(chuàng)建大配置關(guān)聯(lián)小配置
?
<?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>
<property name="connection.driver_class">oracle.jdbc.OracleDriver</property>
<property name="connection.url">jdbc:oracle:thin:@localhost:1521:orcl</property>
<property name="connection.username">scott</property>
<property name="connection.password">0123</property>
<!-- 輸出所有 SQL 語(yǔ)句到控制臺(tái)。 -->
<property name="hibernate.show_sql">true</property>
<!-- 在 log 和 console 中打印出更漂亮的 SQL。 -->
<property name="hibernate.format_sql">true</property>
<!-- 方言 -->
<property name="hibernate.dialect"> org.hibernate.dialect.Oracle10gDialect</property>
<property name="hbm2ddl.auto">update</property>
<mapping resource="cn/happy/entity/Student.hbm.xml"/>
</session-factory>
</hibernate-configuration>
4.編寫(xiě)小配置coding='utf-8'?>
<!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="id" type="int" column="id"></id><property name="name" type="string" column="name"/><property name="age" type="int" column="age"/></class> </hibernate-mapping>5.工具類(lèi)
?
?
package cn.happy.entity;import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.cfg.Configuration;public class HibernateUtil {private static Configuration cfg=new Configuration().configure();private static SessionFactory factory=cfg.buildSessionFactory();//01.方法返回sessionpublic static Session getSession(){return factory.openSession();}//02.關(guān)閉sessionpublic static void closeSession(){getSession().close();}}6.測(cè)試類(lèi)
// 1.2 修改學(xué)生 @Testpublic void updateTest(){Session session = HibernateUtil.getSession();//不被上下文跟蹤對(duì)象 /*Student stu=new Student();stu.setId(3);stu.setName("微冷的雨訓(xùn)練營(yíng)");*///方式二:如何用唄上下文跟蹤的方式//檢索出一條記錄,一個(gè)實(shí)體對(duì)象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!");}?
轉(zhuǎn)載于:https://www.cnblogs.com/Smile-123/p/5815070.html
總結(jié)
以上是生活随笔為你收集整理的Oracle 增删改查的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: spring --(12)bean的生命
- 下一篇: iOS开发系列--网络开发(转)