-
新建hibernate项目
-
添加mysql-connector
- ctrl+alt+shift+s lib add mysql-connector
- ctrl+alt+shift+s lib add mysql-connector
-
创建实体类
- src/entity/User.java
package entity; public class User { //hibernate 要求实体类有一个属性唯一 private int uid; private String username; private String password; private String address; public int getUid() { return uid; } public void setUid(int uid) { this.uid = uid; } 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 getAddress() { return address; } public void setAddress(String address) { this.address = address; } }
-
创建src/rntity/User.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: 实体类的全路径
- table: 数据库表名称
- id
- name: 实体类的id属性名称
- column: 生成表的字段名称
- generator
- class
- native: 生成表主键id自增长
- property
- name: 属性名
- column: 字段名
-->
<class name="entity.User" table="t_user">
<id name="uid">
<generator class="native"/>
</id>
<property name="username"/>
<property name="password"/>
<property name="address"/>
</class>
</hibernate-mapping>
- 配置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>
<!--第一部分: 配置数据库信息-->
<!--ctrl+alt+shift+s lib add mysql-connector-->
<property name="connection.url">
jdbc:mysql://127.0.0.1:3306/hibernate_test?useUnicode=true&characterEncoding=utf- 8&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=UTC
</property>
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="connection.username">root</property>
<property name="connection.password">passwd</property>
<!--第二部分: 可选-->
<!-- DB schema will be updated if needed -->
<property name="hbm2ddl.auto">update</property>
<!--输出sql语句, 并格式化-->
<property name="show_sql">true</property>
<property name="format_sql">true</property>
<!--各个数据库的方言-->
<property name="dialect">org.hibernate.dialect.MySQL5Dialect</property>
<!--第三部分: 映射文件-->
<mapping resource="entity/User.hbm.xml"/>
</session-factory>
</hibernate-configuration>
- 修改Main.java
import entity.User;
import org.hibernate.*;
import org.hibernate.cfg.Configuration;
public class Main {
private static final SessionFactory ourSessionFactory;
static {
try {
//加载核心配置文件
Configuration configuration = new Configuration();
configuration.configure();
//创建SessionFactory对象, 并在数据库中把表创建出来
ourSessionFactory = configuration.buildSessionFactory();
} catch (Throwable ex) {
throw new ExceptionInInitializerError(ex);
}
}
public static Session getSession() throws HibernateException {
//创建Session
return ourSessionFactory.openSession();
}
public static void main(final String[] args) throws Exception {
final Session session = getSession();
try {
User user = new User();
user.setUsername("小李");
user.setPassword("123");
user.setAddress("日本");
session.save(user);
session.beginTransaction().commit();
} finally {
session.close();
}
}
}
- 执行Main.java
- 注意事项
<property name="connection.url">
jdbc:mysql://127.0.0.1:3306/hibernate_test?useUnicode=true&characterEncoding=utf-8&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=UTC
</property>
<property name="dialect">org.hibernate.dialect.MySQL5Dialect</property>
<mapping resource="entity/User.hbm.xml"/> 要放到最后