第一个Hibernate项目

一、构建Hibernate项目

    1.新建Java项目HibernateDemo1

    2.导入Hibernate下的jar包(lib->required下的所有jar包)+jdbc驱动包

    3.导入hibernate.cfg.xml文件到src目录下(在Hibernate文件目录中搜索*.cfg.xml)

     配置该文件如下:  

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
        <!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="hibernate.dialect">org.hibernate.dialect.MySQL5InnoDBDialect</property>
            <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
            <property name="hibernate.connection.url">jdbc:mysql:///hibernate_db</property>
            <property name="hibernate.connection.username">root</property>
        <property name="hibernate.connection.password">root</property>
        <property name="hibernate.hbm2ddl.auto">update</property>
        <property name="hibernate.show_sql">true</property>
        <property name="hibernate.format_sql">true</property>
        <mapping resource="com\eduask\pojo\Person.hbm.xml" />
        </session-factory>
        </hibernate-configuration>

     4.建立mysql数据库hibernate_db

    5.在src目录下建两个包com.eduask.pojo、com.eduask.test

      pojo包下建一个Person类(Person.java),内容如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
package com.eduask.pojo;
import java.util.Date;
public class Person {
    private Integer id;
    private String name;
    private int password;
    private Date birthday;
     
    public Person() {}
    public Person(String name, int password, Date birthday) {
        super();
        this.name = name;
        this.password = password;
        this.birthday = birthday;
    }
    @Override
    public String toString() {
        return "Person [id=" + id + ", name=" + name + ", password=" + password + ", birthday=" + birthday + "]";
    }
    public Integer getId() {
        return id;
    }
    public void setId(Integer id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public int getPassword() {
        return password;
    }
    public void setPassword(int password) {
        this.password = password;
    }
    public Date getBirthday() {
        return birthday;
    }
    public void setBirthday(Date birthday) {
        this.birthday = birthday;
    }
     
     
}

    pojo包下引入xml文件Person.hbm.xml,(Hibernate包中搜索),修改内容如下:

1
2
3
4
5
6
7
8
9
10
11
12
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping SYSTEM "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd" >
<hibernate-mapping>
    <class name="com.eduask.pojo.Person" table="t_person">
        <id name="id">
            <generator class="native"/>
        </id>
        <property name="name" column="t_name"></property>
        <property name="password" length="6"></property>
        <property name="birthday"></property>
    </class>
</hibernate-mapping>

    test包中新建测试类HibernateTest.java,内容如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
package com.eduask.test;
import java.util.List;
import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import com.eduask.pojo.Person;
public class HibernateTest {
    public static void main(String[] args) {
        Configuration config = new Configuration().configure();
        SessionFactory factory = config.buildSessionFactory();
        Session session = factory.openSession();
        Transaction tx = session.beginTransaction();
        Person p = new Person("admin",123456,new java.util.Date()); 
        Criteria c = session.createCriteria(Person.class);
        List<Person> lists = c.list();
        System.out.println(lists.get(0).getName());
        tx.commit();
        session.close();
        factory.close();
    }
}

    运行测试类,结果如下:

Hibernate: 

    insert 

    into

        t_person

        (t_name, password, birthday) 

    values

        (?, ?, ?)

八月 11, 2016 5:17:14 下午 org.hibernate.service.jdbc.connections.internal.DriverManagerConnectionProviderImpl stop

INFO: HHH000030: Cleaning up connection pool [jdbc:mysql:///hibernate_db]



同时在数据库中可以看到t_person表已被创建以及插入的响应数据。



本文转自yeleven 51CTO博客,原文链接:http://blog.51cto.com/11317783/1836998

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/393131.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

前端面试常见逻辑题收集及分析

前端面试中常出现一些有趣的逻辑题,初见的时候有可能会手足无措,但实际多看几个题之后就会有一定的思考逻辑,有种打通任督二脉的感觉.以下是我个人面试经历以及网络上收集来的一些经典题目. 题目: 1.现有一个装有无限水的池塘,你手里有两个空壶,一个容积为6升,一个为5升,请问你…

php htaccess实现缓存,使用.htaccess进行浏览器图片文件缓存,_PHP教程

使用.htaccess进行浏览器图片文件缓存&#xff0c;对于图片类网站&#xff0c;每次打开页面都要重新下载图片&#xff0c;慢不说&#xff0c;还非常浪费流量。这时就需要用到缓存&#xff0c;强制浏览器缓存图片文件缓存文件&#xff0c;提问网站访问数度&#xff0c;减少流量消…

leetcode5. 最长回文子串(动态规划)

给定一个字符串 s&#xff0c;找到 s 中最长的回文子串。你可以假设 s 的最大长度为 1000。 示例 1&#xff1a; 输入: “babad” 输出: “bab” 注意: “aba” 也是一个有效答案。 代码 class Solution {public String longestPalindrome(String s) {int ns.length(),max-…

aws v2.2.exe_如何在AWS Elastic Beanstalk上部署Rails 5.2 PostgreSQL应用

aws v2.2.exeby Evrim Persembe通过埃夫里姆佩塞姆贝 如何在AWS Elastic Beanstalk上部署Rails 5.2 PostgreSQL应用 (How to deploy a Rails 5.2 PostgreSQL app on AWS Elastic Beanstalk) It’s official, using Heroku for all my Rails projects so far has spoiled me ro…

学习中遇到的c++问题,持续更新

原文请訪问我的博客&#xff1a;http://xiaoshig.sinaapp.com/ 向上取整 使用ceil函数。ceil(x)返回的是大于x的最小整数。如&#xff1a; ceil(2.5) 3 ceil(-2.5) -2 sort排序头文件#include <algorithm> 数组初始化总结 整型数组初始化&#xff1a;//仅仅能赋值0…

创建邮箱过程中的问题及解决办法

转自白手起家博客 http://bbs.chinaunix.net/forum.php?modviewthread&tid770141 说明一下&#xff1a;Q代表安装过程中遇到的问题&#xff0c;或者是日志中出现的现象。A&#xff1a;代表解决方法。 Q&#xff1a; Jan 13 11:26:29 mail authdaemond: failed to connect …

php的addslashes,PHP addslashes()用法及代码示例

addslashes()函数是PHP中的内置函数&#xff0c;它返回预定义字符前带有反斜杠的字符串。该参数中不包含任何指定的字符。预定义的字符是&#xff1a;单引号(’)双引号(“)反斜杠(\)NULL注意&#xff1a;addslashes()函数不同于addcslashes()函数接受要在其之前添加斜杠的指定字…

如何在React Native中使用Redux Saga监视网络更改

by Pritish Vaidya通过Pritish Vaidya 如何在React Native中使用Redux Saga监视网络更改 (How to monitor network changes using Redux Saga in React Native) 为什么要使用Redux Saga监视网络更改&#xff1f; (Why should I use Redux Saga to monitor Network Changes?) …

leetcode214. 最短回文串(kmp)

给定一个字符串 s&#xff0c;你可以通过在字符串前面添加字符将其转换为回文串。找到并返回可以用这种方式转换的最短回文串。 示例 1: 输入: “aacecaaa” 输出: “aaacecaaa” 代码 class Solution {public int getShortestPalindrome(String s) {//求next数组的最后一…

跟我一起屏蔽百度搜索页面右侧的内容

苦恼百度搜索热点等冗杂信息很久了&#xff0c;然后今天下定决心解决这个问题了。 第一步&#xff1a;搜索&#xff0c;并安装插件Adblock Plus 第二步&#xff1a;使用拦截器 1.打开拦截器 2.具体使用 点击这一块 添加 转载于:https://www.cnblogs.com/smart-girl/p/11058774.…

JavaScript语法详解(三)

一、JavaScript循环语句 1.for循环、for/in 12345678910111213141516<!DOCTYPE html><html lang"en"> <head><meta charset"UTF-8"> <title>Title</title> </head><body><script> var array [1,2,…

鼠标拖拽吸附效果

JavaScript鼠标拖动自动吸附实例 学了几天的JavaScript&#xff0c;自己动手做了一个简单的鼠标拖动的实例&#xff0c;拖动过程中科自动检测与目标容器的距离&#xff0c;在一定的距离范围内可以自动将被拖动的元素加入到目标容器中&#xff0c;希望对开始学习javascript的童鞋…

php修改mysql数据库中的表格,如何修改mysql数据库表?

修改mysql数据库表的方法&#xff1a;使用“ALTER TABLE”语句&#xff0c;可以改变原有表的结构&#xff0c;例如增加字段或删减字段、修改原有字段数据类型、重新命名字段或表、修改表字符集等&#xff1b;语法“ALTER TABLE [修改选项]”。修改数据表的前提是数据库中已经存…

微软最新GDI漏洞MS08-052安全解决方案

微软最新GDI漏洞MS08-052安全解决方案 Simeon微软于九月九日凌晨爆出有史以来最大的安全漏洞MS08-052&#xff0c;通过该漏洞&#xff0c;攻击者可以将木马藏于图片中&#xff0c;网民无论是通过浏览器浏览、还是用各种看图软件打开、或者在即时聊天窗口、电子邮件、Office文档…

DEM轨迹后处理

方法一&#xff1a;直接在paraview中显示 首先在输出颗粒信息的时候保存global ID&#xff1a;然后在paraview中导入vtp数据&#xff08;不要导入pvd&#xff09;&#xff0c;并使用Temporal Particle To Pathlines这个filter&#xff08;可以直接ctrlspace调出搜索框搜索&…

Oracle的JDBC Url的几种方式

1.普通SID方式jdbc:oracle:thin:username/passwordx.x.x.1:1521:SID2.普通ServerName方式 jdbc:Oracle:thin:username/password//x.x.x.1:1522/ABCD3.RAC方式jdbc:oracle:thin:(DESCRIPTION(ADDRESS_LIST(ADDRESS(PROTOCOLTCP)(HOSTx.x.x.1)(PORT1521))(ADDRESS(PROTOCOLTCP)(H…

leetcode945. 使数组唯一的最小增量(排序)

给定整数数组 A&#xff0c;每次 move 操作将会选择任意 A[i]&#xff0c;并将其递增 1。 返回使 A 中的每个值都是唯一的最少操作次数。 示例 1: 输入&#xff1a;[1,2,2] 输出&#xff1a;1 解释&#xff1a;经过一次 move 操作&#xff0c;数组将变为 [1, 2, 3]。 代码 …

数据科学 python_如何使用Python为数据科学建立肌肉记忆

数据科学 pythonby Zhen Liu刘震 首先&#xff1a;数据预处理 (Up first: data preprocessing) Do you feel frustrated by breaking your data analytics flow when searching for syntax? Why do you still not remember it after looking up it for the third time?? It…

oracle 管道通信,oracle管道化表函数

转自&#xff1a;http://pengfeng.javaeye.com/blog/260360在我所做过和参与的大多数项目中,都会有用户提出的复杂的一些统计报表之内的功能要求,根据统计的复杂程度、效率及JAVA程序调用的方便性方面考虑,主要总结出以下几种方案&#xff1a; 1、SQL语句 该方案只能实现一些相…

ebtables之BROUTING和PREROUTING的redirect的区别

ebtables和iptables实用工具都使用了Netfilter框架&#xff0c;这是它们一致的一方面&#xff0c;然而对于这两者还真有一些需要联动的地方。很多人不明白ebtales的broute表的redirect和nat表PREROUTING的redirect的区别&#xff0c;其实只要记住两点即可&#xff0c;那就是对于…