樓主最近愛上了一個新框架——Spring Boot, 搭建快還不用寫一堆xml,最重要的是自帶Tomcat 真是好.
pom.xml
1 <?xml version="1.0" encoding="UTF-8"?>
 2 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 3     xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
 4     <modelVersion>4.0.0</modelVersion>
 5 
 6     <groupId>com.demo</groupId>
 7     <artifactId>Demo</artifactId>
 8     <version>0.0.1-SNAPSHOT</version>
 9     <packaging>jar</packaging>
10 
11     <name>Demo</name>
12     <description>Demo project for Spring Boot</description>
13 
14     <parent>
15         <groupId>org.springframework.boot</groupId>
16         <artifactId>spring-boot-starter-parent</artifactId>
17         <version>1.5.9.RELEASE</version>
18         <relativePath /> <!-- lookup parent from repository -->
19     </parent>
20 
21     <properties>
22         <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
23         <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
24         <java.version>1.8</java.version>
25     </properties>
26 
27     <dependencies>
28         
29         <dependency>
30             <groupId>org.mybatis.spring.boot</groupId>
31             <artifactId>mybatis-spring-boot-starter</artifactId>
32             <version>1.3.1</version>
33         </dependency>
34         
35         <dependency>
36             <groupId>org.springframework.boot</groupId>
37             <artifactId>spring-boot-starter-web</artifactId>
38         </dependency>
39 
40         <dependency>
41             <groupId>org.springframework.boot</groupId>
42             <artifactId>spring-boot-starter-jdbc</artifactId>
43             <exclusions>
44                 <exclusion>
45                     <groupId>org.apache.tomcat</groupId>
46                     <artifactId>tomcat-jdbc</artifactId>
47                 </exclusion>
48             </exclusions>
49         </dependency>
50 
51         <dependency>
52             <groupId>org.springframework.boot</groupId>
53             <artifactId>spring-boot-starter-test</artifactId>
54             <scope>test</scope>
55         </dependency>
56         
57     </dependencies>
58 
59     <build>
60         <plugins>
61             <plugin>
62                 <groupId>org.springframework.boot</groupId>
63                 <artifactId>spring-boot-maven-plugin</artifactId>
64             </plugin>
65         </plugins>
66     </build>
67 
68 </project>
controller.java
 1 package com.demo.controller;
 2 
 3 import org.springframework.beans.factory.annotation.Autowired;
 4 import org.springframework.stereotype.Controller;
 5 import org.springframework.web.bind.annotation.RequestMapping;
 6 import org.springframework.web.bind.annotation.ResponseBody;
 7 
 8 import com.demo.domain.User;
 9 import com.demo.service.UserService;
10 
11 @Controller
12 @RequestMapping(value = "/uc")
13 public class UserController {
14     
15     @Autowired
16     private UserService userService;
17 
18     @ResponseBody
19     @RequestMapping("/stemp.htm")
20     private String sYeMian(User user){
21         System.out.println("進了方法 syso");
22         
23         User u = new User();
24         u.setMuBan(user.getMuBan());
25         
26         int i = userService.sYeMian(u);
27         
28         if (i>0){
29             return "儲存成功";
30         }
31             return "儲存失敗";
32     }
33 }
DemoApplication.java
 1 package com.demo.example.demo;
 2 
 3 import org.springframework.boot.SpringApplication;
 4 import org.springframework.boot.autoconfigure.SpringBootApplication;
 5 
 6 @SpringBootApplication
 7 public class Demo1Application {
 8 
 9     public static void main(String[] args) {
10         SpringApplication.run(Demo1Application.class, args);
11     }
12 }
然而執行之後就懵逼了
這是咋了,咋的就404了 我路徑也挺對的啊 註解也都寫上了啊 咋就找不到了呢? debug吧它不進方法 看日誌吧,他還不報錯 這傢伙給我急的 百度一下午也沒解決,最後還是看官網才知道錯在了那裡,程式只加載Application.java所在包及其子包下的內容。
解決方案:
一、在Application類中加上@ComponentScan(basePackages = {"com.demo.controller"}) 多個之間用","分隔 當然,這樣治標不治本
 1 package com.demo.example.demo;
 2 
 3 import org.springframework.boot.SpringApplication;
 4 import org.springframework.boot.autoconfigure.SpringBootApplication;
 5 import org.springframework.context.annotation.ComponentScan;
 6 
 7 @SpringBootApplication
 8 @ComponentScan(basePackages = {"com.demo.controller"})
 9 public class Demo1Application {
10 
11     public static void main(String[] args) {
12         SpringApplication.run(Demo1Application.class, args);
13     }
14 }
二、把包的目錄結構修改成正確的
 1 package com.demo;
 2 
 3 import org.springframework.boot.SpringApplication;
 4 import org.springframework.boot.autoconfigure.SpringBootApplication;
 5 import org.springframework.context.annotation.ComponentScan;
 6 
 7 @SpringBootApplication
 8 public class Demo1Application {
 9 
10     public static void main(String[] args) {
11         SpringApplication.run(Demo1Application.class, args);
12     }
13 }