Hibernate入门小案例

Hibernate入门:

       下载Hibernate

       了解Hibernate目录结构

       创建数据库和表

       创建实体类

       创建映射文件

       创建核心配置文件

       测试类

创建数据库hibernate_case 导入.sql文件

扫描二维码关注公众号,回复: 10163008 查看本文章

导入mysql驱动jar包  hibernate必备jar包和日志包

创建实体类 Customer.java

package com.au.hibernate.demo1;

/**
 * 客户管理实体类
 * 
 * @author Au 
 * CREATE TABLE `cst_customer` (
  `cust_id` bigint(32) NOT NULL AUTO_INCREMENT COMMENT '客户编号(主键)',
  `cust_name` varchar(32) NOT NULL COMMENT '客户名称(公司名称)',
  `cust_source` varchar(32) DEFAULT NULL COMMENT '客户信息来源',
  `cust_industry` varchar(32) DEFAULT NULL COMMENT '客户所属行业',
  `cust_level` varchar(32) DEFAULT NULL COMMENT '客户级别',
  `cust_phone` varchar(64) DEFAULT NULL COMMENT '固定电话',
  `cust_mobile` varchar(16) DEFAULT NULL COMMENT '移动电话',
  PRIMARY KEY (`cust_id`)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;
 */
public class Customer {
	private long 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 long getCust_id() {
		return cust_id;
	}

	public void setCust_id(long 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;
	}

	public Customer() {
		super();
	}

	public Customer(long cust_id, String cust_name, String cust_source, String cust_industry, String cust_level,
			String cust_phone, String cust_mobile) {
		super();
		this.cust_id = cust_id;
		this.cust_name = cust_name;
		this.cust_source = cust_source;
		this.cust_industry = cust_industry;
		this.cust_level = cust_level;
		this.cust_phone = cust_phone;
		this.cust_mobile = cust_mobile;
	}

	
	
}

创建映射,配置xml 配置文件名可以任意命名,尽量统一规范:类名.hbm.xml

在包下同级目录创建 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.au.hibernate.demo1.Customer" table="cst_customer">
		<!-- 建立类中的属性与表中的主键对应 -->
		<id name="cust_id" column="cust_id">
			<generator class="native" /> 		<!-- 本地策略 -->
		</id>

		<!-- 建立类中的普通的属性和表的字段的对应 -->
		<property name="cust_name" column="cust_name" />
		<property name="cust_source" column="cust_source" />
		<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>

创建hibernate的核心配置文件:

通常为: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="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
		<property name="hibernate.connection.url">jdbc:mysql:///hibernate_case</property>
		<property name="hibernate.connection.username">root</property>
		<property name="hibernate.connection.password">root</property>
		<!-- 配置Hibernate方言 -->
		<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>

		<!-- 可选配置 -->
		<!-- 打印sql -->
		<property name="hibernate.show_sql">true</property>
		<!-- 格式化sql -->
		<property name="hibernate.format_sql">true</property>

		<!-- 包路径换成 / -->
		<mapping resource="com/au/hibernate/demo1/Customer.hbm.xml" />
	</session-factory>


</hibernate-configuration>

测试类: CustomerDemo1.java

package com.au.hibernate.demo1;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;

import org.junit.Test;

/**
 * 
 * Hibernate入门案例
 * 
 * @author Au
 *
 */
public class HibernateDemo1 {

	@Test
	// 保存客户的案例
	public void demo1() throws Exception {
		// 1.加载Hibernate的核心配置文件
		Configuration configuration = new Configuration().configure();

		// 2.创建一个SessionFactory对象:类似于JDBC中连接池
		SessionFactory sessionFactury = configuration.buildSessionFactory();

		// 3.通过SessionFactory获取到Session对象:类似于JDBC中Connection
		Session session = sessionFactury.openSession();

		// 4.手动开启实物(兼容Hibernate3):
		Transaction transaction = session.beginTransaction();

		// 5.编写代码
		Customer customer = new Customer();
		customer.setCust_name("小明");

		session.save(customer);
		// 6.事务提交
		transaction.commit();

		// 7.资源释放
		session.close();

	}

}

测试运行,成功插入

一个项目应只有一个SessionFactory对象,后期创建项目时,应抽取工具类,实例化一次,直接调用即可

发布了67 篇原创文章 · 获赞 50 · 访问量 6万+

猜你喜欢

转载自blog.csdn.net/Auuuuuuuu/article/details/86767294