1、新建一个普通的java项目,按照上面的步骤引入相关的jar包和配置文件
2、建立User实体类
- import java.util.Date;
- public class User {
- private String id;
- private String username;
- private String password;
- private Date createTime;
- private Date expireTime;
- public String getId() {
- return id;
- }
- public void setId(String 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;
- }
- public Date getCreateTime() {
- return createTime;
- }
- public void setCreateTime(Date createTime) {
- this.createTime = createTime;
- }
- public Date getExpireTime() {
- return expireTime;
- }
- public void setExpireTime(Date expireTime) {
- this.expireTime = expireTime;
- }
- }
2、提供User.hbm.xml文件,完成实体类的映射
- <?xml version="1.0"?>
- <!DOCTYPE hibernate-mapping PUBLIC
- "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
- "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
- <hibernate-mapping>
- <class name="com.example.hibernate.User">
- <id name="id">
- <generator class="uuid"/>
- </id>
- <property name="username"/>
- <property name="password"/>
- <property name="createTime"/>
- <property name="expireTime"/>
- </class>
- </hibernate-mapping>
3、配置hibernate.cfg.xml文件
- <hibernate-configuration>
- <session-factory >
- <!-- mysql数据库驱动 -->
- <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
- <!-- mysql数据库名称 -->
- <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/hibernate_first</property>
- <!-- 数据库的登陆用户名 -->
- <property name="hibernate.connection.username">root</property>
- <!-- 数据库的登陆密码 -->
- <property name="hibernate.connection.password">root</property>
- <!-- 方言:为每一种数据库提供适配器,方便转换 -->
- <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
- <mapping resource="com/example/hibernate/User.hbm.xml"/>
- </session-factory>
- </hibernate-configuration>
4、生成表:编写工具类ExoprtDB.java,将hbm生成ddl
- import org.hibernate.cfg.Configuration;
- import org.hibernate.tool.hbm2ddl.SchemaExport;
- /**
- * 将hbm生成ddl
- * @author BCH
- *
- */
- public class ExoprtDB {
- public static void main(String[] args) {
- //默认读取hibernate.cfg.xml文件
- Configuration cfr = new Configuration().configure();
- SchemaExport export = new SchemaExport(cfr);
- export.create(true, true);
- }
- }
5、向表中添加数据
- import java.util.Date;
- import org.hibernate.Session;
- import org.hibernate.SessionFactory;
- import org.hibernate.cfg.Configuration;
- public class Client {
- public static void main(String[] args) {
- //读取配置文件
- Configuration cfg = new Configuration().configure();
- SessionFactory factory = cfg.buildSessionFactory();
- Session session = null;
- try{
- session = factory.openSession();
- //开启事务
- session.beginTransaction();
- User user = new User();
- user.setUsername("用户名");
- user.setPassword("123");
- user.setCreateTime(new Date());
- user.setExpireTime(new Date());
- session.save(user);
- //提交事务
- session.getTransaction().commit();
- }catch(Exception e){
- e.printStackTrace();
- //回滚事务
- session.getTransaction().rollback();
- }finally{
- if(session != null){
- if(session.isOpen()){
- //关闭session
- session.close();
- }
- }
- }
- }
- }