Hibernate主要配置文件
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://www.hibernate.org/dtd/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://127.0.0.1:3306/hibernate</property> <property name="hibernate.connection.username">root</property> <property name="hibernate.connection.password">mysql</property> <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property> <!-- hibernate 其他信息 --> <property name="hibernate.show_sql">true</property> <property name="hibernate.format_sql">true</property> <!-- 设置我的hibernate执行的时候,是否要创建数据库对象:例如表格 创建的方式:create:执行一次hibernate我就重新创建数据库对象。 update:如果之前已经有这个表格了,那么把这个表格里面的结构更新成我这次要求的样子。如果之前没有这个表格,那么我就重新创建一个。 --> <property name="hibernate.hbm2ddl.auto">update</property> <!-- 让session和本地线程绑定,确保我们的session对象唯一 --> <property name ="hibernate.current_session_context_class">thread</property> <!-- 引入我的映射文件 --> <mapping resource="com/ambow/po/User.hbm.xml"/> </session-factory> </hibernate-configuration>
实体配置文件
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"> <hibernate-mapping> <!-- 配置User实体类和表格t_user的映射关系 name:对应的是实体对象的全名称 table:对应的是数据库中表格的名字 --> <class name="com.ambow.po.User" table="t_user"> <!-- 配置属性和字段的映射关系 --> <!-- 配置主键 name:实体属性 column:字段 --> <id name="uid" column="uid"> <!-- 设置主键自增 native :根据底层数据库的种类去产生不同的主键自增方式。 --> <generator class="native"> </generator> </id> <!-- 配置普通列 name:实体属性 column:字段 --> <property name="username" column="username"></property> <property name="sex" column="sex"></property> <property name="address" column="address"></property> </class> </hibernate-mapping>
log4j日志配置为文件
# # Hibernate, Relational Persistence for Idiomatic Java # # License: GNU Lesser General Public License (LGPL), version 2.1 or later. # See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>. # ### 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{1}:%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=info ### 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