Hibernate实现CRUD操作
public class MyCRUD {
public static void main(String[] args) {
System.out.println("1.查询");
System.out.println("2.新增");
System.out.println("3.更新");
System.out.println("4.删除");
// System.out.println("5.关联映射");
int n;
Scanner cin = new Scanner(System.in);
n= cin.nextInt();
switch(n) {
case 1:{
System.out.println("输入学号");
int c = cin.nextInt();
SelectDao select = new SelectDao();
select.testSelect(c);
break;
}
case 2:{
System.out.println("输入姓名");
String name = cin.next();
System.out.println("输入年龄");
int age = cin.nextInt();
System.out.println("输入成绩");
int score = cin.nextInt();
Student stu = new Student(name,age,score);
SaveDao save = new SaveDao();
save.testSave(stu);
break;
}
case 3:{
System.out.println("输入学号");
int c = cin.nextInt();
System.out.println("输入新名字");
String name = cin.next();
UpdateDao update = new UpdateDao();
update.testUpdate(c, name);
break;
}
case 4:{
System.out.println("输入学号");
int c = cin.nextInt();
DeleteDao delete = new DeleteDao();
delete.testDel(c);
break;
}
default:{
System.out.println("输入错误");
}}}
查询方法
public class SelectDao {
public void testSelect(int n_id){
Configuration configure = new Configuration().configure().addClass(Student.class);
ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder().applySettings(configure.getProperties()).build();
SessionFactory sessionFactory = configure.buildSessionFactory(serviceRegistry);
Session session = sessionFactory.openSession();
try{
Transaction t = session.beginTransaction();
//Student student = session.load(Student.class,n_id);
Query query = session.createQuery("select s.name, s.age, s.score from Student s where s.id=?");
query.setParameter(0, n_id);
List<Object[]> ss = query.list();
for(Object[] s: ss) {
System.out.println("姓名是:"+s[0]);
System.out.println("年龄是:"+s[1]);
System.out.println("分数是:"+s[2]);
}
t.commit();
}catch(Exception e){
e.printStackTrace();
session.getTransaction().markRollbackOnly();
}finally {
session.close();
sessionFactory.close();
}}}
保存方法
public class SaveDao {
public void testSave(Student stu){
Configuration configure = new Configuration().configure().addClass(Student.class);
ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder().applySettings(configure.getProperties()).build();
SessionFactory sessionFactory = configure.buildSessionFactory(serviceRegistry);
Session session = sessionFactory.openSession();
try{
Transaction t = session.beginTransaction();
session.save(stu);//持久态
System.out.println("保存成功");
t.commit();
}catch(Exception e){
e.printStackTrace();
session.getTransaction().markRollbackOnly();
}finally {
session.close();
sessionFactory.close();
}}}
更新方法
public class UpdateDao {
public void testUpdate(int n_id,String new_name){
Configuration configure = new Configuration().configure().addClass(Student.class);
ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder().applySettings(configure.getProperties()).build();
SessionFactory sessionFactory = configure.buildSessionFactory(serviceRegistry);
Session session = sessionFactory.openSession();
//Student student = new Student("Levi",18,100);//瞬时态
try{
Transaction t = session.beginTransaction();
Student student = session.get(Student.class,n_id);
student.setName(new_name);
session.saveOrUpdate(student);//保存并更新
t.commit();
System.out.println("姓名更新成功");
}catch(Exception e){
e.printStackTrace();
session.getTransaction().markRollbackOnly();
}finally {
session.close();
sessionFactory.close();
}}}
删除方法
public class DeleteDao {
public void testDel(int n_id){
Configuration configure = new Configuration().configure().addClass(Student.class);
ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder().applySettings(configure.getProperties()).build();
SessionFactory sessionFactory = configure.buildSessionFactory(serviceRegistry);
Session session = sessionFactory.openSession();
try{
Transaction t = session.beginTransaction();
Student student = session.get(Student.class, n_id);//持久态
session.delete(student);
t.commit();
System.out.println("删除成功");
}catch(Exception e){
e.printStackTrace();
session.getTransaction().markRollbackOnly();
}finally {
session.close();
sessionFactory.close();
}}}