刚开始学习 springmvc , 目前学到了 springmvc 整合 mybatis。 直接上代码:web.xml 文件
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
  <display-name>springmvc-web</display-name>
  
<!--   配置 spring 容器 -->
  <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath:applicationContext-*.xml</param-value>
  </context-param>
  
<!--   配置监听器 -->
  <listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>
  
<!--   配置前端控制器 -->
  <servlet>
    <servlet-name>springmvc</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
      <param-name>contextConfigLocation</param-name>
      <param-value>classpath:springmvc.xml</param-value>
    </init-param>
  </servlet>
  <servlet-mapping>
    <servlet-name>springmvc</servlet-name>
    <url-pattern>*.action</url-pattern>
  </servlet-mapping>
</web-app>sqlMapConfig.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>
</configuration>springmvc.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.2.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd">
        
        <context:component-scan base-package="cn.itcast.springmvc.controller"></context:component-scan><!--  注解驱动 目的:加载新版本的处理器映射器、处理器适配器-->
<mvc:annotation-driven/>
 
<!--  配置视图解析器的前缀和后缀 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/"></property>
<property name="suffix" value=".jsp"></property>
</bean>
</beans>
applicationContext-dao.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.2.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.2.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.2.xsd"> <!-- 加载配置文件 -->
<context:property-placeholder location="classpath:jdbc.properties" />

<!-- 数据库连接池 -->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="${jdbc.driver}" />
<property name="url" value="${jdbc.url}" />
<property name="username" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" />
<property name="maxActive" value="10" />
<property name="maxIdle" value="5" />
</bean>

<!-- mapper配置 -->
<!-- 让spring管理sqlsessionfactory 使用mybatis和spring整合包中的 -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<!-- 数据库连接池 -->
<property name="dataSource" ref="dataSource" />
<!-- 加载mybatis的全局配置文件 -->
<property name="configLocation" value="classpath:sqlMapConfig.xml" />
</bean>

<!-- 配置 Mapper 扫描器 -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="cn.hh.ssm.mapper"/>
</bean>

</beans>applicationContext-service.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.2.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.2.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.2.xsd"><!--  注解扫描器 -->
<context:component-scan base-package="cn.hh.ssm.service"/> <!-- 事务管理器 -->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<!-- 数据源 -->
<property name="dataSource" ref="dataSource" />
</bean>

<!-- 通知 -->
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<!-- 传播行为 -->
<tx:method name="save*" propagation="REQUIRED" />
<tx:method name="insert*" propagation="REQUIRED" />
<tx:method name="delete*" propagation="REQUIRED" />
<tx:method name="update*" propagation="REQUIRED" />
<tx:method name="find*" propagation="SUPPORTS" read-only="true" />
<tx:method name="get*" propagation="SUPPORTS" read-only="true" />
</tx:attributes>
</tx:advice>

<!-- 切面 -->
<aop:config>
<aop:advisor advice-ref="txAdvice"
pointcut="execution(* cn.hh.ssm.service.*.*(..))" />
</aop:config></beans>log4j.properties
# Global logging configuration
log4j.rootLogger=DEBUG, stdout
# Console output...
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%5p [%t] - %m%n
jdbc.properties
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/ssm?characterEncoding=utf-8
jdbc.username=root
jdbc.password=123
mapper层
ItemsMapper.java
package cn.hh.ssm.mapper;import java.util.List;import cn.hh.ssm.pojo.Items;public interface ItemsMapper {
public List<Items> getItems();
}ItemsMapper.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="cn.hh.ssm.mapper.ItemsMapper">
<select id="getItems" resultType="cn.hh.ssm.pojo.Items">
SELECT * FROM items
</select>
</mapper>
service 层
package cn.hh.ssm.service;import java.util.List;import cn.hh.ssm.pojo.Items;public interface ItemsService {
public List<Items> getItems();
}serviceImpl
package cn.hh.ssm.service.impl;import java.util.List;import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;import cn.hh.ssm.mapper.ItemsMapper;
import cn.hh.ssm.pojo.Items;
import cn.hh.ssm.service.ItemsService;@Service
public class ItemsServiceImpl implements ItemsService {
@Autowired
private ItemsMapper itemsMapper; @Override
public List<Items> getItems() {
return itemsMapper.getItems();
}
}controller 层
package cn.hh.ssm.controller;import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;import cn.hh.ssm.service.ItemsService;@Controller
public class ItemsController {
@Autowired
private ItemsService itemsService;

@RequestMapping("/list")
public ModelAndView getItems() {
ModelAndView modelAndView = new ModelAndView();

modelAndView.addObject("itemList", itemsService.getItems());

modelAndView.setViewName("itemList");

return modelAndView;
}
}访问:
http://localhost:8080/springmvc-mybatis/list.action 给出了 404 的提示。
访问
http://localhost:8080/本人新手,请各位帮忙看看。 感激不尽。

解决方案 »

  1.   

    启动 tomcat 的时候没有 error 信息, 给了一大堆的 debug 信息类似于:
    访问的时候:
      

  2.   

    启动 tomcat 的时候没有 error 信息, 给了一大堆的 debug 信息类似于:
    访问的时候:
    楼主,你图片没发上来,还有你那么Dao 层,也就是你ItemsMapper.java 这里面没有@Repository这个注解,你可以修改下,再跑下然后看看Console 任务栏 的信息
      

  3.   

    启动 tomcat 的时候没有 error 信息, 给了一大堆的 debug 信息类似于:
    访问的时候:
    楼主,你图片没发上来,还有你那么Dao 层,也就是你ItemsMapper.java 这里面没有@Repository这个注解,你可以修改下,再跑下然后看看Console 任务栏 的信息
      

  4.   

    启动 tomcat 的时候没有 error 信息, 给了一大堆的 debug 信息类似于:
    访问的时候:
    楼主,你图片没发上来,还有你那么Dao 层,也就是你ItemsMapper.java 这里面没有@Repository这个注解,你可以修改下,再跑下然后看看Console 任务栏 的信息

    你的第二张截图不是有异常嘛,你的数据库连接池那出了问题,url ,username这些都没找到
      

  5.   

    启动 tomcat 的时候没有 error 信息, 给了一大堆的 debug 信息类似于:
    访问的时候:
    楼主,你图片没发上来,还有你那么Dao 层,也就是你ItemsMapper.java 这里面没有@Repository这个注解,你可以修改下,再跑下然后看看Console 任务栏 的信息

    你的第二张截图不是有异常嘛,你的数据库连接池那出了问题,url ,username这些都没找到
    我这里有加载 jdbc.properties 文件呀, 为什么会找不到呢?
      

  6.   

    启动 tomcat 的时候没有 error 信息, 给了一大堆的 debug 信息类似于:
    访问的时候:
    楼主,你图片没发上来,还有你那么Dao 层,也就是你ItemsMapper.java 这里面没有@Repository这个注解,你可以修改下,再跑下然后看看Console 任务栏 的信息

    你的第二张截图不是有异常嘛,你的数据库连接池那出了问题,url ,username这些都没找到
    我这里有加载 jdbc.properties 文件呀, 为什么会找不到呢?
      

  7.   

    楼主按以下步骤排查问题
    1、项目是否正确布署在tomcat上?项目名称是否为:springmvc-mybatis?(排斥是否拼写错误)?
    2、检查tomcat是否启动正常:若不正常报什么错误?
    3、以上都正确了,那就是配置的问题了。建议:楼主以后遇到问题,应该一步一步去排查问题,而不是把代码全部都贴上来。这样,答题的人,很难抓住重点,容易答非所问。
      

  8.   

    兄弟,你Controller层的映射值是list,为什么访问要写list.action,这样不出404才怪
      

  9.   

    把list改成list.action
      

  10.   

    404请求没找到,把你请求映射后面加action
      

  11.   

    你的web.xml  名称为:springmvc-web  你试试:  http://localhost:8080/springmvc-web/list.action
      

  12.   

    最好在mvc中配置一下视图解析器