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;}@Overridepublic 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 : 实体类中的idcolumn : 数据库中主键的名字--><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添加一条数据*/@Testpublic 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();// 创建sessionFactorySessionFactory sessionFactory = configuration.buildSessionFactory();// 获取sessionSession 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返回的代理对象

转载于:https://www.cnblogs.com/xiaoshitoutest/p/7359908.html

Hibernate_01相关推荐

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

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

  2. hibernate系列之一

    通过自己不断的学习框架以及相关知识的学习,自己学会总结了学习路上遇到的一些问题以及疑惑,自己现在跟着相关的学习资料又进行了一些总结和实践,希望通过自己走过的学习之路能够帮助小伙伴们解决一些学习上问题或 ...

最新文章

  1. 他十年前的书在前端圈人手一本,豆瓣评分9.1。现在,王者归来了!
  2. 爬虫好学吗python-小白python学到什么程度可以学习网络爬虫? ?
  3. mac 安装Navicat Premium 注册机
  4. openresty+consul动态配置更新(服务变更发现)
  5. 从技术分工的角度来看996.ICU
  6. iscroll上拉加载、下拉刷新
  7. 怎么解决python遇到问题_新手常见Python错误及异常解决处理方案
  8. 比国内贵3000元!小米11 Ultra将于5月11日登陆欧洲市场
  9. python爬虫什么意思-python的爬虫是什么意思
  10. github【如何删除一个repository(仓库)】
  11. 上海译文公布2019年“新书目录” 名家名译作品结集出版
  12. MP3转换器下载 - 通用mp3转换器
  13. 微信小程序 渲染层网络错误_渲染层网络层错误 微信小程序开发 - 云计算资讯 - 服务器之家...
  14. 台式电脑接路由器步骤_路由器怎么连接台式电脑来上网?
  15. 半监督分类算法简述,self-trainning,co-trainning
  16. RabbitMQ 延迟队列详解
  17. 平衡面板数据中的缺失值可以存在吗?
  18. 维克森林大学计算机科学专业好不好,美国维克森林大学计算机科学硕士专业介绍...
  19. 【杰理AC695X】7脚屏PWM控制亮度
  20. kali升级操作系统

热门文章

  1. 2018-12-28
  2. 国际象棋游戏界面和简易棋谱规则-最新Python学习成果
  3. xlc mysql_mysql – 用于存储产品信息的noSQL?
  4. 关于更新内容次序问题
  5. Go获取命令行参数及信号量处理
  6. [ANE for Android]Java接口部分引用第三方JAR的解决办法
  7. php mysql 获取排名,Mysql排序获取排名的实例代码
  8. 【java学习之路】(javaWeb篇)001.HTML
  9. java xml 面试_Java程序员面试常见的10道XML面试题
  10. mysql报1840_mysql 帮助手册 以及 warning: World-writable config file 以及 ERROR 1840 (HY000) at line 24:...