eclipse采用hibernate连接sql实体类案例

本文详细介绍了Hibernate框架的配置文件hibernate.cfg.xml及其与Customer实体类之间的映射关系,展示了如何通过Customer.hbm.xml文件实现Java类与数据库表的映射,包括属性与字段的对应以及主键生成策略。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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包
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值