1,MyEclipse新建
HibernateSessionFactory就是用来解析hibwenate这个文件的,解析完之后调用getSession()方法来获取session对象
<?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">
<!-- Generated by MyEclipse Hibernate Tools. -->
<hibernate-configuration>
<session-factory>
<!-- 方言(链接什么样的数据库) -->
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<!-- 连接url地址 -->
<property name="connection.url">jdbc:mysql://localhost:3306/jd</property>
<!-- 连接用户名 -->
<property name="connection.username">root</property>
<!-- 连接用户密码 -->
<property name="connection.password">root</property>
<!-- 名称 -->
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="myeclipse.connection.profile">jd</property>
<!-- 执行的时候显示sql语句 -->
<property name="show_sql">true</property>
</session-factory>
</hibernate-configuration>
2,切换至数据库页面
接下来切换到java页面,会自动生成如图所示的内容
自动生成user
package hiber01.domain;
/**
* User entity. @author MyEclipse Persistence Tools
*/
public class User extends AbstractUser implements java.io.Serializable {
// Constructors
/** default constructor */
public User() {
}
/** minimal constructor */
public User(Integer id) {
super(id);
}
/** full constructor */
public User(Integer id, String name, Integer age) {
super(id, name, age);
}
}
4,userDemo
package hiber01.util;
import hiber01.domain.User;
import org.hibernate.Session;
public class UserDemo {
public static void main(String[] args) {
//获取session对象
Session session=HibernateSessionFactory.getSession();
//创建一个实体
User user=new User("zhao",21);
session.getTransaction().begin();
//保存数据
session.save(user);
session.getTransaction().commit();
}
}