spring mybatis映射文件配置问题

时间:2017-03-18 06:31:12

标签: spring-mvc mybatis spring-mybatis

弹簧servlet.xml中

<?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"
    xmlns:aop="http://www.springframework.org/schema/aop" 
    xmlns:jee="http://www.springframework.org/schema/jee"
    xmlns:lang="http://www.springframework.org/schema/lang" 
    xmlns:p="http://www.springframework.org/schema/p"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xmlns:tx="http://www.springframework.org/schema/tx" 
    xmlns:util="http://www.springframework.org/schema/util"
    xsi:schemaLocation="http://www.springframework.org/schema/lang http://www.springframework.org/schema/lang/spring-lang-3.1.xsd
        http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.1.xsd
        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd
        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/util http://www.springframework.org/schema/util/spring-util-3.1.xsd
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">

        <!-- Add support for component scanning -->



        <!-- Add support for conversion, formatting and validation support -->
        <context:component-scan base-package="com.work" />
        <mvc:annotation-driven />

        <!-- Define Spring MVC view resolver -->
        <beans:bean id="jspViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
            <beans:property name="viewClass" value="org.springframework.web.servlet.view.JstlView" />
            <beans:property name="prefix" value="/WEB-INF/jsps/" />
            <beans:property name="suffix" value=".jsp" />
        </beans:bean>


        <!-- Add support for reading web resources: css, images, js, etc... -->
        <mvc:resources location="/images/" mapping="/images/**" />
        <mvc:resources location="/js/" mapping="/js/**" />



</beans:beans>

Village.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<%@ taglib uri="http://www.springframework.org/tags/form" prefix="s"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
    <h1>This is Home</h1>
    <s:form action="saveProcess" modelAttribute="village">
        Name:<s:input path="name"/><br />
        District:<s:input path="district"/><br />
        <input type = "submit" />
    </s:form>

</body>
</html>

MyBatisUtil.java

package com.work.util;
import java.io.IOException;
import java.io.InputStream;
import java.io.Reader;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
public class MyBatisUtil {
    private static SqlSessionFactory sqlSessionFactory;
    static {
        Reader reader;
        try {
            reader = Resources.getResourceAsReader("mybatis-config.xml");
            sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    public static SqlSessionFactory getSqlSessionFactory(){
        return sqlSessionFactory;
    }
} 

的MyBatis-config.xml中

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
  PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
  "http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
  <typeAliases>
     <typeAlias type="com.work.entity.Village" alias="village"/>
  </typeAliases>  
  <environments default="development">
    <environment id="development">
      <transactionManager type="JDBC"/>
      <dataSource type="POOLED">
        <property name="driver" value="com.mysql.jdbc.Driver"/>
        <property name="url" value="jdbc:mysql://localhost:8037/strutscrud"/>
        <property name="username" value="root"/>
        <property name="password" value="root"/>
      </dataSource>
    </environment>
  </environments>
  <mappers>
    <mapper resource="com.work.dao.VillageMapper.xml" />
  </mappers>
</configuration>

VillageMapper.java

package com.work.dao;
import java.io.Reader;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Repository;

import com.work.entity.Village;
import com.work.util.MyBatisUtil;

    @Repository
    public class VillageMapper {
        public void save(Village village){
          SqlSession session = MyBatisUtil.getSqlSessionFactory().openSession();
          session.insert("com.work.dao.VillageMapper.insertVillage", village);
          session.commit();
          session.close();
        }
    } 

VillageMapper.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
  PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
  "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.work.dao.VillageMapper" >
    <resultMap id="villageResult" type="village">
        <id property="id" column="id" />
        <result property="name" column="name"/>
        <result property="district" column="district"/>
    </resultMap>
    <insert id="insertVillage" parameterType="village"  keyProperty="id" useGeneratedKeys="true">
        INSERT into village(name,district) VALUES(#{name}, #{district})         
    </insert>
</mapper> 

VillageController.java

package com.work.controllers;

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.RequestMapping;

import com.work.dao.VillageMapper;
import com.work.entity.Village;

@Controller @RequestMapping("/village")
public class VillageController {

    @Autowired
    VillageMapper villageDAO;

    @RequestMapping("/showHome")
    public String showHome(Model model){
        model.addAttribute("village", new Village());
        return "Village";
    }

    @RequestMapping("/saveProcess")
    public String saveProcess(@ModelAttribute("village") Village village){
        villageDAO.save(village);
        return "redirect:/village/showHome";
    }
}

现在,当我运行应用程序时,我正在获取用户界面,但是当我输入值并单击“保存”时,我收到的错误是这样的:

java.io.IOException: Could not find resource com.work.dao.VillageMapper.xml
    at org.apache.ibatis.io.Resources.getResourceAsStream(Resources.java:89)
    at org.apache.ibatis.io.Resources.getResourceAsStream(Resources.java:76)
    at org.apache.ibatis.builder.xml.XMLConfigBuilder.mapperElement(XMLConfigBuilder.java:253)
    at org.apache.ibatis.builder.xml.XMLConfigBuilder.parseConfiguration(XMLConfigBuilder.java:83)
    at org.apache.ibatis.builder.xml.XMLConfigBuilder.parse(XMLConfigBuilder.java:69)
    at org.apache.ibatis.session.SqlSessionFactoryBuilder.build(SqlSessionFactoryBuilder.java:47)
    at org.apache.ibatis.session.SqlSessionFactoryBuilder.build(SqlSessionFactoryBuilder.java:29)
    at org.apache.ibatis.session.SqlSessionFactoryBuilder.build(SqlSessionFactoryBuilder.java:17)
    at com.work.util.MyBatisUtil.<clinit>(MyBatisUtil.java:15)
    at com.work.dao.VillageMapper.save(VillageMapper.java:17)
    at com.work.controllers.VillageController.saveProcess(VillageController.java:26)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:497)
    at org.springframework.web.method.support.InvocableHandlerMethod.invoke(InvocableHandlerMethod.java:212)
    at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:126)
    at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:96)
    at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:629)
    at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:590)
    at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:80)
    at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:900)
    at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:827)
    at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:874)
    at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:790)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:648)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:291)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
    at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:239)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:219)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:106)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:142)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79)
    at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:617)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:88)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:518)
    at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1091)
    at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:668)
    at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1521)
    at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.run(NioEndpoint.java:1478)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
    at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
    at java.lang.Thread.run(Thread.java:745)

它正在阅读mybatis-config.xml文件,我把这行放在

<mapper resource="com.work.dao.VillageMapper.xml" />

它不读书,该怎么办?

我将mybatis-config.xml文件放在dao包中的project-&gt; src-&gt; resources-&gt; mybatis-config.xml和VillageMapper.xml文件中。

项目

-src
    -com.work.controllers
        -VillageController.java
    -com.work.dao
        -VillageMapper.java
        -VillageMapper.xml
    -com.work.entity
        -Village.java
    -com.work.util
        -MyBatisUtil.java
    -resources
        -mybatis-config.xml

我没有使用过Maven。我该怎么改变或纠正?

1 个答案:

答案 0 :(得分:2)

mybatis-config.xml,您需要将<mapper resource="com.work.dao.VillageMapper.xml" />更改为<mapper resource="com/work/dao/VillageMapper.xml" />

您可以参考here获取有关spring mybatis实现的简单示例,还可以查看here以获取有关不同类型配置的更多详细信息。

相关问题