hibernate例子

首先下载好hibernate包

如果运行之后出现数据库连接错误  则重新把jar包位置放在web-inf的lib下面再addbulidpath

第一步:创建实体类

Hibernate要求实体类必须提供一个不带参数的默认构造方法。因为程序运行时,Hibernate会运用java的反射机制,创建实体类的实例。

 

package hibernateDemo;
 
public class User {
    private int id;   
    private String name;
    private String password;
    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getPassword() {
        return password;
    }
    public void setPassword(String password) {
        this.password = password;
    }
 
}

第二步:在MySql中创建对应的数据表

 CREATE DATABASE hibernate;
            USE hibernate;
            CREATE TABLE USER(
                id INT PRIMARY KEY AUTO_INCREMENT,
                name VARCHAR(50),
                password VARCHAR(50) 
            );

第三步:创建Hibernate的配置文件,放在src目录下,文件名为hibernate.cfg.xml

<?xml version="1.0" encoding="UTF-8"?>
<!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="connection.driver_class">com.mysql.jdbc.Driver</property>
        <property name="connection.url">jdbc:mysql://127.0.0.1:3306/hibernate_database</property>
        <property name="connection.username">root</property>
        <property name="connection.password">admin</property>

        <!-- 配置数据库方言 -->
        <property name="dialect">org.hibernate.dialect.MySQLDialect</property>
        <!-- 全局 -->
        <property name="hibernate.hbm2ddl.auto">update</property>

         <property name="show_sql">true</property><!-- //显示sql语句 -->
         <property name="format_sql">true</property><!-- //格式化sql语句 -->
         
            <!--            没有这句话不会改变数据库 -->
         <property name="hibernate.connection.autocommit">true</property>


 
    <mapping resource="hibernateDemo/User.hbm.xml"/><!--  映射信息 -->
 
</session-factory>
</hibernate-configuration> 

第四步:创建对象-关系映射文件 该文件应该和实体类在同一目录下。命名规则为 实体类名.hbm.xml 例如User.hbm.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC 
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
 
<hibernate-mapping>
<class name="hibernateDemo.User" table="user">
    <id name="id" column="id">
        <generator class="native"></generator>
    </id>
    <property name="name" column="name"></property>
    <property name="password" column="password"></property>
</class>
</hibernate-mapping> 

第五步:测试类

package test;

import hibernateDemo.User;


import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.junit.Test;
import org.hibernate.Transaction;

 public class test {
     
     Transaction tx;
                @Test
                public void test1() {
                    //读取配置文件
                    Configuration conf=new Configuration().configure();
                    //根据配置创建factory
                    SessionFactory sessionfactory=conf.buildSessionFactory();
                    //获得操作数据库的session对象
                    Session session=sessionfactory.openSession(); 
                    //创建对象新增
                  User u=new User();
                   u.setName("liu三");
                   u.setPassword("123456");
                   //将对象保存到数据库
                    session.save(u);
                    
                 // 查询方式1
//                     User user = (User)session.get(User.class, 2);
//                    System.out.println(user.getName());
                    
          
                    //修改
                 /*   User user = (User)session.get(User.class, 2);
                    user.setName("李王二");
                    Transaction tx=session.beginTransaction();
                    session.update(user);
                    tx.commit();*/
                    
                    //删除
                  /*  User user = (User)session.get(User.class, 4);
                    Transaction tx=session.beginTransaction();
                    session.delete(user);
                    tx.commit();*/
                    
                    
                    //关闭资源
                    session.close();
                    sessionfactory.close();
                }
 
}

 

posted @ 2019-02-22 17:11  MagicAsa  阅读(183)  评论(0编辑  收藏  举报