我用hibernate 4.3.6创建基本的spring MVC项目,我使用oracle 11g作为我的数据库。但我经常遇到异常
HTTP Status 500 - Request processing failed; nested exception is org.hibernate.boot.registry.classloading.spi.ClassLoadingException: Unable to load class [oracle.jdbc.driver.OracleDriver]
我已在我的本地安装了ojdbc6 jar并将其添加为maven依赖项以及我尝试添加为外部jar但它似乎无法正常工作。请问我哪里错了?
这是我的POM.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>springMVC</groupId>
<artifactId>assignment</artifactId>
<packaging>war</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>assignment Maven Webapp</name>
<url>http://maven.apache.org</url>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.0.1</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.5.3</version>
</dependency>
<!-- ORACLE database driver -->
<dependency>
<groupId>com.oracle</groupId>
<artifactId>ojdbc6</artifactId>
<version>11.2.0</version>
<scope>system</scope>
<systemPath>F:\ojdbc6.jar</systemPath>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-webmvc -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>4.3.2.RELEASE</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>4.3.6.Final</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
</dependencies>
<build>
<finalName>assignment</finalName>
</build>
</project>
这是我的config.java
package assignment.util;
import org.hibernate.SessionFactory;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;
import org.hibernate.tool.hbm2ddl.SchemaExport;
import org.springframework.stereotype.Component;
import assignment.service.UserEntity;
@Component
public class HibernateUtil {
private static SessionFactory sessionFactorty=null;
public static SessionFactory getSessionFactory(){
if(sessionFactorty==null){
System.out.println("inside hibernate Util");
Configuration configuration=new Configuration();
configuration.addAnnotatedClass(UserEntity.class);
configuration.setProperty("hibernate.dialect", "org.hibernate.dialect.Oracle10gDialect");
configuration.setProperty("hibernate.connection.driver_class", "oracle.jdbc.driver.OracleDriver");
configuration.setProperty("hibernate.connection.username", "SYSTEM");
configuration.setProperty("hibernate.connection.password", "sa");
configuration.setProperty("hibernate.connection.url", "jdbc:oracle:thin:@127.0.0.1:1158:myDb");
SchemaExport schemaExport=new SchemaExport(configuration);
schemaExport.create(true, true);
StandardServiceRegistryBuilder srb=new StandardServiceRegistryBuilder();
srb.applySettings(configuration.getProperties());
ServiceRegistry serviceRegistry=srb.build();
sessionFactorty=configuration.buildSessionFactory(serviceRegistry);
}
return sessionFactorty;
}
public static void shutDown(){
if(sessionFactorty==null)
sessionFactorty.close();
}
}
编辑2: LoginController.java
package assignment.view;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;
import assignment.model.User;
import assignment.model.Validator;
@Controller
@RequestMapping("/login")
public class LoginController {
@Autowired
Validator validator;
@RequestMapping(method=RequestMethod.POST)
public String userLogin(@ModelAttribute("userForm") User user){
System.out.println(user);
validator.process(user);
return "success";
}
@RequestMapping(method=RequestMethod.GET)
public String register(Model model){
User user=new User();
model.addAttribute("userForm", user);
return "Register";
}
}
UserEntity.java
package assignment.service;
import java.io.Serializable;
import java.util.Date;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
@Entity
@Table(name="User")
public class UserEntity implements Serializable {
private static final long serialVersionUID = -6620152467355557520L;
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
private Integer userId;
@Column(nullable=false)
private String userName;
@Column(nullable=false)
private String email;
@Column(nullable=false)
@Temporal(TemporalType.DATE)
private Date dob;
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public Date getDob() {
return dob;
}
public void setDob(Date dob) {
this.dob = dob;
}
}
我的服务类
package assignment.service;
import org.hibernate.Session;
import org.springframework.stereotype.Component;
import assignment.model.User;
import assignment.util.HibernateUtil;
import assignment.util.ServiceUtil;
@Component
public class UserService {
public void saveUser(User user) {
Session session=HibernateUtil.getSessionFactory().openSession();
session.beginTransaction();
UserEntity userEntity=new UserEntity();
userEntity.setDob(ServiceUtil.stringToDateConverter(user.getDob()));
userEntity.setEmail(user.getEmail());
userEntity.setUserName(user.getUserName());
session.save(userEntity);
session.getTransaction().commit();
session.disconnect();
HibernateUtil.shutDown();
}
}
我的webIntilizer(web.xml&#39; java版)
package assignment.config;
import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;
public class WebInitilizer extends AbstractAnnotationConfigDispatcherServletInitializer {
@Override
protected Class<?>[] getRootConfigClasses() {
return new Class<?>[]{RootConfig.class};
}
@Override
protected Class<?>[] getServletConfigClasses() {
return new Class<?>[]{WebConifg.class};
}
@Override
protected String[] getServletMappings() {
return new String[]{"/"};
}
}
答案 0 :(得分:2)
(编辑以提供替代)
比以下更好的方法是在Maven仓库中安装ojdbc6.jar
。
假设您位于ojdbc6.jar
所在的库中,并假设mvn
已在命令行运行中安装(使用您的版本替换版本):
mvn install:install-file -Dfile=ojdbc6.jar -DgroupId=com.oracle -DartifactId=ojdbc6 -Dversion=11.2.0.3 -Dpackaging=jar -DgeneratePom=true
成功完成后,您可以使用新的Maven坐标在pom.xml中引用它:
<dependency>
<groupId>com.oracle</groupId>
<artifactId>ojdbc6</artifactId>
<version>11.2.0.3</version>
</dependency>
错误消息实际上表明您的Oracle jdbc驱动程序尚未成功导入项目。我建议直接在项目中创建一个lib
目录,将ojdbc6.jar
放在那里并将驱动程序的Maven坐标更改为:
<!-- ORACLE database driver -->
<dependency>
<groupId>com.oracle</groupId>
<artifactId>ojdbc6</artifactId>
<version>11.2.0</version>
<scope>system</scope>
<systemPath>${basedir}/lib/ojdbc6.jar</systemPath>
</dependency>
答案 1 :(得分:0)
您是否尝试将ojdbc6.jar放入[JAVA_HOME] / jre / lib / ext