所有的学习我们必须先搭建好hibernate的环境(1、导入对应的jar包,2、hibernate.cfg.xml,3、XXXX.hbm.xml)
第一节:HibernateUtil 封装
导入对应的hibernate的jar包和mysql的jar包
HibernateUtil.java
package com.wishwzp.util; import org.hibernate.SessionFactory;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry; /**
*
* hibernate封装
* @author wishwzp
*
*/
public class HibernateUtil { private static final SessionFactory sessionFactory=buildSessionFactory(); /**
* 绑定SessionFactory
* @return
*/
private static SessionFactory buildSessionFactory(){
// 实例化配置文件
Configuration configuration=new Configuration().configure();
// 实例化服务登记
ServiceRegistry serviceRegistry=new StandardServiceRegistryBuilder().applySettings(configuration.getProperties()).build();
//获取Session工厂
return configuration.buildSessionFactory(serviceRegistry);
} public static SessionFactory getSessionFactory(){
return sessionFactory;
}
}
第二节:XML 版CRUD 实现
导入对应的hibernate的jar包、mysql的jar包和添加每次都需要用到的HibernateUtil.java
Student.java
package com.wishwzp.model; public class Student { private long id;
private String name; public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return "Student [id=" + id + ", name=" + name + "]";
} }
Student.hbm.xml
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"> <hibernate-mapping package="com.wishwzp.model"> <class name="Student" table="t_student">
<id name="id" column="stuId">
<generator class="native"></generator>
</id> <property name="name"></property>
</class> </hibernate-mapping>
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">com.mysql.jdbc.Driver</property>
<property name="connection.url">jdbc:mysql://localhost:3306/hibernate</property>
<property name="connection.username">root</property>
<property name="connection.password">123456</property> <!-- 方言 -->
<property name="dialect">org.hibernate.dialect.MySQL5Dialect</property> <!-- 控制台显示SQL -->
<property name="show_sql">true</property> <!-- 自动更新表结构 -->
<property name="hbm2ddl.auto">update</property> <!-- XML 版CRUD 实现 -->
<mapping resource="com/wishwzp/model/Student.hbm.xml"/> </hibernate-configuration>
StudentTest.java
package com.wishwzp.service; import java.util.List; import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory; import com.wishwzp.model.Student;
import com.wishwzp.util.HibernateUtil; public class StudentTest { private SessionFactory sessionFactory=HibernateUtil.getSessionFactory(); /**
* 添加
*/
private void add(){
// 生成一个session
Session session=sessionFactory.openSession();
// 开启事务
session.beginTransaction(); Student s=new Student();
s.setName("张三");
session.save(s); // 提交事务
session.getTransaction().commit();
// 关闭session
session.close();
} /**
* 删除
*/
private void delete(){
// 生成一个session
Session session=sessionFactory.openSession();
// 开启事务
session.beginTransaction(); Student student=(Student)session.get(Student.class, Long.valueOf(1));
session.delete(student); // 提交事务
session.getTransaction().commit();
// 关闭session
session.close();
} /**
* 更新
*/
private void update(){
// 生成一个session
Session session=sessionFactory.openSession();
// 开启事务
session.beginTransaction(); Student student=(Student)session.get(Student.class, Long.valueOf(2));
student.setName("张三2");
session.save(student); // 提交事务
session.getTransaction().commit();
// 关闭session
session.close();
} /**
* 查询
*/
private void getAllStudent(){
// 生成一个session
Session session=sessionFactory.openSession();
// 开启事务
session.beginTransaction(); String hql="from Student";
Query query=session.createQuery(hql);
List<Student> studentList=query.list();
for(Student student:studentList){
System.out.println(student);
} // 提交事务
session.getTransaction().commit();
// 关闭session
session.close();
} public static void main(String[] args) {
StudentTest studentTest=new StudentTest();
studentTest.add();
// studentTest.delete();
// studentTest.update();
// studentTest.getAllStudent();
}
}
第三节:注解版CRUD 实现
导入对应的hibernate的jar包、mysql的jar包和添加每次都需要用到的HibernateUtil.java
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">com.mysql.jdbc.Driver</property>
<property name="connection.url">jdbc:mysql://localhost:3306/hibernate</property>
<property name="connection.username">root</property>
<property name="connection.password">123456</property> <!-- 方言 -->
<property name="dialect">org.hibernate.dialect.MySQL5Dialect</property> <!-- 控制台显示SQL -->
<property name="show_sql">true</property> <!-- 自动更新表结构 -->
<property name="hbm2ddl.auto">update</property> <!-- 注解版CRUD 实现 -->
<mapping class="com.wishwzp.model.Teacher"/>
</session-factory> </hibernate-configuration>
Teacher.java
package com.wishwzp.model; import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table; import org.hibernate.annotations.GenericGenerator; //实体
@Entity
//这个是你指定是数据库哪张表,我这里指定的是t_teacher
@Table(name="t_teacher")
public class Teacher { private long id;
private String name; @Id//主键
@GeneratedValue(generator="_native")//这里开始使用名为_native的策略
@GenericGenerator(name="_native",strategy="native")//这里定义一个名为_native的策略,是native
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return "Teacher [id=" + id + ", name=" + name + "]";
} }
TeacherTest.java
package com.wishwzp.service; import java.util.List; import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory; import com.wishwzp.model.Teacher;
import com.wishwzp.util.HibernateUtil; public class TeacherTest { private SessionFactory sessionFactory=HibernateUtil.getSessionFactory(); public void add(){
Session session=sessionFactory.openSession(); // 生成一个session
session.beginTransaction(); // 开启事务 Teacher s=new Teacher();
s.setName("张三");
session.save(s); session.getTransaction().commit(); // 提交事务
session.close(); // 关闭session
} public void delete(){
Session session=sessionFactory.openSession(); // 生成一个session
session.beginTransaction(); // 开启事务 Teacher Teacher=(Teacher)session.get(Teacher.class, Long.valueOf(1));
session.delete(Teacher); session.getTransaction().commit(); // 提交事务
session.close(); // 关闭session
} public void update(){
Session session=sessionFactory.openSession(); // 生成一个session
session.beginTransaction(); // 开启事务 Teacher Teacher=(Teacher)session.get(Teacher.class, Long.valueOf(2));
Teacher.setName("张三2");
session.save(Teacher); session.getTransaction().commit(); // 提交事务
session.close(); // 关闭session
} public void getAllTeacher(){
Session session=sessionFactory.openSession(); // 生成一个session
session.beginTransaction(); // 开启事务 String hql="from Teacher";
Query query=session.createQuery(hql);
List<Teacher> TeacherList=query.list();
for(Teacher Teacher:TeacherList){
System.out.println(Teacher);
} session.getTransaction().commit(); // 提交事务
session.close(); // 关闭session
} public static void main(String[] args) {
TeacherTest TeacherTest=new TeacherTest();
// TeacherTest.add();
// TeacherTest.delete();
// TeacherTest.update();
TeacherTest.getAllTeacher();
}
}
注释版的我们就不需要在去配置XXX.hbm.xml了。
第四节:Junit4 单元测试框架引入
导入对应的junit包
百度云:http://pan.baidu.com/s/1dFBbhhZ
密码:3z1i
在测试版CRUD的基础上,我们运用junit重写了运行主类mian()。
TeacherTest2.java
package com.wishwzp.service; import java.util.List; import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.junit.Test; import com.wishwzp.model.Teacher;
import com.wishwzp.util.HibernateUtil; public class TeacherTest2 { private SessionFactory sessionFactory=HibernateUtil.getSessionFactory(); @Test
public void testAdd() {
Session session=sessionFactory.openSession(); // 生成一个session
session.beginTransaction(); // 开启事务 Teacher s=new Teacher();
s.setName("张三");
session.save(s); session.getTransaction().commit(); // 提交事务
session.close(); // 关闭session
} @Test
public void testDelete() {
Session session=sessionFactory.openSession(); // 生成一个session
session.beginTransaction(); // 开启事务 Teacher Teacher=(Teacher)session.get(Teacher.class, Long.valueOf(2));
session.delete(Teacher); session.getTransaction().commit(); // 提交事务
session.close(); // 关闭session
} @Test
public void testUpdate() {
Session session=sessionFactory.openSession(); // 生成一个session
session.beginTransaction(); // 开启事务 Teacher Teacher=(Teacher)session.get(Teacher.class, Long.valueOf(3));
Teacher.setName("张三2");
session.save(Teacher); session.getTransaction().commit(); // 提交事务
session.close(); // 关闭session
} @Test
public void testGetAllTeacher() {
Session session=sessionFactory.openSession(); // 生成一个session
session.beginTransaction(); // 开启事务 String hql="from Teacher";
Query query=session.createQuery(hql);
List<Teacher> TeacherList=query.list();
for(Teacher Teacher:TeacherList){
System.out.println(Teacher);
} session.getTransaction().commit(); // 提交事务
session.close(); // 关闭session
} }