idea配置hibernate环境
在WEB-INF目录下创建lib文件夹,导入前面索下载的jar包(下载文件解压后有一个lib文件夹,在lib文件夹下有一个required文件夹,将该文件夹下的所有jar包导入项目中)这里我用的是mysql5.5,导入对应的mysql驱动包(我所用的mysql驱动下载mysql5.1.32驱动下载),导入jar包后,右键lib,点击Add as Library
-
创建test数据库
-- 创建test数据库
DROP DATABASE IF EXISTS `student`;
CREATE DATABASE `student`;
-- 选择test数据库
USE test;
SET NAMES utf8mb4;
SET FOREIGN_KEY_CHECKS = 0;
-- 创建Student表
DROP TABLE IF EXISTS `student`;
CREATE TABLE `student` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(80) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
`sex` varchar(10) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
`birthday` date NULL DEFAULT NULL,
PRIMARY KEY (`id`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 1 CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Compact;
SET FOREIGN_KEY_CHECKS = 1;
-
在src下创建hibernate核心配置文件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.driver_class">com.mysql.jdbc.Driver</property>
<!--连接url-->
<property name="connection.url">jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=UTF-8</property>
<!--用户名-->
<property name="connection.username">root</property>
<!--密码-->
<property name="connection.password">root</property>
<!--数据库方言-->
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<!--显示SQL语句-->
<property name="show_sql">true</property>
<!--格式化SQL语句-->
<property name="format_sql">true</property>
<!--关系映射-->
<mapping class="entity.Student"/>
<mapping resource="entity/Student.hbm.xml"/>
</session-factory>
</hibernate-configuration>
-
student类
package entity;
import java.util.Date;
/**
* student实体类
*
* @author Hervery
*/
public class Student {
private int id;
private String name;
private String sex;
private Date birthday;
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;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public Date getBirthday() {
return birthday;
}
public void setBirthday(Date birthday) {
this.birthday = birthday;
}
}
-
Student.hbm.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="entity.Student" table="student">
<id name="id" column="id">
<generator class="identity"/>
</id>
<property name="name" column="name"/>
<property name="sex" column="sex"/>
<property name="birthday" column="birthday"/>
</class>
</hibernate-mapping>
-
StudentTest类
package test;
import entity.Student;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.boot.registry.StandardServiceRegistry;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.cfg.Configuration;
import java.text.ParseException;
import java.text.SimpleDateFormat;
/**
* @author Hervery
*/
public class StudentTest {
public static void main(String[] args) throws ParseException {
Configuration configure = new Configuration().configure();
StandardServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder().configure().build();
SessionFactory sessionFactory = configure.buildSessionFactory(serviceRegistry);
Session session = sessionFactory.openSession();
Student student = new Student();
student.setName("Tom");
student.setSex("male");
student.setBirthday(new SimpleDateFormat("yyyy-MM-dd").parse("2020-09-27"));
session.save(student);
session.beginTransaction().commit();
session.close();
sessionFactory.close();
serviceRegistry.close();
}
}
运行结果
现在我们打开数据库看看是否有记录
到这里,我们的hibernate环境就搭建完成了。