hibernate.cfg.xml
<?xml version="1.0" encoding="UTF-8"?>
<!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="connection.driver_class">com.microsoft.sqlserver.jdbc.SQLServerDriver</property>
<property name="connection.url">jdbc:sqlserver://localhost:1433;DatabaseName=hibernate</property>
<property name="dialect">org.hibernate.dialect.SQLServerDialect</property>
<property name="hibernate.connection.username">sa</property>
<property name="hibernate.connection.password">123</property>
<property name="show_sql">ture</property>
<property name="format_sql">true</property>
<property name="hibernate.jdbc.batch_size">16</property>
<mapping resource="com/hibernate01/Customer.hbm.xml" />
</session-factory>
</hibernate-configuration>
Customer.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="com.hibernate01.Customer" table="cst_customer">
<!-- 建立类中的属性与表中的主键对应 -->
<id name="cust_id" column="cust_id" >
<generator class="native"/>
</id>
<!-- 建立类中的普通的属性和表的字段的对应 -->
<property name="cust_name" column="cust_name" length="32" />
<property name="cust_source" column="cust_source" length="32"/>
<property name="cust_industry" column="cust_industry"/>
<property name="cust_level" column="cust_level"/>
<property name="cust_phone" column="cust_phone"/>
<property name="cust_mobile" column="cust_mobile"/>
</class>
</hibernate-mapping>
Customer类
package com.hibernate01;
/*
* sql 2008R2 数据库表结构
*/
/*CREATE TABLE cst_customer (
cust_id INT NOT NULL IDENTITY PRIMARY KEY(cust_id),
cust_name varchar(32) NOT NULL ,
cust_source varchar(32) ,
cust_industry varchar(32) ,
cust_level varchar(32) ,
cust_phone varchar(64),
cust_mobile varchar(16),
) */
/**
* @Description: 表cst_customer实体类(用一句话描述该文件做什么)
* @author xu_yuxin
* @date 2019年10月19日
* @version V1.0
*/
public class Customer {
private int cust_id;
private String cust_name;
private String cust_source;
private String cust_industry;
private String cust_level;
private String cust_phone;
private String cust_mobile;
public int getCust_id() {
return cust_id;
}
public void setCust_id(int cust_id) {
this.cust_id = cust_id;
}
public String getCust_name() {
return cust_name;
}
public void setCust_name(String cust_name) {
this.cust_name = cust_name;
}
public String getCust_source() {
return cust_source;
}
public void setCust_source(String cust_source) {
this.cust_source = cust_source;
}
public String getCust_industry() {
return cust_industry;
}
public void setCust_industry(String cust_industry) {
this.cust_industry = cust_industry;
}
public String getCust_level() {
return cust_level;
}
public void setCust_level(String cust_level) {
this.cust_level = cust_level;
}
public String getCust_phone() {
return cust_phone;
}
public void setCust_phone(String cust_phone) {
this.cust_phone = cust_phone;
}
public String getCust_mobile() {
return cust_mobile;
}
public void setCust_mobile(String cust_mobile) {
this.cust_mobile = cust_mobile;
}
}
CustomerTest测试类
package com.hibernate01;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.junit.Test;
public class CustomerTest {
@Test
public void Test() {
Configuration configuration=new Configuration().configure();
SessionFactory buildSessionFactory = configuration.buildSessionFactory();
Session openSession = buildSessionFactory.openSession();
Transaction beginTransaction = openSession.beginTransaction();
try {
Customer customer = new Customer();
// customer.setCust_id(1);
customer.setCust_name("测试2");
openSession.save(customer);
beginTransaction.commit();
} catch (Exception e) {
System.out.println(e);
}
openSession.close();
buildSessionFactory.close();
}
}
jar包