自己的Hibernate4搭建

Hibernate4.3.11 Anotation版本搭建

第一步:下载Hibernate4 需要的jar包


第二步:在Myeclipse中新建web工程如hibernate4,然后将上面提到的jar包导入进去

第三步:在hibernate4的工程目录下新建hibernate.cfg.xml文件,并在其中加如下内容

<?xml version='1.0'encoding='utf-8'?> 

<!DOCTYPE hibernate-configurationPUBLIC 

       "-//Hibernate/Hibernate Configuration DTD 3.0//EN" 

       "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd"> 

<hibernate-configuration> 

         <session-factory> 

                   <!--Database connection settings --> 

                   <propertyname="connection.driver_class">com.mysql.jdbc.Driver</property> 

                   <propertyname="connection.url">jdbc:mysql://127.0.0.1:3306/talkdb</property> 

                   <propertyname="connection.username">root</property> 

                   <propertyname="connection.password">admin123</property> 

                   <!--JDBC connection pool (use the built-in) --> 

                   <propertyname="connection.pool_size">1</property> 

                   <!--SQL dialect --> 

                   <propertyname="dialect">org.hibernate.dialect.MySQL5Dialect</property> 

                   <!--Enable Hibernate's automatic session context management --> 

                   <propertyname="current_session_context_class">thread</property> 

                   <!--Disable the second-level cache --> 

                   <propertyname="cache.provider_class">org.hibernate.cache.internal.NoCacheProvider</property> 

                   <!--Echo all executed SQL to stdout --> 

                   <propertyname="show_sql">true</property> 

                   <!--Drop and re-create the database schema on startup --> 

                   <propertyname="hbm2ddl.auto">update</property> 

         <mappingclass="com.test.bean.User" /> 

         </session-factory> 

</hibernate-configuration>

第三步: 在src下新建包以及User类    

package com.test.bean;

 

import javax.persistence.Entity;

import javax.persistence.Id;

 

@Entity(name = "TT_TALK_USER")

public class User {

   @Id

   private String id;

   private String username;

   private String password;

   

   public String getId() {

       return id;

    }

   public void setId(String id) {

       this.id = id;

    }

   

   public String getUsername() {

       return username;

    }

   

   public void setUsername(String username) {

       this.username = username;

    }

   

   public String getPassword() {

       return password;

    }

   public void setPassword(String password) {

       this.password = password;

    }

}

第四步:因为还需要连接mysql数据库以及测试,所以再引入mysql的驱动包和junit的jar包


第五步:jar ok, 在src下新建com.test.beanTest包,并在其下新建UserTest类用来测试hibernate4的配置是否成功

package com.test.beanTest;

 

import org.hibernate.Session;

import org.hibernate.SessionFactory;

import org.hibernate.cfg.Configuration;

importorg.hibernate.service.ServiceRegistry;

importorg.hibernate.service.ServiceRegistryBuilder;

import org.junit.BeforeClass;

import org.junit.Test;

 

import com.test.bean.User;

 

public class UserTest {

   private static SessionFactory sessionFactory = null; 

   @BeforeClass 

   public static void beforeClass() { 

       Configuration configuration = new Configuration(); 

       configuration.configure(); 

       ServiceRegistry serviceRegistry = newServiceRegistryBuilder().applySettings(configuration.getProperties()).buildServiceRegistry(); 

       sessionFactory =configuration.buildSessionFactory(serviceRegistry); 

   }  

   @Test 

   public void testSave() {  

       User u = new User();

       u.setId("45678912456789124567891245678912"); 

       u.setUsername("hahah");

       u.setPassword("456123");

       Session session = sessionFactory.getCurrentSession(); 

       session.beginTransaction(); 

        session.save(u); 

       session.getTransaction().commit(); 

   } 

}

第六步:运行UserTest类

package com.test.beanTest;

 

import org.hibernate.Session;

import org.hibernate.SessionFactory;

import org.hibernate.cfg.Configuration;

import org.junit.BeforeClass;

import org.junit.Test;

 

import com.test.bean.User;

 

public class UserTest {

 

   private static SessionFactory sessionFactory = null; 

   @BeforeClass 

   public static void beforeClass() { 

       Configuration configuration = newConfiguration().configure("hibernate.cfg.xml");; 

       sessionFactory = configuration.buildSessionFactory(); 

   }  

   @Test 

   public void testSave() { 

       User u = new User();

       u.setId("45678912456789124567891245678912"); 

       u.setUsername("hahah");

       u.setPassword("456123aaa");

       Session session = sessionFactory.getCurrentSession(); 

       session.beginTransaction(); 

       session.save(u); 

       session.getTransaction().commit(); 

   } 

}

第七步:运行junit测试,出现以下界面,hibernate4.3.11就ok了

 

问题总结:

1.      引包要注意,不同版本对应不同的jar包

2.      因为用的是hibernate4.3.11,bean的注解为@Entity(name = "TT_TALK_USER"),而不是

@Entity

@Table(name = "TT_TEMPLATE") 否则junit会报java.lang.NoSuchMethodError:javax.persistence.Table.indexes()[Ljavax/persistence/Index

3.      Junit4.11使用时需要引入

1.      junit-4.11.jar       2. hamcrest-core-1.3.jar    

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

lswsmj

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值