新建一个JavaProject(不一定非要web工程)

在工程里面新建一个文件夹lib,用来存放jar包

找到Hibernate的下载文件,解压后找到required文件夹,这是需要的jar包

添加到咱们建好的lib文件夹里面

Hibernate下载地址http://hibernate.org/orm/downloads/

还需要的就是jdbc驱动包    mysql-connector-java-5.1.37-bin.jar

下载地址http://www.mysql.com/products/connector/

现在写一个Java POJO类,也就是简单的getter,setter类啦

然后在Java类所在的包新建一个XXX.hbm.xml文件(这个需要Hibernate插件的支持,关于插件我有一篇有介绍)

再在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="connection.username">数据库用户名</property>
<property name="connection.password">数据库密码</property>
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="connection.url">jdbc:mysql://localhost:3306/数据库名</property> <!-- 配置Hibernate基本信息 -->
<!-- Hibernate所使用得数据库方言 -->
<property name="dialect">org.hibernate.dialect.MySQLInnoDBDialect</property> <!-- 执行操作时是否在控制台打印sql -->
<property name="show_sql">true</property> <!-- 是否对sql进行格式化 -->
<property name="format_sql">true</property> <!-- 指定自动生成数据表的策略 -->
<property name="hbm2ddl.auto">update</property> <!-- 指定关联的.hbm.xml文件 -->
<mapping resource="com/biubiu/domain/UserInfo.hbm.xml"/>
<mapping class="com.biubiu.domain.UserInfo"/> </session-factory>
</hibernate-configuration>

最后写一个测试类,测试方法为:

   @Test
public void test() {
//1.创建一个SessionFactory对象
SessionFactory seFactory = null;
final StandardServiceRegistry registry = new StandardServiceRegistryBuilder()
.configure() // configures settings from hibernate.cfg.xml
.build();
try{
seFactory = new MetadataSources( registry ).buildMetadata()
.buildSessionFactory();
} catch(Exception e){
StandardServiceRegistryBuilder.destroy( registry );
} //2.创建一个Session对象 Session session = seFactory.openSession(); //3.开启事务
session.beginTransaction();
//4.执行操作 session.save(new UserInfo("1234@qq.com","测试",
"icuicu","0","img","tianmao")); /*
//!!!!写hql语句,from 类名(区分大小写)
String hql = "from UserInfo";
@SuppressWarnings("unchecked")
List<UserInfo> list = session.createQuery(hql).list();
for(UserInfo u : list){
System.out.println(u);
}
*/
//5.提交事务
session.getTransaction().commit();
//6.关闭Session
session.close();
//7.关闭SessionFactory
seFactory.close(); }

一个是存数据,另外一个是取数据,注意注释起来了

Hibernate5.1.0的hello word的更多相关文章

  1. yii2.0 发送邮件带word小附件

        把 common/config/main-local.php 下的 mailer 注释掉:           'mailer'=>[                    'class ...

  2. [LeetCode] Length of Last Word 求末尾单词的长度

    Given a string s consists of upper/lower-case alphabets and empty space characters ' ', return the l ...

  3. 【leetcode】Length of Last Word

    题目简述 Given a string s consists of upper/lower-case alphabets and empty space characters ' ', return ...

  4. Leetcode: Valid Word Abbreviation

    Given a non-empty string s and an abbreviation abbr, return whether the string matches with the give ...

  5. 【大数据】Linux下Storm(0.9版本以上)的环境配置和小Demo

    一.引言: 在storm发布到0.9.x以后,配置storm将会变得简单很多,也就是只需要配置zookeeper和storm即可,而不再需要配置zeromq和jzmq,由于网上面的storm配置绝大部 ...

  6. POJ 1496 Word Index

    组合数学....和上一题是一样的.... Word Index Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 4303 Acce ...

  7. [LeetCode] Length of Last Word

    Given a string s consists of upper/lower-case alphabets and empty space characters ' ', return the l ...

  8. [LintCode] Length of Last Word 求末尾单词的长度

    Given a string s consists of upper/lower-case alphabets and empty space characters ' ', return the l ...

  9. 18. Word Ladder && Word Ladder II

    Word Ladder Given two words (start and end), and a dictionary, find the length of shortest transform ...

随机推荐

  1. 总结-javascript

    是否可见 $('.btn-accomplish').is(':visible') 通过ajax提交时, {a: vA | ''}; vA没有时,服务器得到的a为"0".如果是两丨, ...

  2. iOS App 无代码入侵的方法hook

    继续Objective-C runtime的研究 最近公司项目在做用户行为分析 于是App端在某些页面切换,交互操作的时候需要给统计系统发送一条消息 在几十个Controller 的项目里,一个一个地 ...

  3. [java]删除数组中的某一个元素

    package org.company.project.test; import java.util.Arrays; import java.util.Scanner; public class Ar ...

  4. iOS缓存功能

    之前做缓存,没有考虑过这个具体的实现. 移动应用在处理网络资源时,一般都会做离线缓存处理,其中以图片缓存最为典型,其中很流行的离线缓存框架为SDWebImage. 但是,离线缓存会占用手机存储空间,所 ...

  5. java 基础三 下雪

    通过repaint()方法进行重画. import javax.swing.JFrame; import javax.swing.JPanel; import java.awt.Graphics; p ...

  6. WCF Security(转载)

    WCF Security 主要包括 "Transfer Security"."Access Control"."Auditing" 几个部分 ...

  7. 命名空间“System.Web”中不存在类型或命名空间名称“Optimization”(是否缺少程序集引用?)

    今天,在.net4.5,mvc4下新建了个区域,运行起来就报这个错误: 命名空间"System.Web"中不存在类型或命名空间名称"Optimization"( ...

  8. 从 IClassFactory 为 CLSID 为 {00024500-0000-0000-C000-000000000046} 的 COM 组件创建实例失败,原因是出现以下错误: 8001010a解决办法

    1.在命令行中输入:dcomcnfg,会显示出“组件服务”管理器 2.打开“组件服务->计算机->我的电脑->DCOM 配置”,找到“Microsoft Word文档”,单击右键,选 ...

  9. XPath 节点

    在 XPath 中,有七种类型的节点:元素.属性.文本.命名空间.处理指令.注释以及文档节点(或称为根节点). XPath 术语 节点(Node) 在 XPath 中,有七种类型的节点:元素.属性.文 ...

  10. c++的重载运算符

    c++中允许重载运算符: 这是我辛苦的结果 #include"iostream"using namespace std;class aaa{ int x;public: aaa() ...