hibernate——helloworld

本文介绍了一个简单的Java应用程序如何利用Hibernate框架实现用户的增删改查(CRUD)操作。通过具体的代码示例展示了如何配置Hibernate连接数据库、保存及获取User对象。
package cn.icss.a_helloworld;

public class User {
    private int id;
    private String name;
    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;
    }
    @Override
    public String toString() {
        return "[User: id="+id+",name="+name+"]";
    }
    
    
}

====================================

package cn.icss.a_helloworld;

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

public class App {
    private static SessionFactory sessionFactory;
    static{
        Configuration cfg = new Configuration();
        cfg.configure("hibernate.cfg.xml");//读取指定的主配置文件
        sessionFactory = cfg.buildSessionFactory();//根据配置生成session工厂
    }
    
    @Test
    public void testSave(){
        User user = new User();
        user.setName("zhangsan");
        
        //保存?
        Session session = sessionFactory.openSession();//打开一个新的session
        Transaction tx = session.beginTransaction();//开始事务
        
        session.save(user);
        
        tx.commit();//提交事务
        
        session.close();//关闭session,释放资源
    }
    
    @Test
    public void testGet(){
        Session session = sessionFactory.openSession();
        Transaction tx = session.beginTransaction();
        
        
        User user = (User) session.get(User.class, 2);//获取
        System.out.println(user);
        
        tx.commit();
        session.close();
    }
}

=========================================


<!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 name="foo">
        <!-- 配置数据库信息 -->
        <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
        <property name="connection.url">jdbc:mysql:///hibernate_20140422</property>
        <property name="connection.driver_class">com.mysql.jdbc.Driver</property>
        <property name="connection.username">root</property>
        <property name="connection.password">root</property>
        <!-- 其他配置 -->
        <!-- 显示生成的sql语句 -->
        <property name="show_sql">true</property>
        <!-- 导入映射文件  -->
        <mapping resource="cn/icss/a_helloworld/User.hbm.xml" />
    </session-factory>
</hibernate-configuration>

=================================

<?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 package="cn.icss.a_helloworld">
    <class name="User" table="t_user">
        <id name="id" type="int" column="id">
            <generator class="native"/>
        </id>
        <property name="name" type="string" column="name" />
    </class>
</hibernate-mapping>


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值