hibernate第一个例子



 hibernate第一个例子

步骤

1  新建web project或者 java project项目

2 添加架包

hibernate第一个例子_第1张图片

3 编写hibernate.cfg.xml

 

<!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="show_sql">true</property>
		<property name="hbm2ddl.auto">create</property>
		<property name="format_sql">true</property>
		<property name="dialect">
			org.hibernate.dialect.MySQLDialect
		</property>
		<property name="connection.driver_class">
			com.mysql.jdbc.Driver
		</property>
		<property name="connection.url">
			jdbc:mysql://localhost:3306/test
		</property>
		<property name="connection.username">root</property>
		<property name="connection.password">***</property>
		<mapping resource="com/***/model/Student.hbm.xml" />
				
	<!--  	oracle 的配置
		<property name="show_sql">true</property>
		<property name="hbm2ddl.auto">create</property>
		<property name="format_sql">true</property>
		<property name="dialect">
			org.hibernate.dialect.OracleDialect
		</property>
		<property name="connection.driver_class">
			oracle.jdbc.driver.OracleDriver
		</property>
		  <property name="hibernate.transaction.factory_class">org.hibernate.transaction.JDBCTransactionFactory</property>
		
		<property name="connection.url">
			jdbc:oracle:thin:@localhost:1521:XE
		</property>
		<property name="connection.username">root</property>
		<property name="connection.password">**</property>
		<mapping resource="com/***/model/Student.hbm.xml" />
		-->
			
	</session-factory>
</hibernate-configuration>

 

 

4  编写实体类

  单实体

Student.java

package com.***.model;

public class Student {
	private int id;
	private String name;
	private String password;
	
	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 String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

}

 

5  编写相对应的**.hbm.xml

这里是:Student.hbm.xml

<?xml version="1.0" encoding="gbk"?>
<!DOCTYPE hibernate-mapping PUBLIC 
	"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
	"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
	
<hibernate-mapping package="com.***.model"><!--映射类所在的包-->
  <class name="Student" table="student_hibernate">
   <!-- mysql -->
    <id name="id" column="studentId">
      <generator class="identity"/>
    </id>
    <!-- oracle
    <id name="id" column="ID">   
            <generator class="sequence">   
                <param name="sequence">DEPARTMENT_ID_SEQ</param>   
            </generator>   
        </id>   
  -->
    <property name="name" column="name"  />
    <property name="password" />
  
   </class>    
</hibernate-mapping>

 

6 添加log4j.properties

# For JBoss: Avoid to setup Log4J outside $JBOSS_HOME/server/default/deploy/log4j.xml!
# For all other servers: Comment out the Log4J listener in web.xml to activate Log4J.
log4j.rootLogger=INFO, stdout, logfile

log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d %p [%c] - %m%n

log4j.appender.logfile=org.apache.log4j.RollingFileAppender
log4j.appender.logfile.File=petstore.log
log4j.appender.logfile.MaxFileSize=512KB
# Keep three backup files.
log4j.appender.logfile.MaxBackupIndex=3
# Pattern to output: date priority [category] - message
log4j.appender.logfile.layout=org.apache.log4j.PatternLayout
log4j.appender.logfile.layout.ConversionPattern=%d %p [%c] - %m%n

 

 

 

7  打开数据库

8 添加Test类

  Configuration cfg=new Configuration().configure();
          SessionFactory sf=cfg.buildSessionFactory();
       
          Session session= sf.openSession();
          Transaction tx=session.beginTransaction();

          Student s=new Student();
          s.setName("测试1");
          s.setPassword("22");
          
          session.save(s); 
      
          
          session.flush();
          tx.commit();
          session.close();

 

9  测试成功

 

你可能感兴趣的:(oracle,sql,mysql,Hibernate,log4j)