一、Hibernate导入相关的包
参考:http://blog.csdn.net/tunni/article/details/54982160
这些包包括相应数据库驱动、hibernate.zip下lib目录下的jar包,其中必须包是required目录下的.jar
二、在项目classpath(类路径,即src目录下)配置hibernate.cfg.xml,并且配置数据库连接
hibernate.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 > <!-- mysql数据库驱动 --> <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property> <!-- mysql数据库名称 --> <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/hibernate_db</property> <!-- 数据库的登陆用户名 --> <property name="hibernate.connection.username">root</property> <!-- 数据库的登陆密码 --> <property name="hibernate.connection.password">admin</property> <!-- 方言:为每一种数据库提供适配器,方便转换 --> <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property> <!-- 建议配置,方便在日志中查看sql语句--> <propertynamepropertyname="hibernate.show_sql">true</property> <propertyname="hibernate.format_sql">true</property> <!--配置类与表的映射文件 --> <mapping resource="com/hibernate/User.hbm.xml"/> </session-factory> </hibernate-configuration>
com.hibernate.User类
package com.hibernate; public class User { private String id; private String username; private String password; public String getId() { return id; } public void setId(String id) { this.id = id; } public String getUsername() { return username; } public void setUsername(String userName) { this.username = userName; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } }
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.hibernate.User" table="user"> <!-- 主键名 --> <id name="id" column="id"> <!-- 生成策略 --> <generator class="uuid"/> </id> <!-- 其他类属性与表字段 --> <property name="username" column="username"/> <property name="password"/> </class> </hibernate-mapping>
hibernate访问工具类
package hibernate; /** * hibernate工具 * @author maokun * */ import org.hibernate.HibernateException; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.cfg.Configuration; public class HibernateUtil { private static final SessionFactory sessionFactory; static { try { //配置文件放在classpath路径,即src目录下 //如果hibernate的配置文件目录为hibernate.cfg.xml,则 //Configuration config = new Configuration().configure(); //或Configuration config = new Configuration().configure("hibernate.cfg.xml"); //或Configuration config = new Configuration().configure("hibernate.cfg.xml"); //配置其他路径如下: //Configuration config = new Configuration().configure("hibernate/hibernate.cfg.xml"); Configuration config = new Configuration().configure("/hibernate/hibernate.cfg.xml"); sessionFactory = config.buildSessionFactory(); } catch(Throwable e) { throw new ExceptionInInitializerError(e); } } public static final ThreadLocal session = new ThreadLocal(); public static Session currentSession() throws HibernateException { Session s = (Session)session.get(); //如果线程没有session,打开新的session if(s == null || !s.isOpen()) { s = sessionFactory.openSession(); session.set(s); } return s; } public static void closeSession() throws HibernateException { Session s = (Session)session.get(); session.set(null); if(s != null) s.close(); } }
三、第一个hibernate例子
先创建hibernate_db数据库,接着创建user表包含id,username,password。
Test类
package hibernate; import org.hibernate.HibernateException; import org.hibernate.Session; import org.hibernate.Transaction; import com.hibernate.User; public class Test { public static void main(String[] args) { User user =new User(); user.setUsername("name"); user.setPassword("pass"); Session session = HibernateUtil.currentSession();//生成Session实例 Transaction tx = session.beginTransaction(); try { session.save(user); //保存持久类对象 tx.commit(); //提交到数据库 session.close(); } catch(HibernateException e) { e.printStackTrace(); tx.rollback(); } } }