Hibernate_Notes_1_helloworld
2015-07-12 16:27
295 查看
持久化
加载:根据特定的OID,把一个对象从数据库加载到内存中。
ORM 概念就不叙述太多了...
主要对象-关系的映射、这个有点666阿!!
也就是之前对数据库的操作变成是对对象的操作。
主要记录helloworld吧、毕竟新手(主要四个步骤:创建Hibernate配置文件、创建持久化类、创建对象-关系映射文件、通过H api编写)
1、环境myeclipse
2、安装hibernate插件 版本:hibernatetools-Update-4.1.1.Final_2013-12-08_01-06-33-B605
3、jar包、自行下载阿。找不到你就别学了...这里版本:hibernate-release-4.2.4.Final
4、用的是mysql jar包版本:mysql-connector-java-5.1.33-bin
5、导入jar包 build path
6、创建Hibernate配置文件(hibernatge.cfg.xml)
具体看代码以及注释
使用神器Navicat
for mysql
加载:根据特定的OID,把一个对象从数据库加载到内存中。
ORM 概念就不叙述太多了...
主要对象-关系的映射、这个有点666阿!!
也就是之前对数据库的操作变成是对对象的操作。
主要记录helloworld吧、毕竟新手(主要四个步骤:创建Hibernate配置文件、创建持久化类、创建对象-关系映射文件、通过H api编写)
1、环境myeclipse
2、安装hibernate插件 版本:hibernatetools-Update-4.1.1.Final_2013-12-08_01-06-33-B605
3、jar包、自行下载阿。找不到你就别学了...这里版本:hibernate-release-4.2.4.Final
4、用的是mysql jar包版本:mysql-connector-java-5.1.33-bin
5、导入jar包 build path
6、创建Hibernate配置文件(hibernatge.cfg.xml)
具体看代码以及注释
<!-- 配置连接数据库的基本信息 --> <property name="connection.username">root</property> <property name="connection.password">7362346</property> <property name="connection.driver_class">com.mysql.jdbc.Driver</property> <property name="connection.url">jdbc:mysql:///hibernate5</property> <!-- 配置hibernate的基本信息 --> <!-- hibernate 所使用的数据库方言--> <property name="dialect">org.hibernate.dialect.MySQLInnoDBDialect</property><span style="white-space:pre"> </span><!--直接查官方文档project/etc/--> <!-- 执行操作时是否在控制台打印sql --> <property name="show_sql">true</property> <!-- 是否对SQL进行格式化 指输出到控制台的sql--> <property name="format_sql">true</property> <!-- 制定自动生成数据表的策略 --> <property name="hbm2ddl.auto">update</property> <!-- 指定关联的 .hbm.xml文件 --> <mapping resource="com/test/hibernate/helloword/News.hbm.xml"/>7、创建持久化类
package com.test.hibernate.helloword; import java.sql.Date; public class News { private Integer id; private String title; private String author; private Date date; 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 getAuthor() { return author; } public void setAuthor(String author) { this.author = author; } public Date getDate() { return date; } public void setDate(Date date) { this.date = date; } public News(String title, String author, Date date) { super(); this.title = title; this.author = author; this.date = date; } public News(){ } @Override public String toString() { return "News [id=" + id + ", title=" + title + ", author=" + author + ", date=" + date + "]"; } }8、创建对象-关系映射文件(hbm.xml)
<?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-7-12 12:21:14 by Hibernate Tools 3.4.0.CR1 --> <hibernate-mapping> <class name="com.test.hibernate.helloword.News" table="NEWS"> <id name="id" type="java.lang.Integer"> <column name="ID" /> <!-- 制定主键的生成方式,native:使用数据库本地方式 --> <generator class="native" /> </id> <property name="title" type="java.lang.String"><span style="white-space:pre"> </span>//name类里的属性名 <column name="TITLE" /><span style="white-space:pre"> </span>//column数据表列名 </property> <property name="author" type="java.lang.String"> <column name="AUTHOR" /> </property> <property name="date" type="java.sql.Date"> <column name="DATE" /> </property> </class> </hibernate-mapping>9、具体使用api
package com.test.hibernate.helloword; import static org.junit.Assert.*; import java.sql.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; import org.junit.Test; public class HibernateTest { @Test public void test() { //1.创建一个sessionFactory对象 4000 SessionFactory sessionFactory = null; Configuration configuration = new Configuration().configure(); ServiceRegistry serviceRegistry = new ServiceRegistryBuilder().applySettings(configuration.getProperties()) .buildServiceRegistry(); sessionFactory = configuration.buildSessionFactory(serviceRegistry); //2.创建一个Session 对象 Session session = sessionFactory.openSession(); //3.开启事务 Transaction transaction=session.beginTransaction(); //4.执行保存执行 News news = new News("Java","sun",new Date(new java.util.Date().getTime())); session.save(news); //5.提交事务 transaction.commit(); //6.关闭session session.close(); //7.关闭SessionFactory对象 sessionFactory.close(); } }运行情况
使用神器Navicat
for mysql
相关文章推荐
- MVC validation
- [swustoj 243] 又是一年CET46
- webstorm 快捷键
- Palindrome Linked List
- IOS GCD多线程介绍
- webstorm 快捷键
- 概率题 面试 笔试
- 同步通信与异步通信区别
- CSS vertical-align属性的用法
- memcached底层工作原理
- 路由器配置DHCP实验
- 刚刚注册写一写
- IOS友盟统计Bug追踪
- cc_美团项目之我的账户页面实现
- viterbi译码算法
- C#里怎么实现点击DataGridView里面再显示一个DataGridView
- Valid Number 有限状态自动机
- 李明杰--小码哥
- CSS的SASS样式编程指南
- hdu5280dp