Hibernate第一个程序流程(2016-3-20)
2016-03-20 03:03
337 查看
第一步:创建实体类News.java
package com.estar.hibernate.entity;
import java.util.Date;
/**
* 实体类
*
* @author Administrator
*
*/
public class News {
private Integer id;
private String author;
private String title;
private Date date;
public Date getDate() {
return date;
}
public void setDate(Date date) {
this.date = date;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public News(String author, String title, Date date) {
super();
this.author = author;
this.title = title;
this.date = date;
}
public News() {
super();
}
@Override
public String toString() {
return "News [author=" + author + ", data=" + date + ", id=" + id
+ ", title=" + title + "]";
}
}
第二步:导入Hibernate需要的包
antlr-2.7.7.jar
c3p0-0.9.2.1.jar
dom4j-1.6.1.jar
hibernate-c3p0-4.2.4.Final.jar
hibernate-commons-annotations-4.0.2.Final.jar
hibernate-core-4.2.4.Final.jar
hibernate-jpa-2.0-api-1.0.1.Final.jar
javassist-3.15.0-GA.jar
jboss-logging-3.1.0.GA.jar
jboss-transaction-api_1.1_spec-1.0.1.Final.jar
junit-4.7.jar
mchange-commons-java-0.2.3.4.jar
mysql-connector-java-5.1.7-bin.jar
第三步骤hibernate.cfg.xml
<?xml version="1.0" encoding="UTF-8"?>
<!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="connection.username">root</property>
<property name="connection.password">123456</property>
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="connection.url">jdbc:mysql://localhost/test</property>
<!-- 配置 hibernate 的基本信息 -->
<!-- hibernate 所使用的数据库方言 -->
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<!-- 执行操作时是否在控制台打印 SQL -->
<property name="show_sql">true</property>
<!-- 是否对 SQL 进行格式化 -->
<property name="format_sql">true</property>
<!-- 指定自动生成数据表的策略 -->
<property name="hibernate.hbm2ddl.auto">update</property>
<!-- 指定关联的 .hbm.xml 文件 -->
<mapping resource="com/estar/hibernate/entity/News.hbm.xml"/>
</session-factory>
</hibernate-configuration>
第四步:News.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.estar.hibernate.entity.News" table="NEWS">
<id name="id" type="java.lang.Integer">
<column name="ID" />
<!-- 指定主键的生成方式, native: 使用数据库本地方式 -->
<generator class="native" />
</id>
<property name="title" type="java.lang.String" column="TITLE">
</property>
<property name="author" type="java.lang.String">
<column name="AUTHOR" />
</property>
<property name="date" type="java.util.Date">
<column name="DATE" />
</property>
</class>
</hibernate-mapping>
时间:2016-3-20 3:06
地点:广州市天河区车陂BRT公交站
package com.estar.hibernate.entity;
import java.util.Date;
/**
* 实体类
*
* @author Administrator
*
*/
public class News {
private Integer id;
private String author;
private String title;
private Date date;
public Date getDate() {
return date;
}
public void setDate(Date date) {
this.date = date;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public News(String author, String title, Date date) {
super();
this.author = author;
this.title = title;
this.date = date;
}
public News() {
super();
}
@Override
public String toString() {
return "News [author=" + author + ", data=" + date + ", id=" + id
+ ", title=" + title + "]";
}
}
第二步:导入Hibernate需要的包
antlr-2.7.7.jar
c3p0-0.9.2.1.jar
dom4j-1.6.1.jar
hibernate-c3p0-4.2.4.Final.jar
hibernate-commons-annotations-4.0.2.Final.jar
hibernate-core-4.2.4.Final.jar
hibernate-jpa-2.0-api-1.0.1.Final.jar
javassist-3.15.0-GA.jar
jboss-logging-3.1.0.GA.jar
jboss-transaction-api_1.1_spec-1.0.1.Final.jar
junit-4.7.jar
mchange-commons-java-0.2.3.4.jar
mysql-connector-java-5.1.7-bin.jar
第三步骤hibernate.cfg.xml
<?xml version="1.0" encoding="UTF-8"?>
<!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="connection.username">root</property>
<property name="connection.password">123456</property>
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="connection.url">jdbc:mysql://localhost/test</property>
<!-- 配置 hibernate 的基本信息 -->
<!-- hibernate 所使用的数据库方言 -->
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<!-- 执行操作时是否在控制台打印 SQL -->
<property name="show_sql">true</property>
<!-- 是否对 SQL 进行格式化 -->
<property name="format_sql">true</property>
<!-- 指定自动生成数据表的策略 -->
<property name="hibernate.hbm2ddl.auto">update</property>
<!-- 指定关联的 .hbm.xml 文件 -->
<mapping resource="com/estar/hibernate/entity/News.hbm.xml"/>
</session-factory>
</hibernate-configuration>
第四步:News.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.estar.hibernate.entity.News" table="NEWS">
<id name="id" type="java.lang.Integer">
<column name="ID" />
<!-- 指定主键的生成方式, native: 使用数据库本地方式 -->
<generator class="native" />
</id>
<property name="title" type="java.lang.String" column="TITLE">
</property>
<property name="author" type="java.lang.String">
<column name="AUTHOR" />
</property>
<property name="date" type="java.util.Date">
<column name="DATE" />
</property>
</class>
</hibernate-mapping>
时间:2016-3-20 3:06
地点:广州市天河区车陂BRT公交站
相关文章推荐
- Eclipse添加xml文件提示及Hibernate配置学习
- hibernate初学之初解篇一
- 关于hibernate.cfg.xml头文件DTD的引入
- Hibernate配置总结
- hibernate的简单配置实例
- 非主键字段外键Hibernate配置
- Hibernate基本示例
- Hibernate环境搭建和配置
- hibernate 3.1使用(import org.hibernate.cfg.AnnotationConfiguration;)出错
- hibernate_核心配置和映射配置
- Hibernate(一)
- 框架之Hibernate01
- 如何配置hibernate框架
- Hibernate配置01
- MyEclipse配置Hibernate
- BZOJ4435 : [Cerc2015]Juice Junctions
- Java开发流程自动化(svn+maven+nexus+jenkins) --自动编译,持续集成,测试,打包,发布/部署
- 第一个python程序
- 手把手教你如何配置Spring JDBC
- hdu3732 多重背包+二进制优化