hibernate基本的配置与验证
导入jar包与mysql驱动包
javaBean
src/com/crazyit/app/domain/News.java
package com.crazyit.app.domain; import javax.persistence.*; @Entity @Table(name="news_info") public class News { private Integer id; private String title; private String content; @Id @GeneratedValue(strategy=GenerationType.IDENTITY) 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; } }
hibernate.cfg.xml
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd"> <hibernate-configuration> <session-factory> <!-- 指定连接数据库所用的驱动 --> <property name="connection.driver_class">com.mysql.jdbc.Driver</property> <!-- 指定连接数据库的URL,其中hibernatedemo是本应用连接的数据库名 --> <property name="connection.url">jdbc:mysql://localhost:3306/hibernatedemo</property> <!-- 指定连接数据库的用户名 --> <property name="connection.username">lidian</property> <!-- 指定连接数据库的密码 --> <property name="connection.password">lidian1234</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.MySQL5InnoDBDialect</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.crazyit.app.domain.News"/> </session-factory> </hibernate-configuration>
验证结果
src/lee/NewsManager.java
package lee; import org.hibernate.cfg.Configuration; import org.hibernate.service.ServiceRegistry; import com.crazyit.app.domain.News; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.Transaction; import org.hibernate.boot.registry.StandardServiceRegistryBuilder;; public class NewsManager { public static void main(String[] args) throws Exception{ //实例化Configuration //不带参数的configure()默认加载hibernate.cfg.xml //如果传入abc.xml作为参数,则不再加载hibernate.cfg.xml,改为加载abc.xml Configuration conf = new Configuration().configure(); ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder() .applySettings(conf.getProperties()).build(); //以Configuration的实例创建SessionFactory SessionFactory sf = conf.buildSessionFactory(serviceRegistry); //创建Session Session se = sf.openSession(); //开始事物 Transaction tx = se.beginTransaction(); //创建消息对象 News news = new News(); news.setTitle("疯狂java"); news.setContent("疯狂java,"+"网址https://www.baidu.com/"); //保存消息 se.save(news); //提交事务 tx.commit(); //关闭session se.close(); sf.close(); } }