*: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的更多相关文章

  1. hibernateUtils

    hibernate3.x版本 package hibernate_01; import org.hibernate.Session; import org.hibernate.SessionFacto ...

  2. Django:(博客系统)使用使用mysql数据&创建post/category/tag实体,并同步到数据中

    背景: 之前也读过一些关于django的一些书,看过别人写的一些博客系统.但是总有一种看别人的都会,但自己写不出来的感觉,于是为了加深对django的学习就开始动手学习了. 环境搭建: 环境:使用py ...

  3. Hibernate(十一):映射继承关系的三种方案

    背景: 在一些项目中,会采用集成的关系来定义数据库实体类,比如:人(Person)与学生(Student),学生来源与人,所以人的基本属性学生也拥有:但学生有的一些属性,人就不具有.人与学生之间很显然 ...

  4. Hibernate(十):n-n关联关系

    背景: 在实际开发中我们会遇到表的多对多关联,比如:一篇博客文章,它可以同时属于JAVA分类.Hibernate分类. 因此,我们在hibernate的学习文章系列中,需要学会如何使用hibernat ...

  5. Hibernate(九):基于主键映射的1-1关联关系

    背景: 在实际开发中我们会遇到新建一个用户表,但这个表字段过长,而且有写字段常用(主要),有些字段比较不常用(次要).此时,我们会考虑到把用户信息拆分到两张表中:member(存储用户主要信息),me ...

  6. Hibernate(八):基于外键映射的1-1关联关系

    背景: 一个部门只有一个一把手,这在程序开发中就会设计数据映射应该设置为一对一关联. 在hibernate代码开发中,实现这个业务有两种方案: 1)基于外键映射的1-1关联: 2)基于主键映射的1-1 ...

  7. Hibernate(五):Hibernate配置文件及C3P0的用法

    配置文件可配项: 参考文档:hibernate-release-5.2.9.Final/documentation/userguide/html_single/Hibernate_User_Guide ...

  8. Hibernate(四):Hello World

    下载hibernate开发包: 在本章之前需要继承hibernate开发插件到eclipse,详细操作请参考我的博文:<Hibernate(一):安装hibernate插件到eclipse环境& ...

  9. Hibernate(一):安装hibernate插件到eclipse环境

    离线安装hibernate插件到eclipse 为什么需要安装hibernate插件到eclipse?在开发eclipse时,很多配置文件信息如果有了hibernate插件集成进来就会有自能提示,方便 ...

随机推荐

  1. Nio经典工作方式

    public void selector() throws IOException { ByteBuffer buffer = ByteBuffer.allocate(1024); Selector ...

  2. Chrome浏览器扩展开发系列之十五:跨域访问的XMLHttpRequest对象

    XMLHttpRequest对象是W3C的标准API,用于访问服务器资源.XMLHttpRequest对象支持多种文本格式,如XML和JSON等.XMLHttpRequest对象可以通过HTTP和HT ...

  3. 推荐系统那点事 —— 基于Spark MLlib的特征选择

    在机器学习中,一般都会按照下面几个步骤:特征提取.数据预处理.特征选择.模型训练.检验优化.那么特征的选择就很关键了,一般模型最后效果的好坏往往都是跟特征的选择有关系的,因为模型本身的参数并没有太多优 ...

  4. PHP发送E-mail---新手教程

    首先下载PHPmailer拓展包,其实就是别人封装好的类库,下载链接:http://pan.baidu.com/s/1slbhGo1 首先去163注册个账号,然后登陆进去,点击设置下面的 POP3/S ...

  5. PHP ORM笔记

    1.ORM是什么? 经常听到程序员的面试中会问到对ORM的了解,但是一直不知道ORM是个什么鬼东西,知道有一天在百度上顺带看到才发现ORM就是我们平时在框架中一直使用的数据库对象操作.ORM(Obje ...

  6. SSO(单点登录)与旅游年卡

    SSO(单点登录)与旅游年卡 SSO英文全称Single Sign On,单点登录.SSO是在多个应用系统中,用户只需要登录一次就可以访问所有相互信任的应用系统.它包括可以将这次主要的登录映射到其他应 ...

  7. Android - FEATURE_NO_TITLE

    Android设置无标题的方法 在onCreate()中写入: requestWindowFeature(Window.FEATURE_NO_TITLE); 例如: protected void on ...

  8. “玲珑杯”ACM比赛 Round #12 (D) 【矩阵快速幂的时间优化】

    //首先,感谢Q巨 题目链接 定义状态向量b[6] b[0]:三面临红色的蓝色三角形个数 b[1]:两面临红色且一面临空的蓝色三角形个数 b[2]:一面临红色且两面临空的蓝色三角形个数 b[3]:三面 ...

  9. Groovy - 介绍

    Groovy 是 用于Java虚拟机的一种敏捷的动态语言,它是一种成熟的面向对象编程语言,既可以用于面向对象编程,又可以用作纯粹的脚本语言.使用该种语言不必编写过多的代码,同时又具有闭包和动态语言中的 ...

  10. 禁止UIWebView随键盘的弹起而往上滚动

    问题:当UIWebView中的html有输入框,点击输入框,UIWebView会随键盘的弹起而整体往上移动,收起键盘后,UIWebView无法回到原来的位置; 问题的原因:由于UIWebView继承的 ...