您的位置:首页 > 其它

大师养成计划之二:hibernate框架的使用------实例演示

2016-02-04 11:03 393 查看
搭建hibernate项目框架的步骤:

一、导入jar包



二、new .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>

<!-- hibernate的基本信息 -->
<property name="connection.username">root</property>
<property name="connection.password">root</property>
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="connection.url">jdbc:mysql:///hibernate</property>

<!-- hibernate的基本配置 -->

<!-- hibernate使用的数据库方言 -->
<property name="dialect">org.hibernate.dialect.MySQLInnoDBDialect</property>

<!-- 运行时是否打印sql语句 -->
<property name="show_sql">true</property>

<!-- 运行时是否格式化sql -->
<property name="format_sql">true</property>

<!-- 生成数据表的策略  这里是更新-->
<property name="hbm2ddl.auto">update</property>

<!-- 设置hibernate的事务隔离级别 -->
<property name="connection.isolation">2</property>

<!-- 配置c3p0数据源 -->
<property name="hibernate.c3p0.max_size">10</property>
<property name="hibernate.c3p0.min_size">5</property>
<property name="hibernate.c3p0.acquire_increment">2</property>
<property name="hibernate.c3p0.timeout">2000</property>
<property name="hibernate.c3p0.acquire_increment">2000</property>
<property name="c3p0.max_statements">10</property>

<!--@里的内容对于mysql是无效的,对于oracle是有效的 -->
<!--@ 设定JDBC的Statement读取数据的时候每次从数据库中取出的记录条数 -->
<property name="hibernate.jdbc.fetch_size">100</property>

<!-- @设定对数据库进行批量删除,批量更新和批量插入的时候的批次大小 -->
<property name="jdbc.batch_size">30</property>

<!-- 需要关联的hibernate映射文件 .hbm.xml -->
<mapping resource="entities/News.hbm.xml"/>

</session-factory>
</hibernate-configuration>


三、new 实体类

package entities;

import java.util.Date;

/**
* @author zxN
* @version 创建时间:2016年1月3日 下午2:13:57
* 类说明
*/
class News {
private Integer id;
private String title;
private String author;
private Date date;
public News() {
super();
// TODO Auto-generated constructor stub
}
public News( String title, String author, Date date) {
super();

this.title = title;
this.author = author;
this.date = date;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
public Date getDate() {
return date;
}
public void setDate(Date date) {
this.date = date;
}
@Override
public String toString() {
return "News [id=" + id + ", title=" + title + ", author=" + author
+ ", date=" + date + "]";
}

}


四、new 实体类的 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">
<!-- Generated 2015-12-24 15:32:00 by Hibernate Tools 3.4.0.CR1 -->
<!-- hbm.xml -->
<hibernate-mapping package="entities">
<class name="entities.News" table="NEWS">
<id name="id" type="java.lang.Integer">
<!-- 列名 -->
<column name="ID" />
<!-- 设置主键为自动增长 -->
<generator class="native"/>
</id>
<property name="title" type="java.lang.String">
<column name="TITLE" />
</property>
<property name="author" type="java.lang.String">
<column name="AUTHOR" />
</property>
<property name="date" type="java.util.Date">
<column name="DATE" />
</property>

<!-- 映射大对象 -->
<!-- 若希望精确映射SQL类型,可以使用sql-type属性 -->
<property name="content" >
<column name="CONTENT" sql-type="mediumtext"></column>
</property>

<property name="image" type="blob">
<column name="IMAGE" sql-type="mediumblob"></column>
</property>
</class>
</hibernate-mapping>


在这个文件里,设置主键的方式,若为自动递增的,应设置为native

并将这个文件关联到cfg.xml里边去(PS:路径前面不要有斜线!!!)

五、新建junit测试类
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: