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 ...
随机推荐
- C#网络编程之---TCP协议的同步通信(二)
上一篇学习日记C#网络编程之--TCP协议(一)中以服务端接受客户端的请求连接结尾既然服务端已经与客户端建立了连接,那么沟通通道已经打通,载满数据的小火车就可以彼此传送和接收了.现在让我们来看看数据的 ...
- MVC第二天
一.过滤器filter 注意:如果继承自接口需要让类实现FilterAttribute,才可以作为特性使用使用方式1:作为Controller或Action的特性使用方式2:在Global中注册为全局 ...
- NOI上看到的几个小学奥数
:余数相同问题 查看 提交 统计 提问 总时间限制: 1000ms 内存限制: 65536kB 描述 已知三个正整数 a,b,c. 现有一个大于1的整数x,将其作为除数分别除a,b,c,得到的余数相同 ...
- ECharts饼图试玩
处理类似提交问卷的数据,要生成图表,用了ECharts,好方便的. 简陋效果: 1.表单存储 有单选和多选题,单选直接存储各选项数字值,1,2,3,4...中一个:多选用|分隔存储选项值,如1|3,2 ...
- kafka总结
近期在做kafka metrics. 参考了几个开源的项目,诸如kafka manager, Burrow, kafkaOffsetMonitor,东西都很不错,可惜没有一个是用java编写的,最终自 ...
- drop表后仍占表空间解决办法
练习oracle时create了很多表,drop表后select * from tab; 网上找了好些方法,但是好多都适用... SQL>purge recyclebin; 回收站已清空.
- zookeeper清除事物日志
dataDir=/data/zookeeper/data dataLogDir=/data/zookeeper/log zk事物日志(快照)存放目录,高负荷工作的时候,会产生大量的日志,需 ...
- PHP无限极分类,多种方法|很简单,这里说的很详细,其它地方说的很不好懂
当你学习php无限极分类的时候,大家都觉得一个字"难"我也觉得很难,所以,现在都还在看,因为工作要用到,所以,就必须得研究研究. 到网上一搜php无限极分类,很多,但好多都是一 ...
- NGINX、PHP-FPM开机自动启动
NGINX SHELL脚本 放到/etc/init.d/下取名nginx #!/bin/sh # # nginx - this script starts and stops the nginx ...
- C# 去掉List重复元素的方法
因为用到list,要去除重复数据,尝试了几种方法.记录于此... 测试数据: List<string> li1 = new List<string> { "8&quo ...