hibernate简单的搭建

版权声明:本文为博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/weixin_43521890/article/details/84103872

hibernate

它是一个轻量级,企业级,开源的ORM持久层框架。是可以操作数据库的框架。
框架:它是一个架构。
通常情况下,软件工程的持久层解决方案,一个为主一个为辅。两者并存(写SQL语句的和不写SQL语句的)
轻量级:指的是使用时依赖的资源很少。(目前我们使用的阶段,只依赖log4j,c3p0连接池)
企业级:指的是在企业级应用中使用的比较多
开源的:开放源代码的
ORM操作方式:建立对象关系映射,实现操作实体类就相当于操作数据库表

Hibernate是一个开放源代码的对象关系映射框架,它对JDBC进行了非常轻量级的对象封装,它将POJO与数据库表建立映射关系,是一个全自动的orm框架,hibernate可以自动生成SQL语句,自动执行,使得Java程序员可以随心所欲的使用对象编程思维来操纵数据库。 Hibernate可以应用在任何使用JDBC的场合,既可以在Java的客户端程序使用,也可以在Servlet/JSP的Web应用中使用,最具革命意义的是,Hibernate可以在应用EJB的JaveEE架构中取代CMP,完成数据持久化的重任。

hibernate的下载
网址:http://hibernate.org 第一步:进入hibernate官网,进入ORM
在这里插入图片描述

http://hibernate.org/orm/第二步:选择releases,选择版本,这里选的是5.0,带有橙色提示的是最新版本,带有绿色提示的是最稳定的版本

http://hibernate.org/orm/releases/5.0/    第三步:选择5.0版本的小版本,这里选的是5.07,点击Download下载
在这里插入图片描述

下载中
在这里插入图片描述

下载完成后,是一个压缩包
在这里插入图片描述

解压到一个新创的文件夹下
在这里插入图片描述

文件目录
在这里插入图片描述

主要jar包
在这里插入图片描述

日志jar包
在这里插入图片描述

数据库驱动

在这里插入图片描述

准备项目

<1>创建一个java项目,导入jar包到lib下(并build path)

在这里插入图片描述
<2>创建一个实体类,实现序列化接口
在这里插入图片描述
<3>在实体类所在的包下,创建一个student.hbm.xml的xml文件
在这里插入图片描述
<4>为student.hbm.xml添加dtd约束
在这里插入图片描述——在这里插入图片描述——在这里插入图片描述——在这里插入图片描述
打开,复制dtd约束到student.hbm.xml文件中
在这里插入图片描述
完成
在这里插入图片描述
<5>在student.hbm.xml中配置实体类和数据库表的映射
在这里插入图片描述
<6>在类的根路径下创建名称为hibernate.cfg.xml的配置文件
在这里插入图片描述
<7>为hibernate.cfg.xml导入dtd
在这里插入图片描述——在这里插入图片描述——在这里插入图片描述——在这里插入图片描述
打开,复制dtd约束到hibernate.cfg.xml文件中
在这里插入图片描述
完成
在这里插入图片描述
<8>配置hibernate.cfg.xml
标签下:
配置sessionFactory
sessionFactory的作用就是用于创建session对象的
session对象就是hibernate中操作数据库的核心对象。
sessionFactory三大部分:
第一部分:
连接数据库信息
第二部分:
hibernate的可选配置
第三部分:
映射文件位置
在这里插入图片描述
<9>创建数据库
在这里插入图片描述
<10>创建测试类HibernateDemo
在这里插入图片描述
<11>向数据库添加数据
在这里插入图片描述
运行成功
在这里插入图片描述

<12>查看数据库
在这里插入图片描述

展开阅读全文

springMvc hibernate 搭建出错

02-28

rnorg.springframework.beans.factory.BeanCreationException: Error creating bean with name 'studentController': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: public com.mvc.service.StudentService com.mvc.controller.StudentController.studentService; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No matching bean of type [com.mvc.service.StudentService] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: @org.springframework.beans.factory.annotation.Autowired(required=true)rn at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:286)rn at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1055)rn at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:511)rn at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:450)rn at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:290)rn at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222)rn at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:287)rn at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:189)rn at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:562)rn at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:871)rn at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:423)rn at org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:443)rn at org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:459)rn at org.springframework.web.servlet.FrameworkServlet.initWebApplicationContext(FrameworkServlet.java:340)rn at org.springframework.web.servlet.FrameworkServlet.initServletBean(FrameworkServlet.java:307)rn at org.springframework.web.servlet.HttpServletBean.init(HttpServletBean.java:127)rn at javax.servlet.GenericServlet.init(GenericServlet.java:212)rn at org.apache.catalina.core.StandardWrapper.loadServlet(StandardWrapper.java:1161)rn at org.apache.catalina.core.StandardWrapper.allocate(StandardWrapper.java:806)rn at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:129)rn at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:175)rn at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:128)rn at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:104)rn at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)rn at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:261)rn at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:844)rn at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:581)rn at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:447)rn at java.lang.Thread.run(Thread.java:619)rnCaused by: org.springframework.beans.factory.BeanCreationException: Could not autowire field: public com.mvc.service.StudentService com.mvc.controller.StudentController.studentService; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No matching bean of type [com.mvc.service.StudentService] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: @org.springframework.beans.factory.annotation.Autowired(required=true)rn at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:507)rn at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:84)rn at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:283)rn ... 28 morernCaused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No matching bean of type [com.mvc.service.StudentService] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: @org.springframework.beans.factory.annotation.Autowired(required=true)rn at org.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoSuchBeanDefinitionException(DefaultListableBeanFactory.java:901)rn at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:770)rn at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:685)rn at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:478)rn ... 30 morern2015-2-28 9:45:58 org.apache.catalina.core.StandardWrapperValve invokern严重: Allocate exception for servlet springrnorg.springframework.beans.factory.NoSuchBeanDefinitionException: No matching bean of type [com.mvc.service.StudentService] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: @org.springframework.beans.factory.annotation.Autowired(required=true)rn at org.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoSuchBeanDefinitionException(DefaultListableBeanFactory.java:901)rn at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:770)rn at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:685)rn at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:478)rn at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:84)rn at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:283)rn at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1055)rn at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:511)rn at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:450)rn at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:290)rn at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222)rn at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:287)rn at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:189)rn at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:562)rn at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:871)rn at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:423)rn at org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:443)rn at org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:459)rn at org.springframework.web.servlet.FrameworkServlet.initWebApplicationContext(FrameworkServlet.java:340)rn at org.springframework.web.servlet.FrameworkServlet.initServletBean(FrameworkServlet.java:307)rn at org.springframework.web.servlet.HttpServletBean.init(HttpServletBean.java:127)rn at javax.servlet.GenericServlet.init(GenericServlet.java:212)rn at org.apache.catalina.core.StandardWrapper.loadServlet(StandardWrapper.java:1161)rn at org.apache.catalina.core.StandardWrapper.allocate(StandardWrapper.java:806)rn at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:129)rn at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:175)rn at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:128)rn at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:104)rn at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)rn at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:261)rn at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:844)rn at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:581)rn at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:447)rn at java.lang.Thread.run(Thread.java:619)rn 论坛

eclipse hibernate搭建 问题

03-11

mysql建表:rnCREATE DATABASE login;rnCREATE TABLE userlogin(rnrnNAME VARCHAR(20) NOT NULL,rnPASSWORD INT(20) NOT NULLrn)rnDROP TABLE userlogin;rnINSERT INTO userlogin VALUES('jack',123456);rnINSERT INTO userlogin VALUES('tom',123456);rnINSERT INTO userlogin VALUES('mary',123456);rnrneclipse:rnrnrnhibernate.cfg.xml:rnrnrnrnrn rn rn >rn com.mysql.jdbc.Driverrn jdbc:mysql://localhost:3306/loginrn rootrn ljfrn truern rn rnrnrnlogin.java:rnrnpackage com.test;rnpublic class login rnprivate String username;rnprivate String password;rnpublic String getUsername() rnreturn username;rnrnpublic void setUsername(String username) rnthis.username = username;rnrnpublic String getPassword() rnreturn password;rnrnpublic void setPassword(String password) rnthis.password = password;rnrnrnrnlogin.hbm.xml:rnrnrnrnrn rn rn rn rn rn rn rn rnrnrnrnHibernateUtil:rnrnpackage com.util;rnimport org.hibernate.SessionFactory;rnimport org.hibernate.cfg.Configuration;rnpublic class HibernateUtil rn private static final SessionFactory sessionFactory;rn static rn try rn // Create the SessionFactory from hibernate.cfg.xmlrn //sessionFactory = new Configuration().configure().buildSessionFactory();rn Configuration config=new Configuration().configure();rn //实现hibernate初始化rn sessionFactory=config.buildSessionFactory();rn catch (Throwable ex) rn // Make sure you log the exception, as it might be swallowedrn System.err.println("Initial SessionFactory creation failed." + ex);rn throw new ExceptionInInitializerError(ex);rn rn rnrn public static SessionFactory getSessionFactory() rn return sessionFactory;rn rnrnrnloginDao:rnrnpackage com.dao;rnimport org.hibernate.Session;rnimport org.hibernate.Transaction;rnimport com.test.login;rnimport com.util.HibernateUtil;rnpublic class loginDao rnpublic void sava(login Login) rnSession session=HibernateUtil.getSessionFactory().openSession();rnTransaction tran=session.beginTransaction();rnsession.save(Login);rntran.commit();rn session.close();rnrnrnloginText:rnpackage com.test;rnimport com.dao.loginDao;rnpublic class loginText rnpublic static void main(String[] args) rnlogin Login=new login();rnLogin.setUsername("ljf");rnLogin.setPassword("yes");rnloginDao ldao=new loginDao();rnldao.sava(Login);rn rnrnrn运行报错:rnSLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".rnSLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details.rnInitial SessionFactory creation failed.java.lang.NoClassDefFoundError: org/slf4j/impl/StaticLoggerBinderrnException in thread "main" java.lang.ExceptionInInitializerErrorrnat com.util.HibernateUtil.(HibernateUtil.java:20)rnat com.dao.loginDao.sava(loginDao.java:11)rnat com.test.loginText.main(loginText.java:11)rnCaused by: java.lang.NoClassDefFoundError: org/slf4j/impl/StaticLoggerBinderrnat org.slf4j.LoggerFactory.getSingleton(LoggerFactory.java:223)rnat org.slf4j.LoggerFactory.bind(LoggerFactory.java:120)rnat org.slf4j.LoggerFactory.performInitialization(LoggerFactory.java:111)rnat org.slf4j.LoggerFactory.getILoggerFactory(LoggerFactory.java:269)rnat org.slf4j.LoggerFactory.getLogger(LoggerFactory.java:242)rnat org.slf4j.LoggerFactory.getLogger(LoggerFactory.java:255)rnat org.hibernate.cfg.Configuration.(Configuration.java:152)rnat com.util.HibernateUtil.(HibernateUtil.java:14)rn... 2 morernCaused by: java.lang.ClassNotFoundException: org.slf4j.impl.StaticLoggerBinderrnat java.net.URLClassLoader$1.run(URLClassLoader.java:366)rnat java.net.URLClassLoader$1.run(URLClassLoader.java:355)rnat java.security.AccessController.doPrivileged(Native Method)rnat java.net.URLClassLoader.findClass(URLClassLoader.java:354)rnat java.lang.ClassLoader.loadClass(ClassLoader.java:425)rnat sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)rnat java.lang.ClassLoader.loadClass(ClassLoader.java:358)rn... 10 morern搭建架包说明:rnhibernate-distribution-3.3.2.GA 中lib中所有架包,hibernate3.jar,commons-long.2.3.jar ,rnslf4j-api-1.5.8.jar ,log4j-1.2.16jar, mysql-connector-java-3.0.16-ga-bin.jar 论坛

没有更多推荐了,返回首页