自从学习了springboot和springcloud之后,一直都没有再用ssm。

今天,我决定复习一下ssm搭建,所以做了一个小项目,只有简简单单的查询数据功能,万万没想到,居然给我报了个404。

 

先上代码:以下代码中的注解,有一部分是我个人理解,可能有错误!

applicationContext.xml

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xmlns:p="http://www.springframework.org/schema/p"
  5. xmlns:aop="http://www.springframework.org/schema/aop"
  6. xmlns:context="http://www.springframework.org/schema/context"
  7. xmlns:jee="http://www.springframework.org/schema/jee"
  8. xmlns:tx="http://www.springframework.org/schema/tx"
  9. xsi:schemaLocation="
  10. http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
  11. http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
  12. http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
  13. http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-4.0.xsd
  14. http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd">
  15. <context:component-scan base-package="com.learn.ssm">
  16. <!-- context:component-scan对路径下的文件进行扫描,如果扫描到文件中带有@Service,@Component,@Repository,@Controller等这些注解的类,则把这些类注册为bean
  17. -->
  18. </context:component-scan>
  19. <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
  20. <property name="url" value="jdbc:mysql://127.0.0.1:3306/yxktv" />
  21. <property name="driverClassName" value="com.mysql.jdbc.Driver" />
  22. <property name="username" value="root" />
  23. <property name="password" value="x5" />
  24. </bean>
  25. <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
  26. <property name="dataSource" ref="dataSource" />
  27. </bean>
  28. <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
  29. <property name="basePackage" value="com.learn.ssm.mapper" />
  30. <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property>
  31. </bean>
  32. </beans>

springmvc.xml

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xmlns:p="http://www.springframework.org/schema/p"
  5. xmlns:aop="http://www.springframework.org/schema/aop"
  6. xmlns:context="http://www.springframework.org/schema/context"
  7. xmlns:jee="http://www.springframework.org/schema/jee"
  8. xmlns:tx="http://www.springframework.org/schema/tx"
  9. xmlns:mvc="http://www.springframework.org/schema/mvc"
  10. xsi:schemaLocation="
  11. http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
  12. http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
  13. http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
  14. http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
  15. http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-4.0.xsd
  16. http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd">
  17.  
  18. <mvc:annotation-driven />
  19.  
  20. <!-- 静态资源配置 -->
  21. <mvc:resources mapping="/static/**" location="/static/" />
  22.  
  23. <!-- 视图解析器 -->
  24. <bean id="viewResolver"
  25. class="org.springframework.web.servlet.view.InternalResourceViewResolver">
  26. <property name="prefix" value="/" />
  27. <property name="suffix" value=".jsp"></property>
  28. </bean>
  29.  
  30. <!-- 使用注解的包,包括子集 -->
  31. <context:component-scan
  32. base-package="com.learn.ssm.controller" />
  33.  
  34.  
  35. </beans>

web.xml

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <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_3_0.xsd" id="WebApp_ID" version="3.0">
  3. <display-name>ssm</display-name>
  4. <context-param><!-- 初始化上下文参数 -->
  5. <param-name>contextConfigLocation</param-name>
  6. <param-value>classpath:applicationContext.xml</param-value>
  7. </context-param>
  8. <listener><!-- 监听器 用于监听种种操作,也可以自动激发一些操作 -->
  9. <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  10. </listener>
  11. <servlet>
  12. <servlet-name>SpringMVC</servlet-name>
  13. <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
  14. <init-param>
  15. <param-name>contextConfigLocation</param-name>
  16. <param-value>classpath:springmvc.xml</param-value>
  17. </init-param>
  18. <load-on-startup>1</load-on-startup>
  19. <!-- load-on-startup 当值为0或者大于0时,表示容器在应用启动时就加载这个servlet;
  20. 当是一个负数时或者没有指定时,则指示容器在该servlet被选择时才加载。
  21. 正数的值越小,启动该servlet的优先级越高。
  22. -->
  23. </servlet>
  24. <servlet-mapping>
  25. <servlet-name>SpringMVC</servlet-name>
  26. <url-pattern>*.do</url-pattern>
  27. <!-- 如果url-pattern定义的是路径,那么以后所有对这个路径下资源的请求都会由servlet-name中定义的servlet处理.
  28. 如果url-pattern定义的是资源格式例如*.do等,那么对于所有符合这种格式的资源的请求都由指定的servlet处理。
  29. -->
  30. </servlet-mapping>
  31. <welcome-file-list>
  32. <welcome-file>index.html</welcome-file>
  33. </welcome-file-list>
  34. </web-app>

SsmController.java

package com.learn.ssm.controller;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.learn.ssm.pojo.stu;
import com.learn.ssm.service.SsmService;
@RestController
@CrossOrigin
public class SsmController {
 @Autowired
 SsmService ssmService;
 @GetMapping(“/show”)
 public List<stu> show(){
  return ssmService.show();
 }
}

SsmMapper.xml

package com.learn.ssm.mapper;
import java.util.List;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import com.learn.ssm.pojo.stu;
@Mapper
public interface SsmMapper {
 @Select(“select * from stu”)
 public List<stu> show();
}

SsmService.xml

  1. package com.learn.ssm.service;
  2. import java.util.List;
  3. import org.springframework.beans.factory.annotation.Autowired;
  4. import org.springframework.stereotype.Service;
  5. import com.learn.ssm.mapper.SsmMapper;
  6. import com.learn.ssm.pojo.stu;
  7. @Service
  8. public class SsmService {
  9. @Autowired
  10. SsmMapper ssmMapper;
  11. public List<stu> show(){
  12. return ssmMapper.show();
  13. }
  14. }

stu.java

  1. package com.learn.ssm.pojo;
  2. public class stu {
  3. String name;
  4. String sex;
  5. int id;
  6. int age;
  7. int date;
  8. int classs;
  9. int tall;
  10. int num;
  11. public String getName() {
  12. return name;
  13. }
  14. public void setName(String name) {
  15. this.name = name;
  16. }
  17. public String getSex() {
  18. return sex;
  19. }
  20. public void setSex(String sex) {
  21. this.sex = sex;
  22. }
  23. public int getId() {
  24. return id;
  25. }
  26. public void setId(int id) {
  27. this.id = id;
  28. }
  29. public int getAge() {
  30. return age;
  31. }
  32. public void setAge(int age) {
  33. this.age = age;
  34. }
  35. public int getDate() {
  36. return date;
  37. }
  38. public void setDate(int date) {
  39. this.date = date;
  40. }
  41. public int getClasss() {
  42. return classs;
  43. }
  44. public void setClasss(int classs) {
  45. this.classs = classs;
  46. }
  47. public int getTall() {
  48. return tall;
  49. }
  50. public void setTall(int tall) {
  51. this.tall = tall;
  52. }
  53. public int getNum() {
  54. return num;
  55. }
  56. public void setNum(int num) {
  57. this.num = num;
  58. }
  59. }

 

 当我在浏览器中输入http://localhost:8080/show.do时,报404,原因是我这个地址写错了,应该是http://localhost:8080/ssm20190627/show.do。

被自己蠢哭了… …

版权声明:本文为wxdmw原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://www.cnblogs.com/wxdmw/p/11099558.html