Hibernate的配置

1.在 Src 目录下

hibernate.cfg.xml


        "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
        "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
    
   
   
    true
    jdbc:mysql://127.0.0.1:3306/test
    org.hibernate.dialect.MySQLDialect
   
    root
   

   
    123456
   

   
    com.mysql.jdbc.Driver
   

   
   
   



   


2.Person.hbm.xml  在Person.java包下



        "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
        "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
    
   
   
    true
    jdbc:mysql://127.0.0.1:3306/test
    org.hibernate.dialect.MySQLDialect
   
    root
   

   
    123456
   

   
    com.mysql.jdbc.Driver
   

   
   
   



   


3.所需要的Java包

antlr-2.7.2.jar
commons-collections-3.2.2.jar
commons-logging-1.1.3.jar
dom4j-1.6.1.jar
hibernate3.jar
javassist-3.11.0.GA.jar
jta-1.1.jar
mysql-connector-java-5.1.30-bin.jar
slf4j-api-1.7.12.jar



4.实例代码


package hibernate3.object;


import java.io.File;
import java.util.Iterator;


import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;


public class FirstHibernate {
public static void main(String[] args) {
Configuration configuration=new Configuration().configure("hibernate.cfg.xml");
SessionFactory sessionFactory=configuration.buildSessionFactory();
Person person=new Person();
person.setName("neo");
person.setAge("20");
Session session=sessionFactory.openSession();
Transaction transaction=session.beginTransaction();
session.save(person);
transaction.commit();
System.out.println("新增加人员成功");
Object pObject=session.get(Person.class, "1");
System.out.println(pObject);
session.close();
sessionFactory.close();
File file=new File("C:\\JavaProject\\FirstHibernate\\WebContent\\WEB-INF\\lib");
File[] LIST=file.listFiles();
for (File file2 : LIST) {
System.out.println(file2.getName());
}
}




}

你可能感兴趣的:(Java编程)