Hibernate实现数据库写入
项目目录
代码;
HibernateTest.java
package com.home.hibernate;
import static org.junit.Assert.*;
import javax.imageio.spi.ServiceRegistry;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.junit.Test;
public class HibernateTest {
@Test
public void test() {
Configuration configuration = new Configuration().configure();
org.hibernate.boot.registry.StandardServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder().applySettings(configuration.getProperties()).build();
SessionFactory sessionFactory = configuration.buildSessionFactory(serviceRegistry);
Session session = sessionFactory.openSession();
Transaction transaction = session.beginTransaction();
User user = new User("Sky","333","[email protected]","厦门");
session.save(user);
transaction.commit();
session.close();
sessionFactory.close();
}
}
User.java
package com.home.hibernate;
public class User {
private int userId;
private String userName;
private String password;
private String email;
private String address;
public int getUserId() {
return userId;
}
public void setUserId(int userId) {
this.userId = userId;
}
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 getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public User(String userName, String password, String email, String address) {
super();
this.userName = userName;
this.password = password;
this.email = email;
this.address = address;
}
public User() {
super();
}
@Override
public String toString() {
return "User [userId=" + userId + ", userName=" + userName + ", password=" + password + ", email=" + email
+ ", address=" + address + "]";
}
}
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">
<!-- Generated 2018-11-16 16:16:03 by Hibernate Tools 3.5.0.Final -->
<hibernate-mapping>
<class name="com.home.hibernate.User" table="USER">
<id name="userId" type="int">
<column name="USERID" />
<generator class="native" />
</id>
<property name="userName" type="java.lang.String">
<column name="USERNAME" />
</property>
<property name="password" type="java.lang.String">
<column name="PASSWORD" />
</property>
<property name="email" type="java.lang.String">
<column name="EMAIL" />
</property>
<property name="address" type="java.lang.String">
<column name="ADDRESS" />
</property>
</class>
</hibernate-mapping>
hibernate.cfg.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<!-- 1. 配置连接数据库的基本信息 -->
<property name="connection.username">root</property>
<property name="connection.password">root</property>
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="connection.url">jdbc:mysql:///test</property>
<!-- 2. 配置hibernate 的基本信息 -->
<!-- hibernate 所使用的数据库方言 -->
<property name="hibernate.dialect">org.hibernate.dialect.MySQLInnoDBDialect</property>
<!-- 执行操作时是否在控制台打印SQL -->
<property name="show_sql">true</property>
<!-- 是否对SQL 进行格式化 -->
<property name="format_sql">true</property>
<!-- 指定自动生成数据表的策略 -->
<property name="hbm2ddl.auto">update</property>
<!-- 指定关联的 .hbm.xml 文件 -->
<mapping resource="com/home/hibernate/User.hbm.xml"/>
</session-factory>
</hibernate-configuration>