(二)Hibernate4 CRUD 体验
所有的学习我们必须先搭建好hibernate的环境(1、导入对应的jar包,2、hibernate.cfg.xml,3、XXXX.hbm.xml)
第一节:HibernateUtil 封装
导入对应的hibernate的jar包和mysql的jar包
HibernateUtil.java
1 package com.wishwzp.util; 2 3 import org.hibernate.SessionFactory; 4 import org.hibernate.boot.registry.StandardServiceRegistryBuilder; 5 import org.hibernate.cfg.Configuration; 6 import org.hibernate.service.ServiceRegistry; 7 8 /** 9 * 10 * hibernate封装 11 * @author wishwzp 12 * 13 */ 14 public class HibernateUtil { 15 16 private static final SessionFactory sessionFactory=buildSessionFactory(); 17 18 /** 19 * 绑定SessionFactory 20 * @return 21 */ 22 private static SessionFactory buildSessionFactory(){ 23 // 实例化配置文件 24 Configuration configuration=new Configuration().configure(); 25 // 实例化服务登记 26 ServiceRegistry serviceRegistry=new StandardServiceRegistryBuilder().applySettings(configuration.getProperties()).build(); 27 //获取Session工厂 28 return configuration.buildSessionFactory(serviceRegistry); 29 } 30 31 public static SessionFactory getSessionFactory(){ 32 return sessionFactory; 33 } 34 }
第二节:XML 版CRUD 实现
导入对应的hibernate的jar包、mysql的jar包和添加每次都需要用到的HibernateUtil.java
Student.java
1 package com.wishwzp.model; 2 3 public class Student { 4 5 private long id; 6 private String name; 7 8 public long getId() { 9 return id; 10 } 11 public void setId(long id) { 12 this.id = id; 13 } 14 public String getName() { 15 return name; 16 } 17 public void setName(String name) { 18 this.name = name; 19 } 20 @Override 21 public String toString() { 22 return "Student [id=" + id + ", name=" + name + "]"; 23 } 24 25 26 }
Student.hbm.xml
1 <?xml version="1.0"?> 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="com.wishwzp.model"> 7 8 <class name="Student" table="t_student"> 9 <id name="id" column="stuId"> 10 <generator class="native"></generator> 11 </id> 12 13 <property name="name"></property> 14 </class> 15 16 </hibernate-mapping>
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 6 <hibernate-configuration> 7 8 <session-factory> 9 10 <!--数据库连接设置 --> 11 <property name="connection.driver_class">com.mysql.jdbc.Driver</property> 12 <property name="connection.url">jdbc:mysql://localhost:3306/hibernate</property> 13 <property name="connection.username">root</property> 14 <property name="connection.password">123456</property> 15 16 17 <!-- 方言 --> 18 <property name="dialect">org.hibernate.dialect.MySQL5Dialect</property> 19 20 <!-- 控制台显示SQL --> 21 <property name="show_sql">true</property> 22 23 <!-- 自动更新表结构 --> 24 <property name="hbm2ddl.auto">update</property> 25 26 <!-- XML 版CRUD 实现 --> 27 <mapping resource="com/wishwzp/model/Student.hbm.xml"/> 28 29 </hibernate-configuration>
StudentTest.java
1 package com.wishwzp.service; 2 3 import java.util.List; 4 5 import org.hibernate.Query; 6 import org.hibernate.Session; 7 import org.hibernate.SessionFactory; 8 9 import com.wishwzp.model.Student; 10 import com.wishwzp.util.HibernateUtil; 11 12 public class StudentTest { 13 14 private SessionFactory sessionFactory=HibernateUtil.getSessionFactory(); 15 16 /** 17 * 添加 18 */ 19 private void add(){ 20 // 生成一个session 21 Session session=sessionFactory.openSession(); 22 // 开启事务 23 session.beginTransaction(); 24 25 Student s=new Student(); 26 s.setName("张三"); 27 session.save(s); 28 29 // 提交事务 30 session.getTransaction().commit(); 31 // 关闭session 32 session.close(); 33 } 34 35 /** 36 * 删除 37 */ 38 private void delete(){ 39 // 生成一个session 40 Session session=sessionFactory.openSession(); 41 // 开启事务 42 session.beginTransaction(); 43 44 Student student=(Student)session.get(Student.class, Long.valueOf(1)); 45 session.delete(student); 46 47 // 提交事务 48 session.getTransaction().commit(); 49 // 关闭session 50 session.close(); 51 } 52 53 /** 54 * 更新 55 */ 56 private void update(){ 57 // 生成一个session 58 Session session=sessionFactory.openSession(); 59 // 开启事务 60 session.beginTransaction(); 61 62 Student student=(Student)session.get(Student.class, Long.valueOf(2)); 63 student.setName("张三2"); 64 session.save(student); 65 66 // 提交事务 67 session.getTransaction().commit(); 68 // 关闭session 69 session.close(); 70 } 71 72 /** 73 * 查询 74 */ 75 private void getAllStudent(){ 76 // 生成一个session 77 Session session=sessionFactory.openSession(); 78 // 开启事务 79 session.beginTransaction(); 80 81 String hql="from Student"; 82 Query query=session.createQuery(hql); 83 List<Student> studentList=query.list(); 84 for(Student student:studentList){ 85 System.out.println(student); 86 } 87 88 // 提交事务 89 session.getTransaction().commit(); 90 // 关闭session 91 session.close(); 92 } 93 94 public static void main(String[] args) { 95 StudentTest studentTest=new StudentTest(); 96 studentTest.add(); 97 // studentTest.delete(); 98 // studentTest.update(); 99 // studentTest.getAllStudent(); 100 } 101 }
第三节:注解版CRUD 实现
导入对应的hibernate的jar包、mysql的jar包和添加每次都需要用到的HibernateUtil.java
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 6 <hibernate-configuration> 7 8 <session-factory> 9 10 <!--数据库连接设置 --> 11 <property name="connection.driver_class">com.mysql.jdbc.Driver</property> 12 <property name="connection.url">jdbc:mysql://localhost:3306/hibernate</property> 13 <property name="connection.username">root</property> 14 <property name="connection.password">123456</property> 15 16 17 <!-- 方言 --> 18 <property name="dialect">org.hibernate.dialect.MySQL5Dialect</property> 19 20 <!-- 控制台显示SQL --> 21 <property name="show_sql">true</property> 22 23 <!-- 自动更新表结构 --> 24 <property name="hbm2ddl.auto">update</property> 25 26 <!-- 注解版CRUD 实现 --> 27 <mapping class="com.wishwzp.model.Teacher"/> 28 </session-factory> 29 30 </hibernate-configuration>
Teacher.java
1 package com.wishwzp.model; 2 3 import javax.persistence.Entity; 4 import javax.persistence.GeneratedValue; 5 import javax.persistence.Id; 6 import javax.persistence.Table; 7 8 import org.hibernate.annotations.GenericGenerator; 9 10 //实体 11 @Entity 12 //这个是你指定是数据库哪张表,我这里指定的是t_teacher 13 @Table(name="t_teacher") 14 public class Teacher { 15 16 private long id; 17 private String name; 18 19 @Id//主键 20 @GeneratedValue(generator="_native")//这里开始使用名为_native的策略 21 @GenericGenerator(name="_native",strategy="native")//这里定义一个名为_native的策略,是native 22 public long getId() { 23 return id; 24 } 25 public void setId(long id) { 26 this.id = id; 27 } 28 public String getName() { 29 return name; 30 } 31 public void setName(String name) { 32 this.name = name; 33 } 34 @Override 35 public String toString() { 36 return "Teacher [id=" + id + ", name=" + name + "]"; 37 } 38 39 40 }
TeacherTest.java
1 package com.wishwzp.service; 2 3 import java.util.List; 4 5 import org.hibernate.Query; 6 import org.hibernate.Session; 7 import org.hibernate.SessionFactory; 8 9 import com.wishwzp.model.Teacher; 10 import com.wishwzp.util.HibernateUtil; 11 12 public class TeacherTest { 13 14 private SessionFactory sessionFactory=HibernateUtil.getSessionFactory(); 15 16 public void add(){ 17 Session session=sessionFactory.openSession(); // 生成一个session 18 session.beginTransaction(); // 开启事务 19 20 Teacher s=new Teacher(); 21 s.setName("张三"); 22 session.save(s); 23 24 session.getTransaction().commit(); // 提交事务 25 session.close(); // 关闭session 26 } 27 28 public void delete(){ 29 Session session=sessionFactory.openSession(); // 生成一个session 30 session.beginTransaction(); // 开启事务 31 32 Teacher Teacher=(Teacher)session.get(Teacher.class, Long.valueOf(1)); 33 session.delete(Teacher); 34 35 session.getTransaction().commit(); // 提交事务 36 session.close(); // 关闭session 37 } 38 39 public void update(){ 40 Session session=sessionFactory.openSession(); // 生成一个session 41 session.beginTransaction(); // 开启事务 42 43 Teacher Teacher=(Teacher)session.get(Teacher.class, Long.valueOf(2)); 44 Teacher.setName("张三2"); 45 session.save(Teacher); 46 47 session.getTransaction().commit(); // 提交事务 48 session.close(); // 关闭session 49 } 50 51 public void getAllTeacher(){ 52 Session session=sessionFactory.openSession(); // 生成一个session 53 session.beginTransaction(); // 开启事务 54 55 String hql="from Teacher"; 56 Query query=session.createQuery(hql); 57 List<Teacher> TeacherList=query.list(); 58 for(Teacher Teacher:TeacherList){ 59 System.out.println(Teacher); 60 } 61 62 session.getTransaction().commit(); // 提交事务 63 session.close(); // 关闭session 64 } 65 66 public static void main(String[] args) { 67 TeacherTest TeacherTest=new TeacherTest(); 68 // TeacherTest.add(); 69 // TeacherTest.delete(); 70 // TeacherTest.update(); 71 TeacherTest.getAllTeacher(); 72 } 73 }
注释版的我们就不需要在去配置XXX.hbm.xml了。
第四节:Junit4 单元测试框架引入
导入对应的junit包
百度云:http://pan.baidu.com/s/1dFBbhhZ
密码:3z1i
在测试版CRUD的基础上,我们运用junit重写了运行主类mian()。
TeacherTest2.java
1 package com.wishwzp.service; 2 3 4 5 import java.util.List; 6 7 import org.hibernate.Query; 8 import org.hibernate.Session; 9 import org.hibernate.SessionFactory; 10 import org.junit.Test; 11 12 import com.wishwzp.model.Teacher; 13 import com.wishwzp.util.HibernateUtil; 14 15 public class TeacherTest2 { 16 17 private SessionFactory sessionFactory=HibernateUtil.getSessionFactory(); 18 19 @Test 20 public void testAdd() { 21 Session session=sessionFactory.openSession(); // 生成一个session 22 session.beginTransaction(); // 开启事务 23 24 Teacher s=new Teacher(); 25 s.setName("张三"); 26 session.save(s); 27 28 session.getTransaction().commit(); // 提交事务 29 session.close(); // 关闭session 30 } 31 32 @Test 33 public void testDelete() { 34 Session session=sessionFactory.openSession(); // 生成一个session 35 session.beginTransaction(); // 开启事务 36 37 Teacher Teacher=(Teacher)session.get(Teacher.class, Long.valueOf(2)); 38 session.delete(Teacher); 39 40 session.getTransaction().commit(); // 提交事务 41 session.close(); // 关闭session 42 } 43 44 @Test 45 public void testUpdate() { 46 Session session=sessionFactory.openSession(); // 生成一个session 47 session.beginTransaction(); // 开启事务 48 49 Teacher Teacher=(Teacher)session.get(Teacher.class, Long.valueOf(3)); 50 Teacher.setName("张三2"); 51 session.save(Teacher); 52 53 session.getTransaction().commit(); // 提交事务 54 session.close(); // 关闭session 55 } 56 57 @Test 58 public void testGetAllTeacher() { 59 Session session=sessionFactory.openSession(); // 生成一个session 60 session.beginTransaction(); // 开启事务 61 62 String hql="from Teacher"; 63 Query query=session.createQuery(hql); 64 List<Teacher> TeacherList=query.list(); 65 for(Teacher Teacher:TeacherList){ 66 System.out.println(Teacher); 67 } 68 69 session.getTransaction().commit(); // 提交事务 70 session.close(); // 关闭session 71 } 72 73 }