Hibernate是一个轻量级的ORM开源框架。Hibernate解脱了面对繁琐的JDBC的开发者,使得变成更加面向对象化,将之前的连接语句,增删改查等语句进行了很好的封装,使得业务逻辑等的编写更加的容易;使移植数据库也变得十分的容易,即根据不同的数据库配置configuration.cfg.xml就可以轻松改变数据库;下面以一个代码段稍作解释:
一 :每个相对应的实体都会有相对应的映射文件,映射文件的作用主要是维护实体间的关系。
实体类User:
package com.bjpowernode.hibernate; 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; } }
映射文件:
<?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>
将映射文件生成dll文件:
package com.bjpowernode.hibernate; 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); } }
二 :在configuration.cfg.xml中配置基本信息,并将映射文件信息配置到configuration.cfg.xml中
configuration.cfg.xml配置文件:
<!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="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property> <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/hibernate_first</property> <property name="hibernate.connection.username">root</property> <property name="hibernate.connection.password">bjpowernode</property> <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property> <property name="hibernate.show_sql">true</property> <mapping resource="com/bjpowernode/hibernate/User.hbm.xml"/> </session-factory> </hibernate-configuration>
三:建立客户端类Client,添加用户数据到数据库
package com.bjpowernode.hibernate; import java.util.Date; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.cfg.Configuration; public class Client { public static void main(String[] args) { //读取hibernate.cfg.xml文件 Configuration cfg = new Configuration().configure(); //建立SessionFactory SessionFactory factory = cfg.buildSessionFactory(); //取得session Session session = null; try { session = factory.openSession(); //开启事务 session.beginTransaction(); User user = new User(); user.setName("张三"); user.setPassword("123"); user.setCreateTime(new Date()); user.setExpireTime(new Date()); //保存User对象 session.save(user); //提交事务 session.getTransaction().commit(); }catch(Exception e) { e.printStackTrace(); //回滚事务 session.getTransaction().rollback(); }finally { if (session != null) { if (session.isOpen()) { //关闭session session.close(); } } } } }
总结:上面只是一个小例子,但是从小例子中我们依稀可以感受到那种封装给我们带来的便捷了。
时间: 2024-10-05 20:17:14