Hibernate(一)——使用SchemaExport生成数据表
2015-09-21 17:24
656 查看
Hibernate是一个开放源代码的对象关系映射框架,它对JDBC进行了非常轻量级的对象封装,使得Java程序员可以随心所欲的使用对象编程思维来操纵数据库。
今天就来演示一下Hibernate最初级的操作,使用SchemaExport创建数据表。
1.首先建立POJO类
[java]
view plaincopyprint?
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映射文件
[html]
view plaincopyprint?
<?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
[html]
view plaincopyprint?
<!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类
[java]
view plaincopyprint?
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日志文件
[html]
view plaincopyprint?
### 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
<
c9f5
/div>
现在可以测试了。首先在mysql中创建hibernate_test数据库:
![](https://img-blog.csdn.net/20130914093935531?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQveGlhb3hpYW44MDIz/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center)
运行ExportDB的main方法,结果如图:
![](https://img-blog.csdn.net/20130914093404484?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQveGlhb3hpYW44MDIz/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center)
![](https://img-blog.csdn.net/20130914094030765?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQveGlhb3hpYW44MDIz/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center)
Hibernate是一个开放源代码的对象关系映射框架,它对JDBC进行了非常轻量级的对象封装,使得Java程序员可以随心所欲的使用对象编程思维来操纵数据库。
今天就来演示一下Hibernate最初级的操作,使用SchemaExport创建数据表。
1.首先建立POJO类
[java]
view plaincopyprint?
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;
}
}
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映射文件
[html]
view plaincopyprint?
<?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>
<?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
[html]
view plaincopyprint?
<!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>
<!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类
[java]
view plaincopyprint?
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);
}
}
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日志文件
[html]
view plaincopyprint?
### 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
<
c9f5
/div>
### 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方法,结果如图:
相关文章推荐
- PHP设计模式——适配器模式(Adapter)
- 第三周项目--线性表
- 会议室开发项目的总结
- 安卓:ContetnResolver之查询显示联系人信息及对其的操作
- 关于子函数变量生命周期以及指针的一些问题
- 1005. 继续(3n+1)猜想 (25)
- Java中 List、Set、Map
- Java程序的运行机制和JVM
- 个人C++ 编码规范
- 第3周SHH数据结构—【项目3 - 求集合并集】
- 【第4周 项目1 - 建立单链表】
- linux自学心得之--安装内核头文件
- 第4周、项目1—建立单链表
- Could not execute auto check for display colors using command /usr/bin/xdpyinfo.(
- 从标准输入读取C源代码,并验证所有的花括号都正确的成对出现
- Java:按值传递还是按引用传递详细解说
- Debian软件包管理工具apt-get和dpkg操作备忘
- Bitmap与Drawable相互转化
- 潘鹏整理WPF(6)带标题的内容控件GroupBox&&Expander&&TabControl
- 第四周项目1建立单链表