hibernate4之helloworld
目录结构
实现步骤
1. 安装MySQL数据库并添加数据库hibernate4;
2. 新建工程org.rabbitx.hibernate4;
3. 在此工程下添加目录lib并添加需要的 jar 包;
hibernate包(hibernate-release-4.2.4.Final\lib\required目录下所有包):
antlr-2.7.7.jar
dom4j-1.6.1.jar
hibernate-commons-annotations-4.0.2.Final.jar
hibernate-core-4.2.4.Final.jar
hibernate-jpa-2.0-api-1.0.1.Final.jar
javassist-3.15.0-GA.jar
jboss-logging-3.1.0.GA.jar
jboss-transaction-api_1.1_spec-1.0.1.Final.jar
MySQL驱动包:
mysql-connector-java-5.1.30-bin.jar
日志包(可选):
log4j-1.2.17.jar
4. 手动或使用hibernate tool插件添加hibernate配置文件
<?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> <!-- 配置连接数据库的基本信息 --> <!-- 1. hibernate.connection.* 此处可以简写为connection.*,但与spring集成之后不能简写; 2. jdbc:mysql//localhost:3306/hibernate4可以简写为jdbc:mysql///hibernate4,因为默认值是localhost:3306; --> <property name="hibernate.connection.username">root</property> <property name="hibernate.connection.password">root</property> <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property> <property name="hibernate.connection.url">jdbc:mysql://192.168.1.105:3306/hibernate4</property> <!-- 配置hibernate基本信息 --> <!-- 配置数据库方言 --> <!-- 数据库方言值可以在hibernate-release-4.2.4.Final\project\etc\hibernate.properties文件找查询 --> <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> <!-- 指定关联的 .hbm.xml 文件 --> <mapping resource="org/rabbitx/hibernate4/helloworld/News.hbm.xml"/> </session-factory> </hibernate-configuration>
5. 添加持久化类
package org.rabbitx.hibernate4.helloworld; import java.util.Date; public class News { private Integer id; private String message; private Date date; public News() { super(); } public News(String message,Date date) { this.message = message; this.date = date; } public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getMessage() { return message; } public void setMessage(String message) { this.message = message; } public Date getDate() { return date; } public void setDate(Date date) { this.date = date; } @Override public String toString() { return "News [id=" + id + ", message=" + message + ", date=" + date + "]"; } }
6. 手动或使用hibernate tool插件添加对象-关系映射文件
<?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-8-19 23:17:56 by Hibernate Tools 3.4.0.CR1 --> <hibernate-mapping> <class name="org.rabbitx.hibernate4.helloworld.News" table="TBL_NEWS"> <id name="id" type="java.lang.Integer"> <column name="ID" /> <generator class="native" /> </id> <property name="message" type="java.lang.String"> <column name="MESSAGE" /> </property> <property name="date" type="java.util.Date"> <column name="DATE" /> </property> </class> </hibernate-mapping>
7. 通过Hibernate API 访问数据库
package org.rabbitx.hibernate4.helloworld; import java.util.Date; 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 Main { public static void main(String[] args) { //1. 创建 Configuration对象:对应 hibernate的基本配置信息和 对象关系映射信息 Configuration cfg = new Configuration().configure(); //2. 创建一个 ServiceRegistry 对象: hibernate 4.x 新添加的对象.hibernate 的任何配置和服务都需要在该对象中注册后才能有效. ServiceRegistry serviceRegistry = new ServiceRegistryBuilder().applySettings(cfg.getProperties()).buildServiceRegistry(); //3. 创建一个 SessionFactory 对象 SessionFactory sessionFactory = cfg.buildSessionFactory(serviceRegistry); //4. 创建一个 Session 对象 Session session = sessionFactory.openSession(); //5. 开启事务 Transaction transaction = session.beginTransaction(); //6. 执行保存操作 News news = new News("Tianjin explosion",new Date()); session.save(news); //7. 提交事务 transaction.commit(); //8. 关闭Session session.close(); //9. 关闭SessionFactory sessionFactory.close(); } }
8. 运行后查看数据库