一、建立java项目
二、引入jar包
概念了解:
1、库中包含多个jar包,可以建立自己的库,过程如下:
2、Add JARs向库中添加jar包(包括:hibernate依赖的所有第三方jar包、hibernate核心包、数据库的驱动包)3、将库添加到项目中
三、配置文件
四、建立实体类,映射实体类核心配置文件:hibernate.cfg.xml;放到src目录下即可
<!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> <!--配置解析,现在为mysql,这样在匹配时翻译为mysql的语句--> <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property> </session-factory> </hibernate-configuration>
1、建立实体类
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; } }
2、进行映射
<?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"> <!--id的代表主键--> <generator class="uuid"/><!--id生成策略使用 UUID--> </id> <property name="name"/> <property name="password"/> <property name="createTime"/> <property name="expireTime"/> </class> </hibernate-mapping>
五、将User.hbm.xml文件加入到hibernate.cfg.xml
<mapping resource="com/bjpowernode/hibernate/User.hbm.xml"/>
以上配置到 property 后
六、编写工具类,将实体对应的建立到库中package com.bjpowernode.hibernate; import org.hibernate.cfg.Configuration; import org.hibernate.tool.hbm2ddl.SchemaExport; 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); } }
七、实现添加数据到库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(); } } } } }
小结:一、 hibernate.cfg.xml ①配置文件连接数据 ②配置映射文件二、建立实体类三、 配置映射文件: 类名.hbm.xml