標籤:build update res comm div java entity private standard
一:.項目結構
二、代碼
1)HibernateUtil
package util;import org.hibernate.SessionFactory;import org.hibernate.boot.MetadataSources;import org.hibernate.boot.registry.StandardServiceRegistry;import org.hibernate.boot.registry.StandardServiceRegistryBuilder;public class HibernateUtil { private static SessionFactory sf; static { StandardServiceRegistry registry = new StandardServiceRegistryBuilder() .configure()//實驗方法給個參數,看設定檔名能否不是hibernate.cfg.xml .build(); sf = new MetadataSources(registry) .buildMetadata() .buildSessionFactory(); } public static SessionFactory getSessionFactory(){ return sf; }}
2)設定檔hibernate.cfg.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> <property name="connection.driver_class">oracle.jdbc.driver.OracleDriver</property> <property name="connection.url">jdbc:oracle:thin:@localhost:1521:orcl</property> <property name="connection.username">myuser</property> <property name="connection.password">123</property> <property name="dialect">org.hibernate.dialect.Oracle10gDialect</property> <property name="hbm2ddl.auto">create</property> <property name="show_sql">true</property> <property name="format_sql">true</property> <mapping resource="entity/StudentMapping.xml"/> </session-factory></hibernate-configuration>
3.1)實體物件
package entity;public class Student { private String sno; private String sname; public String getSno() { return sno; } public void setSno(String sno) { this.sno = sno; } public String getSname() { return sname; } public void setSname(String sname) { this.sname = sname; } }
3.2)實體對應檔
<?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="Students"> <id name="sno" column="id"> <generator class="assigned"></generator> </id> <property name="sname"></property> </class> </hibernate-mapping>
4)dao方法
package dao;import java.util.List;import org.hibernate.Session;import org.hibernate.SessionFactory;import org.hibernate.Transaction;import entity.Student;import util.HibernateUtil;public class StudentDao { private SessionFactory sf; public StudentDao() { sf = HibernateUtil.getSessionFactory(); } /*添加*/ public void insert(Student stu){ Session s = sf.openSession(); Transaction ts = s.beginTransaction(); /*添加*/ s.save(stu); ts.commit(); s.close(); } /*修改*/ public void udate(Student stu){ Session s = sf.openSession(); Transaction ts = s.beginTransaction(); /*修改*/ s.update(stu); ts.commit(); s.close(); } /*刪除*/ public void delete(String sno){ Session session = sf.openSession(); Transaction tx = session.beginTransaction(); Student stu = session.get(Student.class, sno); /*刪除*/ session.delete(stu); tx.commit(); session.close(); } /*查詢*/ public List<Student> getAll(){ Session s = sf.openSession(); String hql = "from Student"; /*查詢*/ List<Student> list = s.createQuery(hql).list(); s.close(); return list; } }
5)test測試
package test;import dao.StudentDao;import entity.Student;public class Main { public static void main(String[] args) { StudentDao dao = new StudentDao(); /*添加*/ Student stu = new Student(); stu.setSno("22"); stu.setSname("z22"); dao.insert(stu); /*修改*/ /*Student stu = new Student(); stu.setSno("11"); stu.setSname("2222222"); dao.udate(stu);*/ /*查詢*/ /* List<Student> list = dao.getAll(); for(Student s: list){ System.out.println(s.getSname()); }*/ /*刪除*/ /*dao.delete("22");*/ }}
/*以上個人整理筆記,如果有誤或者有不懂的地方,歡迎評論與指出*/
Hibernate ORM架構——續第一章:Hibernate的增刪改查(第一個hibernate代碼的最佳化)