1.构建数据库连接
2.新建maven项目,利用工具生成hibernate相应的类和xml文件
新建pojo包
右击项目 点击Configure Facets 选择hibernate
选择包
选择驱动
选择jar包
3.数据库反向生成类和xml文件
pom.xml添加mysql的驱动包
<!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java --> ???<dependency> ???????<groupId>mysql</groupId> ???????<artifactId>mysql-connector-java</artifactId> ???????<version>5.1.38</version> ???</dependency>
4.使用junit创建测试类
package com.hwua.test;import java.io.Serializable;import org.hibernate.Session;import org.hibernate.SessionFactory;import org.hibernate.cfg.Configuration;import org.junit.Test;import com.hwua.pojo.Emps;public class Test1 { ???@Test ???public void save(){ ???????//1.读取配置文件 ????????//1.1 configure()方法,默认加载src目录下的hibernate.cfg.xml ???????Configuration cfg=new Configuration().configure(); ???????????????//1.2如果配置文件不符合加载规则,可以使用以下两种方式加载 ???????//Configuration cfg=new Configuration().configure(file); ???????//Configuration cfg=new Configuration().configure(path); ???????????????//1.3可以使用Configuration对象加载映射文件(不推荐) ???????//cfg.addClass(Emps.class); ???????//推荐hibernate.cfg.xml 使用mapping属性引入配置文件(自动生成类的时候已经引入) ???????????????//2.创建SessionFactroy对象 ???????//SessionFactory 创建session的工厂 ???????SessionFactory factory=cfg.buildSessionFactory(); ???????????????//3.获取session对象 ???????//3.1 获得一个全新的session; ???????Session session=factory.openSession(); ???????//3.2 获得当前的session,本地线程绑定的session,没有session的时候创建一个新的 ???????//Session currentSession = factory.getCurrentSession(); ???????????????//4.操作数据库 ???????Emps emp=new Emps(); ???????emp.setEname("Tom"); ???????emp.setAge(20); ???????emp.setSex("男"); ???????emp.setDeptno(30); ???????emp.setSalary(10000); ???????session.save(emp); ???????//5.关闭资源 ???????session.close(); ???????factory.close(); ???}}
5.相关属性介绍
5.1主键生成策略:
http://www.cnblogs.com/flyoung2008/articles/2165759.html
5.2 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"><!-- Generated by MyEclipse Hibernate Tools. ??????????????????--><hibernate-configuration><session-factory> ???<property name="myeclipse.connection.profile">mysql_test</property> ???<property name="dialect"> ???????org.hibernate.dialect.MySQLDialect ???</property> ???<property name="connection.password">oracle</property> ???<property name="connection.username">root</property> ???<property name="connection.url"> ???????jdbc:mysql://localhost:3306/test ???</property> ???<property name="connection.driver_class"> ???????com.mysql.jdbc.Driver ???</property> ???<!-- 自动生成表结构 --> ???<property name="hbm2ddl.auto">update</property> ???<!-- 将session与线程绑定, 只有配置了该配置,才能使用currentSession--> ???<property name="hibernate.connection.autocommit">thread</property> ???<property name="show_sql">true</property> ???<property name="format_sql">true</property> ???<mapping resource="com/hwua/pojo/Locs.hbm.xml" /> ???<mapping resource="com/hwua/pojo/Depts.hbm.xml" /> ???<mapping resource="com/hwua/pojo/Emps.hbm.xml" /></session-factory></hibernate-configuration>
hibernate在maven中自动生成
原文地址:https://www.cnblogs.com/xiaoaofengyue/p/8280118.html