package hsl.domain; public class News { // 消息类的标识属性 private Integer id; // 消息标题 private String title; // 消息内容 private String content; // id属性的setter和getter方法 public void setId(Integer id) { this.id = id; } public Integer getId() { return this.id; } // title属性的setter和getter方法 public void setTitle(String title) { this.title = title; } public String getTitle() { return this.title; } // content属性的setter和getter方法 public void setContent(String content) { this.content = content; } public String getContent() { return this.content; } }
<?xml version="1.0" encoding="gb2312"?> <!-- 指定Hiberante3映射文件的DTD信息 --> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"> <!-- hibernate-mapping是映射文件的根元素 --> <hibernate-mapping package="hsl.domain"> <!-- 每个class元素对应一个持久化对象 --> <class name="News" table="news_table"> <!-- id元素定义持久化类的标识属性 --> <id name="id"> <!-- 指定主键生成策略 --> <generator class="identity" /> </id> <!-- property元素定义常规属性 --> <property name="title" /> <property name="content" /> </class> </hibernate-mapping>
<?xml version="1.0" encoding="GBK"?> <!-- 指定Hibernate配置文件的DTD信息 --> <!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd"> <!-- hibernate- configuration是连接配置文件的根元素 --> <hibernate-configuration> <session-factory> <!-- 指定连接数据库所用的驱动 --> <property name="connection.driver_class">com.mysql.jdbc.Driver</property> <!-- 指定连接数据库的url,hibernate连接的数据库名 --> <property name="connection.url">jdbc:mysql://localhost/hibernate</property> <!-- 指定连接数据库的编码 --> <property name="connection.characterEncoding">utf8</property> <!-- 指定连接数据库的用户名 --> <property name="connection.username">root</property> <!-- 指定连接数据库的密码 --> <property name="connection.password">8656216</property> <!-- 指定连接池里最大连接数 --> <property name="hibernate.c3p0.max_size">20</property> <!-- 指定连接池里最小连接数 --> <property name="hibernate.c3p0.min_size">1</property> <!-- 指定连接池里连接的超时时长 --> <property name="hibernate.c3p0.timeout">5000</property> <!-- 指定连接池里最大缓存多少个Statement对象 --> <property name="hibernate.c3p0.max_statements">100</property> <property name="hibernate.c3p0.idle_test_period">3000</property> <property name="hibernate.c3p0.acquire_increment">2</property> <property name="hibernate.c3p0.validate">true</property> <!-- 指定数据库方言 --> <!--<property name="dialect">org.hibernate.dialect.MySQLInnoDBDialect</property>--> <property name="dialect">org.hibernate.dialect.MySQLDialect</property> <!-- 根据需要自动创建数据表 --> <property name="hbm2ddl.auto">update</property> <!-- 显示Hibernate持久化操作所生成的SQL --> <property name="show_sql">true</property> <!-- 将SQL脚本进行格式化后再输出 --> <property name="hibernate.format_sql">true</property> <!-- 罗列所有的映射文件 --> <mapping resource="hsl/domain/News.hbm.xml"/> </session-factory> </hibernate-configuration>
除此之外,由于Hibernate 3.6及以上版本都使用 SLF4J 作为日志工具,我们可以登录 http://www.slf4j.org/download.html,下载 SLF4J 日志工具slf4j-1.7.7.zip,将解压目录下的 slf4j-api-1.7.7.jar 和 slf4j-nop-1.7.7.jar 这两个JAR 包也添加到系统的类加载路径下。
package hsl; import org.hibernate.*; import org.hibernate.cfg.*; import hsl.domain.*; public class NewsManager { @SuppressWarnings("deprecation") public static void main(String[] args) throws Exception { // 实例化Configuration, Configuration conf = new Configuration() // 下面方法默认加载hibernate.cfg.xml文件 .configure(); // 以Configuration创建SessionFactory SessionFactory sf = conf.buildSessionFactory(); // 创建Session Session sess = sf.openSession(); // 开始事务 Transaction tx = sess.beginTransaction(); // 创建消息实例 News n = new News(); // 设置消息标题和消息内容 n.setTitle("中国风"); n.setContent("中国风," + "网站地址hanshilei.3322.org"); // 保存消息 sess.save(n); // 提交事务 tx.commit(); // 关闭Session sess.close(); sf.close(); } }注意:如果用的是 myEclipse 需要做如下修改配置
项目名上右键--〉myeclipse-->add hibernate capabilites -->next-->hibernate config file --> existing -->选择现有工程存在的hibernate配置文件--> next --> 不生成factory class --> end
运行 src\hsl\NewsManager.java 文件得到结果