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文件,这是配置文件,包含数据库的信息之类的

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <!DOCTYPE hibernate-configuration PUBLIC
 3         "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
 4         "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
 5 <hibernate-configuration>
 6     <session-factory>
 7
 8         <!-- 配置连接数据库的基本信息 -->
 9         <property name="connection.username">数据库用户名</property>
10         <property name="connection.password">数据库密码</property>
11         <property name="connection.driver_class">com.mysql.jdbc.Driver</property>
12         <property name="connection.url">jdbc:mysql://localhost:3306/数据库名</property>
13
14         <!-- 配置Hibernate基本信息 -->
15         <!-- Hibernate所使用得数据库方言 -->
16         <property name="dialect">org.hibernate.dialect.MySQLInnoDBDialect</property>
17
18         <!-- 执行操作时是否在控制台打印sql -->
19         <property name="show_sql">true</property>
20
21         <!-- 是否对sql进行格式化 -->
22         <property name="format_sql">true</property>
23
24         <!-- 指定自动生成数据表的策略 -->
25         <property name="hbm2ddl.auto">update</property>
26
27         <!-- 指定关联的.hbm.xml文件 -->
28         <mapping resource="com/biubiu/domain/UserInfo.hbm.xml"/>
29         <mapping class="com.biubiu.domain.UserInfo"/>
30
31     </session-factory>
32 </hibernate-configuration>

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

 1   @Test
 2     public void test() {
 3         //1.创建一个SessionFactory对象
 4         SessionFactory seFactory = null;
 5         final StandardServiceRegistry registry = new StandardServiceRegistryBuilder()
 6                 .configure() // configures settings from hibernate.cfg.xml
 7                 .build();
 8         try{
 9             seFactory = new MetadataSources( registry ).buildMetadata()
10                        .buildSessionFactory();
11         } catch(Exception e){
12             StandardServiceRegistryBuilder.destroy( registry );
13         }
14
15
16         //2.创建一个Session对象
17
18         Session session = seFactory.openSession();
19
20         //3.开启事务
21         session.beginTransaction();
22         //4.执行操作
23
24         session.save(new UserInfo("[email protected]","测试",
25                 "icuicu","0","img","tianmao"));
26
27         /*
28         //!!!!写hql语句,from 类名(区分大小写)
29         String hql = "from UserInfo";
30         @SuppressWarnings("unchecked")
31         List<UserInfo> list = session.createQuery(hql).list();
32         for(UserInfo u : list){
33             System.out.println(u);
34         }
35         */
36         //5.提交事务
37         session.getTransaction().commit();
38         //6.关闭Session
39         session.close();
40         //7.关闭SessionFactory
41         seFactory.close();
42
43     }

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

时间: 2024-08-06 20:04:59

Hibernate5.1.0的hello word的相关文章

使用NPOI2.0+版本导出word

原文:http://www.cnblogs.com/afutureBoss/p/4074397.html?utm_source=tuicool&utm_medium=referral XWPFDocument doc = new XWPFDocument(); //创建新的word文档 XWPFParagraph p0 = doc.CreateParagraph(); //向新文档中添加段落 p0.SetAlignment(ParagraphAlignment.LEFT); //段落对其方式为居

LeetCode58 Length of Last Word

题目: Given a string s consists of upper/lower-case alphabets and empty space characters ' ', return the length of last word in the string. If the last word does not exist, return 0. Note: A word is defined as a character sequence consists of non-space

Java [Leetcode 58]Length of Last Word

题目描述: Given a string s consists of upper/lower-case alphabets and empty space characters ' ', return the length of last word in the string. If the last word does not exist, return 0. Note: A word is defined as a character sequence consists of non-spa

【Leetcode】【Easy】Length of Last Word

Given a string s consists of upper/lower-case alphabets and empty space characters ' ', return the length of last word in the string. If the last word does not exist, return 0. Note: A word is defined as a character sequence consists of non-space cha

leetcode Word Ladder II

和上题 Word Ladder I题目差不多,不过这里是要记录所有最段路径的可能. 不同点在于,我们不能再BFS每层的时候把相距一个字符的字符串在dict中删除,因为hot -> hit 的话其他的例如 jit -> hit 就是hit可以出现在两条路径里头.所以不能立马删除.但是我们发现我们可以删除的是我们遍历完的每层的字符串,我们用known来存遍历完的层,unknown来存没有遍历的层,那么每次求得下一层unknown的时候,就首先把known里面有的从dic中删除. 主要思路还是和上一

word相关转换类库

using System;using System.Collections.Generic;using System.Text;using Microsoft.Office.Interop.Word;using System.IO;using System.Web;using System.Data;using System.Reflection;using Microsoft.Win32;using System.Text.RegularExpressions;using System.Net

LeetCode Length of Last Word

1. 题目 Given a string s consists of upper/lower-case alphabets and empty space characters ' ', return the length of last word in the string. If the last word does not exist, return 0. Note: A word is defined as a character sequence consists of non-spa

leetcode || 58、Length of Last Word

problem: Given a string s consists of upper/lower-case alphabets and empty space characters ' ', return the length of last word in the string. If the last word does not exist, return 0. Note: A word is defined as a character sequence consists of non-

leetCode 58. Length of Last Word 字符串

58. Length of Last Word Given a string s consists of upper/lower-case alphabets and empty space characters ' ', return the length of last word in the string. If the last word does not exist, return 0. Note: A word is defined as a character sequence c