Java笔记之hibernate(二):增删改查
0.说在前面
1.新增
1.1 新建SaveTest类
package com.hibernate.demo.test; import org.hibernate.SessionFactory; import org.hibernate.Transaction; import org.hibernate.cfg.Configuration; import org.hibernate.classic.Session; import com.hibernate.demo.bean.Employee; public class SaveTest { public static void main(String[] args) { //加载配置文件,创建会话工厂类 SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory(); //创建会话对象 Session session = sessionFactory.openSession(); //开启事务 Transaction transaction = session.beginTransaction(); //新建Employee对象 Employee employee=new Employee(); employee.setEmpName("emp111"); //保存对象 session.save(employee); //提交事务 transaction.commit(); //关闭会话对象 session.close(); //关闭会话工厂对象 sessionFactory.close(); } }
1.2 运行SaveTest类
SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder". SLF4J: Defaulting to no-operation (NOP) logger implementation SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details. Hibernate: insert into t_employee (emp_name) values (?)
1.3 查看数据库
2.查询
2.1 新建GetTest类
package com.hibernate.demo.test; import org.hibernate.SessionFactory; import org.hibernate.cfg.Configuration; import org.hibernate.classic.Session; import com.hibernate.demo.bean.Employee; public class GetTest { public static void main(String[] args) { //加载配置文件,创建会话工厂对象 SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory(); //创建会话对象 Session session = sessionFactory.openSession(); //根据id获取Employee对象 Employee employee = (Employee) session.get(Employee.class, 1); System.out.println(employee); //关闭会话对象 session.close(); //关闭会话工厂对象 sessionFactory.close(); } }
2.2 运行GetTest类
SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder". SLF4J: Defaulting to no-operation (NOP) logger implementation SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details. Hibernate: select employee0_.emp_id as emp1_0_0_, employee0_.emp_name as emp2_0_0_ from t_employee employee0_ where employee0_.emp_id=? Employee [empId=1, empName=张三]
3.修改
3.1新建UpdateTest类
package com.hibernate.demo.test; import org.hibernate.SessionFactory; import org.hibernate.Transaction; import org.hibernate.cfg.Configuration; import org.hibernate.classic.Session; import com.hibernate.demo.bean.Employee; public class UpdateTest { public static void main(String[] args) { //加载配置文件,创建会话工厂对象 SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory(); //创建会话对象 Session session = sessionFactory.openSession(); //开启事务 Transaction transaction = session.beginTransaction(); //根据id获取Employee对象 Employee employee = (Employee) session.get(Employee.class, 1); System.out.println(employee); //修改信息 employee.setEmpName("张三plus"); //更新数据库信息 session.update(employee); //提交事务 transaction.commit(); //再根据id去获取Employee对象 Employee employee2 = (Employee) session.get(Employee.class, 1); System.out.println(employee2); //关闭会话对象 session.close(); //关闭会话工厂对象 sessionFactory.close(); } }
3.2 运行UpdateTest类
SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder". SLF4J: Defaulting to no-operation (NOP) logger implementation SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details. Hibernate: select employee0_.emp_id as emp1_0_0_, employee0_.emp_name as emp2_0_0_ from t_employee employee0_ where employee0_.emp_id=? Employee [empId=1, empName=张三] Hibernate: update t_employee set emp_name=? where emp_id=? Employee [empId=1, empName=张三plus]
4.删除
4.1创建DeleteTest类
package com.hibernate.demo.test; import org.hibernate.SessionFactory; import org.hibernate.Transaction; import org.hibernate.cfg.Configuration; import org.hibernate.classic.Session; import com.hibernate.demo.bean.Employee; public class DeleteTest { public static void main(String[] args) { //加载配置文件,创建会话工厂对象 SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory(); //创建会话对象 Session session = sessionFactory.openSession(); //开启事务 Transaction transaction = session.beginTransaction(); //根据id获取Employee对象 Employee employee = (Employee) session.get(Employee.class, 1); System.out.println(employee); //删除 session.delete(employee); //提交事务 transaction.commit(); //根据id再去获取Employee对象 Employee employee2 = (Employee) session.get(Employee.class, 1); System.out.println(employee2); //关闭会话对象 session.close(); //关闭会话工厂对象 sessionFactory.close(); } }
4.2运行DeleteTest类
SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder". SLF4J: Defaulting to no-operation (NOP) logger implementation SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details. Hibernate: select employee0_.emp_id as emp1_0_0_, employee0_.emp_name as emp2_0_0_ from t_employee employee0_ where employee0_.emp_id=? Employee [empId=1, empName=张三plus] Hibernate: delete from t_employee where emp_id=? Hibernate: select employee0_.emp_id as emp1_0_0_, employee0_.emp_name as emp2_0_0_ from t_employee employee0_ where employee0_.emp_id=? null