HIbernate实现增、删、改、查。
生活随笔
收集整理的這篇文章主要介紹了
HIbernate实现增、删、改、查。
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
//大配置
<?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">sb</property><property name="connection.password">sb</property><!-- 輸出所有 SQL 語句到控制臺(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><!-- 關(guān)聯(lián)小配置 -->
<property name="hbm2ddl.auto">update</property> //與小配置進(jìn)行關(guān)聯(lián)
<mapping resource="entity/Student.hbm.xml"/></session-factory></hibernate-configuration>
1 public static void main(String[]args){ 2 Class clazz=Student.class(); 3 System.out.println(clazz); 4 } Session session; Transaction tx; @Before //方法執(zhí)行前 public void beffore(){session=HibernateUtil.getSession();tx=Session.beginTransaction; } @After //方法之前后 public void after(){ tx.commit(); HibernateUtil.CloseSession; }
?
//小配置 <?xml version='1.0' encoding='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="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>?
package entity; //學(xué)生實(shí)體類 public class Student {private Integer age;private String name;private Integer id; @Override public String toString() {return "Student [age=" + age + ", name=" + name + ", id=" + id + "]"; } 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; } public Integer getId() {return id; } public void setId(Integer id) {this.id = id; } }?
//工具類 方便調(diào)用 更簡潔 package Util;import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.cfg.Configuration;public class HibernateUtil {private static Configuration cf=new Configuration().configure();private static SessionFactory sf=cf.buildSessionFactory();public static Session getSession(){return sf.openSession();}public static void CloseSession(){getSession().close();} }?
1 public static void main(String[]args){ 2 Class clazz=Student.class(); 3 System.out.println(clazz); 4 } Session session; Transaction tx; @Before //方法執(zhí)行前 public void beffore(){session=HibernateUtil.getSession();tx=Session.beginTransaction; } @After //方法之前后 public void after(){ tx.commit(); HibernateUtil.CloseSession; }
1.增加
public void addtest(){ //創(chuàng)建學(xué)生對象Student stu=new Studetn();stu.setId(1);stu.setName("李澤陽");stu.sestage(23);//找到和數(shù)據(jù)庫的接口 Session---SessionFactory---configur.buidSessionFactory();Configuration cf=new Configurration().Configur("Hibernate.cfg.xml");SessionFactory sf=cf.buildSessionFactory();session=sf.openSession();tx=Session.beginTransaction(); //保存 Session.save(stu); }2.刪除
public void deltest(){Student stu=new Student();stu.setId(1);Session.delect(stu);System.out.println("delect OK!"); }3.修改
public void updatetest(){Student stu=(Student)Session.get(Student.class,1);stu.setName("陽陽陽");Session.update(stu);System.out.println("update OK!"); }4.查詢
public void selecttest(){Student stu=(Student)Session.get(Student.class,1);System.out.println("select OK!"); }?
轉(zhuǎn)載于:https://www.cnblogs.com/lizeyang/p/5815064.html
總結(jié)
以上是生活随笔為你收集整理的HIbernate实现增、删、改、查。的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: CentOS 6.5/6.6 安装mys
- 下一篇: Sublime text 简单配置