本文主要是介绍hibernate之helloworld(四步),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
1、导入jar包
hibernate3.jar
required/*.jar
2、编写pojo,如:
public class Student {private int id;private String name;private int age;public int getId() {return id;}public void setId(int id) {this.id = id;}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、编写pojo的映像配置文件
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC"-//Hibernate/Hibernate Mapping DTD 3.0//EN""http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="pojo"><class name="Student" table="Student"><id name="id" column="id"></id><property name="name"></property><property name="age"></property></class>
</hibernate-mapping>
4、编写hibernate配置文件
<?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><!-- Database connection settings --><property name="connection.driver_class">com.microsoft.sqlserver.jdbc.SQLServerDriver</property><property name="connection.url">jdbc:sqlserver://localhost:1433;DatabaseName=hibernate</property><property name="connection.username">sa</property><property name="connection.password">ty123456</property><!-- JDBC connection pool (use the built-in) --><property name="connection.pool_size">1</property><!-- SQL dialect --><property name="dialect">org.hibernate.dialect.HSQLDialect</property><!-- Enable Hibernate's automatic session context management --><property name="current_session_context_class">thread</property><!-- Disable the second-level cache --><property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</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">update</property><mapping resource="pojo/Student.hbm.xml"/></session-factory>
</hibernate-configuration>
5、编写测试类
public class StudentTest {public static void main(String args[]){Student stu = new Student();stu.setId(0);stu.setName("涂有");stu.setAge(21);Configuration conf = new Configuration();SessionFactory sessionFactory = conf.configure().buildSessionFactory();Session session = sessionFactory.openSession();session.beginTransaction();session.save(stu);session.getTransaction().commit();session.close();sessionFactory.close();}
}
这篇关于hibernate之helloworld(四步)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!