Spring MVC 数据库连接

Spring MVC database connection

我必须创建与数据库的连接以在 Spring MVC 中插入某些值,但是在创建连接时我的 servlet-context.xml 中出现以下错误:

Multiple annotations found at this line: - Cannot locate BeanDefinitionParser for element [bean] - cvc-complex-type.2.4.c: The matching wildcard is strict, but no declaration can be found for element 'bean'. - Configuration problem: Cannot locate BeanDefinitionParser for element [bean] Offending resource: file [E:/General Workspace/Spring Workspace/Record_mvc/ src/main/webapp/WEB-INF/spring/appServlet/servlet-context.xml]

我已经用 id="dataSource" 声明了 bean,但它似乎不起作用。你能帮我理解为什么它找不到元素 'bean' 的声明吗?它是什么意思

"the matching wildcard is strict"

这是调度程序的代码。提前致谢。

<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns="http://www.springframework.org/schema/mvc"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:beans="http://www.springframework.org/schema/beans"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
        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">

    <!-- DispatcherServlet Context: defines this servlet's request-processing 
        infrastructure -->

    <!-- Enables the Spring MVC @Controller programming model -->
    <annotation-driven />

    <!-- Handles HTTP GET requests for /resources/** by efficiently serving 
        up static resources in the ${webappRoot}/resources directory -->
    <resources mapping="/resources/**" location="/resources/" />

    <!-- Resolves views selected for rendering by @Controllers to .jsp resources 
        in the /WEB-INF/views directory -->
    <beans:bean
        class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <beans:property name="prefix" value="/WEB-INF/views/" />
        <beans:property name="suffix" value=".jsp" />

    </beans:bean>
    <context:component-scan base-package="com.lead.mvc" />

    <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName" value="com.mysql.jdbc.Driver" />
        <property name="url" value="jdbc:mysql://localhost:8080/leadmanager" />
        <property name="username" value="root" />
        <property name="password" value="root" />
    </bean>

    <!-- <bean id="jdbcTransactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource" />
    </bean> -->
</beans:beans>

好的,所以问题是你没有为bean指定通配符,所以你应该写:

<beans:bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName" value="com.mysql.jdbc.Driver" />
        <property name="url" value="jdbc:mysql://localhost:8080/leadmanager" />
        <property name="username" value="root" />
        <property name="password" value="root" />
</beans:bean>

然后应该可以了。