首先是开发环境,自己本地安装MySQL(虽然我更加熟悉的是Oracle),作为一个JAVA程序员,IDE首选当然是Eclipse,但是STS功能会更加的强大一些,再加上Hibernte的插件,用起来会舒服很多,JDK版本为JDK8,使用maven搭建。
直接上项目构建文件pom.xml的配置代码,设置项目字符集UTF-8
<properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> </properties>
添加依赖库
<dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.37</version> </dependency> <dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate-core</artifactId> <version>4.3.10.Final</version> </dependency>
由于是maven项目,需要用maven插件来指定项目的jdk版本
<plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.3</version> <configuration> <source>1.8</source> <target>1.8</target> </configuration> </plugin>
基本的环境搭建完成,配置Hibernate主配置文件hibernate.cfg.xml,由于是maven项目,在源文件夹src/main/resources下创建配置文件
<?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="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property> <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/hibernate</property> <property name="hibernate.connection.username">root</property> <property name="hibernate.connection.password">123456</property> <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property> <property name="show_sql">true</property> <property name="format_sql">true</property> <property name="hbm2ddl.auto">update</property> </session-factory> </hibernate-configuration>
开始编码了,第一步要创建一个持久化类,业界称PO,其实就是一个java bean
package po; 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; } }
创建对象关系映射文件News.hbm.xml,在源文件夹src/main/resources下创建一个与持久化类一样的包,将对象关系映射文件创建这个包下
<?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-12-31 11:06:22 by Hibernate Tools 3.5.0.Final --> <hibernate-mapping> <class name="po.News" table="BD_NEWS"> <id name="id" type="java.lang.Integer"> <column name="ID" /> <generator class="native" /> </id> <property name="title" type="java.lang.String"> <column name="TITLE" /> </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>
将对象关系映射文件添加到hibernate的主配置文件中
<mapping resource="po/News.hbm.xml"/>
测试代码
Configuration configuration = new Configuration().configure(); ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder().applySettings(configuration.getProperties()).build(); SessionFactory sesstionFactory = configuration.buildSessionFactory(serviceRegistry); Session session = sesstionFactory.openSession(); Transaction transaction = session.beginTransaction(); News n = new News(); n.setAuthor("测试作者"); n.setTitle("测试标题"); n.setDate(new Date(new java.util.Date().getTime())); try { session.save(n); transaction.commit(); } catch (Exception e) { transaction.rollback(); e.printStackTrace(); }finally{ session.close(); sesstionFactory.close(); }