Hibernate是一个开放源代码的对象关系映射框架,它对JDBC进行了非常轻量级的对象封装,使得Java程序员可以随心所欲的使用对象编程思维来操纵数据库。
今天就来演示一下Hibernate最初级的操作,使用SchemaExport创建数据表。
1.首先建立POJO类
package com.bjpowernode.hibernate; import java.util.Date; /** * 用户 * @author Longxuan * */ public class User { private String id; private String name; private String password; private Date createTime; private Date expireTime; public String getId() { return id; } public void setId(String 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; } public Date getCreateTime() { return createTime; } public void setCreateTime(Date createTime) { this.createTime = createTime; } public Date getExpireTime() { return expireTime; } public void setExpireTime(Date expireTime) { this.expireTime = expireTime; } }
2、根据POJO类里面里面相关的字段,在包中创建User.hbm.xml映射文件
<?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> <class name="com.bjpowernode.hibernate.User" > <!--hibernate为我们生成主键id--> <id name="id"> <generator class="uuid" /> </id> <!--默认把类的变量映射为相同名字的表列,当然我们使用column属性修改表字段--> <property name="name" column="name"></property> <property name="password"></property> <property name="createTime"></property> <property name="expireTime"></property> </class> </hibernate-mapping>
3、在src中建立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 name="foo"> <!-- 数据库的连接也可以直接使用hibernate.properties文件 --> <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property> <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/hibernate_test</property> <property name="hibernate.connection.username">root</property> <property name="hibernate.connection.password">root</property> <property name="hibernate.dialect" >org.hibernate.dialect.MySQLDialect</property><!-- 指定sql方言 --> <property name="hibernate.show_sql">true</property><!-- 设置是否显示生成sql语句 --> <property name="hibernate.format_sql">true</property><!-- 设置是否格式化sql语句--> <mapping resource="com/bjpowernode/hibernate/User.hbm.xml" /> </session-factory> </hibernate-configuration>
4、建立ExportDB类
package com.bjpowernode.hibernate; import org.hibernate.cfg.Configuration; import org.hibernate.tool.hbm2ddl.SchemaExport; /** * 将hbm生成ddl * @author Longxuan * */ public class ExportDB { /** * @param args */ public static void main(String[] args) { // 默认读取hibernate.cfg.xml文件 Configuration cfg = new Configuration().configure(); // 生成并输出sql到文件(当前目录)和数据库 SchemaExport export = new SchemaExport(cfg); // true 在控制台打印sql语句,true 导入sql语句到数据库,即可执行 export.create(true, true); } }
5、建立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{1}:%L - %m%n ### set log levels - for more verbose logging change 'info' to 'debug' ### log4j.rootLogger=warn, stdout
现在可以测试了。首先在mysql中创建hibernate_test数据库:
运行ExportDB的main方法,结果如图: