Hibernate环境搭建与测试

hibernate是一个优秀的轻量级ORM框架,封装了JDBC操作,可以让编程人员以面向对象的方式操作数据库

一. 先到hibernate官网下载最新hibernate包,我下载的是2017最新版本hibernate-release-5.2.10.Final.zip

http://hibernate.org/orm/


二.解压文件,把required文件下所有jar包导入工程

hibernate-release-5.2.10.Final\lib\required



三. 导入其他包:必须包含日志包、MySQL或Oracle驱动包


3. 创建数据库表单,和javaBean实体,这里可以自己随便创建一个

这里创建一个Customer对象 和 mysql表单,属性相互对应


4.建立映射关系

a. 在实体包下面建立对应的xml文件,名字互相对应,最好和类使用一样的名字,后缀名.hbm.xlm

b.建立对象映射xml约束,也就是在xml前面加一行标签叙述,这行字在hibernate导入的包里,具体路径如下:

·

粘贴过来后看到这个提示就说明约束建立成功

c 配置映射关系

<?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.ssh.domain.Customer" table="cst_customer">
		<!--主键 -->
		<id name="cust_id" column="cust_id">
			<!--主键生成策略  -->
			<generator class="native"></generator>
		</id>
		<!-- 属性 name代表类属性,column代表数据库列名-->
		<property name="cust_name" column="cust_name"></property>
		<property name="cust_user_id" column="cust_user_id"></property>
		<property name="cust_create_id" column="cust_create_id"></property>
		<property name="cust_source" column="cust_source"></property>
		<property name="cust_industry" column="cust_industry"></property>
		<property name="cust_level" column="cust_level"></property>
		<property name="cust_linkman" column="cust_linkman"></property>
		<property name="cust_phone" column="cust_phone"></property>
		<property name="cust_mobile" column="cust_mobile"></property>
	
	</class>
</hibernate-mapping>

四. 编写hibernate核心配置文件

约束方式和上面一样,但是命名必须是hibernate.cfg.xml,且必须放在src根目录下

\hibernate-release-5.2.10.Final\project\etc目录下有个文件hibernate.properties

在这个文件里面搜索你要配置数据库连接信息,copy到xml中进行配置,比如mysql就搜索mysql,会看到以下行,然后进行配置:

## MySQL

#hibernate.dialect org.hibernate.dialect.MySQLDialect
#hibernate.dialect org.hibernate.dialect.MySQLInnoDBDialect
#hibernate.dialect org.hibernate.dialect.MySQLMyISAMDialect
#hibernate.connection.driver_class com.mysql.jdbc.Driver
#hibernate.connection.url jdbc:mysql:///test
#hibernate.connection.username gavin
#hibernate.connection.password

<?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 -->
	<session-factory>
		<!-- 必须配置的5个参数:数据库4个参数和数据库方言 -->
		<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
		<property name="hibernate.connection.url">jdbc:mysql:///ssh</property>
		<property name="hibernate.connection.username">root</property>
		<property name="hibernate.connection.password">root</property>
		<!-- 数据库方言,不同数据库不同配置 -->
		<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>
		<!-- 生成数据库表结构 ,create:每次删除原表重新建新表,测试使用-->
		<!-- 生成数据库表结构 ,update:如果没有表,则创建表,如果有表则正常更新; 可以自动增加字段也就是表列-->
		<!--validate校验映射关系,与配置不一致则报错  -->
		<property name="hibernate.hbm2ddl.auto">update</property>
		<!-- 配置映射文件,引入配置文件路径 -->
		<mapping resource="com/ssh/domain/Customer.hbm.xml"/>
	</session-factory>
</hibernate-configuration>
四 编写测试类,确认是否搭建成功


package com.ssh.demo;

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

import com.ssh.domain.Customer;

public class Demo1 {
/*
 * 第一个程序:测试hibernate
 */
	@Test
	public void test1(){
		 //1加载配置文件
		Configuration config = new Configuration();
		//默认加载src下hibernate.cfg.xml文件
		config.configure();
		//2创建SessionFactory对象
		SessionFactory factory = config.buildSessionFactory();
		//3创建session对象
		Session session = factory.openSession();
		//4开启事务
		Transaction trans = session.beginTransaction();
		//5执行代码
		Customer cus = new Customer();
		cus.setCust_name("raylu");
		cus.setCust_mobile("23423424");
		//保存数据
		session.save(cus);
		//6提交事务//回滚事务
		trans.commit();
		//7释放资源
		factory.close();
		session.close();
	}
}

看看数据库有数据了说明执行成功!



猜你喜欢

转载自blog.csdn.net/java_raylu/article/details/73702820