<!-- Drop and re-create the database schema on startup --> <property name="hbm2ddl.auto">update</property>
有update create validate create-drop四种属性
update 表示如果映射的类中有加的类 会自动改表
create是指如果数据库没有创表可以自动帮创
validate会在执行操作前可以先验证有没有表
create-drop是指关闭sessionfactory之后自动drop掉表
一般实际开发先建表
搭建Log4j日志环境
加入jar包
引入配置文件
Log4j.properties
### direct log messages to stdout ### log4j.appender.stdout=org.apache.log4j.ConsoleAppender log4j.appender.stdout.Target=System.out log4j.appender.stdout.layout=org.apache.log4j.PatternLayout log4j.appender.stdout.layout.ConversionPattern=%d{ABSOLUTE} %5p %c:%L - %m%n ### direct messages to file hibernate.log ### #log4j.appender.file=org.apache.log4j.FileAppender #log4j.appender.file.File=hibernate.log #log4j.appender.file.layout=org.apache.log4j.PatternLayout #log4j.appender.file.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n ### set log levels - for more verbose logging change 'info' to 'debug' ### log4j.rootLogger=warn, stdout #log4j.logger.org.hibernate=info #log4j.logger.org.hibernate=debug ### log HQL query parser activity #log4j.logger.org.hibernate.hql.ast.AST=debug ### log just the SQL #log4j.logger.org.hibernate.SQL=debug ### log JDBC bind parameters ### #log4j.logger.org.hibernate.type=info #log4j.logger.org.hibernate.type=debug ### log schema export/update ### log4j.logger.org.hibernate.tool.hbm2ddl=debug ### log HQL parse trees #log4j.logger.org.hibernate.hql=debug ### log cache activity ### #log4j.logger.org.hibernate.cache=debug ### log transaction activity #log4j.logger.org.hibernate.transaction=debug ### log JDBC resource acquisition #log4j.logger.org.hibernate.jdbc=debug ### enable the following line if you want to track down connection ### ### leakages when using DriverManagerConnectionProvider ### #log4j.logger.org.hibernate.connection.DriverManagerConnectionProvider=trace
可以注释掉不想要的内容
搭建Junit环境
学习maven 加入Junit类库
新建一个test
新建Junite test case
加入注解@Test
package com.easy; import static org.junit.Assert.*; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.cfg.AnnotationConfiguration; import org.hibernate.cfg.Configuration; import org.junit.AfterClass; import org.junit.BeforeClass; import org.junit.Test; public class Teachertest { public static SessionFactory sf = null; @BeforeClass public static void beforeclass(){ sf = new AnnotationConfiguration().configure().buildSessionFactory(); } @Test public void test() { student su = new student(); su.setId(13); su.setName("haha功"); su.setAge(22); Session session = sf.openSession(); session.beginTransaction(); session.save(su); session.getTransaction().commit(); session.close(); } @AfterClass public static void afterclass(){ sf.close(); } }
按照junite测试