下面以Hibernate官方站点提供的HibernateTools作为插件来开发Hibernate应用:
1,下载安装HibernateTools;
2,新建工程,添加类库
1>Mysql驱动;
2>hibernate-release-4.3.10.Final\lib\required下的10个jar包;
3> hibernate-release-4.3.10.Final\lib\optional\c3p0下的3个jar包;
3,创建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="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property> <!--指定连接数据库的密码 --> <property name="hibernate.connection.password">123456</property> <!--指定连接数据库的url,其中anliwendb是本应用连接的数据库名 --> <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/db</property> <!--指定连接数据库的用户名 --> <property name="hibernate.connection.username">root</property> <!--指定数据库方言 --> <property name="hibernate.dialect">org.hibernate.dialect.MySQL5InnoDBDialect</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="hbm2ddl.auto">update</property> <!--显示hibernate持久化操作所生成的sql --> <property name="show_sql">true</property> <!--将sql脚本格式化在进行输出 --> <property name="hibernate.format_sql">true</property> <!--罗列所有持久化的类名 --> <mapping class="com.anlw.news.News"/> </session-factory> </hibernate-configuration>
4,创建POJO文件
package com.anlw.news; import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.GenerationType; import javax.persistence.Id; import javax.persistence.Table; @Entity @Table(name="testtable") public class News { @Id @GeneratedValue(strategy=GenerationType.IDENTITY) 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; } }
5,主程序
package com.anlw.news; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.Transaction; import org.hibernate.cfg.Configuration; import org.hibernate.service.ServiceRegistry; import org.hibernate.service.ServiceRegistryBuilder; public class Manager { public static void main(String[] args) { //实例化Configuration Configuration conf = new Configuration().configure(); ServiceRegistry serviceRegistry = new ServiceRegistryBuilder(). applySettings(conf.getProperties()).buildServiceRegistry(); //以Configuration实例创建SessionFactory实例 SessionFactory sf = conf.buildSessionFactory(serviceRegistry); //创建Session Session sess = sf.openSession(); //开始事物 Transaction tx = sess.beginTransaction(); //创建消息对象 News n = new News(); //设置消息的标题和内容 n.setTitle("这是title"); n.setContent("这是content"); //保存消息 sess.save(n); //提交事物 tx.commit(); //关闭session sess.close(); sf.close(); System.out.println("ok"); } }
时间: 2024-10-22 14:52:47