hibernate——helloworld

来源:互联网 发布:成都余香科技知乎 编辑:程序博客网 时间:2024/06/06 20:33
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>


0 0
原创粉丝点击