Hibernate_01
*:first-child {
margin-top: 0 !important;
}
body>*:last-child {
margin-bottom: 0 !important;
}
/* BLOCKS
=============================================================================*/
p, blockquote, ul, ol, dl, table, pre {
margin: 15px 0;
}
/* HEADERS
=============================================================================*/
h1, h2, h3, h4, h5, h6 {
margin: 20px 0 10px;
padding: 0;
font-weight: bold;
-webkit-font-smoothing: antialiased;
}
h1 tt, h1 code, h2 tt, h2 code, h3 tt, h3 code, h4 tt, h4 code, h5 tt, h5 code, h6 tt, h6 code {
font-size: inherit;
}
h1 {
font-size: 28px;
color: #000;
}
h2 {
font-size: 24px;
border-bottom: 1px solid #ccc;
color: #000;
}
h3 {
font-size: 18px;
}
h4 {
font-size: 16px;
}
h5 {
font-size: 14px;
}
h6 {
color: #777;
font-size: 14px;
}
body>h2:first-child, body>h1:first-child, body>h1:first-child+h2, body>h3:first-child, body>h4:first-child, body>h5:first-child, body>h6:first-child {
margin-top: 0;
padding-top: 0;
}
a:first-child h1, a:first-child h2, a:first-child h3, a:first-child h4, a:first-child h5, a:first-child h6 {
margin-top: 0;
padding-top: 0;
}
h1+p, h2+p, h3+p, h4+p, h5+p, h6+p {
margin-top: 10px;
}
/* LINKS
=============================================================================*/
a {
color: #4183C4;
text-decoration: none;
}
a:hover {
text-decoration: underline;
}
/* LISTS
=============================================================================*/
ul, ol {
padding-left: 30px;
}
ul li > :first-child,
ol li > :first-child,
ul li ul:first-of-type,
ol li ol:first-of-type,
ul li ol:first-of-type,
ol li ul:first-of-type {
margin-top: 0px;
}
ul ul, ul ol, ol ol, ol ul {
margin-bottom: 0;
}
dl {
padding: 0;
}
dl dt {
font-size: 14px;
font-weight: bold;
font-style: italic;
padding: 0;
margin: 15px 0 5px;
}
dl dt:first-child {
padding: 0;
}
dl dt>:first-child {
margin-top: 0px;
}
dl dt>:last-child {
margin-bottom: 0px;
}
dl dd {
margin: 0 0 15px;
padding: 0 15px;
}
dl dd>:first-child {
margin-top: 0px;
}
dl dd>:last-child {
margin-bottom: 0px;
}
/* CODE
=============================================================================*/
pre, code, tt {
font-size: 12px;
font-family: Consolas, "Liberation Mono", Courier, monospace;
}
code, tt {
margin: 0 0px;
padding: 0px 0px;
white-space: nowrap;
border: 1px solid #eaeaea;
background-color: #f8f8f8;
border-radius: 3px;
}
pre>code {
margin: 0;
padding: 0;
white-space: pre;
border: none;
background: transparent;
}
pre {
background-color: #f8f8f8;
border: 1px solid #ccc;
font-size: 13px;
line-height: 19px;
overflow: auto;
padding: 6px 10px;
border-radius: 3px;
}
pre code, pre tt {
background-color: transparent;
border: none;
}
kbd {
-moz-border-bottom-colors: none;
-moz-border-left-colors: none;
-moz-border-right-colors: none;
-moz-border-top-colors: none;
background-color: #DDDDDD;
background-image: linear-gradient(#F1F1F1, #DDDDDD);
background-repeat: repeat-x;
border-color: #DDDDDD #CCCCCC #CCCCCC #DDDDDD;
border-image: none;
border-radius: 2px 2px 2px 2px;
border-style: solid;
border-width: 1px;
font-family: "Helvetica Neue",Helvetica,Arial,sans-serif;
line-height: 10px;
padding: 1px 4px;
}
/* QUOTES
=============================================================================*/
blockquote {
border-left: 4px solid #DDD;
padding: 0 15px;
color: #777;
}
blockquote>:first-child {
margin-top: 0px;
}
blockquote>:last-child {
margin-bottom: 0px;
}
/* HORIZONTAL RULES
=============================================================================*/
hr {
clear: both;
margin: 15px 0;
height: 0px;
overflow: hidden;
border: none;
background: transparent;
border-bottom: 4px solid #ddd;
padding: 0;
}
/* TABLES
=============================================================================*/
table th {
font-weight: bold;
}
table th, table td {
border: 1px solid #ccc;
padding: 6px 13px;
}
table tr {
border-top: 1px solid #ccc;
background-color: #fff;
}
table tr:nth-child(2n) {
background-color: #f8f8f8;
}
/* IMAGES
=============================================================================*/
img {
max-width: 100%
}
-->
Hibernate
hibernate是一个持久层orm框架,目的:简化项目开发,提高开发效率。
快速入门
- 下载hibernate,然后导入hibernate->lib->required下面的所有jar包,还有就是数据库驱动的jar包。
- 创建表cst_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_address` varchar(128) DEFAULT NULL COMMENT '客户联系地址',
`cust_phone` varchar(64) DEFAULT NULL COMMENT '客户联系电话',
PRIMARY KEY (`cust_id`)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;
创建相应的实体类 Customer.java
public class Customer implements Serializable {
private static final long serialVersionUID = 1L;
private Long id;// 主键
private String custName;// 客户姓名
private String custSource; // 客户来源
private String custIndustry; // 所属行业
private String custLevel; // 客户级别
private String custAddress; // 地址
private String custPhone; // 电话 public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getCustName() {
return custName;
}
public void setCustName(String custName) {
this.custName = custName;
}
public String getCustSource() {
return custSource;
}
public void setCustSource(String custSource) {
this.custSource = custSource;
}
public String getCustIndustry() {
return custIndustry;
}
public void setCustIndustry(String custIndustry) {
this.custIndustry = custIndustry;
}
public String getCustLevel() {
return custLevel;
}
public void setCustLevel(String custLevel) {
this.custLevel = custLevel;
}
public String getCustAddress() {
return custAddress;
}
public void setCustAddress(String custAddress) {
this.custAddress = custAddress;
}
public String getCustPhone() {
return custPhone;
}
public void setCustPhone(String custPhone) {
this.custPhone = custPhone;
}
@Override
public String toString() {
return "Customer [id=" + id + ", custName=" + custName + ", custSource=" + custSource + ", custIndustry="
+ custIndustry + ", custLevel=" + custLevel + ", custAddress=" + custAddress + ", custPhone="
+ custPhone + "]";
}
}
创建对应的配置文件 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 : 实体类的全限定名
table : 对应数据库的表名(如果类名和数据库表的名字一样,可以省略table配置)
-->
<class name="com.xiaoshitou.domain.Customer" table="cst_customer">
<!-- id标签:主键
name : 实体类中的id
column : 数据库中主键的名字
-->
<id name="id" column="cust_id">
<!-- gennerator class:主键的生成策略;native表示,主键由数据库自动维护 -->
<generator class="native"></generator>
</id> <!-- property:主键以外的普通字段
name=>实体类中的成员变量,column=>表中的字段名 -->
<property name="custName" column="cust_name"></property>
<property name="custSource" column="cust_source"></property>
<property name="custIndustry" column="cust_industry"></property>
<property name="custLevel" column="cust_level"></property>
<property name="custAddress" column="cust_address"></property>
<property name="custPhone" column="cust_phone"></property>
</class>
</hibernate-mapping>
编写hiberna核心配置文件:一定要放在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://localhost:3306/hibernate</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">123456</property>
<!-- 配置数据库方言 -->
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property> <!-- 显示sql -->
<property name="hibernate.show_sql">true</property>
<!-- 格式化sql -->
<property name="hibernate.format">true</property>
<!-- create-drop:启动时会创建表,sessionFactory关闭时删除表,
create: 每次都会重新创建表,
update: 如果实体类和数据库的表字段不一样时,会修改数据库中的字段,保证实体类和数据库表是同步的,
validate: 如果实体类和数据库表字段不一致时,会报错 -->
<property name="hibernate.hbm2ddl.auto">update</property> <!-- 把实体类的配置文件加载到核心配置文件中 -->
<mapping resource="com/xiaoshitou/domain/Customer.hbm.xml"/> </session-factory>
</hibernate-configuration>
编写测试类,使用hibernate来添加一条数据
/**
* 快速入门:利用hibernate添加一条数据
*/
@Test
public void test01() {
// 创造一个customer对象
Customer customer = new Customer();
customer.setCustName("东方科技");
customer.setCustLevel("VIP");
customer.setCustSource("朋友推荐");
customer.setCustIndustry("无人机");
customer.setCustAddress("东方1号路");
customer.setCustPhone("8221365"); // 利用hibernate完成保存
Configuration configuration = new Configuration();
configuration.configure();
// 创建sessionFactory
SessionFactory sessionFactory = configuration.buildSessionFactory();
// 获取session
Session session = sessionFactory.openSession();
// 开启事务
Transaction tx = session.beginTransaction();
// 保存
session.save(customer);
// 提交事务,关闭资源
tx.commit();
session.close();
sessionFactory.close();
}
hibernate 使用c3p0连接池
- 导入jar包:hibernate-release-5.0.7.Final\lib\optional\c3p0
在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>
<!-- 使用c3p0连接池 -->
<property name="hibernate.connection.provider_class">org.hibernate.connection.C3P0ConnectionProvider</property>
<!-- 在数据库连接池中,最大连接数 -->
<property name="hibernate.c3p0.max_size">20</property>
<!-- 在数据库连接池中,最小连接数 -->
<property name="hibernate.c3p0.min_size">5</property>
<!-- 如果连接池中,某个连接空闲时间超过设置的时间,将会被从连接池冲清除 -->
<property name="hibernate.c3p0.timeout">5000</property>
<!-- 每个3000秒检查,连接池中的空闲连接,单位秒 -->
<property name="hibernate.c3p0.idle_test_period">3000</property> <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/hibernate</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">123456</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-drop:启动时会创建表,sessionFactory关闭时删除表,
create: 每次都会重新创建表,
update: 如果实体类和数据库的表字段不一样时,会修改数据库中的字段,保证实体类和数据库表是同步的,
validate: 如果实体类和数据库表字段不一致时,会报错 -->
<property name="hibernate.hbm2ddl.auto">update</property> <!-- 把实体类的配置文件加载到核心配置文件中 -->
<mapping resource="com/xiaoshitou/domain/Customer.hbm.xml"/> </session-factory>
</hibernate-configuration>
session中的saveOrUpdate方法:
- 如果对象中没有设置id,就会新增一条记录
- 如果对象中设置了id,并且数据库中,有对应id的记录,那么就是修改记录
- 如果对象中设置了id,在数据库中没有对应id的记录,那么将会执行失败,抛出异常。
session中get和load方法的区别
- get是立即加载(立即执行sql语句),load是延迟加载(在真正使用对象的属性时,才会发送sql语句)
- get返回的对象是要查询的实体对象,load返回的代理对象
Hibernate_01的更多相关文章
- hibernateUtils
hibernate3.x版本 package hibernate_01; import org.hibernate.Session; import org.hibernate.SessionFacto ...
- Django:(博客系统)使用使用mysql数据&创建post/category/tag实体,并同步到数据中
背景: 之前也读过一些关于django的一些书,看过别人写的一些博客系统.但是总有一种看别人的都会,但自己写不出来的感觉,于是为了加深对django的学习就开始动手学习了. 环境搭建: 环境:使用py ...
- Hibernate(十一):映射继承关系的三种方案
背景: 在一些项目中,会采用集成的关系来定义数据库实体类,比如:人(Person)与学生(Student),学生来源与人,所以人的基本属性学生也拥有:但学生有的一些属性,人就不具有.人与学生之间很显然 ...
- Hibernate(十):n-n关联关系
背景: 在实际开发中我们会遇到表的多对多关联,比如:一篇博客文章,它可以同时属于JAVA分类.Hibernate分类. 因此,我们在hibernate的学习文章系列中,需要学会如何使用hibernat ...
- Hibernate(九):基于主键映射的1-1关联关系
背景: 在实际开发中我们会遇到新建一个用户表,但这个表字段过长,而且有写字段常用(主要),有些字段比较不常用(次要).此时,我们会考虑到把用户信息拆分到两张表中:member(存储用户主要信息),me ...
- Hibernate(八):基于外键映射的1-1关联关系
背景: 一个部门只有一个一把手,这在程序开发中就会设计数据映射应该设置为一对一关联. 在hibernate代码开发中,实现这个业务有两种方案: 1)基于外键映射的1-1关联: 2)基于主键映射的1-1 ...
- Hibernate(五):Hibernate配置文件及C3P0的用法
配置文件可配项: 参考文档:hibernate-release-5.2.9.Final/documentation/userguide/html_single/Hibernate_User_Guide ...
- Hibernate(四):Hello World
下载hibernate开发包: 在本章之前需要继承hibernate开发插件到eclipse,详细操作请参考我的博文:<Hibernate(一):安装hibernate插件到eclipse环境& ...
- Hibernate(一):安装hibernate插件到eclipse环境
离线安装hibernate插件到eclipse 为什么需要安装hibernate插件到eclipse?在开发eclipse时,很多配置文件信息如果有了hibernate插件集成进来就会有自能提示,方便 ...
随机推荐
- Linux 重启和关机命令
shutdown -r 05:30 在凌晨五点30分关机 shutdown -r 05:30 & 后台执行 shutdown -c 取消前一个关机命令 ...
- 谈谈webpack 的优势
其优势主要可以归类为如下几个: 1. webpack 是以 commonJS 的形式来书写脚本滴,但对 AMD/CMD 的支持也很全面,方便旧项目进行代码迁移. 2. 能被模块化的不仅仅是 JS 了. ...
- java 文件的编码 问题
package com.io; public class Encodedemo { public static void main(String[] args)throws Exception{ // ...
- jsp注册页面验证,easyui的jsp+js表单验证
1.1下面的代码是写在Js里面的,就直接写进去不用什么其他东西,这样一个表单验证就好了(1.2图) $.extend($.fn.validatebox.defaults.rules, { phone: ...
- java字符串,包,数组及空心正方形,菱形的实例
一.数组:相同类型的多个对像引用类型:所有的类,接口,数组,int[] ints(变量名) = new int[3]new:指的是在内存空间重新开辟一块区域 String s1 = "abc ...
- php 极简框架ES发布(代码总和不到 400 行)
ES 框架简介 ES 是一款 极简,灵活, 高性能,扩建性强 的php 框架. 未开源之前在商业公司 经历数年,数个高并发网站 实践使用! 框架结构 整个框架核心四个文件,所有文件加起来放在一起总行数 ...
- 图片预加载之模拟img.load()
function imgBatchLoad(){ var instance = this; this.loadCount = 0; this.images = []; this.imgCount = ...
- 快手 Android 工程师面经
看着我把简历投完之后弹出的"完成"字样,我就十分的激动了,我是一名应届毕业生,老老实实的那种,学过的知识我都一步一个脚印的复习的完了,Lintcode上该刷的题,也妥妥的完成了,但 ...
- jvm学习002 虚拟机类加载过程以及主动引用和被动引用
虚拟机把描述类的数据从class文件加载到内存,并对数据进行校验,转换解析和初始化,最终形成可以被虚拟机直接使用的Java类型,这就是虚拟机的类加载机制. 类从被加载到虚拟机内存中开始,到卸载出内存为 ...
- 51nod_1417:天堂里的游戏
题目链接: https://www.51nod.com/onlineJudge/questionCode.html#!problemId=1417 假设 ans=n/m,C=(A+B)/2 若出正面 ...