由于博主喜欢使用IntelliJ IDEA 开发,个人觉得IntelliJ IDEA 功能太强大了,所以很喜欢。如果是Eclipse操作Hibernate就需要做一系列烦絮的导包工作。下面我将用IntelliJ IDEA 使用Hibernate框架,创建一个简单对数据库插入信息案例。
目录
五、创建一个测试test类,对Hibernate对数据库操作进行测试
整体项目结构:
一、数据建库
1、数据库使用mysql,在mysql创建数据库名为“hiberbate”(由于过于慌张,本来数据库名叫Hibernate的,将就用吧)
2、创建一个表“Student”
二、用IDEA创建Hibernate项目
1、创建新项目窗口中,点击java,勾选web Application,鼠标向下滑动勾选Hibernate,再勾选上Create degault hibernate configuration and main class就会自动生成hibernate.cfg.xml文件
点击next创建项目名字(本次项目取名为Hibernate01),点击finish。Hibernate项目创建成功,IDEA自动在项目中创建lib文件夹,并且下载好了Hibernate相关的包
三、引用数据库mysql到项目中
1、点击IDEA右上角的Database
2、点击+号,展开点击Mysql
3、创建MySQL连接,完成后点击Apply,点击ok
完成后就出现下图
4、映射数据库到项目中,点击IDEA左下角persistence,鼠标右击项目名Hibernate01,
根据箭头指示点击By Database Schema。
出现如下图:
随后在指定包中自动生成映射文件:
四、配置hibernate.cfg.xml文件
<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="connection.url">jdbc:mysql://localhost:3306/hiberbate</property>
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="connection.username">root</property>
<property name="connection.password">15282896494</property>
<!--配置本地事务-->
<property name="hibernate.current_session_context_class">thread</property>
<!--配置数据库方言-->
<property name="dialect">org.hibernate.dialect.MySQL5Dialect</property>
<property name="show_sql">true</property>
<property name="format_sql">true</property>
<!--指定自动生成数据表的策略-->
<property name="hbm2ddl.auto">update</property>
<!--映射文件-->
<mapping resource="yongjie/ben/StudentEntity.hbm.xml"></mapping>
</session-factory>
</hibernate-configuration>
五、创建一个测试test类,对Hibernate对数据库操作进行测试
Test.java文件
package yongjie.ben;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
class majorTest {
private static SessionFactory sessionFactory;
private static Session session;
private static Transaction transaction;
public static void main(String[] args){
//1. 创建配置对象
Configuration config = new Configuration().configure("/hibernate.cfg.xml");
//3. 创建会话工厂对象
sessionFactory = config.buildSessionFactory();
//4. 会话对象
session = sessionFactory.openSession();
//5. 开启事务
transaction = session.beginTransaction();
//6. 生成专业对象
StudentEntity studentEntity = new StudentEntity();
//新增
studentEntity.setId(11);
studentEntity.setName("11");
studentEntity.setPassword("11");
studentEntity.setAddress("11");
studentEntity.setSex("11");
//7. 保存对象进入数据库
session.save(studentEntity);
//8. 提交事务
transaction.commit();
//9. 关闭会话
session.close();
}
}
运行通过后,控制台出现如下图
刷新数据库,出现添加的数据。
六、遇到的坑
1、出现报错:Unable to create requested service [org.hibernate.engine.jdbc.env.spi.JdbcEnvironment]
检查hibernate.cfg.xml文件是否输入完整,数据库名字和密码是否输入正确。
2、方言在Hibernate5以上用
<property name="dialect">org.hibernate.dialect.MySQL5Dialect</property>
3、com.mysql.jdbc.Driver报错未找到包,需要问们手动导入。
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>