关闭

hibernate第一个实例

标签: hibernate
153人阅读 评论(0) 收藏 举报
分类:

前段时间学习了jdbc的知识,然后决定把hibernate框架学习一下,这里写个helloworld记录一下。

创建数据库

1 create database hibstu;

2 use hibstu;

3 CREATE TABLE user(id INT PRIMARY KEY AUTO_INCREMENT, NAME VARCHAR(20), age INT);

 

创建maven项目,添加以下依赖,版本可以自行选择

<dependency>

            <groupId>org.hibernate</groupId>

            <artifactId>hibernate</artifactId>

            <version>3.1</version>

        </dependency>

        <dependency>

            <groupId>mysql</groupId>

            <artifactId>mysql-connector-java</artifactId>

            <version>5.1.37</version>

        </dependency>

        <dependency>

            <groupId>junit</groupId>

            <artifactId>junit</artifactId>

            <version>4.12</version>

        </dependency>

        <dependency>

            <groupId>javax.transaction</groupId>

            <artifactId>jta</artifactId>

            <version>1.1</version>

        </dependency>

配置hibernate.cfg.xml

 

   <!DOCTYPE hibernate-configuration PUBLIC 

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

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

 

<hibernate-configuration>

    <session-factory>

        <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>

        <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/mybatis</property>

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

        <property name="hibernate.connection.password">xxx</property>

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

        <mapping resource="org/hibernate/tutorial/domain/User.hbm.xml"/>

    </session-factory>

         

</hibernate-configuration> 

 

 

User.hbm.xml

<?xml version="1.0"?>

<!DOCTYPE hibernate-mapping PUBLIC

    "-//Hibernate/Hibernate Mapping DTD 3.0//EN"

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

<hibernate-mapping>

    <class name="org.hibernate.tutorial.domain.User"

            table="user"

    >

        <id name="id"column="id">

            <generator class="native"/>

        </id>

        <property name="name" column="name"/>

        <property name="age" column="age"/>

    </class>

</hibernate-mapping>

 

User

package org.hibernate.tutorial.domain;

 

publicclassUser {

 

    privateintid;

   

    private String name;

   

    privateint  age;

 

    publicint getId() {

        returnid;

    }

 

    publicvoid setId(intid) {

        this.id = id;

    }

 

    public String getName() {

        returnname;

    }

 

    publicvoid setName(String name) {

        this.name = name;

    }

 

    publicint getAge() {

        returnage;

    }

 

    publicvoid setAge(intage) {

        this.age = age;

    }

 

 

}

 

 

测试插入,查找

package org.hibernate.tutorial.test;

import org.hibernate.Session;

import org.hibernate.SessionFactory;

import org.hibernate.cfg.Configuration;

import org.hibernate.tutorial.domain.User;

 

public class HibernateTest {

 

         publicstatic void main(String[] args) {

                 

                  //读取hibernate.cfg.xml文件

                  Configurationcfg = new Configuration().configure();

                 

                  //建立SessionFactory

                  SessionFactoryfactory = cfg.buildSessionFactory();

                 

                  //取得session

                  Sessionsession = null;

                  try{

                          session= factory.openSession();

                          //开启事务

                          session.beginTransaction();

                          Useruser = new User();

                          user.setName("张三");

                          user.setAge(23);

                          //保存User对象

                          session.save(user);

                         

//     先注释掉,空表查找失败的话,由于事务的原因,插入会被回滚吧

//                      Useru1=(User)session.load(User.class,new Integer(1));

//                      System.out.print("从数据库加载数据的用户名为"+u1.getName());

                          //提交事务

                          session.getTransaction().commit();

                  }catch(Exceptione) {

                          e.printStackTrace();

                          //回滚事务

                          session.getTransaction().rollback();

                  }finally{

                          if(session != null) {

                                   if(session.isOpen()) {

                                            //关闭session

                                            session.close();

                                   }

                          }

                  }

         }

}


0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:14128次
    • 积分:395
    • 等级:
    • 排名:千里之外
    • 原创:24篇
    • 转载:6篇
    • 译文:0篇
    • 评论:8条
    最新评论