Hibernate 学习过程


1、简单例子

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"
>

<!--  Generated by MyEclipse Hibernate Tools.                    -->
< hibernate-configuration >

< session-factory >
   
< property  name ="connection.username" > root </ property >
   
< property  name ="connection.url" > jdbc:mysql://localhost:3306/test </ property >
   
< property  name ="dialect" > org.hibernate.dialect.MySQLDialect </ property >
   
< property  name ="myeclipse.connection.profile" > MySql </ property >
   
< property  name ="connection.password" > root </ property >
   
< property  name ="connection.driver_class" > com.mysql.jdbc.Driver </ property >
   
< property  name ="show_sql" > true </ property >
   
< property  name ="transaction.factory_class" > org.hibernate.transaction.JDBCTransactionFactory </ property >
   
< property  name ="cache.provider_class" > org.hibernate.cache.NoCacheProvider </ property >
   
< mapping  resource ="com/study/hibernate/Person.hbm.xml"   />

</ session-factory >

</ hibernate-configuration >


Person.hbm.xml//与数据库进行字段映射、也可用Annotation

<? xml version="1.0" ?>
<! DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"
>
<!--
Mapping file autogenerated by MyEclipse - Hibernate Tools
-->
< hibernate-mapping >
< class  name ="com.study.hibernate.Person"  table ="person"
   catalog
="test" >
   
< id  name ="id"  type ="java.lang.String" >
    
< column  name ="id"  length ="32"   />
    
< generator  class ="assigned"   />
   
</ id >
   
< property  name ="name"  type ="java.lang.String" >
    
< column  name ="name"  length ="20"  not-null ="true"   />
   
</ property >
   
< property  name ="password"  type ="java.lang.String" >
    
< column  name ="password"  length ="20"  not-null ="true"   />
   
</ property >
   
< property  name ="sex"  type ="java.lang.String" >
    
< column  name ="sex"  length ="2"   />
   
</ property >
   
< property  name ="email"  type ="java.lang.String" >
    
< column  name ="email"  length ="30"   />
   
</ property >
</ class >
</ hibernate-mapping >


POJO类

package  com.study.hibernate;

public   class  Person {
private  String id;

private  String name;

private  String password;

private  String sex;

private  String email;

public  String getEmail() {
   
return  email;
}

public   void  setEmail(String email) {
   
this .email  =  email;
}

public  String getId() {
   
return  id;
}

public   void  setId(String 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;
}

public  String getSex() {
   
return  sex;
}

public   void  setSex(String sex) {
   
this .sex  =  sex;
}

}


操作类

package  com.study.hibernate;

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

public   class  PersonOperate {
private  Session session  =   null ;

public  PersonOperate(){
   Configuration config 
=   new  Configuration().configure();
 
   SessionFactory factory 
=  config.buildSessionFactory();
 
   
this .session  =  factory.openSession();
}
public   void  insert(Person p){
   Transaction trc 
=   this .session.beginTransaction();
   
this .session.save(p);
   trc.commit();
}

}


测试类

package  com.study.hibernate;

public   class  TestPO {
public   static   void  main(String[] args) {
   
//  TODO Auto-generated method stub
   Person p  =   new  Person();
   p.setId(
" san " );
   p.setName(
" he " );
   p.setPassword(
" yong " );
   p.setSex(
" " );
   p.setEmail(
" [email protected] " );
 
   PersonOperate po 
=   new  PersonOperate();
   po.insert(p);

}

}

只是配置简单例子的话还是蛮简单的,未完待续……

你可能感兴趣的:(Hibernate 学习过程)