浏览代码

0518 整合mybatis

Qing 1 年之前
父节点
当前提交
d911cf93e5

+ 13 - 0
springmvc-demo/.idea/artifacts/springmvc_demo_Web_exploded.xml

@@ -0,0 +1,13 @@
+<component name="ArtifactManager">
+  <artifact type="exploded-war" name="springmvc-demo:Web exploded">
+    <output-path>$PROJECT_DIR$/out/artifacts/springmvc_demo_Web_exploded</output-path>
+    <root id="root">
+      <element id="javaee-facet-resources" facet="springmvc-demo/web/Web" />
+      <element id="directory" name="WEB-INF">
+        <element id="directory" name="classes">
+          <element id="module-output" name="springmvc-demo" />
+        </element>
+      </element>
+    </root>
+  </artifact>
+</component>

+ 1 - 0
springmvc-demo/.idea/misc.xml

@@ -1,5 +1,6 @@
 <?xml version="1.0" encoding="UTF-8"?>
 <project version="4">
+  <component name="ExternalStorageConfigurationManager" enabled="true" />
   <component name="MavenProjectsManager">
     <option name="originalFiles">
       <list>

+ 130 - 29
springmvc-demo/.idea/workspace.xml

@@ -10,34 +10,41 @@
     <option name="autoReloadType" value="SELECTIVE" />
   </component>
   <component name="ChangeListManager">
-    <list default="true" id="8a346ac7-ee3f-4cad-99c9-d339051f5d36" name="Changes" comment="">
+    <list default="true" id="8a346ac7-ee3f-4cad-99c9-d339051f5d36" name="Changes" comment="0515 springmvc">
       <change afterPath="$PROJECT_DIR$/../.idea/VIP31.iml" afterDir="false" />
       <change afterPath="$PROJECT_DIR$/../JavaSE/JavaSe01/JavaSe01.iml" afterDir="false" />
-      <change afterPath="$PROJECT_DIR$/.gitignore" afterDir="false" />
-      <change afterPath="$PROJECT_DIR$/.idea/encodings.xml" afterDir="false" />
-      <change afterPath="$PROJECT_DIR$/.idea/google-java-format.xml" afterDir="false" />
-      <change afterPath="$PROJECT_DIR$/.idea/inspectionProfiles/Project_Default.xml" afterDir="false" />
-      <change afterPath="$PROJECT_DIR$/.idea/misc.xml" afterDir="false" />
-      <change afterPath="$PROJECT_DIR$/.idea/uiDesigner.xml" afterDir="false" />
-      <change afterPath="$PROJECT_DIR$/.idea/vcs.xml" afterDir="false" />
-      <change afterPath="$PROJECT_DIR$/.idea/workspace.xml" afterDir="false" />
-      <change afterPath="$PROJECT_DIR$/pom.xml" afterDir="false" />
-      <change afterPath="$PROJECT_DIR$/src/main/java/com/sf/MyServletContainerInitializer.java" afterDir="false" />
-      <change afterPath="$PROJECT_DIR$/src/main/java/com/sf/MyWebAppInitializer.java" afterDir="false" />
-      <change afterPath="$PROJECT_DIR$/src/main/java/com/sf/SpringMvcConfig.java" afterDir="false" />
-      <change afterPath="$PROJECT_DIR$/src/main/java/com/sf/controller/BookController.java" afterDir="false" />
-      <change afterPath="$PROJECT_DIR$/src/main/java/com/sf/servlet/UserServlet.java" afterDir="false" />
-      <change afterPath="$PROJECT_DIR$/src/main/resources/spring-mvc.xml" afterDir="false" />
-      <change afterPath="$PROJECT_DIR$/src/main/webapp/WEB-INF/web.xml" afterDir="false" />
-      <change afterPath="$PROJECT_DIR$/src/main/webapp/hello.jsp" afterDir="false" />
-      <change afterPath="$PROJECT_DIR$/src/main/webapp/index.jsp" afterDir="false" />
+      <change afterPath="$PROJECT_DIR$/.idea/artifacts/springmvc_demo_Web_exploded.xml" afterDir="false" />
+      <change afterPath="$PROJECT_DIR$/src/main/java/com/sf/controller/RequestController.java" afterDir="false" />
+      <change afterPath="$PROJECT_DIR$/src/main/java/com/sf/dao/BookDao.java" afterDir="false" />
+      <change afterPath="$PROJECT_DIR$/src/main/java/com/sf/entity/Book.java" afterDir="false" />
+      <change afterPath="$PROJECT_DIR$/src/main/java/com/sf/service/BookService.java" afterDir="false" />
+      <change afterPath="$PROJECT_DIR$/src/main/java/com/sf/service/impl/BookServiceImpl.java" afterDir="false" />
+      <change afterPath="$PROJECT_DIR$/src/main/resources/db.properties" afterDir="false" />
+      <change afterPath="$PROJECT_DIR$/src/main/resources/mapper/BookMapper.xml" afterDir="false" />
+      <change afterPath="$PROJECT_DIR$/src/main/resources/mybatis-config.xml" afterDir="false" />
+      <change afterPath="$PROJECT_DIR$/src/main/resources/spring.xml" afterDir="false" />
+      <change afterPath="$PROJECT_DIR$/src/test/java/com/sf/TestMybatis.java" afterDir="false" />
+      <change afterPath="$PROJECT_DIR$/src/test/java/com/sf/TestSpringMvc.java" afterDir="false" />
       <change beforePath="$PROJECT_DIR$/../spring-demo/.idea/workspace.xml" beforeDir="false" afterPath="$PROJECT_DIR$/../spring-demo/.idea/workspace.xml" afterDir="false" />
+      <change beforePath="$PROJECT_DIR$/.idea/misc.xml" beforeDir="false" afterPath="$PROJECT_DIR$/.idea/misc.xml" afterDir="false" />
+      <change beforePath="$PROJECT_DIR$/.idea/workspace.xml" beforeDir="false" afterPath="$PROJECT_DIR$/.idea/workspace.xml" afterDir="false" />
+      <change beforePath="$PROJECT_DIR$/pom.xml" beforeDir="false" afterPath="$PROJECT_DIR$/pom.xml" afterDir="false" />
+      <change beforePath="$PROJECT_DIR$/src/main/java/com/sf/MyServletContainerInitializer.java" beforeDir="false" afterPath="$PROJECT_DIR$/src/main/java/com/sf/MyServletContainerInitializer.java" afterDir="false" />
+      <change beforePath="$PROJECT_DIR$/src/main/java/com/sf/controller/BookController.java" beforeDir="false" afterPath="$PROJECT_DIR$/src/main/java/com/sf/controller/BookController.java" afterDir="false" />
+      <change beforePath="$PROJECT_DIR$/src/main/resources/spring-mvc.xml" beforeDir="false" afterPath="$PROJECT_DIR$/src/main/resources/spring-mvc.xml" afterDir="false" />
+      <change beforePath="$PROJECT_DIR$/src/main/webapp/WEB-INF/web.xml" beforeDir="false" afterPath="$PROJECT_DIR$/src/main/webapp/WEB-INF/web.xml" afterDir="false" />
+      <change beforePath="$PROJECT_DIR$/src/main/webapp/hello.jsp" beforeDir="false" afterPath="$PROJECT_DIR$/src/main/webapp/hello.jsp" afterDir="false" />
     </list>
     <option name="SHOW_DIALOG" value="false" />
     <option name="HIGHLIGHT_CONFLICTS" value="true" />
     <option name="HIGHLIGHT_NON_ACTIVE_CHANGELIST" value="false" />
     <option name="LAST_RESOLUTION" value="IGNORE" />
   </component>
+  <component name="ChangesViewManager">
+    <option name="groupingKeys">
+      <option value="directory" />
+    </option>
+  </component>
   <component name="FileTemplateManagerImpl">
     <option name="RECENT_TEMPLATES">
       <list>
@@ -50,10 +57,10 @@
   <component name="Git.Settings">
     <option name="RECENT_GIT_ROOT_PATH" value="$PROJECT_DIR$/.." />
   </component>
-  <component name="KubernetesApiPersistence"><![CDATA[{}]]></component>
-  <component name="KubernetesApiProvider"><![CDATA[{
-  "isMigrated": true
-}]]></component>
+  <component name="KubernetesApiPersistence">{}</component>
+  <component name="KubernetesApiProvider">{
+  &quot;isMigrated&quot;: true
+}</component>
   <component name="LogFilters">
     <option name="FILTER_ERRORS" value="false" />
     <option name="FILTER_WARNINGS" value="false" />
@@ -61,6 +68,20 @@
     <option name="FILTER_DEBUG" value="true" />
     <option name="CUSTOM_FILTER" />
   </component>
+  <component name="MavenImportPreferences">
+    <option name="generalSettings">
+      <MavenGeneralSettings>
+        <option name="localRepository" value="$MAVEN_REPOSITORY$" />
+        <option name="useMavenConfig" value="false" />
+        <option name="userSettingsFile" value="$USER_HOME$/.m2/settings.xml" />
+      </MavenGeneralSettings>
+    </option>
+  </component>
+  <component name="ProjectColorInfo">{
+  &quot;customColor&quot;: &quot;&quot;,
+  &quot;associatedIndex&quot;: 6
+}</component>
+  <component name="ProjectId" id="2gcQAwWb8LN43APOkgvdE8X1inq" />
   <component name="ProjectLevelVcsManager">
     <ConfirmationsSetting value="2" id="Add" />
   </component>
@@ -69,6 +90,8 @@
   </component>
   <component name="PropertiesComponent"><![CDATA[{
   "keyToString": {
+    "JUnit.TestMybatis.testMybatis.executor": "Run",
+    "JUnit.TestSpringMvc.test.executor": "Run",
     "Maven. [org.apache.maven.plugins:maven-archetype-plugin:RELEASE:generate].executor": "Run",
     "RunOnceActivity.ShowReadmeOnStart": "true",
     "SHARE_PROJECT_CONFIGURATION_FILES": "true",
@@ -76,21 +99,70 @@
     "Tomcat Server.Tomcat 11.0.0-M16.executor": "Run",
     "git-widget-placeholder": "master",
     "kotlin-language-version-configured": "true",
+    "last_opened_file_path": "/Users/Qing/IdeaProjects/SiFu/VIP31/springmvc-demo",
     "node.js.detected.package.eslint": "true",
     "node.js.detected.package.tslint": "true",
     "node.js.selected.package.eslint": "(autodetect)",
     "node.js.selected.package.tslint": "(autodetect)",
     "nodejs_package_manager_path": "npm",
-    "project.structure.last.edited": "Artifacts",
-    "project.structure.proportion": "0.0",
-    "project.structure.side.proportion": "0.0",
+    "project.structure.last.edited": "Modules",
+    "project.structure.proportion": "0.1809181",
+    "project.structure.side.proportion": "0.2794899",
     "settings.editor.selected.configurable": "MavenSettings",
     "vue.rearranger.settings.migration": "true"
   }
 }]]></component>
-  <component name="RunManager">
+  <component name="RecentsManager">
+    <key name="CopyFile.RECENT_KEYS">
+      <recent name="$PROJECT_DIR$" />
+    </key>
+  </component>
+  <component name="RunManager" selected="Tomcat Server.Tomcat 10.0.21">
+    <configuration name="TestMybatis.testMybatis" type="JUnit" factoryName="JUnit" temporary="true" nameIsGenerated="true">
+      <module name="springmvc-demo" />
+      <extension name="coverage">
+        <pattern>
+          <option name="PATTERN" value="com.sf.*" />
+          <option name="ENABLED" value="true" />
+        </pattern>
+      </extension>
+      <option name="PACKAGE_NAME" value="com.sf" />
+      <option name="MAIN_CLASS_NAME" value="com.sf.TestMybatis" />
+      <option name="METHOD_NAME" value="testMybatis" />
+      <option name="TEST_OBJECT" value="method" />
+      <method v="2">
+        <option name="Make" enabled="true" />
+      </method>
+    </configuration>
+    <configuration name="TestSpringMvc.test" type="JUnit" factoryName="JUnit" temporary="true" nameIsGenerated="true">
+      <module name="springmvc-demo" />
+      <extension name="coverage">
+        <pattern>
+          <option name="PATTERN" value="com.sf.*" />
+          <option name="ENABLED" value="true" />
+        </pattern>
+      </extension>
+      <option name="PACKAGE_NAME" value="com.sf" />
+      <option name="MAIN_CLASS_NAME" value="com.sf.TestSpringMvc" />
+      <option name="METHOD_NAME" value="test" />
+      <option name="TEST_OBJECT" value="method" />
+      <method v="2">
+        <option name="Make" enabled="true" />
+      </method>
+    </configuration>
+    <configuration default="true" type="JetRunConfigurationType">
+      <method v="2">
+        <option name="Make" enabled="true" />
+      </method>
+    </configuration>
+    <configuration default="true" type="KotlinStandaloneScriptRunConfigurationType">
+      <option name="filePath" />
+      <method v="2">
+        <option name="Make" enabled="true" />
+      </method>
+    </configuration>
     <configuration name="Tomcat 10.0.21" type="#com.intellij.j2ee.web.tomcat.TomcatRunConfigurationFactory" factoryName="Local" APPLICATION_SERVER_NAME="Tomcat 10.0.21" ALTERNATIVE_JRE_ENABLED="false" nameIsGenerated="true">
-      <option name="OPEN_IN_BROWSER_URL" value="http://localhost:8080/springmvc_demo_war/hello" />
+      <option name="OPEN_IN_BROWSER_URL" value="http://localhost:8080/springmvc_demo/hello" />
       <option name="UPDATING_POLICY" value="restart-server" />
       <deployment>
         <artifact name="springmvc-demo:war">
@@ -183,6 +255,17 @@
         </option>
       </method>
     </configuration>
+    <list>
+      <item itemvalue="JUnit.TestMybatis.testMybatis" />
+      <item itemvalue="JUnit.TestSpringMvc.test" />
+      <item itemvalue="Tomcat Server.Tomcat 10.0.21" />
+    </list>
+    <recent_temporary>
+      <list>
+        <item itemvalue="JUnit.TestMybatis.testMybatis" />
+        <item itemvalue="JUnit.TestSpringMvc.test" />
+      </list>
+    </recent_temporary>
   </component>
   <component name="SharedIndexes">
     <attachedChunks>
@@ -200,11 +283,29 @@
       <option name="number" value="Default" />
       <option name="presentableId" value="Default" />
       <updated>1715752794105</updated>
-      <workItem from="1715752795429" duration="9220000" />
+      <workItem from="1715752795429" duration="9289000" />
+      <workItem from="1715994247914" duration="64000" />
+      <workItem from="1715994329537" duration="276000" />
+      <workItem from="1715994638690" duration="21000" />
+      <workItem from="1715994732370" duration="6964000" />
+      <workItem from="1716002556205" duration="2317000" />
+    </task>
+    <task id="LOCAL-00001" summary="0515 springmvc">
+      <option name="closed" value="true" />
+      <created>1715763981629</created>
+      <option name="number" value="00001" />
+      <option name="presentableId" value="LOCAL-00001" />
+      <option name="project" value="LOCAL" />
+      <updated>1715763981629</updated>
     </task>
+    <option name="localTasksCounter" value="2" />
     <servers />
   </component>
   <component name="TypeScriptGeneratedFilesManager">
     <option name="version" value="3" />
   </component>
+  <component name="VcsManagerConfiguration">
+    <MESSAGE value="0515 springmvc" />
+    <option name="LAST_COMMIT_MESSAGE" value="0515 springmvc" />
+  </component>
 </project>

+ 110 - 32
springmvc-demo/pom.xml

@@ -1,34 +1,112 @@
 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
-  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
-  <modelVersion>4.0.0</modelVersion>
-  <groupId>com.sf</groupId>
-  <artifactId>springmvc-demo</artifactId>
-  <packaging>war</packaging>
-  <version>1.0-SNAPSHOT</version>
-  <name>springmvc-demo Maven Webapp</name>
-  <url>http://maven.apache.org</url>
-
-  <dependencies>
-    <dependency>
-      <groupId>org.junit.jupiter</groupId>
-      <artifactId>junit-jupiter</artifactId>
-      <version>5.11.0-M1</version>
-    </dependency>
-
-    <dependency>
-      <groupId>org.springframework</groupId>
-      <artifactId>spring-webmvc</artifactId>
-      <version>6.1.6</version>
-    </dependency>
-
-    <dependency>
-      <groupId>jakarta.servlet</groupId>
-      <artifactId>jakarta.servlet-api</artifactId>
-      <version>6.1.0-M2</version>
-    </dependency>
-
-  </dependencies>
-  <build>
-    <finalName>springmvc-demo</finalName>
-  </build>
+         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
+    <modelVersion>4.0.0</modelVersion>
+    <groupId>com.sf</groupId>
+    <artifactId>springmvc-demo</artifactId>
+    <packaging>war</packaging>
+    <version>1.0-SNAPSHOT</version>
+    <name>springmvc-demo-new Maven Webapp</name>
+    <url>http://maven.apache.org</url>
+
+    <properties>
+        <spring.version>6.1.6</spring.version>
+<!--        <spring.version>6.1.5</spring.version>-->
+    </properties>
+
+    <dependencies>
+        <!-- 单元测试junit -->
+        <dependency>
+            <groupId>org.junit.jupiter</groupId>
+            <artifactId>junit-jupiter</artifactId>
+            <version>5.10.1</version>
+        </dependency>
+
+        <!-- servlet-api是web项目的基本依赖 -->
+        <dependency>
+            <groupId>jakarta.servlet</groupId>
+            <artifactId>jakarta.servlet-api</artifactId>
+            <version>6.1.0-M1</version>
+        </dependency>
+
+        <!-- springmvc -->
+        <dependency>
+            <groupId>org.springframework</groupId>
+            <artifactId>spring-webmvc</artifactId>
+            <version>${spring.version}</version>
+        </dependency>
+
+        <!-- 测试 -->
+        <dependency>
+            <groupId>org.springframework</groupId>
+            <artifactId>spring-test</artifactId>
+            <version>${spring.version}</version>
+        </dependency>
+
+        <!-- spring aop -->
+        <dependency>
+            <groupId>org.springframework</groupId>
+            <artifactId>spring-aspects</artifactId>
+            <version>${spring.version}</version>
+        </dependency>
+        <dependency>
+            <groupId>org.aspectj</groupId>
+            <artifactId>aspectjrt</artifactId>
+            <version>1.9.21.M1</version>
+        </dependency>
+
+        <!-- 数据库相关 -->
+        <dependency>
+            <groupId>mysql</groupId>
+            <artifactId>mysql-connector-java</artifactId>
+            <version>8.0.33</version>
+        </dependency>
+        <dependency>
+            <groupId>com.alibaba</groupId>
+            <artifactId>druid</artifactId>
+            <version>1.2.20</version>
+        </dependency>
+
+        <dependency>
+            <groupId>org.projectlombok</groupId>
+            <artifactId>lombok</artifactId>
+            <version>1.18.30</version>
+        </dependency>
+
+        <!-- spring整合jdbc -->
+        <dependency>
+            <groupId>org.springframework</groupId>
+            <artifactId>spring-jdbc</artifactId>
+            <version>${spring.version}</version>
+        </dependency>
+
+        <!-- spring整合事务 -->
+        <dependency>
+            <groupId>org.springframework</groupId>
+            <artifactId>spring-tx</artifactId>
+            <version>${spring.version}</version>
+        </dependency>
+
+        <!-- spring整合mybatis -->
+        <dependency>
+            <groupId>org.springframework</groupId>
+            <artifactId>spring-orm</artifactId>
+            <version>${spring.version}</version>
+        </dependency>
+
+        <dependency>
+            <groupId>org.mybatis</groupId>
+            <artifactId>mybatis</artifactId>
+            <version>3.5.14</version>
+        </dependency>
+
+        <dependency>
+            <groupId>org.mybatis</groupId>
+            <artifactId>mybatis-spring</artifactId>
+            <version>3.0.3</version>
+        </dependency>
+
+    </dependencies>
+    <build>
+        <finalName>springmvc-demo-new</finalName>
+    </build>
 </project>

+ 20 - 20
springmvc-demo/src/main/java/com/sf/MyServletContainerInitializer.java

@@ -6,23 +6,23 @@ import org.springframework.web.servlet.support.AbstractDispatcherServletInitiali
 
 // 想替换web.xml时
 // AbstractDispatcherServletInitializer 抽象的初始化器
-public class MyServletContainerInitializer extends AbstractDispatcherServletInitializer {
-    // 初始化容器
-    @Override
-    protected WebApplicationContext createServletApplicationContext() {
-        AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext();
-        context.register(SpringMvcConfig.class);
-        return context;
-    }
-
-    // 配置映射关系
-    @Override
-    protected String[] getServletMappings() {
-        return new String[]{"/"};
-    }
-
-    @Override
-    protected WebApplicationContext createRootApplicationContext() {
-        return null;
-    }
-}
+//public class MyServletContainerInitializer extends AbstractDispatcherServletInitializer {
+//    // 初始化容器
+//    @Override
+//    protected WebApplicationContext createServletApplicationContext() {
+//        AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext();
+//        context.register(SpringMvcConfig.class);
+//        return context;
+//    }
+//
+//    // 配置映射关系
+//    @Override
+//    protected String[] getServletMappings() {
+//        return new String[]{"/"};
+//    }
+//
+//    @Override
+//    protected WebApplicationContext createRootApplicationContext() {
+//        return null;
+//    }
+//}

+ 32 - 7
springmvc-demo/src/main/java/com/sf/controller/BookController.java

@@ -1,23 +1,48 @@
 package com.sf.controller;
 
+import com.sf.entity.Book;
+import com.sf.service.BookService;
+import lombok.experimental.StandardException;
+import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Controller;
-import org.springframework.web.bind.annotation.RequestMapping;
+import org.springframework.web.bind.annotation.*;
 import org.springframework.web.servlet.ModelAndView;
 
+import java.util.List;
+
 // 控制器
 @Controller
+@RequestMapping("/book")
 public class BookController {
 
+    @Autowired
+    private BookService bookService;
+
     // 使用ModelAndView 来处理请求和响应中的 数据模型和视图
-    // @RequestMapping 意思是请求映射
-    // http://localhost:8080/springmvc_demo_war/hello
-    @RequestMapping("/hello")
+    // @RequestMapping 意思是请求映射  是把http请求映射到当前方法上
+    // 虽然默认的请求是get 但不局限于请求方式 比如post
+    // http://localhost:8080/springmvc_demo_war/book/hello
+//    @RequestMapping("/hello")
+    // @RequestMapping(method = RequestMethod.GET)
+    @GetMapping("/hello")
+//    @RequestMapping(value = "/hello",method = RequestMethod.GET)
     public ModelAndView hello() {
         ModelAndView modelAndView = new ModelAndView();
-        //  req.setAttribute("name","Spring MVC From Servlet");
-        modelAndView.addObject("name","Spring MVC From Controller");
-        //  req.getRequestDispatcher("/hello.jsp").forward(req,resp);
+        List<Book> bookList = bookService.findAll();
+        modelAndView.addObject("data", bookList.toString());
         modelAndView.setViewName("/hello.jsp");
         return modelAndView;
     }
+
+    @PostMapping("/other")
+//    @RequestMapping(value = "/hello",method = RequestMethod.POST)
+//    @PutMapping
+//    @RequestMapping(value = "/other",method = RequestMethod.PUT)
+//    @DeleteMapping
+//    @RequestMapping(value = "/other",method = RequestMethod.DELETE)
+    public ModelAndView helloPost() {
+        ModelAndView modelAndView = new ModelAndView();
+        return modelAndView;
+    }
+    // http://localhost:8080/springmvc_demo_war/book/other
 }

+ 58 - 0
springmvc-demo/src/main/java/com/sf/controller/RequestController.java

@@ -0,0 +1,58 @@
+package com.sf.controller;
+
+import org.springframework.stereotype.Controller;
+import org.springframework.web.bind.annotation.GetMapping;
+import org.springframework.web.bind.annotation.RequestMapping;
+import org.springframework.web.bind.annotation.RequestMethod;
+import org.springframework.web.bind.annotation.RequestParam;
+
+@Controller
+public class RequestController {
+
+    //    @RequestMapping(value = "/request",method = RequestMethod.GET)
+    @GetMapping("/request")
+    public String get() {
+        return "hello";
+    }
+
+    @RequestMapping(value = "/request", method = RequestMethod.POST)
+    public String post() {
+        return "hello";
+    }
+
+    @RequestMapping(value = "/request", method = RequestMethod.PUT)
+    public String put() {
+        return "hello";
+    }
+
+    @RequestMapping(value = "/request", method = RequestMethod.DELETE)
+    public String delete() {
+        return "hello";
+    }
+
+    // 支持ant风格的路径
+    // ? 代表任意单个字符
+    // * 代表任意多个字符
+    // ** 代表任意层数的任意目录
+    @RequestMapping(value = {"/requestAll", "/requestOther", "/requestSth","/request/*"})
+    public String all() {
+        return "hello";
+    }
+
+    @RequestMapping(value = "/requestHeader",headers = "content-type=text/plain")
+    public String header(){
+        return "hello";
+    }
+
+
+    // params 限定请求的参数
+    // http://localhost:8080/requestParams?id=111
+    // http://localhost:8080/requestParams?id=222
+    // http://localhost:8080/requestParams?id=333
+    @RequestMapping(value = "/requestParams",params = "id=111")
+    public String params(@RequestParam("id") String id){
+        return "hello";
+    }
+
+}
+

+ 9 - 0
springmvc-demo/src/main/java/com/sf/dao/BookDao.java

@@ -0,0 +1,9 @@
+package com.sf.dao;
+
+import com.sf.entity.Book;
+
+import java.util.List;
+
+public interface BookDao {
+    List<Book> findAll();
+}

+ 34 - 0
springmvc-demo/src/main/java/com/sf/entity/Book.java

@@ -0,0 +1,34 @@
+package com.sf.entity;
+
+import lombok.AllArgsConstructor;
+import lombok.Builder;
+import lombok.Data;
+import lombok.NoArgsConstructor;
+
+import java.io.Serializable;
+
+@Data
+@NoArgsConstructor
+@AllArgsConstructor
+@Builder
+public class Book implements Serializable {
+
+    private static final long serialVersionUID = 5047814726963775513L;
+
+    private Long id;
+
+    private String bookId;
+
+    private String bookName;
+
+    private String typeId;
+
+    private String authorId;
+
+    private String authorName;
+
+    private String bookImg;
+
+    private String bookDesc;
+
+}

+ 10 - 0
springmvc-demo/src/main/java/com/sf/service/BookService.java

@@ -0,0 +1,10 @@
+package com.sf.service;
+
+import com.sf.entity.Book;
+
+import java.util.List;
+
+public interface BookService {
+
+    List<Book> findAll();
+}

+ 21 - 0
springmvc-demo/src/main/java/com/sf/service/impl/BookServiceImpl.java

@@ -0,0 +1,21 @@
+package com.sf.service.impl;
+
+import com.sf.dao.BookDao;
+import com.sf.entity.Book;
+import com.sf.service.BookService;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.stereotype.Service;
+
+import java.util.List;
+
+@Service
+public class BookServiceImpl implements BookService {
+
+    @Autowired
+    private BookDao bookDao;
+
+    @Override
+    public List<Book> findAll() {
+        return bookDao.findAll();
+    }
+}

+ 4 - 0
springmvc-demo/src/main/resources/db.properties

@@ -0,0 +1,4 @@
+jdbc.url=jdbc:mysql://localhost:3306/novels?useUnicode=true&characterEncoding=utf-8&allowPublicKeyRetrieval=true&useSSL=false&serverTimezone=Asia/Shanghai
+jdbc.driverName=com.mysql.cj.jdbc.Driver
+jdbc.username=root
+jdbc.password=root123456

+ 7 - 0
springmvc-demo/src/main/resources/mapper/BookMapper.xml

@@ -0,0 +1,7 @@
+<?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.sf.dao.BookDao">
+    <select id="findAll" resultType="com.sf.entity.Book">
+        select * from book;
+    </select>
+</mapper>

+ 12 - 0
springmvc-demo/src/main/resources/mybatis-config.xml

@@ -0,0 +1,12 @@
+<?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>
+    <settings>
+        <!-- 使用mybatis的自动驼峰转换-->
+        <setting name="mapUnderscoreToCamelCase" value="true"/>
+    </settings>
+    <mappers>
+<!--        <mapper resource="mapper/BookMapper.xml"></mapper>-->
+    </mappers>
+</configuration>

+ 8 - 1
springmvc-demo/src/main/resources/spring-mvc.xml

@@ -2,8 +2,15 @@
 <beans xmlns="http://www.springframework.org/schema/beans"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xmlns:context="http://www.springframework.org/schema/context"
+       xmlns:mvc="http://www.springframework.org/schema/mvc"
        xsi:schemaLocation="http://www.springframework.org/schema/beans
-        https://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd">
+        https://www.springframework.org/schema/beans/spring-beans.xsd
+        http://www.springframework.org/schema/context
+        https://www.springframework.org/schema/context/spring-context.xsd
+        http://www.springframework.org/schema/mvc
+        https://www.springframework.org/schema/mvc/spring-mvc.xsd">
 
     <context:component-scan base-package="com.sf.controller"/>
+    <!-- 开启注解-->
+    <mvc:annotation-driven></mvc:annotation-driven>
 </beans>

+ 35 - 0
springmvc-demo/src/main/resources/spring.xml

@@ -0,0 +1,35 @@
+<?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:context="http://www.springframework.org/schema/context"
+       xsi:schemaLocation="http://www.springframework.org/schema/beans
+        https://www.springframework.org/schema/beans/spring-beans.xsd
+        http://www.springframework.org/schema/context
+        https://www.springframework.org/schema/context/spring-context.xsd">
+
+    <!-- 加载properties文件 -->
+    <context:property-placeholder location="classpath:db.properties"/>
+
+    <bean id="datasource" class="com.alibaba.druid.pool.DruidDataSource">
+        <property name="url" value="${jdbc.url}"></property>
+        <property name="username" value="${jdbc.username}"></property>
+        <property name="password" value="${jdbc.password}"></property>
+        <property name="driverClassName" value="${jdbc.driverName}"></property>
+    </bean>
+
+    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
+        <property name="dataSource" ref="datasource"></property>
+        <property name="configLocation" value="classpath:mybatis-config.xml"></property>
+        <property name="mapperLocations" value="classpath*:mapper/**/*.xml"/>
+    </bean>
+
+    <!-- 配置mapper扫描 -->
+    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
+        <property name="basePackage" value="com.sf.dao"></property>
+        <!-- 这里配置上述的 sqlSessionFactoryBeanName 注意要使用的是value 而不是ref-->
+        <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property>
+    </bean>
+
+    <context:component-scan base-package="com.sf.service"></context:component-scan>
+
+</beans>

+ 49 - 25
springmvc-demo/src/main/webapp/WEB-INF/web.xml

@@ -1,29 +1,53 @@
 <!DOCTYPE web-app PUBLIC
- "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
- "http://java.sun.com/dtd/web-app_2_3.dtd" >
+        "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
+        "http://java.sun.com/dtd/web-app_2_3.dtd" >
 
-<web-app></web-app>
-<!--<web-app xmlns="https://jakarta.ee/xml/ns/jakartaee"-->
-<!--         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"-->
-<!--         xsi:schemaLocation="https://jakarta.ee/xml/ns/jakartaee-->
-<!--                      https://jakarta.ee/xml/ns/jakartaee/web-app_5_0.xsd"-->
-<!--         version="5.0">-->
+<!--<web-app>-->
+<!--</web-app>-->
+<web-app xmlns="https://jakarta.ee/xml/ns/jakartaee"
+         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+         xsi:schemaLocation="https://jakarta.ee/xml/ns/jakartaee
+                      https://jakarta.ee/xml/ns/jakartaee/web-app_5_0.xsd"
+         version="5.0">
 
-<!--    <servlet>-->
-<!--        <servlet-name>dispatcherServlet</servlet-name>-->
-<!--        &lt;!&ndash; 配置来自springmvc的一个servlet &ndash;&gt;-->
-<!--        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>-->
-<!--        <init-param>-->
-<!--            <param-name>contextConfigLocation</param-name>-->
-<!--            <param-value>classpath:spring-mvc.xml</param-value>-->
-<!--        </init-param>-->
-<!--        &lt;!&ndash; tomcat启动时就创建对象&ndash;&gt;-->
-<!--        <load-on-startup>1</load-on-startup>-->
-<!--    </servlet>-->
+    <!-- 上下文参数和监听器 -->
+    <context-param>
+        <param-name>contextConfigLocation</param-name>
+        <param-value>classpath:spring.xml</param-value>
+    </context-param>
+    <listener>
+        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
+    </listener>
 
-<!--    &lt;!&ndash; 配置映射关系  /意思是拦截所有请求&ndash;&gt;-->
-<!--    <servlet-mapping>-->
-<!--        <servlet-name>dispatcherServlet</servlet-name>-->
-<!--        <url-pattern>/</url-pattern>-->
-<!--    </servlet-mapping>-->
-<!--</web-app>-->
+    <servlet>
+        <servlet-name>dispatcherServlet</servlet-name>
+        <!-- 配置来自springmvc的一个servlet -->
+        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
+        <init-param>
+            <param-name>contextConfigLocation</param-name>
+            <param-value>classpath:spring-mvc.xml</param-value>
+        </init-param>
+        <!-- tomcat启动时就创建对象-->
+        <load-on-startup>1</load-on-startup>
+    </servlet>
+
+    <!-- 配置映射关系  /意思是拦截所有请求-->
+    <servlet-mapping>
+        <servlet-name>dispatcherServlet</servlet-name>
+        <url-pattern>/</url-pattern>
+    </servlet-mapping>
+
+    <!-- 过滤器 -->
+    <filter>
+        <filter-name>characterEncodingFilter</filter-name>
+        <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
+        <init-param>
+            <param-name>encoding</param-name>
+            <param-value>UTF-8</param-value>
+        </init-param>
+    </filter>
+    <filter-mapping>
+        <filter-name>characterEncodingFilter</filter-name>
+        <url-pattern>/*</url-pattern>
+    </filter-mapping>
+</web-app>

+ 1 - 1
springmvc-demo/src/main/webapp/hello.jsp

@@ -18,6 +18,6 @@ To change this template use File | Settings | File Templates.
     <title>Title</title>
 </head>
 <body>
-<h2>你好,${name}</h2>
+<h2>${data}</h2>
 </body>
 </html>

+ 25 - 0
springmvc-demo/src/test/java/com/sf/TestMybatis.java

@@ -0,0 +1,25 @@
+package com.sf;
+
+import com.sf.entity.Book;
+import org.apache.ibatis.session.SqlSession;
+import org.apache.ibatis.session.SqlSessionFactory;
+import org.junit.jupiter.api.Test;
+import org.mybatis.spring.SqlSessionFactoryBean;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.test.context.junit.jupiter.SpringJUnitConfig;
+
+import java.util.List;
+
+@SpringJUnitConfig(locations = {"classpath:spring.xml"})
+public class TestMybatis {
+
+    @Autowired
+    private SqlSessionFactoryBean sqlSessionFactoryBean;
+
+    @Test
+    public void testMybatis() throws Exception {
+        SqlSession sqlSession = sqlSessionFactoryBean.getObject().openSession();
+        List<Book> list = sqlSession.selectList("com.sf.dao.BookDao.findAll");
+        System.out.println(list);
+    }
+}

+ 22 - 0
springmvc-demo/src/test/java/com/sf/TestSpringMvc.java

@@ -0,0 +1,22 @@
+package com.sf;
+
+import com.sf.controller.BookController;
+import com.sf.service.BookService;
+import org.junit.jupiter.api.Test;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.test.context.junit.jupiter.web.SpringJUnitWebConfig;
+
+@SpringJUnitWebConfig(locations = {"classpath:spring.xml","classpath:spring-mvc.xml"})
+public class TestSpringMvc {
+
+    @Autowired
+    private BookService bookService;
+
+    @Autowired
+    private BookController bookController;
+
+    @Test
+    public void test(){
+        System.out.println(bookController.hello());
+    }
+}