利用idea开发工具hibernate工具包实现后台对Oracle数据库的增删改查
hibernate.cfg.xml文件的配置:
1 <?xml version="1.0" encoding="UTF-8"?> 2 <!DOCTYPE hibernate-configuration PUBLIC 3 "-//Hibernate/Hibernate Configuration DTD 3.0//EN" 4 "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd"> 5 <hibernate-configuration> 6 <session-factory> 7 <!-- 首先设定jdbc链接相关的信息(驱动,url,用户名,密码) -->与数据库连接的信息 8 <property name="connection.driver_class">oracle.jdbc.driver.OracleDriver</property> 9 <property name="connection.url">jdbc:oracle:thin:@localhost:1521:orcl</property> 10 <property name="connection.username">hbm</property> 11 <property name="connection.password">123</property> 12 13 <!-- 设定方言 --> 14 <property name="dialect">org.hibernate.dialect.Oracle10gDialect</property> 15 16 <!-- 设定让hibernate帮我们自动创建表,不设定可以吗? --> 17 <!-- <property name="hbm2ddl.auto">create</property> --> 18 <property name="show_sql">true</property> 19 <property name="format_sql">true</property> 20 21 <mapping resource="entity/StudentMapper.xml"/> 22 </session-factory> 23 24 </hibernate-configuration>
hibernateUtil.java文件:
1 package util; 2 3 import org.hibernate.SessionFactory; 4 import org.hibernate.boot.MetadataSources; 5 import org.hibernate.boot.registry.StandardServiceRegistry; 6 import org.hibernate.boot.registry.StandardServiceRegistryBuilder; 7 8 public class HibernateUtil { 9 private static SessionFactory sf; 10 //静态代码块,是类加载的时候会执行 11 //整个项目,它只执行一次 12 static{ 13 14 StandardServiceRegistry registry = 15 new StandardServiceRegistryBuilder() 16 .configure()//实验方法给个参数,看配置文件名能否不是hibernate.cfg.xml 17 .build(); 18 19 sf = new MetadataSources(registry) 20 .buildMetadata() 21 .buildSessionFactory(); 22 } 23 //为什么要创建一个返回SessionFactory的方法 24 //而不是创建一个返回Session的方法 25 //是为了让别人有选择的机会,让它选择OpenSession 26 //还是getCurrentSession来创建Session 27 public static SessionFactory getSessionFactory(){ 28 return sf; 29 } 30 31 //在此hibernateUtil中,你还可以创建一些返回Session的方法 32 33 }
studentMapper.,xml对实体文件的配置:
1 <?xml version="1.0" encoding="UTF-8"?> 2 <!DOCTYPE hibernate-mapping PUBLIC 3 "-//Hibernate/Hibernate Mapping DTD 3.0//EN" 4 "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"> 5 6 <hibernate-mapping package="entity"> 7 <class name="Student" table="students"> 8 <id name="sno" > 9 <generator class="assigned"></generator> 10 </id> 11 <property name="sname" column="name"></property> 12 </class> 13 </hibernate-mapping>
student.xml实体文件:
1 package entity; 2 3 public class Student { 4 private String sno; 5 private String sname; 6 public String getSno() { 7 return sno; 8 } 9 public void setSno(String sno) { 10 this.sno = sno; 11 } 12 public String getSname() { 13 return sname; 14 } 15 public void setSname(String sname) { 16 this.sname = sname; 17 } 18 }
studentDao.java文件:
1 package dao; 2 3 import java.util.List; 4 5 6 7 import org.hibernate.Session; 8 import org.hibernate.SessionFactory; 9 import org.hibernate.Transaction; 10 11 import entity.Student; 12 import util.HibernateUtil; 13 14 public class StudentDao { 15 16 private SessionFactory sf ; 17 18 public StudentDao(){ 19 sf = HibernateUtil.getSessionFactory(); 20 } 21 22 public void insert(Student s){ 23 Session session = sf.openSession(); 24 25 Transaction tx = session.beginTransaction(); 26 session.save(s); 27 tx.commit(); 28 session.close(); 29 } 30 31 public void update(Student s){ 32 Session session = sf.openSession(); 33 Transaction tx = session.beginTransaction(); 34 session.update(s); 35 tx.commit(); 36 session.close(); 37 } 38 39 public void delete(String sno){ 40 Session session = sf.openSession(); 41 Transaction tx = session.beginTransaction(); 42 Student stu = session.get(Student.class, sno); 43 session.delete(stu); 44 tx.commit(); 45 session.close(); 46 } 47 48 49 public List<Student> getAll(){ 50 String hql = "from Student"; 51 Session s = sf.openSession(); 52 List<Student> list = s.createQuery(hql).list(); 53 s.close(); 54 return list; 55 } 56 }
main.java文件:
1 package com.nf; 2 3 4 import java.util.List; 5 6 import org.hibernate.Session; 7 import org.hibernate.SessionFactory; 8 import org.hibernate.Transaction; 9 import org.hibernate.boot.MetadataSources; 10 import org.hibernate.boot.registry.StandardServiceRegistry; 11 import org.hibernate.boot.registry.StandardServiceRegistryBuilder; 12 13 import dao.StudentDao; 14 import entity.Student; 15 import util.HibernateUtil; 16 17 public class Main { 18 19 public static void main(String[] args) { 20 //insertTest(); 21 //getAllTest(); 22 23 StudentDao dao = new StudentDao(); 24 /* Student s = new Student(); 25 s.setSno("22");//因为主键是assigned的策略,所以需要我们自己赋值 26 s.setSname("222name"); 27 dao.insert(s);*/ 28 29 /* Student s = new Student(); 30 s.setSno("22");//因为主键是assigned的策略,所以需要我们自己赋值 31 s.setSname("2xxname"); 32 dao.update(s); 33 */ 34 35 /*List<Student> list = dao.getAll(); 36 for(Student s: list){ 37 System.out.println(s.getSname()); 38 }*/ 39 40 dao.delete("22"); 41 } 42 private static void insertTest() { 43 // TODO Auto-generated method stub 44 StandardServiceRegistry registry = 45 new StandardServiceRegistryBuilder() 46 .configure()//实验方法给个参数,看配置文件名能否不是hibernate.cfg.xml 47 .build(); 48 49 SessionFactory sf = new MetadataSources(registry) 50 .buildMetadata() 51 .buildSessionFactory(); 52 53 54 Session s = sf.openSession(); 55 56 Transaction tx = s.beginTransaction(); 57 Person p = new Person(); 58 p.setPid(100); 59 p.setName("cj"); 60 s.save(p); 61 tx.commit(); 62 s.close(); 63 sf.close(); 64 } 65 private static void getAllTest() { 66 67 // TODO Auto-generated method stub 68 SessionFactory sf = HibernateUtil.getSessionFactory(); 69 70 //sf.getCurrentSession() 71 Session s = sf.openSession(); 72 String hql ="from Person"; 73 List<Person> list = s.createQuery(hql).list(); 74 for(Person p: list){ 75 System.out.println(p.getName()); 76 } 77 78 79 s.close(); 80 sf.close(); 81 } 82 }
hardDream!!