写一个hibernate样例

1.准备pom如下:

[html]  view plain  copy
  1. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  2.     xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">  
  3.     <modelVersion>4.0.0</modelVersion>  
  4.     <groupId>com</groupId>  
  5.     <artifactId>hibernate_demo</artifactId>  
  6.     <packaging>war</packaging>  
  7.     <version>0.0.1-SNAPSHOT</version>  
  8.     <name>hibernate_demo Maven Webapp</name>  
  9.     <url>http://maven.apache.org</url>  
  10. <properties>  
  11.         <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>  
  12.     </properties>  
  13.     <dependencies>  
  14.         <dependency>  
  15.             <groupId>junit</groupId>  
  16.             <artifactId>junit</artifactId>  
  17.             <version>3.8.1</version>  
  18.             <scope>test</scope>  
  19.         </dependency>  
  20.         <dependency>  
  21.             <groupId>org.hibernate</groupId>  
  22.             <artifactId>hibernate-core</artifactId>  
  23.             <version>4.2.8.Final</version>  
  24.         </dependency>  
  25.         <!-- Hibernate uses jboss-logging for logging, for the tutorials we will   
  26.             use the sl4fj-simple backend -->  
  27.         <dependency>  
  28.             <groupId>org.slf4j</groupId>  
  29.             <artifactId>slf4j-simple</artifactId>  
  30.             <version>1.6.1</version>  
  31.         </dependency>  
  32.         <dependency>  
  33.             <groupId>mysql</groupId>  
  34.             <artifactId>mysql-connector-java</artifactId>  
  35.             <version>5.1.6</version>  
  36.         </dependency>  
  37.     </dependencies>  
  38.     <build>  
  39.         <finalName>hibernate_demo</finalName>  
  40.     </build>  
  41. </project>  


2.创建持久化类:

[html]  view plain  copy
  1. package hibernate_demo;  
  2.   
  3. public class UserModel {  
  4.     private String uuid;  
  5.     private int userId;  
  6.     private String name;  
  7.     private int age;  
  8.   
  9.     public String getUuid() {  
  10.         return uuid;  
  11.     }  
  12.     public void setUuid(String uuid) {  
  13.         this.uuid = uuid;  
  14.     }  
  15.     public int getUserId() {  
  16.         return userId;  
  17.     }  
  18.     public void setUserId(int userId) {  
  19.         this.userId = userId;  
  20.     }  
  21.     public String getName() {  
  22.         return name;  
  23.     }  
  24.     public void setName(String name) {  
  25.         this.name = name;  
  26.     }  
  27.     public int getAge() {  
  28.         return age;  
  29.     }  
  30.     public void setAge(int age) {  
  31.         this.age = age;  
  32.     }  
  33.       
  34. }  


3.创建cfg.xml:

[html]  view plain  copy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <!DOCTYPE hibernate-configuration PUBLIC  
  3. "-//Hibernate/Hibernate Configuration DTD 3.0//EN"  
  4. "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">  
  5. <hibernate-configuration>  
  6.     <session-factory>  
  7.         <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>  
  8.         <property name="hibernate.connection.url">jdbc:mysql:///hibernate</property>  
  9.         <property name="hibernate.connection.username">root</property>  
  10.         <property name="hibernate.connection.password"></property>  
  11.         <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>  
  12.           
  13.         <!-- Echo all executed SQL to stdout -->  
  14.         <property name="show_sql">true</property>  
  15.   
  16.         <!-- Drop and re-create the database schema on startup -->  
  17.         <property name="hbm2ddl.auto">create</property>  
  18.           
  19.         <mapping resource="hibernate_demo/UserModel.hbm.xml"/>  
  20.     </session-factory>  
  21. </hibernate-configuration>  


4.创建持久化类的映射文件hbm.xml:

[html]  view plain  copy
  1. <?xml version="1.0" encoding="UTF-8"?>    
  2. <!DOCTYPE hibernate-mapping PUBLIC    
  3.         '-//Hibernate/Hibernate Mapping DTD 3.0//EN'    
  4.         'http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd'>  
  5. <hibernate-mapping>  
  6.     <class name="hibernate_demo.UserModel" table="tbl_user">  
  7.         <id name="userId">  
  8.             <generator class="native" />  
  9.         </id>  
  10.         <property name="uuid"></property>  
  11.         <property name="name"></property>  
  12.         <property name="age"></property>  
  13.     </class>  
  14. </hibernate-mapping>  


5.创建test类:

[html]  view plain  copy
  1. package hibernate_demo;  
  2.   
  3. import org.hibernate.Session;  
  4. import org.hibernate.SessionFactory;  
  5. import org.hibernate.Transaction;  
  6. import org.hibernate.cfg.Configuration;  
  7. import org.junit.After;  
  8. import org.junit.Before;  
  9. import org.junit.Test;  
  10.   
  11. public class testC {  
  12.   
  13.     private SessionFactory sessionFactory;  
  14.       
  15.   
  16.     @Before  
  17.     public void bef()  
  18.     {   
  19.         sessionFactory = new Configuration().configure().buildSessionFactory();  
  20.     }  
  21.       
  22.     @After  
  23.     public void aft()  
  24.     {  
  25.         if (sessionFactory != null) {  
  26.             sessionFactory.close();  
  27.         }  
  28.     }  
  29.       
  30.     @Test  
  31.     public void testdemo() throws SecurityException, IllegalStateException  
  32.              {  
  33.   
  34.         UserModel um = new UserModel();  
  35.         um.setUuid("1");  
  36.         um.setName("name1");  
  37.         um.setAge(1);  
  38.         Session s = sessionFactory.openSession();  
  39.         Transaction t = s.beginTransaction();  
  40.         s.save(um);  
  41.         t.commit();  
  42.           
  43.     }  
  44. }  

最新的test方法:

[html]  view plain  copy
  1. package hibernate_demo;  
  2.   
  3. import org.hibernate.Session;  
  4. import org.hibernate.SessionFactory;  
  5. import org.hibernate.Transaction;  
  6. import org.hibernate.cfg.Configuration;  
  7. import org.hibernate.service.ServiceRegistry;  
  8. import org.hibernate.service.ServiceRegistryBuilder;  
  9. import org.junit.After;  
  10. import org.junit.Before;  
  11. import org.junit.Test;  
  12.   
  13. public class testC {  
  14.   
  15.     private SessionFactory sessionFactory;  
  16.     private ServiceRegistry serviceRegistry;  
  17.       
  18.     @Before  
  19.     public void bef()  
  20.     {   
  21.         Configuration configuration = new Configuration();    
  22.         configuration.configure();    
  23.         serviceRegistry = new ServiceRegistryBuilder().applySettings(configuration.getProperties()).buildServiceRegistry();            
  24.         sessionFactory = configuration.buildSessionFactory(serviceRegistry);    
  25.     }  
  26.       
  27.     @After  
  28.     public void aft()  
  29.     {  
  30.         if (sessionFactory != null) {  
  31.             sessionFactory.close();  
  32.         }  
  33.     }  
  34.       
  35.     @Test  
  36.     public void testdemo() throws SecurityException, IllegalStateException  
  37.              {  
  38.   
  39.         UserModel um = new UserModel();  
  40.         um.setUuid("2");  
  41.         um.setName("name2");  
  42.         um.setAge(23);  
  43.         Session s = sessionFactory.openSession();  
  44.         Transaction t = s.beginTransaction();  
  45.         s.save(um);  
  46.         t.commit();  
  47.           
  48.     }  
  49. }  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值