Oracle 增删改查
Oracle入門案例:
1.創建實體類Student 并重寫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.導入jar包
3.創建大配置關聯小配置
?
<?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 語句到控制臺。 -->
<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.編寫小配置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.工具類
?
?
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.關閉sessionpublic static void closeSession(){getSession().close();}}6.測試類
// 1.2 修改學生 @Testpublic void updateTest(){Session session = HibernateUtil.getSession();//不被上下文跟蹤對象 /*Student stu=new Student();stu.setId(3);stu.setName("微冷的雨訓練營");*///方式二:如何用唄上下文跟蹤的方式//檢索出一條記錄,一個實體對象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/Smile-123/p/5815070.html
總結
以上是生活随笔為你收集整理的Oracle 增删改查的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: spring --(12)bean的生命
- 下一篇: iOS开发系列--网络开发(转)