hibernate框架学习笔记2:配置文件详解
实体类:
- package domain;
- 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_linkman;
- 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_linkman() {
- return cust_linkman;
- }
- public void setCust_linkman(String cust_linkman) {
- this.cust_linkman = cust_linkman;
- }
- 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;
- }
- @Override
- public String toString() {
- return "Customer [cust_id=" + cust_id + ", cust_name=" + cust_name + "]";
- }
- }
ORM元数据配置文件:
- <?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="domain" >
- <!--
- class元素: 配置实体与表的对应关系的
- name: 完整类名
- table:数据库表名
- -->
- <class name="Customer" table="cst_customer" >
- <!-- id元素:配置主键映射的属性
- name: 填写主键对应属性名
- column(可选): 填写表中的主键列名.默认值:列名会默认使用属性名
- type(可选):填写列(属性)的类型.hibernate会自动检测实体的属性类型.
- 每个类型有三种填法: java类型|hibernate类型|数据库类型
- not-null(可选):配置该属性(列)是否不能为空. 默认值:false
- length(可选):配置数据库中列的长度. 默认值:使用数据库类型的最大长度
- -->
- <id name="cust_id" >
- <!-- generator:主键生成策略(后边介绍) -->
- <generator class="native"></generator>
- </id>
- <!-- property元素:除id之外的普通属性映射
- name: 填写属性名
- column(可选): 填写列名
- type(可选):填写列(属性)的类型.hibernate会自动检测实体的属性类型.
- 每个类型有三种填法: java类型|hibernate类型|数据库类型
- not-null(可选):配置该属性(列)是否不能为空. 默认值:false
- length(可选):配置数据库中列的长度. 默认值:使用数据库类型的最大长度
- -->
- <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>
核心配置文件:
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>
- <!-- 数据库url -->
- <property name="hibernate.connection.url">jdbc:mysql:///hibernate</property>
- <!-- 数据库连接用户名 -->
- <property name="hibernate.connection.username">root</property>
- <!-- 数据库连接密码 -->
- <property name="hibernate.connection.password">xuyiqing</property>
- <!-- 数据库方言
- 不同的数据库中,sql语法略有区别. 指定方言可以让hibernate框架在生成sql语句时.针对数据库的方言生成.
- sql99标准: DDL 定义语言 库表的增删改查
- DCL 控制语言 事务 权限
- DML 操纵语言 增删改查
- 注意: MYSQL在选择方言时,请选择最短的方言.
- -->
- <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
- <!-- #hibernate.show_sql true
- #hibernate.format_sql true
- -->
- <!-- 将hibernate生成的sql语句打印到控制台 -->
- <property name="hibernate.show_sql">true</property>
- <!-- 将hibernate生成的sql语句格式化(语法缩进) -->
- <property name="hibernate.format_sql">true</property>
- <!--
- ## auto schema export 自动导出表结构. 自动建表
- #hibernate.hbm2ddl.auto create 自动建表.每次框架运行都会创建新的表.以前表将会被覆盖,表数据会丢失.(开发环境中测试使用)
- #hibernate.hbm2ddl.auto create-drop 自动建表.每次框架运行结束都会将所有表删除.(开发环境中测试使用)
- #hibernate.hbm2ddl.auto update(推荐使用) 自动生成表.如果已经存在不会再生成.如果表有变动.自动更新表(不会删除任何数据).
- #hibernate.hbm2ddl.auto validate 校验.不自动生成表.每次启动会校验数据库中表是否正确.校验失败.
- -->
- <property name="hibernate.hbm2ddl.auto">update</property>
- <!-- 引入orm元数据
- 路径书写: 填写src下的路径
- -->
- <mapping resource="domain/Customer.hbm.xml" />
- </session-factory>
- </hibernate-configuration>
hibernate框架学习笔记2:配置文件详解的更多相关文章
- Hibernate学习笔记--映射配置文件详解
参考资料: http://blog.163.com/hzd_love/blog/static/13199988120108265317988/ http://www.cnblogs.com/often ...
- ORM框架对比以及Mybatis配置文件详解
ORM框架对比以及Mybatis配置文件详解 0.数据库操作框架的历程 (1) JDBC JDBC(Java Data Base Connection,java数据库连接)是一种用于执行SQL语句 ...
- Linux学习之用户配置文件详解(十四)
Linux学习之用户配置文件详解 目录 用户信息文件/etc/password 影子文件/etc/shadow 组信息文件/etc/group 组密码文件/etc/gshadow 用户信息文件/etc ...
- expect学习笔记及实例详解【转】
1. expect是基于tcl演变而来的,所以很多语法和tcl类似,基本的语法如下所示:1.1 首行加上/usr/bin/expect1.2 spawn: 后面加上需要执行的shell命令,比如说sp ...
- Struts2学习笔记(二)——配置详解
1.Struts2配置文件加载顺序: default.properties(默认常量配置) struts-default.xml(默认配置文件,主要配置bean和拦截器) struts-plugin. ...
- hibernate框架学习笔记3:API详解
Configuration对象: package api; import org.hibernate.SessionFactory; import org.hibernate.cfg.Configur ...
- j2ee开发之hibernate框架学习笔记
hibernate框架技术重点学习笔记 1.针对不同的数据库,有不同的数据库实现类,使其符号对应的数据库? mysqlDaoImpl oracleDaoImpl ... ... 2.对象和表记录的转换 ...
- Struts2学习笔记二 配置详解
Struts2执行流程 1.简单执行流程,如下所示: 在浏览器输入请求地址,首先会被过滤器处理,然后查找主配置文件,然后根据地址栏中输入的/hello去每个package中查找为/hello的name ...
- hibernate框架学习笔记6:事务
MySQL的事务.JDBC事务操作: 详细见这篇文章:比较详细 http://www.cnblogs.com/xuyiqing/p/8430214.html 如何在hibernate中配置隔离级别: ...
随机推荐
- python空字典列表两种生成方式对赋值带来的不同影响
最近在实现朴素贝叶斯算法碰到一个很有趣的现象(基于Python3.6.3) 我生成一个由10个空字典构成的列表: 第一种生成方式(可能大多数人都会用这种方法,形式简单) >>a = [{} ...
- 谈下spring下的 aop日志记录
在我们开发当中 我们需要对系统用户行为和 系统异常信息有个统一记录 以便后期的 用户行为分析和bug修复 当我们有这个需求时 我们的通常采取方式很多 1.比如我们定义一个规范 开发一个接口 ...
- vue cli创建的项目 当你后期使用了ES6语法,如何解决浏览器兼容问题
最近开发了一个项目,开发过程中,由于需要使用了async await ,于是发现,只有少数的浏览器支持,极大多数的浏览器是不支持这玩意的,在网上各种找解决方案,基本都是失败,最后总结了两个方案之后,尝 ...
- Carries SCU - 4437
Carries frog has nn integers a1,a2,-,ana1,a2,-,an, and she wants to add them pairwise. Unfortunately ...
- Css Secret 案例全套
Css Secret 案例全套 github地址 案例地址 该书揭示了 47 个鲜为人知的 CSS 技巧,主要内容包括背景与边框.形状. 视觉效果.字体排印.用户体验.结构与布局.过渡与动画等.去年买 ...
- [BZOJ2326] [HNOI2011] 数学作业 (矩阵乘法)
Description Input Output Sample Input Sample Output HINT Source Solution 递推式长这样:$f[n]=f[n-1]*10^k+n$ ...
- 论文学习:Fully Convolutional Networks for Semantic Segmentation
发表于2015年这篇<Fully Convolutional Networks for Semantic Segmentation>在图像语义分割领域举足轻重. 1 CNN 与 FCN 通 ...
- angular采坑记录
在angular中会遇到一些莫名的问题,导致不能完成想要的功能,可能是某项用法使用错误,或许是angular相对应不支持,或者是我们功力根本就没有达到.为了在每次采坑之后能有所收获,再遇到时能理解其根 ...
- 2018第一波iOS经典笔试题(现场实拍)
序言 作为一个开发者,眼里不仅仅只存在于那一行又一行的代码,更还有那诗和远方. 注明:面试是对自我审视的一种过程,面试题和iOS程序员本身技术水平没任何关联,无论你能否全部答出,都不要对自己产生任何正 ...
- IE浏览器右键菜单插件开发(下篇)——如何用c#安装、卸载IE右键插件
建立Installer安装类,如图: 代码如下: using Microsoft.Win32; using ResourceShare.UserClient.Common; using System; ...