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