创建一个hibernate helloword
1、下载hibernate包
http://hibernate.org/orm/
点download下载最终版
安装hibernate插件hibernatetools
2、创建一个java工程
导入必须的jar包
D:\projects\hibernate-release-5.2.3.Final\lib\required
3、创建一个测试数据库
4、编写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> <!-- 连接数据库的基本信息 --> <property name="connection.username">root</property> <property name="connection.password">root</property> <property name="connection.driver_class">com.mysql.jdbc.Driver</property> <property name="connection.url">jdbc:mysql://127.0.0.1:3306/hibernate?useUnicode=true&characterEncoding=UTF-8</property> <!--配置hibernate的基本信息 --> <property name="dialect">org.hibernate.dialect.MySQL5InnoDBDialect</property> <property name="show_sql">true</property> <property name="format_sql">true</property> <property name="hbm2ddl.auto">update</property> <!-- 指定关联的配置文件 --> <mapping resource="com/hy/News.hbm.xml"/> </session-factory> </hibernate-configuration>
5、编写一个简单的实体bean
package com.hy; import java.sql.Date; public class News { private Integer id; private String title; private String content; private String author; private Date createdate; public News() { } public News(String title, String content, String author, Date createdate) { super(); this.title = title; this.content = content; this.author = author; this.createdate = createdate; } public Date getCreatedate() { return createdate; } public void setCreatedate(Date createdate) { this.createdate = createdate; } 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; } public String getAuthor() { return author; } public void setAuthor(String author) { this.author = author; } }
6、编写一个测试类
package com.hy; import java.sql.Date; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.Transaction; import org.hibernate.boot.registry.StandardServiceRegistryBuilder; import org.hibernate.cfg.Configuration; import org.hibernate.service.ServiceRegistry; import org.junit.Test; public class HibernateTest { @Test public void test() { Configuration configuration = new Configuration().configure("hibernate.cfg.xml"); SessionFactory sessionFactory = null; //ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder().applySettings(configuration.getProperties()).build(); sessionFactory = configuration.buildSessionFactory(); Session session = sessionFactory.openSession(); Transaction transaction = session.beginTransaction(); News news = new News("java", "java学习方法大全", "小米", new Date(new java.util.Date().getTime())); //News news = new News("java", "java123", "wh", new Date(new java.util.Date().getTime())); session.save(news); transaction.commit(); session.close(); sessionFactory.close(); } } ;
注意的点:
1、数据库编码必须都是utf8 否则保存中文有问题 修改my.ini文件 设置数据库和表的编码都是utf8
2、配置方言的时候要注意你的mysql版本 5.0以上用org.hibernate.dialect.MySQL5InnoDBDialect,5以下用org.hibernate.dialect.MySQLInnoDBDialect否则生成表有问题,如:type=innodb 或 engine=innodb的问题
3、创建sessionFactory的时候有部分版本要用serviceRegistry ,部分版本不用,否则会报错找不到entity
邮箱:wangh_2@sina.com