步骤一 java
首先在数据库中创建一数据库 Person sql
步骤二 数据库
在myEclipse 中创建一普通java工程 HibernateTest session
创建一lib文件夹拷贝须要的相应hibernate中的jar包 app
其中最后一个是 数据库驱动包 ,而后从hibernate源文件那个解压文件中的project 中的etc(配置文件)文件夹中 拷贝hiberna.cfg.xml ssh
把hiberna.cfg.xml 拷贝到config中 打开以后 删除<session-Factory name=”foo”中的全部内容> 首先来配置主要的 hiberna.cfg.xml 配置这个的时候咱们要参考 hibernate.properties sqlserver
由于我是用的sqlserver的因此选择这个 测试 |
配置完后以下所示 ui
<hibernate-configuration>
<session-factory name="foo">
<property name="show_sql">true</property>
<property name="hibernate.dialect">org.hibernate.dialect.SQLServerDialect</property>
<property name="hibernate.connection.username">sa</property>
<property name="hibernate.connection.password">123456</property>
<property name="hibernate.connection.driver_class">com.microsoft.sqlserver.jdbc.SQLServerDriver</property>
<property name="hibernate.connection.url">jdbc:sqlserver://localhost:1433;databaseName=Person</property>
<property name="hibernate.hbm2ddl.auto">update</property>
<hibernate-configuration>
<session-factory name="foo">
<property name="show_sql">true</property>
<property name="hibernate.dialect">org.hibernate.dialect.SQLServerDialect</property>
<property name="hibernate.connection.username">sa</property>
<property name="hibernate.connection.password">123456</property>
<property name="hibernate.connection.driver_class">com.microsoft.sqlserver.jdbc.SQLServerDriver</property>
<property name="hibernate.connection.url">jdbc:sqlserver://localhost:1433;databaseName=Person</property>
<property name="hibernate.hbm2ddl.auto">update</property> url
<!-- 这个是最后添加创建,配置好的Person.hbm.xml-->
<mapping resource="com/hibernate/test/Person.hbm.xml"/>
</session-factory>
</hibernate-configuration>
切记这个也能够在hibernate.properties中能够找到
#hibernate.hbm2ddl.auto create-drop
#hibernate.hbm2ddl.auto create
#hibernate.hbm2ddl.auto update
#hibernate.hbm2ddl.auto validate
第一个貌似是建立以后就删除掉
第二个是本次建立的时候就删除上次建立的
第三个为更新 就是就算你上次建立过的本次建立添加了也不会删除上次的内容
因此这里咱们选择update
最后咱们来建立表中所须要的内容 由于只是作个测试 因此简单的建立一个类
TestCreateDB
建立完以后 咱们就能够去配置关于这个类的 hibernate配置文件了
这就须要拷贝 ect中的employee.hbm.xml 拷贝到当前类的包的下面 而且更名为 类名.cfg.xml(Person.cfg.cml)
而后咱们来配置Person.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="com.hibernate.test.Person" table="tb_Person">
<id name="pid" type="integer">
<generator class="native"></generator>
</id>
<property name="uname" type="string">
</property>
<property name="upwd" type="string">
</property>
</class>
</hibernate-mapping>
这个相信你们一看就能明白吧 <property>中的name值就是表中的字段名 type就是字段的属性
注意的是第一个 是建立的这张表的主键 自增加 (好像hibernate中是强制这样要求的) 第一列 class 就是当前类的全类名 table 就是须要创建的表的名字 好了到这里了准备工做差很少就作完了
最后 咱们用junit(昨天才学的)进行测试
建立一个测试类
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.junit.Test;
import com.hibernate.test.Person;
public class TestCreateTable {
private static Configuration cf;
//这里为了之后方便 一次建立了 Configuration
static{
cf=new Configuration();
//这里由于我建立了一个config sourc folder 里面有一个hibernate的配置文件
//(hibernate.cfg.xml)
cf.configure("hibernate/hibernate.cfg.xml");
}
public static SessionFactory getSessionFactory(){
/返回sessionFactory
return cf.buildSessionFactory();
}
public void testCreateDb(){
//开启session
Session session=getSessionFactory().openSession();
//开启事务(必定要开启事务和提交事务,否则不会成功)
Transaction tr=session.beginTransaction();
Person person=new Person();
person.setUname("张三");
person.setUpwd("123");
//保存到数据中
session.save(person);
tr.commit();
}
}
到这里就成功的完成了
嘿嘿 第一次发这种本身感受有点点技术含量的帖子,不知道有哪里作得很差的 望你们指出啊。之后我会改正的。
(才学ssh,不少东西不懂,因此就把最近所学的一些东西放上来 供你们参考和指出不正确的地方)