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 ...
随机推荐
- 微博mid和id转换
mid为62进制编码,id为常见的10进制编码. id从低位到高位,7个数字为一组,转换为62进制,并顺序合并,即转换为mid. mid从地位到高位,4个字母为一组,转换为10进制,并右移7位,计算和 ...
- test hypertext links for validity, accessibility, and recent modification
https://www.w3.org/Protocols/rfc2616/rfc2616-sec9.html The HEAD method is identical to GET except th ...
- Electronic oscillator
https://en.wikipedia.org/wiki/Electronic_oscillator An electronic oscillator is an electronic circui ...
- (转)百度编辑器java使用
说在前面 工作中会遇到很多需要使用富文本编辑器的地方,比如我现在发布这篇文章离不开这个神器,而且现在网上编辑器太多了.记得之前,由于工作需要自己封装过一个编辑器的公共插件,是用ckeditor改版的, ...
- rem的使用
浏览器的默认字体高是16px. 兼容性: 目前,IE9+,Firefox.Chrome.Safari.Opera 的主流版本都支持了rem. 对于不支持的浏览器,要多写一个绝对单位的声明,这样浏览器就 ...
- UNIX常用命令
以下只说明各常用指令的基本用法, 若需详细说明, 请用 man 去读详细的 manual. 1.关於档案/目录处理的命令 ls--列目录 这是最基本的档案指令. ls 的意义为 "list& ...
- django--models操作
1.models的功能 操作数据库 提交验证 在django的admin中,使用的是modelForms所以在验证的时候,尽管在models后有error_ message参数也不会根据此来提示.具体 ...
- linux配置tomcat以service方式启动(转)
在/etc/init.d目录下新建文件,命名为tomcat 对tomcat文件进行编辑. cat /etc/init.d/tomcat #!/bin/bash # description: Tomca ...
- if语句中的判断条件(nginx)
if语句中的判断条件 正则表达式匹配: ==:等值比较; ~:与指定正则表达式模式匹配时返回"真",判断匹配与否时区分字符大小写: ~*:与指定正则表达 ...
- PostgreSQL ROW_NUMBER() OVER()
转自:http://blog.csdn.net/luojinbai/article/details/45078809 语法: ROW_NUMBER() OVER( [ PRITITION BY co ...