1.准备pom如下:
[html] view plain copy <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com</groupId> <artifactId>hibernate_demo</artifactId> <packaging>war</packaging> <version>0.0.1-SNAPSHOT</version> <name>hibernate_demo Maven Webapp</name> <url>http://maven.apache.org</url> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> </properties> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>3.8.1</version> <scope>test</scope> </dependency> <dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate-core</artifactId> <version>4.2.8.Final</version> </dependency> <!-- Hibernate uses jboss-logging for logging, for the tutorials we will use the sl4fj-simple backend --> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-simple</artifactId> <version>1.6.1</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.6</version> </dependency> </dependencies> <build> <finalName>hibernate_demo</finalName> </build> </project>2.创建持久化类:
[html] view plain copy package hibernate_demo; public class UserModel { private String uuid; private int userId; private String name; private int age; public String getUuid() { return uuid; } public void setUuid(String uuid) { this.uuid = uuid; } public int getUserId() { return userId; } public void setUserId(int userId) { this.userId = userId; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } }3.创建cfg.xml:
[html] view plain copy <?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"> <hibernate-configuration> <session-factory> <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property> <property name="hibernate.connection.url">jdbc:mysql:///hibernate</property> <property name="hibernate.connection.username">root</property> <property name="hibernate.connection.password"></property> <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property> <!-- Echo all executed SQL to stdout --> <property name="show_sql">true</property> <!-- Drop and re-create the database schema on startup --> <property name="hbm2ddl.auto">create</property> <mapping resource="hibernate_demo/UserModel.hbm.xml"/> </session-factory> </hibernate-configuration>4.创建持久化类的映射文件hbm.xml:
[html] view plain copy <?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="hibernate_demo.UserModel" table="tbl_user"> <id name="userId"> <generator class="native" /> </id> <property name="uuid"></property> <property name="name"></property> <property name="age"></property> </class> </hibernate-mapping>5.创建test类:
[html] view plain copy package hibernate_demo; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.Transaction; import org.hibernate.cfg.Configuration; import org.junit.After; import org.junit.Before; import org.junit.Test; public class testC { private SessionFactory sessionFactory; @Before public void bef() { sessionFactory = new Configuration().configure().buildSessionFactory(); } @After public void aft() { if (sessionFactory != null) { sessionFactory.close(); } } @Test public void testdemo() throws SecurityException, IllegalStateException { UserModel um = new UserModel(); um.setUuid("1"); um.setName("name1"); um.setAge(1); Session s = sessionFactory.openSession(); Transaction t = s.beginTransaction(); s.save(um); t.commit(); } }最新的test方法:
[html] view plain copy package hibernate_demo; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.Transaction; import org.hibernate.cfg.Configuration; import org.hibernate.service.ServiceRegistry; import org.hibernate.service.ServiceRegistryBuilder; import org.junit.After; import org.junit.Before; import org.junit.Test; public class testC { private SessionFactory sessionFactory; private ServiceRegistry serviceRegistry; @Before public void bef() { Configuration configuration = new Configuration(); configuration.configure(); serviceRegistry = new ServiceRegistryBuilder().applySettings(configuration.getProperties()).buildServiceRegistry(); sessionFactory = configuration.buildSessionFactory(serviceRegistry); } @After public void aft() { if (sessionFactory != null) { sessionFactory.close(); } } @Test public void testdemo() throws SecurityException, IllegalStateException { UserModel um = new UserModel(); um.setUuid("2"); um.setName("name2"); um.setAge(23); Session s = sessionFactory.openSession(); Transaction t = s.beginTransaction(); s.save(um); t.commit(); } }