搭建好环境之后,为了避免过多的重复,可以建立工具类,以便更好的复用。
第一个(ExportDB):程序入口类,加载配置文件,生成数据库表
import org.hibernate.cfg.Configuration; import org.hibernate.tool.hbm2ddl.SchemaExport; /** * 将hbm生成ddl * @author Administrator * */ public class ExportDB { public static void main(String[] args) { //默认读取hibernate.cfg.xml文件 Configuration cfg = new Configuration().configure(); SchemaExport export = new SchemaExport(cfg); export.create(true, true); } }
第二个(HibernateUtils):工具类,读取相应的配置文件,建立Session管理工厂
import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.cfg.Configuration; public class HibernateUtils { private static SessionFactory factory; static { try { //读取hibernate.cfg.xml文件 Configuration cfg = new Configuration().configure(); //建立SessionFactory factory = cfg.buildSessionFactory(); }catch(Exception e) { e.printStackTrace(); } } public static Session getSession() { return factory.openSession(); } public static void closeSession(Session session) { if (session != null) { if (session.isOpen()) { session.close(); } } } public static SessionFactory getSessionFactory() { return factory; } }有了上面的程序入口类和上篇环境搭建里提到的配置文件,在只需要写你需要的实体类和实体类对应的配置文件,即可成功导出相应的数据表。
a、用户实体类:
import java.util.Date; public class User { private String id; private String name; private String password; private Date createTime; private Date expireTime; public String getId() { return id; } public void setId(String id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public Date getCreateTime() { return createTime; } public void setCreateTime(Date createTime) { this.createTime = createTime; } public Date getExpireTime() { return expireTime; } public void setExpireTime(Date expireTime) { this.expireTime = expireTime; } }
b、用户对应的配置文件(User.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"> <hibernate-mapping> <class name="com.bjpowernode.hibernate.User"> <id name="id"> <generator class="uuid"/> </id> <property name="name"/> <property name="password"/> <property name="createTime"/> <property name="expireTime"/> </class> </hibernate-mapping>
c、在配置文件(hibernate.cfg.xml)加入下面这句代码
<mapping resource="com/bjpowernode/hibernate/User.hbm.xml"/>最后执行ExportDB类,生成的表如下图所示:
使用工具类进行单元测试,例如:SessionTest测试类
import java.util.Date; import junit.framework.TestCase; import org.hibernate.Session; import org.hibernate.Transaction; public class SessionTest extends TestCase { public void testSave1() { Session session = null; Transaction tx = null; try { //拿到session session = HibernateUtils.getSession(); //开启事务 tx = session.beginTransaction(); //给实体赋值 User user = new User(); user.setName("李四"); user.setPassword("123"); user.setCreateTime(new Date()); user.setExpireTime(new Date()); //保存数据到数据库 session.save(user); //提交事务 tx.commit(); }catch(Exception e) { //打印堆栈信息 e.printStackTrace(); //事务回滚 if (tx != null) { tx.rollback(); } }finally { //关闭session HibernateUtils.closeSession(session); } }执行后将会有一条数据存入user表里,如下图:
配置文件里的具体的配置信息以后的博客里会陆续的逐一介绍。