目录
实现:保存一个客户到数据库
idea建maven项目
项目结构
pom.xml
只需这三个依赖core,log4j,mysql-connector
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>5.4.1.Final</version>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.44</version>
</dependency>
Customer.java
package com.luobo.entity;
/**
* . Description:
*
* @author: ws
* @version: 1.0
*/
public class Customer {
private int id;
private String name;
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;
}
}
Customer.hbm.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!--导入dtd约束 core.jar 里org.hibernate.hibernate-mapping-3.0.dtd找-->
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="com.luobo.entity">
<class name="Customer" table="customer">
<id name="id" column="cus_id">
<!--generator指定主键生成方式-->
<generator class="native"/>
</id>
<property name="name" column="cus_name"></property>
</class>
</hibernate-mapping>
hibernate.cfg.xml
hibernate-release-5.4.1.Final\project\etc\hibernate.properties
<?xml version="1.0" encoding="UTF-8" ?>
<!--导入dtd约束 core.jar 里org.hibernate.hibernate-configuration-3.0.dtd找-->
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<!--配置信息:下载hibernate-release-5.4.1.Final.zip,hibernate-release-5.4.1.Final\project\etc\hibernate.properties-->
<!--配置sessionFactory
1.连接数据库信息
2.hibernate可选配置
3.映射文件的位置-->
<session-factory>
<!--1.连接数据库信息-->
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql:///test</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">123456</property>
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<!--2.hibernate可选配置-->
<property name="hibernate.show_sql">true</property>
<property name="hibernate.format_sql">true</property>
<property name="hibernate.hbm2ddl.auto">update</property>
<!--3.映射文件的位置-->
<mapping resource="com/luobo/entity/Customer.hbm.xml"/>
</session-factory>
</hibernate-configuration>
CustomerTest.java
import com.luobo.entity.Customer;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.junit.Test;
/**
* . Description:
*
* @author: ws
* @version: 1.0
*/
public class CustomerTest {
/*
*步骤分析:
* 1.解析主配置文件
* 2.根据配置文件创建sessionFactory
* 3.根据sessionFactory创建session
* 4.开启事务
* 5.执行操作(保存)
* 6.提交事务
* 7.释放资源
* */
@Test
public void test() {
Customer c = new Customer();
c.setName("2");
//1.解析主配置文件 org.hibernate.cfg.Configuration
Configuration cfg = new Configuration();
cfg.configure();
SessionFactory sessionFactory = cfg.buildSessionFactory();
Session session = sessionFactory.openSession();
Transaction tx = session.beginTransaction();
session.save(c);
tx.commit();
session.close();
sessionFactory.close();
}
}
附:若需session与线程绑定
配置文件加(亲测貌似不加也行)
<!--session与线程绑定,getCurrentSession-->
<property name="current_session_context_class">thread</property>
pom.xml加
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-c3p0</artifactId>
<version>5.4.1.Final</version>
</dependency>
<dependency>
<groupId>com.mchange</groupId>
<artifactId>c3p0</artifactId>
<version>0.9.5.2</version>
</dependency>
验证:c3p0还是jdbc
public void test() {
Configuration cfg = new Configuration();
cfg.configure();
SessionFactory sessionFactory = cfg.buildSessionFactory();
Session session = sessionFactory.openSession();
session.doWork(new Work() {
public void execute(Connection connection) throws SQLException {
System.out.println(connection.getClass().getName());
}
});
}