创建数据库表
create table user
(
id int auto_increment
primary key,
username varchar(50) null,
password varchar(50) null,
name varchar(50) null
);
查看表
创建一个Java项目使用Maven
写pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.example</groupId>
<artifactId>Hibernate</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
</properties>
<dependencies>
<!-- https://mvnrepository.com/artifact/org.hibernate/hibernate-core -->
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>5.4.31.Final</version>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.12</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.6</version>
</dependency>
</dependencies>
</project>
创建实体类User
package main;
public class User {
private int id;
private String userName;
private String passWord;
private String name;
public int getId() {
return id;
}
public void setId(int 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 String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return "User{" +
"id=" + id +
", userName='" + userName + '\'' +
", passWord='" + passWord + '\'' +
", name='" + name + '\'' +
'}';
}
}
编写hibernate.cfg.xml
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property><!-- mysql驱动 -->
<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/spring?serverTimezone=Asia/Shanghai</property><!-- 数据库连接,填上面IDEA连数据库的那个界面里的URL -->
<property name="hibernate.connection.username">root</property><!-- MySQL用户名 -->
<property name="hibernate.connection.password">123456</property><!-- MySLQ密码 -->
<property name="show_sql">true</property>
<mapping resource="dao/user.hbm.xml"/><!-- 选择User的配置文件 -->
</session-factory>
</hibernate-configuration>
编写user.hbm.xml
<?xml version="1.0"?>
<!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="main.User" table="user">
<id name="id" column="id">
<generator class="native"/>
</id>
<property name="userName" column="username"/>
<property name="passWord" column="password"/>
<property name="name" column="name"/>
</class>
</hibernate-mapping>
编写测试是类
package dao;
import main.User;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
public class mainUser {
public static void main(String[] args){
Configuration cfg = null;
SessionFactory sf = null;
Session session = null;
Transaction ts = null;
User u = new User();
u.setUserName("wangwu");
u.setPassWord("123456");
u.setName("王五");
try {
cfg = new Configuration().configure();
sf = cfg.buildSessionFactory();
session = sf.openSession();
ts = session.beginTransaction();
session.save(u);
ts.commit();
}catch (HibernateException e){
e.printStackTrace();
if(ts != null){
ts.rollback();
}
}finally {
session.close();
sf.close();
}
}
}