1.创建Maven项目,选择Archetype为archetype-maven-quickstart
2.添加mysql依赖
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.0.2</version>
</dependency>
注意:版本对应
3.添加Hibernate依赖
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>5.4.1.Final</version>
</dependency>
4.添加Eclipse Hibernate插件
打开Help->Install new software,输入:http://download.jboss.org/jbosstools/updates/stable/kepler/
选择:Jboos Web and Java EE Development下面的Hibernate Tools
5.创建Hibernate配置文件
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.password">123456</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost:3308/codingdict</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<mapping resource="com/codingdict/test_hibernate/Person.hbm.xml"/>
</session-factory>
</hibernate-configuration>
6.创建实体类
package com.codingdict.test_hibernate;
public class Person {
public Person() {
super();
// TODO Auto-generated constructor stub
}
public Person(int id, String name) {
super();
this.id = id;
this.name = name;
}
private int id;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
private String name;
}
7.创建映射文件
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!-- Generated 2019-3-13 13:27:21 by Hibernate Tools 3.4.0.CR1 -->
<hibernate-mapping>
<class name="com.codingdict.test_hibernate.Person" table="PERSON">
<id name="id" type="int">
<column name="ID" />
<generator class="assigned" />
</id>
<property name="name" type="java.lang.String">
<column name="NAME" />
</property>
</class>
</hibernate-mapping>
8.创建表
create table Person(id int auto_increment,name varchar(20));
9.测试
package com.codingdict.test_hibernate;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.boot.MetadataSources;
import org.hibernate.boot.registry.StandardServiceRegistry;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
public class App {
private static SessionFactory sessionFactory=null;
public static void main(String[] args) {
// A SessionFactory is set up once for an application!
final StandardServiceRegistry registry = new StandardServiceRegistryBuilder().configure()
.build();
try {
sessionFactory = new MetadataSources(registry).buildMetadata().buildSessionFactory();
sessionFactory.openSession();
Session session = sessionFactory.openSession();
Transaction tx = session.beginTransaction();
session.save(new Person(1,"tom"));
tx.commit();
} catch (Exception e) {
System.out.println(e);
}
}
}