hibernate入门实例(一)(初学者)

来源:互联网 发布:php get请求 编辑:程序博客网 时间:2024/06/05 06:55
<span style="font-family: Arial, Helvetica, sans-serif; background-color: rgb(255, 255, 255);">最近学者框架知识</span>

这是我做的hibernate只是框架

1、Hibernate包下载地址:
http://prdownloads.sourceforge.net/hibernate/?sort_by=date&sort=desc



1、首先先建立一个java项目 添加jjar ,我的目录结构


建立包 ,复制在下载的hibernate包中的hibernate.cfg.xml

<!--  ~ Hibernate, Relational Persistence for Idiomatic Java  ~  ~ License: GNU Lesser General Public License (LGPL), version 2.1 or later.  ~ See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.  --><!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><!-- 配置数据库信息 --> <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property><property name="connection.url">jdbc:mysql:///spring</property><property name="connection.driver_class">com.mysql.jdbc.Driver</property><property name="connection.username">root</property><property name="hibernate.connection.password">123456</property><!-- 其他配置 --><property name="hibernate.show_sql">true</property><property name="hibernate.format_sql">false</property><!-- create:先删除,再创建update:如果表不存在就创建,不一样就更新,一样就什么都不做。create-drop:初始化时创建表,SessionFactory执行close()时删除表。validate:验证表结构是否一致,如果不一致,就抛异常。 --><property name="hbm2ddl.auto">update</property><!-- 导入映射文件 是“/”不是“.” --><mapping resource="com/idfar/hibernate/test/User.hbm.xml"/></session-factory></hibernate-configuration>

建立个User.java

package com.idfar.hibernate.test;/** * 实体 *  * @author tyg *  */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;}@Overridepublic String toString() {return "[User: id=" + id + ", name=" + name + "]";}}
</pre><p></p><p><strong><span style="font-size:18px;">user表的映射文件</span></strong></p><p>User.hbm.xml</p><p><pre name="code" class="html"><?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="com.idfar.hibernate.test"><class name="User" table="t_user"><id name="id" type="int" column="id">            <generator class="native"/></id><property name="name" type="string" column="name" length="20"/></class></hibernate-mapping>

测试AddTest.java类

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


0 0
原创粉丝点击