一:使用Hibernate对数据库进行增删改查操作
准备环境 eclipse+mysql5.3+hibernate5.3
Hibernate配置文件
<?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="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property> <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property> <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/hibernate?useUnicode=true&characterEncoding=UTF-8</property> <property name="hibernate.connection.username">root</property> <property name="hibernate.connection.password"></property> <property name="hibernate.show_sql">true</property> <property name="hibernate.format_sql">true</property> <mapping resource="com/hibernate/entity/User.hbm.xml"/> </session-factory> </hibernate-configuration>
Hibernate工具类
public class HibernateUtil { private static SessionFactory factory; private static StandardServiceRegistry registry; //得到sessionFactory static { try { //1.获取注册的register registry=new StandardServiceRegistryBuilder().configure().build(); //2.得到factory factory =new MetadataSources(registry).buildMetadata().buildSessionFactory(); }catch(Exception e) { e.printStackTrace(); StandardServiceRegistryBuilder.destroy(registry); } } //获得session对象 public static Session openSession() { return factory.openSession(); } //关闭sessionFactory public static void closeFactory() { factory.close(); } }
用户实体类:
package com.hibernate.entity; public class User { private Integer id; private String userName; private String passWord; public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getUserName() { return userName; } public void setUserName(String userName) { this.userName = userName; } public String getPassWord() { return passWord; } public void setPassWord(String passWord) { this.passWord = passWord; } @Override public String toString() { return "User [id=" + id + ", userName=" + userName + ", passWord=" + passWord + "]"; } }
用户配置文件:
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> <hibernate-mapping package="com.hibernate.entity"> <class name="User" table="t_user"> <id name="id" type="int" > <column name="id"></column> <generator class="native"></generator> </id> <property name="userName" column="user_name" type="java.lang.String"> </property> <property name="passWord" type="java.lang.String"></property> </class> </hibernate-mapping>
1.Hibernate增加操作
//持久化操作 public static void saveUser(User user) { Transaction tx = null; Session session = null; try { //1.获得session对象 session=HibernateUtil.openSession(); //2.获得要保存的实体对象 //3.保存实体 tx=session.beginTransaction(); session.save(user); tx.commit(); //4.关闭session }catch(Exception e) { tx.rollback(); }finally { session.close(); } }
2.Hibernate查询操作
public static User getUserById(int id) { Session session = null; User user = null; try { //1.获取session session=HibernateUtil.openSession(); //2.去查询 user=session.get(User.class, id);//Hibernate会包装成数据类型 //3.关闭session session.close(); }catch(Exception e) { e.printStackTrace(); }finally { session.close(); } return user; }
3.Hibernate修改操作
public static void updateUser(User user) { //先查询出用户然后再更新 Session session = null; Transaction tx=null; try { //1.先获取session对象 session=HibernateUtil.openSession(); //2.对数据进行更改 tx=session.beginTransaction(); user.setPassWord("456"); //3.进行保存更新操作 session.update(user); tx.commit(); }catch(Exception e){ e.printStackTrace(); tx.rollback(); }finally { //4.session关闭 session.close(); } }
4.Hibernate删除操作
public static void deleteUser(User user) { //先查询到对象后再删除 //1.打开session //2.删除用户 //3.关闭session Session session = null; Transaction tx=null; try { //1.先获取session对象 session=HibernateUtil.openSession(); //2.对数据进行更改 tx=session.beginTransaction(); session.delete(user); tx.commit(); }catch(Exception e){ e.printStackTrace(); tx.rollback(); }finally { //4.session关闭 session.close(); } } }