首先hibernate.cfg.xml构造,在该文件src文件夹

<!DOCTYPE hibernate-configuration PUBLIC  

            "-//Hibernate/Hibernate Configuration DTD 3.0//EN"  

            "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">  

    <hibernate-configuration>

    <session-factory>

        <!-- Database connection settings -->

        <property name="connection.driver.class">com.mysql.jdbc.Driver</property>

        <property name="connection.url">jdbc:mysql://192.168.1.81:3306/test</property>

        <property name="connection.username">root</property>

        <property name="connection.password">123456</property>



        <!-- JDBC connection pool (use the built-in) -->

        <property name="connection.pool_size">1</property>



        <!-- SQL dialect -->

        <property name="dialect">org.hibernate.dialect.MySQL5Dialect</property>



        <!-- Enable Hibernate's automatic session conttext management -->

        <property name="current_session_context_class">thread</property>



        <!-- Disable the second-level cache -->

        <property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>



        <!-- Echo all executed SQL to show -->

        <property name="show_sql">true</property>



        <!-- Format SQL -->

        <property name="format_sql">true</property>



        <!-- Drop and re-create the database schema on startup -->

        <property name="hbm2ddl.auto">update</property>



        <mapping resource="com/bjsxt/hibernate/Student.hbm.xml"/>

    </session-factory>

</hibernate-configuration>

Student.java 文件

package com.bjsxt.hibernate;



public class Student {

    private String uname;

    private int age;

    private String sex;



    public String getUname() {

        return uname;

    }



    public void setUname(String uname) {

        this.uname = uname;

    }



    public int getAge() {

        return age;

    }



    public void setAge(int age) {

        this.age = age;

    }



    public String getSex() {

        return sex;

    }



    public void setSex(String sex) {

        this.sex = sex;

    }



}

Student.hbm.xml的配置

<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd" >  

    <hibernate-mapping>  

        <class name="com.bjsxt.hibernate.Student" table="userinfo">  

            <id name="uname"></id>  

            <property name="age"/>  

            <property name="sex"/>  

        </class>  

    </hibernate-mapping>

測试的Test文件

package com.bjsxt.hibernate;



import java.util.List;



import org.hibernate.Query;

import org.hibernate.Session;

import org.hibernate.SessionFactory;

import org.hibernate.cfg.AnnotationConfiguration;



public class Test {

    public static void main(String args[]) {

        query();

    }



    // 插入数据

    public static void insert() {

        Student s = new Student();

        s.setUname("xiaoming");

        s.setSex("女");

        s.setAge(13);

        SessionFactory sessionFactory = new AnnotationConfiguration()

                .configure().buildSessionFactory();

        Session session = sessionFactory.getCurrentSession();

        session.beginTransaction();

        session.save(s);

        session.getTransaction().commit();



    }



    // 删除数据

    public static void delete() {

        Student s = new Student();

        s.setUname("xiaoming");

        SessionFactory sessionFactory = new AnnotationConfiguration()

                .configure().buildSessionFactory();

        Session session = sessionFactory.getCurrentSession();

        session.beginTransaction();

        session.delete(s);

        ;

        session.getTransaction().commit();



    }



    // 改动数据

    public static void updata() {

        SessionFactory sessionFactory = new AnnotationConfiguration()

                .configure().buildSessionFactory();

        Session session = sessionFactory.getCurrentSession();

        session.beginTransaction();

        Student s = (Student) session.get(Student.class, "xiaoming");

        if (s != null) {

            s.setSex("女");

            s.setAge(100);

            session.update(s);

            ;

            session.getTransaction().commit();

        }

    }



    // 查询数据

    public static void query() {

        SessionFactory sessionFactory = new AnnotationConfiguration()

                .configure().buildSessionFactory();

        Session session = sessionFactory.getCurrentSession();

        session.beginTransaction();

        Query query = session

                .createQuery("from Student where uname='小明' ");

        List<Student> list = query.list();

        for(Student s:list){

            System.out.println(s.getAge()+"\n");

        }

    }



}

版权声明:本文博主原创文章,博客,未经同意不得转载。

hibernate 基本和简单易用的更多相关文章

  1. FineBI:一个简单易用的自助BI工具

    过去,有关企业数据分析的重担都压在IT部门,传统BI分析更多面向的是具有IT背景的人员.但随着业务分析需求的增加,很多公司都希望为业务用户提供自助分析服务,将分析工作落实到业务人员手中.但同时,分析工 ...

  2. DataAccess通用数据库访问类,简单易用,功能强悍

    以下是我编写的DataAccess通用数据库访问类,简单易用,支持:内联式创建多个参数.支持多事务提交.支持参数复用.支持更换数据库类型,希望能帮到大家,若需支持查出来后转换成实体,可以自行扩展dat ...

  3. 快速上手seajs——简单易用Seajs

    快速上手seajs——简单易用Seajs   原文  http://www.cnblogs.com/xjchenhao/p/4021775.html 主题 SeaJS 简易手册 http://yslo ...

  4. [.net 面向对象程序设计进阶] (22) 团队开发利器(一)简单易用的代码管理工具VSS

    [.net 面向对象程序设计进阶] (22) 团队开发利器(一)简单易用的代码管理工具VSS 本篇要点:在进阶篇快要结束的时候说说源代码管理器,我们的开发,不是一个人可以完成的事,团队协作很重要,而且 ...

  5. 设计与开发一款简单易用的Web报表工具(支持常用关系数据及hadoop、hbase等)

    EasyReport是一个简单易用的Web报表工具(支持Hadoop,HBase及各种关系型数据库),它的主要功能是把SQL语句查询出的行列结构转换成HTML表格(Table),并支持表格的跨行(Ro ...

  6. 分享一个简单易用的RPC开源项目—Tatala

    http://zijan.iteye.com/blog/2041894 这个项目最早(2008年)是用于一个网络游戏的Cache Server,以及一个电子商务的Web Session服务.后来不断增 ...

  7. ZOOM - 简单易用的 jQuery 照片相册插件

    jQuery 最令人印象深刻的应用之一就是对图片的处理,它可以让帮助你在你的项目中加入一些让人惊叹的图片切换效果.ZOOM 是一款全屏效果的 jQuery 图片切换展示插件,支持键盘前后按键切换,支持 ...

  8. BasicModal - 简单易用的现代 Web App 弹窗

    BasicModal 是为现代 Web 应用程序打造的弹窗系统.它包括所有你需要显示的信息,问题或接收用户的输入.这里的弹窗还可以链接起来,所以你可以很容易地建立一个预定义顺序的安装帮助或显示对话框. ...

  9. PNotify – 简单易用的 JS 通知,消息提示插件

    PNotify 是一个 JavaScript 通知插件,前身为 Pines Notify.它旨在提供无与伦比的灵活性,同时很容易使用.它可以提供无阻塞的通知,允许用户无需关闭通知或者提示信息就可以点击 ...

随机推荐

  1. ThinkPHP框架视图详细介绍 View 视图--模板(九)

    原文:ThinkPHP框架视图详细介绍 View 视图--模板(九) 视图也是ThinkPHP使用的核心部分: 一.模板的使用 a.规则 模板文件夹下[TPL]/[分组文件夹/][模板主题文件夹/]和 ...

  2. 以JTextPanel为例Swing的鼠标事件详解

    如下界面可以通过该界面研究一下Swing的鼠标事件: 图中用红粗线圈起来的为JtextPanel,该Panel添加了鼠标事件监听器,鼠标事件监听器有三种,分别为MouseWheelListener,M ...

  3. Oracle“记录被另一个用户锁住” 无法更新删除的解决办法

    1.查看数据库锁,诊断锁的来源及类型: select object_id,session_id,locked_mode from v$locked_object; 或者用以下命令: select b. ...

  4. php 多进程中的信号问题

    1.以下代码sleep时间远小于20 <?php // 当子进程退出时,会触发该函数 function sig_handler($sig) { switch($sig) { case SIGCH ...

  5. c++程序猿经典面试题

    1.请问i的值会输出什么? #include"iostream.h" int i=1; void main() { int i=i; cout<<i<<en ...

  6. Oracle数据库案例整理-Oracle系统执行时故障-Shared Pool内存不足导致数据库响应缓慢

    1.1       现象描写叙述 数据库节点响应缓慢,部分用户业务受到影响. 查看数据库告警日志,開始显示ORA-07445错误,然后是大量的ORA-04031错误和ORA-00600错误. 检查数据 ...

  7. php获取分类以下的全部子类方法

    获取分类以下的全部子类方法: static function getMenuTree($arrCat, $parent_id = 0, $level = 0,$all=True) { static $ ...

  8. ubuntu linux 13.04更新

    首先备份源列表: sudo cp /etc/apt/sources.list /etc/apt/sources.list_backup 而后用gedit或其他编辑器打开: gksu gedit /et ...

  9. Oracle 数据迁移(从Oracle11G迁移到更高的版本号Oracle10G低版本号)

    1.数据库状况    生产环境是11G,linux系统,測试环境是10G,windows系统,须要从生产环境导出一个用户下全部的数据,导入測试环境中. 由于数据量比較小,准备採用EXP和IMP工具来做 ...

  10. C++用于修饰的keyword

    1.const修饰指针 (1)const int *p=&x:不可改动*p的内容,但能够让指针p指向还有一个地址,和int const *p一样 (2)int *const p=&x: ...