Hibernate5.1.0的hello word
新建一个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的更多相关文章
- yii2.0 发送邮件带word小附件
把 common/config/main-local.php 下的 mailer 注释掉: 'mailer'=>[ 'class ...
- [LeetCode] Length of Last Word 求末尾单词的长度
Given a string s consists of upper/lower-case alphabets and empty space characters ' ', return the l ...
- 【leetcode】Length of Last Word
题目简述 Given a string s consists of upper/lower-case alphabets and empty space characters ' ', return ...
- Leetcode: Valid Word Abbreviation
Given a non-empty string s and an abbreviation abbr, return whether the string matches with the give ...
- 【大数据】Linux下Storm(0.9版本以上)的环境配置和小Demo
一.引言: 在storm发布到0.9.x以后,配置storm将会变得简单很多,也就是只需要配置zookeeper和storm即可,而不再需要配置zeromq和jzmq,由于网上面的storm配置绝大部 ...
- POJ 1496 Word Index
组合数学....和上一题是一样的.... Word Index Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 4303 Acce ...
- [LeetCode] Length of Last Word
Given a string s consists of upper/lower-case alphabets and empty space characters ' ', return the l ...
- [LintCode] Length of Last Word 求末尾单词的长度
Given a string s consists of upper/lower-case alphabets and empty space characters ' ', return the l ...
- 18. Word Ladder && Word Ladder II
Word Ladder Given two words (start and end), and a dictionary, find the length of shortest transform ...
随机推荐
- 短期连载 第1回 万代南梦宫工作室动画的流派 BNS动作捕捉汐留工作室的采访
原文链接:http://cgworld.jp/interview/201607-bandainamco.html 因开发了[偶像大师],[铁拳],[XX传说]系列等各种游戏而被广为人知的万代南 ...
- 「iOS造轮子」之UIButton 用Block响应事件
俗语说 一个不懒的程序员不是好程序员 造轮子,也只是为了以后更好的coding. coding,简易明了的代码更是所有程序员都希望看到的 无论是看自己的代码,还是接手别人的代码 都希望一看都知道这代码 ...
- MyEclipse 快捷键
MyEclipse 快捷键1(CTRL) Ctrl+1 快速修复Ctrl+D: 删除当前行Ctrl+Q 定位到最后编辑的地方Ctrl+L 定位在某行Ctrl+O 快速显示 OutLineCtrl+T ...
- android studio 2.0 GPU Debugger使用说明
GPU Debugger GPU Debugging Tools The GPU debugging tools are an experimental feature intended to hel ...
- goprotocbuf的安装和使用
首先得到 protobuf 相应的包文件 ,在终端上输入如下 wget http://protobuf.googlecode.com/files/protobuf-2.5.0.tar.gz 下载完毕后 ...
- 使用"关键词"来整理自己的知识库
以前一直使用MyBase作为整理知识的工具,但是用到后来发现,当整理的知识越来越多时,树形目录的节点个数就会变得越来越庞大,层次越来越深,反而很难定位要查看或编辑的知识.最近发现使用"关键字 ...
- 阻止网页内部滚动条mousewheel事件冒泡
function preventScroll(id){ var _this = document.getElementById(id); if(navigator.userAgent.indexOf( ...
- 4,4s屏幕过渡页处理
//#import "sys/utsname.h" struct utsname systemInfo; uname(&systemInfo); NSString *dev ...
- NSSet
版权声明:本文为博主原创文章,未经博主允许不得转载. 一.NSSet的创建和初始化 创建一个空的NSSet集合:+ (void)set; 用数组来创建NSSet集合:+ (id)setWithArra ...
- swift language
API reference Swift UIKit Swift 菜鸟教程 Great Installed Visual Studio Code, I found I cannot open it fr ...