Hibernate注解版CRUD
注解版就是不需要类名.hbm.xml文件
直接在实体类注解,从而与数据库表、字段相关联
可以发现model这里并没有Teacher.hbm.xml文件
Teacher.java @的地方就是注解,使得实体类和表相关联
package com.java.model;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;
import org.hibernate.annotations.GenericGenerator;
@Entity //说明他是映射实体类
@Table(name="t_teather") //这个实体类映射到表t_teather
public class Teacher {
private String name;
private long id;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Id //代表他是主键
@GeneratedValue(generator="_native") //使用定义好的生成策略
@GenericGenerator(name="_native",strategy="native") //定义主键生成策略
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
@Override
public String toString() {
return "Teacher [name=" + name + ", id=" + id + "]";
}
}
TeacherTest
package com.java.service;
import java.util.List;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import com.java.model.Teacher;
import com.java.util.HibernateUtil;
public class TeacherTest {
private SessionFactory sessionFactory=HibernateUtil.getSessionFactory();
private void add(){
Session session=sessionFactory.openSession(); // 生成一个session
session.beginTransaction(); // 开启事务
Teacher t=new Teacher();
t.setName("张三");
session.save(t);
session.getTransaction().commit(); // 提交事务
session.close(); // 关闭session
}
private 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
}
private 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
}
private void getAllteacher(){
Session session=sessionFactory.openSession(); // 生成一个session
session.beginTransaction(); // 开启事务
//from一个类,不是表名
String hql="from teacher";
Query query=session.createQuery(hql);
List 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();
}
}
然后在hibernate.cfg.xml导入映射文件
com.mysql.jdbc.Driver
jdbc:mysql://localhost:3306/hibernate
root
123456
org.hibernate.dialect.MySQL5Dialect
true
update
结果
Junit4单元测试框架引入
因为每次测试增删查改,都不停删除添加注释,所以引入Junit4单元测试框
1、导入Junit4包,Build Path
选择Junit4
完成
2、使用Junit4要求增删查改的方法是public
package com.java.service;
import java.util.List;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import com.java.model.Teacher;
import com.java.util.HibernateUtil;
public class TeacherTest {
private SessionFactory sessionFactory=HibernateUtil.getSessionFactory();
public void add(){
Session session=sessionFactory.openSession(); // 生成一个session
session.beginTransaction(); // 开启事务
Teacher t=new Teacher();
t.setName("张三");
session.save(t);
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(); // 开启事务
//from一个类,不是表名
String hql="from Teacher";
Query query=session.createQuery(hql);
List 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();
}
}
3、使用方法:创建Junit Test Case
选择方法,这里不需要main
得到下面代码
"@Test”表明下面这个方法是一个测试方法,我们先删除自动生成的fail()函数的代码。添加以下代码
org.junit.Assert.*;就是静态导入的我们实现单元测试要用到的一些方法
package com.java.service;
import static org.junit.Assert.*;
import java.util.List;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.junit.Test;
import com.java.model.Teacher;
import com.java.util.HibernateUtil;
public class JunitTeacherTest {
private SessionFactory sessionFactory=HibernateUtil.getSessionFactory();
@Test
public void testAdd() {
Session session=sessionFactory.openSession(); // 生成一个session
session.beginTransaction(); // 开启事务
Teacher t=new Teacher();
t.setName("张三");
session.save(t);
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(1));
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(2));
teacher.setName("张三2");
session.save(teacher);
session.getTransaction().commit(); // 提交事务
session.close(); // 关闭session
}
@Test
public void testGetAllteacher() {
Session session=sessionFactory.openSession(); // 生成一个session
session.beginTransaction(); // 开启事务
//from一个类,不是表名
String hql="from Teacher";
Query query=session.createQuery(hql);
List teacherList=query.list();
for(Teacher teacher:teacherList){
System.out.println(teacher);
}
session.getTransaction().commit(); // 提交事务
session.close(); // 关闭session
}
}
然后测试里面的方法
就可以测试方法了
转载于:https://my.oschina.net/u/3848699/blog/2222725