请帮我解决这个问题。
[RMI TCP连接(3)-127.0.0.1]警告 org.springframework.web.context.support.XmlWebApplicationContext - 上下文初始化期间遇到异常 - 取消 刷新尝试 org.springframework.beans.factory.BeanCreationException:创建名为' transactionManager'的bean时出错在ServletContext中定义 resource [/WB-INF/mvc-dispatcher-servlet.xml]:调用init 方法失败;嵌套异常是 org.hibernate.service.UnknownUnwrapTypeException:无法解包到 请求类型[javax.sql.DataSource]
my mvc-dispatcher-servlet.xml
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd">
<context:component-scan base-package="com.springapp.mvc"/>
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/pages/"/>
<property name="suffix" value=".jsp"/>
</bean>
<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="configLocation" value="classpath:hibernate.cfg.xml"></property>
</bean>
<bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"/>
</bean>
<tx:annotation-driven transaction-manager="transactionManager"/>
</beans>
的hibernate.cfg.xml
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-configuration SYSTEM
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/company</property>
<property name="hibernate.connection.username">Kate</property>
<property name="hibernate.connection.password">Ant0987M@+</property>
<property name="hibernate.hbm2ddl.auto">update</property>
<property name="show_sql">false</property>
<mapping class="com.springapp.mvc.User"/>
</session-factory>
</hibernate-configuration>
User.java
package com.springapp.mvc;
import javax.persistence.*;
@Entity
@Table(name = "user")
public class User {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "id")
private Long id;
@Column(name = "firstname")
private String firstName;
@Column(name = "lastname")
private String lastName;
@Column(name = "email")
private String email;
public User() {
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String name) {
this.firstName = name;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
}
UserController.java
package com.springapp.mvc;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
@Controller
public class UserController {
@Autowired
private UserRepository userRepository;
static final Logger logger = LogManager.getLogger("MyLogger");
@RequestMapping(value = "/", method = {RequestMethod.GET, RequestMethod.HEAD})
public String listUsers(ModelMap model) {
logger.info("in meth listUsers");
logger.trace("trace in listUsers");
model.addAttribute("user", new User());
model.addAttribute("users", userRepository.findAll());
return "users";
}
@RequestMapping(value = "/add", method = {RequestMethod.POST, RequestMethod.HEAD})
public String addUser(@ModelAttribute("user") User user, BindingResult result) {
logger.info("in meth addUser");
userRepository.save(user);
return "redirect:/"/* + userRepository.getClass().getName()*/;
}
@RequestMapping("/delete/{userId}")
public String deleteUser(@PathVariable("userId") Long userId) {
logger.info("in meth deleteUser");
userRepository.delete(userRepository.findOne(userId));
return "redirect:/";
}
@RequestMapping(value = "/api/users", method = {RequestMethod.GET, RequestMethod.HEAD})
public
@ResponseBody
String listUsersJson(ModelMap model) throws JSONException {
JSONArray userArray = new JSONArray();
for (User user : userRepository.findAll()) {
JSONObject userJSON = new JSONObject();
userJSON.put("id", user.getId());
userJSON.put("firstName", user.getFirstName());
userJSON.put("lastName", user.getLastName());
userJSON.put("email", user.getEmail());
userArray.put(userJSON);
}
return userArray.toString();
}
}
UserRepository.java
package com.springapp.mvc;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.repository.Repository;
import java.util.List;
public interface UserRepository extends Repository <User, Long> {
public <S extends User> S save(S entity);
public User findOne(Long aLong);
public List<User> findAll();
public void delete(User entity);
}
答案 0 :(得分:0)
在您的配置中,从配置
中删除休眠。 <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/company</property>
<property name="hibernate.connection.username">Kate</property>
<property name="hibernate.connection.password">Ant0987M@+</property>
更正了配置
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="connection.url">jdbc:mysql://localhost:3306/company</property>
<property name="connection.username">Kate</property>
<property name="connection.password">Ant0987M@+</property>