hibernate笔记之配置文件和映射文件和主要api示例

来源:互联网 发布:javzoo最新域名 编辑:程序博客网 时间:2024/06/13 03:21
src目录下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_32</property><property name="hibernate.connection.username">root</property><property name="hibernate.connection.password">root</property><property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property><property name="hibernate.show_sql">true</property><property name="hibernate.format_sql">true</property><property name="hibernate.hbm2ddl.auto">update</property><mapping resource="cn/itheima/domain/Customer.hbm.xml" /></session-factory></hibernate-configuration>
配置文件主要解释:要在此配置文件中指定数据对象类的映射文件位置:<mapping resource="cn/itheima/domain/Customer.hbm.xml" />框架建表格的四种策略:1.hibernate.hbm2ddl.auto    create自动建表.每次框架运行都会创建新的表.以前表将会被覆盖,表数据会丢失.(开发环境中测试使用)2.hibernate.hbm2ddl.auto   create-drop 自动建表.每次框架运行结束都会将所有表删除.(开发环境中测试使用)3.hibernate.hbm2ddl.auto   update 自动生成表.如果已经存在不会再生成.如果表有变动.自动更新表(不会删除任何数据).4.hibernate.hbm2ddl.auto   validate校验.不自动生成表.每次启动会校验数据库中表是否正确. 
实体类映射文件:<?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="cn.itheima.domain" ><class name="Customer" table="cst_customer" ><id name="cust_id"  ><!-- generator:主键生成策略(明天讲) --><generator class="native"></generator></id><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>
实体类映射文件:说明:class元素: 配置实体与表的对应关系的name: 完整类名,如果父元素配置了package属性,可简写table:数据库表名id元素:配置主键映射的属性name: 填写主键对应属性名column(可选): 填写表中的主键列名.默认值:列名会默认使用属性名type(可选):填写列(属性)的类型.hibernate会自动检测实体的属性类型.   每个类型有三种填法: java类型|hibernate类型|数据库类型not-null(可选):配置该属性(列)是否不能为空. 默认值:falselength(可选):配置数据库中列的长度. 默认值:使用数据库类型的最大长度property元素:除id之外的普通属性映射  name: 填写属性名  column(可选): 填写列名  type(可选):填写列(属性)的类型.hibernate会自动检测实体的属性类型.每个类型有三种填法: java类型|hibernate类型|数据库类型  not-null(可选):配置该属性(列)是否不能为空. 默认值:false  length(可选):配置数据库中列的长度. 默认值:使用数据库类型的最大长度
//持久化javaBeanpublic class Customer {/* * 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_linkman` VARCHAR(64) 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; */private Long cust_id;private String cust_name;private String cust_source;private String cust_industry;private String cust_level;private String cust_linkman;private String cust_phone;private String cust_mobile;  //省略了属性的get和set方法}
//主要API演示public void test(){Configuration conf = new Configuration().configure();SessionFactory sf = conf.buildSessionFactory();Session session = sf.openSession();//3 获得session//Transaction tx = session.getTransaction();tx.begin();Transaction tx = session.beginTransaction();//开启获取事务并开启                session.save(new Customer("name") );//保存对象Customer c = session.get(Customer.class, 1l);//获得对象                session.update(c.setName(“name1”));//更新对象session.delete(c);//删除对象tx2.commit();//提交事务session.close();//释放资源sf.close();//释放资源}说明:Configuration:  配置加载类.用于加载主配置,orm元数据加载SessionFactory: 负责创建session对象.并发请求时可能会同时需要多个链接,每个链接能对应一个sessionSessionfactory:负责保存和使用所有配置信息.消耗内存资源大.一般一个web项目中,只创建一个SessionFactory对象Session:       表示hibernate框架与数据库之间的连接session类似于JDBC的connection对象. 还用于对数据库中数据的增删改查.Transaction:   事物,保证代码块的完整执行否则回滚
主键生成策略:1. increment:适用于short,int,long作为主键.不是使用的数据库自动增长机制.        Hibernate中提供的一种增长机制.        先进行查询 :select max(id) from user;        再进行插入 :获得最大值+1作为新的记录的主键.        问题:不能在集群环境下或者有并发访问的情况下使用.2.identity:适用于short,int,long作为主键。但是这个必须使用在有自动增长数据库中.采用的是数据库底层的自动增长机制.底层使用的是数据库的自动增长(auto_increment).像Oracle数据库没有自动增长.3. sequence:适用于short,int,long作为主键.底层使用的是序列的增长方式.Oracle数据库底层没有自动增长,想自动增长需要使用序列.4. uuid:适用于char,varchar类型的作为主键.使用随机的字符串作为主键.5. native:本地策略.根据底层的数据库不同,自动选择适用于该种数据库的生成策略.(short,int,long)    如果底层使用的MySQL数据库:相当于identity.    如果底层使用Oracle数据库:相当于sequence.6.assigned:主键的生成不用Hibernate管理了.必须手动设置主键.


阅读全文
0 0
原创粉丝点击