码迷,mamicode.com
首页 > Web开发 > 详细

框架——hibernate

时间:2017-09-04 22:34:36      阅读:167      评论:0      收藏:0      [点我收藏+]

标签:java框架

2、构建hibernate框架
配置文件(核心).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" >
<!-- package:统一包,下面类名的包结构可以省略 -->
<hibernate-mapping package="com.ly.crm.entity">
	<!-- class:定义实体类和表的关系
		name:实体类,完整类名(package已经填写好包,包名可以省略)
		table:实体类对应的表名 -->
	<class name="Customer" table="cst_customer">
		<!-- id:表中的主键
			name:类中的主键属性
			column(可选):表中的主键字段 -->
		<id name="cust_id" column="cust_id">
		<!-- generator主键生成策略 -->
			<generator class="native"></generator>
		</id>
		<!-- property:类中除了主键外的普通属性
			name:类中的属性
			column(可选):表中的字段,和属性名一致的字段
			type(可选):类型,hibernate会自动检测
						三种填法:Java类型“Java.lang.String”|hibernate类型“string”|数据库类型 sql-type="varchar"
			length(可选):长度,默认数据库允许的最大长度
			not-null:非空约束,默认false -->
		<property name="cust_name" column="cust_name"></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>
===========================================================================================
1、导入dtd结构
2、准备数据库,实体
3、配置对象关系映射
4、配置主配置文件,写在src下面,名字hibernate.cfg.xml
源码(解压的那个文件下)/project/etc/hibernate.properties
Mysql  5个
Show_sql  2个
## auto schema export  1个
 
导入元数据映射文件
Mapping resource=””
==========================================================================================
<?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>
  <!--
  	#hibernate.dialect org.hibernate.dialect.MySQLDialect   会自动检测mysql
	#hibernate.dialect org.hibernate.dialect.MySQLInnoDBDialect   5.5以后
	#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
  -->
  <!-- 数据库驱动 -->
  <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
  <!-- 数据库连接url -->
  <property name="hibernate.connection.url">jdbc:mysql:///crm_hibernate</property>
  <property name="hibernate.connection.username">root</property>
  <property name="hibernate.connection.password">123456</property>
  <!-- 数据库方言:推荐使用最短的   5.0
  	sql语句在不同的数据库中,会有细微差异,在生成sql时,要根据不同的方言生成
  	sql99标准:
  		DDL:数据定义语言,表结构增删改
  		DML:数据操纵语言,表内容增删改
  		DCL:数据控制语句,权限
  		TCL:
  		DQL:属于DML的
   -->
  <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
  <!-- 
  	#hibernate.show_sql true
	## format SQL in log and console
	hibernate.format_sql true
   -->
   <!-- 打印/显示sql语句 -->
   <property name="hibernate.show_sql">true</property>
   <!-- 格式化sql语句 -->
   <property name="hibernate.format_sql">true</property>
   <!-- 
   		## auto schema export	自动导出表结构	
		#hibernate.hbm2ddl.auto create-drop
		#hibernate.hbm2ddl.auto create
		#hibernate.hbm2ddl.auto update
		#hibernate.hbm2ddl.auto validate
    -->
    <!-- 格式化sql语句
    	create:自动建表(会先删除之前的表),测试环境使用
    	update:更新表内容,自动检测表结构(建议使用)
    	create-drop:先建表,再删表,不希望有残留数据影响数据库,测试环境使用
    	validate:校验,要严格符合表结构才能对表数据进行操作 -->
    <property name="hibernate.hbm2ddl.auto">update</property>
    <!-- 引入orm元数据映射文件,路径从src开始 -->
    <mapping resource="com/ly/crm/entity/Customer.hbm.xml"/>
   </session-factory>
</hibernate-configuration>
==========================================================================================
5、测试单元
3、配置文件详解
1、加载配置文件;--无参构造器
加载src路径下的hibernate.cfg.xml文件
Configuration cf=new Configuration().configure();
2、获得sessionFactory工厂对象;
作用:获得session对象
注意:1、sessionFactory非常消耗内存,占用资源
  2、sessionFactory线程安全的工厂类
结论:在一个项目中,SessionFactory只有一个对象
SessionFactory sessionFactory=cf.buildSessionFactory()
3、获得session会话;
Session session=sessionFactory.openSession();
4、开启事务;
Transaction tx=session.beginTransaction();
5、利用session对数据库进行操作;
//-----------------------------------------
新增Customer(实体类)对象
Customer c=new Customer( );
c.setName(“XX”);
session.save(c);
//-----------------------------------------
查询id为1的customer对象
Customer c=session.get(Customer.class,1l);
c.setName(“XX”);
修改
session.update(c);
删除
session.delete(c);
//---------------------------------------------
6、事物提交/回滚;
tx.commit();
7、释放资源
session.close();
sessionFactory.close();


本文出自 “Java学习” 博客,请务必保留此出处http://12181171.blog.51cto.com/12171171/1962638

框架——hibernate

标签:java框架

原文地址:http://12181171.blog.51cto.com/12171171/1962638

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!