Hibernate学习笔记(三)--基于xml配置文件的单表增删改查

一、目录结构

Hibernate学习笔记(三)--基于xml配置文件的单表增删改查

二、代码实现 

2.1测试类FirstDemo.java(com.myfirst)

import org.hibernate.Hibernate;
import org.hibernate.Session;
import org.hibernate.Transaction;

import com.myfirst.domain.Student;
import com.myfirst.util.HibernateUtil;

public class FirstDemo {
	public static void main(String[] args) {
		Session session = HibernateUtil.getSessionFactory().openSession();
		Transaction tx = session.beginTransaction();
		//Student students = new Student();
		Student s = new Student();
		s.setName("黄子涛");
		//students.setName("黄子涛");
		//students.setId(2);
		//session.update(students);
		session.delete(s);
		tx.commit();
		Student stu = (Student)session.get(Student.class,1);
		System.out.println(stu.getName()+stu.getId());
		session.close();
	}
	
}

2.2 实体类(com.myfirst.domain)

2.2.1实体类Student

public class Student {
	private Integer id;
	private String name;
	public Integer getId() {
		return id;
	}
	public void setId(Integer id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	
	
	
}

2.2.2Student.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="com.myfirst.domain">
 	<class name="Student" table="TB_STUDENT" >
 		<id name="id"  type="java.lang.Integer">
 			<column name="id"/>
 			<generator class="identity"/>
 		</id>
 		<property name="name" column="NAME"></property>
 	</class>
 </hibernate-mapping>

 2.3工具类HibernateUtil

 

import org.hibernate.SessionFactory;
import org.hibernate.boot.registry.StandardServiceRegistry;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.cfg.Configuration;

public class HibernateUtil {
	private static SessionFactory sf;
	static {
		Configuration config = new Configuration().configure();
		StandardServiceRegistry ssr = new StandardServiceRegistryBuilder().applySettings(config.getProperties()).build();
		
		sf= config.buildSessionFactory(ssr);
	}
	public static SessionFactory getSessionFactory() {
		return sf;
	}
}

2.4数据库文件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="connection.driver_class">com.mysql.jdbc.Driver</property>
			<property name="connection.url">jdbc:mysql://localhost:3306/db_student?characterEncoding=utf-8</property>
			<property name="connection.username">root</property>
			<property name="connection.password"></property>
			<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
 
 
			<!-- 显示sql语句 -->
			<property name="show_sql">true</property>
			<!-- 格式化代码 -->
			<property name="format_sql">true</property>
			<property name="hbm2ddl.auto">update</property>
			<mapping resource="com\myfirst\domain\Student.hbm.xml" />
		</session-factory>
		</hibernate-configuration>

2.5相关架包下载https://pan.baidu.com/s/1pneY6jMftQCFqkjtfLDVzQ