一、hibernate框架介绍如下
1 <?xml version="1.0" encoding="UTF-8"?> 2 <!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd" > 3 <hibernate-configuration> 4 <!-- session会话工厂配置数据源 --> 5 <session-factory> 6 <!-- 1.链接数据库的driver --> 7 <property name="connection.driver_class">oracle.jdbc.driver.OracleDriver</property> 8 <!-- 2.链接数据库的url --> 9 <property name="connection.url">jdbc:oracle:thin:@127.0.0.1:1521:orcl</property> 10 <!-- 3.链接数据库的用户名 --> 11 <property name="connection.username">scott</property> 12 <!-- 4.链接数据库的密码 --> 13 <property name="connection.password">tiger</property> 14 <!-- 5.配置数据库的方言 --> 15 <property name="dialect">org.hibernate.dialect.Oracle10gDialect</property> 16 <!-- 6.配置是否在控制台显示hibernate封装的sql --> 17 <property name="show_sql">true</property> 18 <!-- 7.sql语句格式化 --> 19 <property name="format_sql">true</property> 20 </session-factory> 21 </hibernate-configuration>
二、使用hibernate实现全套正删改查
2.1 创建项目Hibernate_Part1并在项目的WebRoot下的WEB-INF下的lib文件下加入如下jar文件:
1 slf4j-log4j12-1.5.8.jar 2 antlr-2.7.6.jar 3 commons-collections-3.1.jar 4 commons-dbcp.jar 5 commons-dbcp-1.2.1.jar 6 commons-lang-1.0.1.jar 7 commons-logging-1.0.4.jar 8 commons-pool-1.2.jar 9 dom4j-1.6.1.jar 10 hibernate3.jar 11 javassist-3.9.0.GA.jar 12 jta.jar 13 log4j-1.2.8.jar 14 slf4j-api-1.5.8.jar
2.2 在项目的src下创建hibernate.cfg.xml主配置文件
1 <?xml version="1.0" encoding="UTF-8"?> 2 <!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd" > 3 <hibernate-configuration> 4 <session-factory> 5 <!-- oracle驱动 --> 6 <property name="connection.driver_class">oracle.jdbc.driver.OracleDriver</property> 7 <!-- 连接 数据库url--> 8 <property name="connection.url">jdbc:oracle:thin:@127.0.0.1:1521:orcl</property> 9 <!-- 连接数据库用户名 --> 10 <property name="connection.username">scott</property> 11 <!-- 连接数据库密码 --> 12 <property name="connection.password">tiger</property> 13 <!-- 数据库 方言--> 14 <property name="dialect">org.hibernate.dialect.Oracle10gDialect</property> 15 <!-- 显示sql语句 --> 16 <property name="show_sql">true</property> 17 <!-- sql语句格式化 --> 18 <property name="format_sql">true</property> 19 20 <!--注册映射文件 --> 21 <mapping resource="com/entity/Dept.hbm.xml"/> 22 </session-factory> 23 </hibernate-configuration>
2.3 在src下的com.entity包下创建Dept.java文件
1 package com.entity; 2 3 4 public class Dept{ 5 6 private Integer deptno; 7 private String deptname; 8 private String loc; 9 10 public Dept() { 11 } 12 13 public Dept(Integer deptno, String deptname, String loc) { 14 this.deptno = deptno; 15 this.deptname = deptname; 16 this.loc = loc; 17 } 18 19 public Integer getDeptno() { 20 return deptno; 21 } 22 23 public void setDeptno(Integer deptno) { 24 this.deptno = deptno; 25 } 26 27 public String getDeptname() { 28 return deptname; 29 } 30 31 public void setDeptname(String deptname) { 32 this.deptname = deptname; 33 } 34 35 public String getLoc() { 36 return loc; 37 } 38 39 public void setLoc(String loc) { 40 this.loc = loc; 41 } 42 43 @Override 44 public String toString() { 45 return "Dept [deptname=" + deptname + ", deptno=" + deptno + ", loc=" + loc 46 + "]"; 47 } 48 49 50 }
2.4 在src下的com.entity包下创建Dept.hbm.xml文件
1 <?xml version="1.0" encoding="UTF-8"?> 2 <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd" > 3 <hibernate-mapping> 4 <class name="com.entity.Dept" table="DEPT"> 5 <id name="deptno" type="java.lang.Integer" column="DEPTNO"> 6 <generator class="assigned"/> 7 </id> 8 <property name="deptname" type="java.lang.String" column="DNAME"/> 9 <property name="loc" type="java.lang.String" column="LOC"/> 10 </class> 11 </hibernate-mapping>
2.5 在src下的com.dao包下创建DeptDao.java文件
1 package com.dao; 2 import java.io.Serializable; 3 4 import org.hibernate.Session; 5 import org.hibernate.SessionFactory; 6 import org.hibernate.Transaction; 7 import org.hibernate.cfg.Configuration; 8 9 import com.entity.Dept; 10 11 public class DeptDao implements Serializable{ 12 /** 13 * 14 */ 15 private static final long serialVersionUID = 7318695909579058942L; 16 17 18 /** 19 * 1.get方法查询当查不到返回null 20 * 关闭session能读取到 21 * @param id 22 */ 23 public static void getDept(Integer id){ 24 //1.读取配置文件 25 Configuration configuration=new Configuration().configure("/hibernate.cfg.xml"); 26 27 //2.创建Session工厂 28 SessionFactory sessionFactory=configuration.buildSessionFactory(); 29 30 //3.打开Session 31 Session session=sessionFactory.openSession(); 32 33 //4.开启事务(查询不需要) 34 Transaction tx=session.beginTransaction(); 35 36 //5.执行操作 37 Dept dept=(Dept) session.get(Dept.class, id); 38 if(dept!=null){ 39 System.out.println("查到了"); 40 System.out.println(dept); 41 42 }else{ 43 System.out.println("没查到"); 44 } 45 46 //6.提交事务(查询不需要) 47 tx.commit(); 48 49 50 //7.关闭session 51 session.close(); 52 53 54 55 56 } 57 58 /** 59 * 2.load方法查询当查不到报出异常 60 * 关闭session读取不到,报出异常 61 * @param id 62 */ 63 public static void loadDept(Integer id){ 64 //1.读取配置文件 65 Configuration configuration=new Configuration().configure("/hibernate.cfg.xml"); 66 67 //2.创建Session工厂 68 SessionFactory sessionFactory=configuration.buildSessionFactory(); 69 70 //3.打开Session 71 Session session=sessionFactory.openSession(); 72 73 //4.开启事务(查询不需要) 74 Transaction tx=session.beginTransaction(); 75 76 //5.执行操作 77 Dept dept=(Dept) session.load(Dept.class, id); 78 79 //6.提交事务(查询不需要) 80 tx.commit(); 81 82 System.out.println(dept); 83 84 //7.关闭session 85 session.close(); 86 87 } 88 89 /** 90 * 添加 91 * @param dept 92 */ 93 public static void saveDept(Dept dept){ 94 //1.读取配置文件 95 Configuration configuration=new Configuration().configure("/hibernate.cfg.xml"); 96 97 //2.创建Session工厂 98 SessionFactory sessionFactory=configuration.buildSessionFactory(); 99 100 //3.打开Session 101 Session session=sessionFactory.openSession(); 102 103 //4.开启事务 104 Transaction tx=session.beginTransaction(); 105 106 //5.执行操作 107 session.save(dept); 108 109 110 //6.提交事务 111 tx.commit(); 112 113 System.out.println("插入成功"); 114 115 //7.关闭session 116 session.close(); 117 } 118 119 /** 120 * 修改1 121 * 先查询单条再次修改 122 * @param dept 123 */ 124 public static void updateDept(Integer id){ 125 //1.读取配置文件 126 Configuration configuration=new Configuration().configure("/hibernate.cfg.xml"); 127 128 //2.创建Session工厂 129 SessionFactory sessionFactory=configuration.buildSessionFactory(); 130 131 //3.打开Session 132 Session session=sessionFactory.openSession(); 133 134 //4.开启事务 135 Transaction tx=session.beginTransaction(); 136 137 //5.执行操作 138 //查询 139 Dept dept=(Dept) session.get(Dept.class, id); 140 141 //修改 142 dept.setDeptname("不知道"); 143 //6.提交事务 144 tx.commit(); 145 146 System.out.println("修改成功"); 147 148 //7.关闭session 149 session.close(); 150 } 151 152 153 /** 154 * 删除对象 155 * 先查询单条再删除 156 * @param dept 157 */ 158 public static void deleteDept(Integer id){ 159 //1.读取配置文件 160 Configuration configuration=new Configuration().configure("/hibernate.cfg.xml"); 161 162 //2.创建Session工厂 163 SessionFactory sessionFactory=configuration.buildSessionFactory(); 164 165 //3.打开Session 166 Session session=sessionFactory.openSession(); 167 168 //4.开启事务 169 Transaction tx=session.beginTransaction(); 170 171 //5.执行操作 172 //查询 173 Dept dept=(Dept) session.get(Dept.class, id); 174 175 //删除对象 176 session.delete(dept); 177 178 //6.提交事务 179 tx.commit(); 180 181 System.out.println("删除成功"); 182 183 //7.关闭session 184 session.close(); 185 } 186 187 public static void main(String[] args) { 188 // getDept(10); 189 // loadDept(10); 190 // Dept dept=new Dept(1, "TB07", "青鸟学社"); 191 // saveDept(dept); 192 // updateDept(1); 193 // getDept(1); 194 deleteDept(1); 195 getDept(1); 196 } 197 198 199 }
2.6 运行结果自己试试看就知道了
三、使用hibernate的工具类实现全套正删改查
2.1 创建项目Hibernate_Part1并在项目的WebRoot下的WEB-INF下的lib文件下加入如下jar文件:
2.2 在项目的src下创建hibernate.cfg.xml主配置文件
1 <?xml version="1.0" encoding="UTF-8"?> 2 <!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd" > 3 <hibernate-configuration> 4 <session-factory> 5 <property name="connection.driver_class">oracle.jdbc.driver.OracleDriver</property> 6 <property name="connection.url">jdbc:oracle:thin:@127.0.0.1:1521:orcl</property> 7 <property name="connection.username">holly</property> 8 <property name="connection.password">sys</property> 9 <property name="dialect">org.hibernate.dialect.Oracle10gDialect</property> 10 <property name="show_sql">true</property> 11 <property name="format_sql">true</property> 12 13 <mapping resource="com/entity/Teacher.hbm.xml"/> 14 </session-factory> 15 </hibernate-configuration>
2.3 在src下的com.entity包下创建Teacher.java文件
1 package com.entity; 2 3 public class Teacher { 4 private int id; 5 private String name; 6 7 public Teacher() { 8 } 9 10 public Teacher( String name) { 11 this.name = name; 12 } 13 public Teacher(int id, String name) { 14 this.id = id; 15 this.name = name; 16 } 17 18 public int getId() { 19 return id; 20 } 21 22 public void setId(int id) { 23 this.id = id; 24 } 25 26 public String getName() { 27 return name; 28 } 29 30 public void setName(String name) { 31 this.name = name; 32 } 33 34 @Override 35 public String toString() { 36 return "Teacher [id=" + id + ", name=" + name + "]"; 37 } 38 39 40 41 42 }
2.4 在src下的com.entity包下创建Teacher.hbm.xml文件
1 <?xml version="1.0" encoding="UTF-8"?> 2 <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd" > 3 <hibernate-mapping> 4 <class name="com.entity.Teacher" table="TEACHER"> 5 <id name="id" type="java.lang.Integer" column="ID"> 6 <generator class="sequence"> 7 <param name="sequence">seq_teacher</param> 8 </generator> 9 </id> 10 11 <property name="name" type="java.lang.String" column="NAME"/> 12 </class> 13 </hibernate-mapping>
2.5 在src下的com.util包下创建HibernateUtil.java文件
1 package com.util; 2 3 import org.hibernate.HibernateException; 4 import org.hibernate.Session; 5 import org.hibernate.SessionFactory; 6 import org.hibernate.cfg.Configuration; 7 8 public class HibernateUtil { 9 private static ThreadLocal<Session> thread=new ThreadLocal<Session>(); 10 private static Configuration config=null; 11 private static SessionFactory factory=null; 12 13 static{ 14 try { 15 config=new Configuration().configure("/hibernate.cfg.xml"); 16 factory=config.buildSessionFactory(); 17 } catch (HibernateException e) { 18 System.out.println("读取配置文件失败"); 19 e.printStackTrace(); 20 } 21 } 22 23 public static Session getSession(){ 24 Session session=thread.get(); 25 if(session==null){ 26 session=factory.openSession(); 27 thread.set(session); 28 } 29 return session; 30 31 } 32 33 public static void closeSession(){ 34 Session session=thread.get(); 35 thread.set(null); 36 session.close(); 37 } 38 }
2.6 在src下的com.dao包下创建TeacherDao.java文件
1 package com.dao; 2 3 import org.hibernate.Session; 4 import org.hibernate.Transaction; 5 6 import com.entity.Teacher; 7 import com.util.HibernateUtil; 8 9 public class TeacherDao { 10 public static void main(String[] args) { 11 Teacher teacher=new Teacher(2,"死猪投资2"); 12 //insertTeacher(teacher); 13 updateTeacher(teacher); 14 getTeacher(2); 15 16 } 17 18 private static void updateTeacher(Teacher teacher) { 19 Session session=HibernateUtil.getSession(); 20 Transaction tx=session.beginTransaction(); 21 session.merge(teacher); 22 tx.commit(); 23 System.out.println("修改成功"); 24 HibernateUtil.closeSession(); 25 26 } 27 28 private static void getTeacher(int id) { 29 Session session=HibernateUtil.getSession(); 30 Teacher teacher=(Teacher) session.get(Teacher.class, id); 31 System.out.println(teacher); 32 HibernateUtil.closeSession(); 33 } 34 35 private static void insertTeacher(Teacher teacher) { 36 Session session=HibernateUtil.getSession(); 37 Transaction tx=session.beginTransaction(); 38 session.save(teacher); 39 tx.commit(); 40 System.out.println("保存成功"); 41 HibernateUtil.closeSession(); 42 } 43 44 45 46 }
2.7 运行结果自己试试看就知道了
原文:http://www.cnblogs.com/holly8/p/5738758.html