今天特意的又看了一下Hibernate环境搭建,手写了配置文件,收获颇大。
1.实体的创建
package com.hibernate.testNews; public class News { //消息类的标识属性 private Integer id; //消息标题 private String title; //消息内容 private String content; 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 getContent() { return content; } public void setContent(String content) { this.content = content; } }
2.给实体配置配置文件:News.hbm.xml
创建方式:对应包->new File-->basic xml-->create Xml file from a DTD file-->Select XML Catalog entry
内容如下:
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd" > <hibernate-mapping package="com.hibernate.testNews"> <!-- 每个class元素对应一个持久化对象 --> <class name="News" table="news_table"> <!-- id元素定义持久化类的标识属性 --> <id name="id"> <!-- 指定主键生成策略 --> <generator class="identity"></generator> </id> <!-- property元素定义常规属性 --> <property name="title"/> <property name="content"></property> </class> </hibernate-mapping>
3.编写配置文件Hibernate.cfg.xml
创建方式:src下创建:--xml(Basic Templates)--next xml file from DTD file
<?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是连接配置文件的根元素 --> <hibernate-configuration> <session-factory> <!-- 指定连接数据库所需的驱动 --> <property name="connection.driver_class">com.mysql.jdbc.Driver</property> <!-- 数据库名 --> <property name="connection.url">jdbc:mysql://localhost/hibernate</property> <!-- 数据库用户名 --> <property name="connection.username">root</property> <!-- 数据库密码 --> <property name="connection.password">123</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.acquire_increment">2</property> <property name="hibernate.c3p0.idle_test_period">3000</property> <property name="hibernate.c3p0.max_statements">100</property> <property name="hibernate.c3p0.validate"></property> <!-- 指定数据库的方言 --> <property name="dialect">org.hibernate.dialect.MySQLInnoDBDialect</property> <!-- 根据需要自动创建数据库表 --> <property name="hbm2ddl.auto">update</property> <!-- 罗列所有的映射文件 --> <mapping resource="com/hibernate/testNews/News.hbm.xml"/> </session-factory> </hibernate-configuration>
5.编写测试类
package com.hibernate.Test; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.Transaction; import org.hibernate.cfg.Configuration; import com.hibernate.testNews.News; public class NewsManager { public static void main(String[] args){ //实例化configuration,并加载hibernate.cfg.xml文件 Configuration conf=new Configuration().configure(); //以configuration创建sessionFactory SessionFactory sf=conf.buildSessionFactory(); //创建Session Session sess=sf.openSession(); //开始事务 Transaction tx=sess.beginTransaction(); //创建消息实例 News n=new News(); //设置消息标题和消息内容 n.setTitle("我是汪方晨"); n.setContent("我在练习Hibernate"); //保存消息 sess.save(n); //提交事务 tx.commit(); //关闭Session sess.close(); sf.close(); } }
然后到数据库中看,已经自动生成了表