1、需要先创建学生实体:
package pers.zhb.domain; public class Student { private int studentno; private String sname; private String sex; private String birthday; private String classno; private float point; private String phone; private String email; @Override public String toString() { return "Student{" + "studentno='" + studentno + '\'' + ", sname='" + sname + '\'' + ", sex='" + sex + '\'' + ", birthday='" + birthday + '\'' + ", classno='" + classno + '\'' + ", point=" + point + ", phone='" + phone + '\'' + ", email='" + email + '\'' + '}'; } public int getStudentno() { return studentno; } public void setStudentno(int studentno) { this.studentno = studentno; } public String getSname() { return sname; } public void setSname(String sname) { this.sname = sname; } public String getSex() { return sex; } public void setSex(String sex) { this.sex = sex; } public String getBirthday() { return birthday; } public void setBirthday(String birthday) { this.birthday = birthday; } public String getClassno() { return classno; } public void setClassno(String classno) { this.classno = classno; } public float getPoint() { return point; } public void setPoint(float point) { this.point = point; } public String getPhone() { return phone; } public void setPhone(String phone) { this.phone = phone; } public String getEmail() { return email; } public void setEmail(String email) { this.email = email; } }
2、学生实体与学生表的映射文件(Student.hbm.xml):
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"> <hibernate-mapping package="pers.zhb.domain"> <class name="Student" table="student"> <id name="studentno" column="studentno"> <generator class="native"></generator> </id> <property name="birthday" column="birthday"></property> <property name="classno" column="classno"></property> <property name="email" column="email"></property> <property name="phone" column="phone"></property> <property name="sex" column="sex"></property> <property name="sname" column="sname"></property> <property name="point" column="point"></property> </class> </hibernate-mapping>
3、主配置文件(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="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property> <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/stu_mangement</property> <property name="hibernate.connection.username">root</property> <property name="hibernate.connection.password">root</property> <!--配置hibernate信息-可选的--> <property name="hibernate.show_sql">true</property><!--输出底层sql语句--> <property name="hibernate.format_sql">true</property><!--格式化输出sql语句--> <property name="hibernate.hbm2ddl.auto">create</property><!--hibernate帮助创建表,如果已经有表更新表, 如果没有则创建新表--> <property name="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</property> <!--配置数据库的方言,让hibernate识别框架自己的特有语句--> <!--把映射文件放到核心配置文件--> <mapping resource="pers/zhb/domain/Student.hbm.xml"/><!--都在src目录下--> </session-factory> </hibernate-configuration>
4、添加学生信息:
import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.Transaction; import org.hibernate.cfg.Configuration; import pers.zhb.domain.Student; class HinernateTest{ public static void testAdd() { //(1)加载hibernate核心配置文件 //src目录下的配置文件名称是固定的:hibernate.cfg.xml Configuration configuration= new Configuration(); configuration.configure(); //(2)读取hibernate核心配置文件的内容,创建sessionFactory对象,根据映射关系创建相应的表 SessionFactory sessionFactory=configuration.buildSessionFactory(); //(3)利用sessionFactory对象创建session对象 //类似于创建连接 Session session=sessionFactory.openSession(); //(4)开启事务 Transaction transaction=session.beginTransaction(); //(5)具体操作 Student student=new Student(); student.setSname("zzzz"); student.setSex("男"); student.setBirthday("2019-11-11"); student.setClassno("201911"); student.setPoint(111); student.setPhone("1873496511"); student.setEmail("20171514@qq.com"); session.save(student); //(6)提交事务 transaction.commit(); //(7)关闭资源 session.close(); sessionFactory.close(); } public static void main(String []args){ HinernateTest hinernateTest=new HinernateTest(); hinernateTest.testAdd(); } }
获取学生对象,并对该对象设置值。
5、查询学生信息(根据主键查询):
import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.Transaction; import org.hibernate.cfg.Configuration; import pers.zhb.domain.Student; public class HiberateSel { public static void testSel() { Configuration configuration = new Configuration(); configuration.configure(); SessionFactory sessionFactory = configuration.buildSessionFactory(); Session session = sessionFactory.openSession(); Transaction transaction = session.beginTransaction(); Student student=session.get(Student.class,824113307); System.out.println(student); transaction.commit(); session.close(); sessionFactory.close(); } public static void main(String []args){ HiberateSel hinernateSel=new HiberateSel(); hinernateSel.testSel(); } }
即直接获取学生对象。
6、修改学生信息:
import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.Transaction; import org.hibernate.cfg.Configuration; import pers.zhb.domain.Student; public class HibernateUpdate { public static void testUpdate() { Configuration configuration = new Configuration(); configuration.configure(); SessionFactory sessionFactory = configuration.buildSessionFactory(); Session session = sessionFactory.openSession(); Transaction transaction = session.beginTransaction(); //获得要修改的对象 Student student=session.get(Student.class,824113307); student.setEmail("201715@qq.com"); student.setClassno("1234567"); student.setPhone("18739496622"); student.setBirthday("2019-11-11"); student.setSex("女"); student.setSname("tom"); student.setPoint(666); transaction.commit(); session.close(); sessionFactory.close(); } public static void main(String []args){ HibernateUpdate hibernateUpdate=new HibernateUpdate(); hibernateUpdate.testUpdate(); } }
需要先通过主键获取要修改的学生对象,然后对相应的学生属性进行修改。
7、删除学生:
import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.Transaction; import org.hibernate.cfg.Configuration; import pers.zhb.domain.Student; public class HibernateDel{ public static void testDel() { Configuration configuration = new Configuration(); configuration.configure(); SessionFactory sessionFactory = configuration.buildSessionFactory(); Session session = sessionFactory.openSession(); Transaction transaction = session.beginTransaction(); //获得要修改的对象 Student student=session.get(Student.class,824113307); session.delete(student); transaction.commit(); session.close(); sessionFactory.close(); } public static void main(String []args){ HibernateDel hibernateDel=new HibernateDel(); hibernateDel.testDel(); } }
通过主键查找到学生对象,调用函数删除该学生。