hibernate举例:
银行转账实例
(1)首先创建一个工程,在工程的src下new一个包名(根据实际情况),我这里取得是com.zhiyou100.hibernate;然后再该包下面一次new出:dao包,model包,Service包,和test包;(做这些的前提是先导入hibernate的jar包以及数据库的驱动包)具体如下图
使用hibernate需要导入的jar包如图:
(2)hibernateUtil工具类
public class HibernateUtil {
private static final Configuration CONF;
private static final SessionFactory FACTORY;
static {
CONF = new Configuration().configure();
FACTORY = CONF.buildSessionFactory();
}
public static Session getSession (){
return FACTORY.openSession();
}
public static Session getCurrentSession (){
return FACTORY.getCurrentSession();
}
}
(3)dao层的接口和实现类分别为
(4)model类(此处省略了model类里面的get和set方法)
(5)Service层的接口以及实现类
Impl
(6)test测试类
(7)hibernate.cfg.xml文件