• springMVC入门


    目录

    一、实现浏览器展示hello world

    1.1 添加pom依赖

    1.2 添加springmvc-servlet.xml 

     1.3 web.xml

    1.4 配置Tomcat

    1.5 代码 HelloController 

     二、springMVC的工作原理

     三、crud之常用注释以及返回值

    常用注解

     返回值得处理

     静态资源

    springMVC Spring Web MVC是一种基于Java的实现了MVC设计模式的、请求驱动类型的、轻量级Web框架。

    一、实现浏览器展示hello world

    1.1 添加pom依赖

    
        org.springframework
        spring-webmvc
        ${spring.version}
    

    pom.xml

    1. "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. <parent>
    5. <artifactId>ideamavenartifactId>
    6. <groupId>org.examplegroupId>
    7. <version>1.0-SNAPSHOTversion>
    8. parent>
    9. <modelVersion>4.0.0modelVersion>
    10. <artifactId>ssmartifactId>
    11. <packaging>warpackaging>
    12. <name>ssm Maven Webappname>
    13. <url>http://www.example.comurl>
    14. <properties>
    15. <project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
    16. <maven.compiler.source>1.8maven.compiler.source>
    17. <maven.compiler.target>1.8maven.compiler.target>
    18. <maven.compiler.plugin.version>3.7.0maven.compiler.plugin.version>
    19. <spring.version>5.0.2.RELEASEspring.version>
    20. <mybatis.version>3.4.5mybatis.version>
    21. <mysql.version>5.1.44mysql.version>
    22. <pagehelper.version>5.1.2pagehelper.version>
    23. <mybatis.spring.version>1.3.1mybatis.spring.version>
    24. <commons.dbcp2.version>2.1.1commons.dbcp2.version>
    25. <commons.pool2.version>2.4.3commons.pool2.version>
    26. <log4j2.version>2.9.1log4j2.version>
    27. <servlet.version>4.0.0servlet.version>
    28. <lombok.version>1.18.2lombok.version>
    29. properties>
    30. <dependencies>
    31. <dependency>
    32. <groupId>junitgroupId>
    33. <artifactId>junitartifactId>
    34. <version>4.12version>
    35. dependency>
    36. <dependency>
    37. <groupId>org.springframeworkgroupId>
    38. <artifactId>spring-contextartifactId>
    39. <version>${spring.version}version>
    40. dependency>
    41. <dependency>
    42. <groupId>org.springframeworkgroupId>
    43. <artifactId>spring-ormartifactId>
    44. <version>${spring.version}version>
    45. dependency>
    46. <dependency>
    47. <groupId>org.springframeworkgroupId>
    48. <artifactId>spring-txartifactId>
    49. <version>${spring.version}version>
    50. dependency>
    51. <dependency>
    52. <groupId>org.springframeworkgroupId>
    53. <artifactId>spring-aspectsartifactId>
    54. <version>${spring.version}version>
    55. dependency>
    56. <dependency>
    57. <groupId>org.springframeworkgroupId>
    58. <artifactId>spring-webartifactId>
    59. <version>${spring.version}version>
    60. dependency>
    61. <dependency>
    62. <groupId>org.springframeworkgroupId>
    63. <artifactId>spring-testartifactId>
    64. <version>${spring.version}version>
    65. dependency>
    66. <dependency>
    67. <groupId>org.mybatisgroupId>
    68. <artifactId>mybatisartifactId>
    69. <version>${mybatis.version}version>
    70. dependency>
    71. <dependency>
    72. <groupId>mysqlgroupId>
    73. <artifactId>mysql-connector-javaartifactId>
    74. <version>${mysql.version}version>
    75. dependency>
    76. <dependency>
    77. <groupId>com.github.pagehelpergroupId>
    78. <artifactId>pagehelperartifactId>
    79. <version>${pagehelper.version}version>
    80. dependency>
    81. <dependency>
    82. <groupId>org.mybatisgroupId>
    83. <artifactId>mybatis-springartifactId>
    84. <version>${mybatis.spring.version}version>
    85. dependency>
    86. <dependency>
    87. <groupId>org.apache.commonsgroupId>
    88. <artifactId>commons-dbcp2artifactId>
    89. <version>${commons.dbcp2.version}version>
    90. dependency>
    91. <dependency>
    92. <groupId>org.apache.commonsgroupId>
    93. <artifactId>commons-pool2artifactId>
    94. <version>${commons.pool2.version}version>
    95. dependency>
    96. <dependency>
    97. <groupId>org.apache.logging.log4jgroupId>
    98. <artifactId>log4j-coreartifactId>
    99. <version>${log4j2.version}version>
    100. dependency>
    101. <dependency>
    102. <groupId>org.apache.logging.log4jgroupId>
    103. <artifactId>log4j-apiartifactId>
    104. <version>${log4j2.version}version>
    105. dependency>
    106. <dependency>
    107. <groupId>org.apache.logging.log4jgroupId>
    108. <artifactId>log4j-webartifactId>
    109. <version>${log4j2.version}version>
    110. dependency>
    111. <dependency>
    112. <groupId>javax.servletgroupId>
    113. <artifactId>javax.servlet-apiartifactId>
    114. <version>${servlet.version}version>
    115. <scope>providedscope>
    116. dependency>
    117. <dependency>
    118. <groupId>org.projectlombokgroupId>
    119. <artifactId>lombokartifactId>
    120. <version>${lombok.version}version>
    121. <scope>providedscope>
    122. dependency>
    123. <dependency>
    124. <groupId>org.springframeworkgroupId>
    125. <artifactId>spring-webmvcartifactId>
    126. <version>${spring.version}version>
    127. dependency>
    128. dependencies>
    129. <build>
    130. <finalName>ssmfinalName>
    131. <resources>
    132. <resource>
    133. <directory>src/main/javadirectory>
    134. <includes>
    135. <include>**/*.xmlinclude>
    136. includes>
    137. resource>
    138. <resource>
    139. <directory>src/main/resourcesdirectory>
    140. <includes>
    141. <include>jdbc.propertiesinclude>
    142. <include>*.xmlinclude>
    143. includes>
    144. resource>
    145. resources>
    146. <pluginManagement>
    147. <plugins>
    148. <plugin>
    149. <groupId>org.apache.maven.pluginsgroupId>
    150. <artifactId>maven-compiler-pluginartifactId>
    151. <version>${maven.compiler.plugin.version}version>
    152. <configuration>
    153. <source>${maven.compiler.source}source>
    154. <target>${maven.compiler.target}target>
    155. <encoding>${project.build.sourceEncoding}encoding>
    156. configuration>
    157. plugin>
    158. <plugin>
    159. <groupId>org.mybatis.generatorgroupId>
    160. <artifactId>mybatis-generator-maven-pluginartifactId>
    161. <version>1.3.2version>
    162. <dependencies>
    163. <dependency>
    164. <groupId>mysqlgroupId>
    165. <artifactId>mysql-connector-javaartifactId>
    166. <version>${mysql.version}version>
    167. dependency>
    168. dependencies>
    169. <configuration>
    170. <overwrite>trueoverwrite>
    171. configuration>
    172. plugin>
    173. <plugin>
    174. <artifactId>maven-clean-pluginartifactId>
    175. <version>3.1.0version>
    176. plugin>
    177. <plugin>
    178. <artifactId>maven-resources-pluginartifactId>
    179. <version>3.0.2version>
    180. plugin>
    181. <plugin>
    182. <artifactId>maven-compiler-pluginartifactId>
    183. <version>3.8.0version>
    184. plugin>
    185. <plugin>
    186. <artifactId>maven-surefire-pluginartifactId>
    187. <version>2.22.1version>
    188. plugin>
    189. <plugin>
    190. <artifactId>maven-war-pluginartifactId>
    191. <version>3.2.2version>
    192. plugin>
    193. <plugin>
    194. <artifactId>maven-install-pluginartifactId>
    195. <version>2.5.2version>
    196. plugin>
    197. <plugin>
    198. <artifactId>maven-deploy-pluginartifactId>
    199. <version>2.8.2version>
    200. plugin>
    201. plugins>
    202. pluginManagement>
    203. build>
    204. project>

     

     因为版本一样,所以之前的相同依赖不需要删除,若不同则要删除,否则会版本冲突

    添加jstl依赖

              jstl

              jstl

              1.2

         

         

              taglibs

              standard

              1.1.2

         

         

    1.2 添加springmvc-servlet.xml 

    1.扫描base-package

            定义base-package

            开启注释扫描

            开启动态代理

    2.配置视图解析器

            本地资源视图解析器:InternalResourceViewResolver

            多功能视图解析器

    3.做静态资源映射

    1. "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:context="http://www.springframework.org/schema/context"
    5. xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:aop="http://www.springframework.org/schema/aop"
    6. xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
    7. http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd">
    8. <aop:aspectj-autoproxy/>
    9. <context:component-scan base-package="com.cdl.ssm"/>
    10. <mvc:annotation-driven>mvc:annotation-driven>
    11. <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    12. <property name="viewClass"
    13. value="org.springframework.web.servlet.view.JstlView">property>
    14. <property name="prefix" value="/"/>
    15. <property name="suffix" value=".jsp"/>
    16. bean>
    17. beans>

     1.3 web.xml

    spring与web容器的集成:监听器

    springMVC和web容器的集成:servlet (DispatcherServlet)

    1. <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
    2. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    3. xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
    4. version="3.1">
    5. <display-name>Archetype Created Web Applicationdisplay-name>
    6. <context-param>
    7. <param-name>contextConfigLocationparam-name>
    8. <param-value>classpath:applicationContext.xmlparam-value>
    9. context-param>
    10. <listener>
    11. <listener-class>org.springframework.web.context.ContextLoaderListenerlistener-class>
    12. listener>
    13. <servlet>
    14. <servlet-name>SpringMVCservlet-name>
    15. <servlet-class>org.springframework.web.servlet.DispatcherServletservlet-class>
    16. <init-param>
    17. <param-name>contextConfigLocationparam-name>
    18. <param-value>/WEB-INF/springmvc-servlet.xmlparam-value>
    19. init-param>
    20. <load-on-startup>1load-on-startup>
    21. <async-supported>trueasync-supported>
    22. servlet>
    23. <servlet-mapping>
    24. <servlet-name>SpringMVCservlet-name>
    25. <url-pattern>/url-pattern>
    26. servlet-mapping>
    27. web-app>

    applicationContext.xml

    1. "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:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"
    5. xmlns:aop="http://www.springframework.org/schema/aop"
    6. xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd">
    7. <import resource="applicationContext-mybatis.xml">import>
    8. beans>

    1.4 配置Tomcat

     

     

     

     

     

    1.5 代码 HelloController 

    1. package com.cdl.controller;
    2. import org.springframework.stereotype.Controller;
    3. import org.springframework.web.bind.annotation.RequestMapping;
    4. /**
    5. * @author cdl
    6. * @site www.cdl.com
    7. * @create 2022-08-16 22:33
    8. *
    9. * @Controller 被其标记的类 会交给 spring进行管理
    10. */
    11. @Controller
    12. public class HelloController {
    13. //自定义mvc:浏览器发送请求 http://localhost:8080/hello.ation?mthodName=hello
    14. //springmvc:浏览器发送请求 http://localhost:8080/helloReq
    15. @RequestMapping("/helloReq")
    16. public String hello(){
    17. System.out.println("hello springmvc...");
    18. return "hello";
    19. }
    20. }

    报错:

     翻译得:

     解决:扫描有问题
    springmvc-servlet.xml

    1. "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:context="http://www.springframework.org/schema/context"
    5. xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:aop="http://www.springframework.org/schema/aop"
    6. xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
    7. http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd">
    8. <aop:aspectj-autoproxy/>
    9. <context:component-scan base-package="com.cdl"/>
    10. <mvc:annotation-driven>mvc:annotation-driven>
    11. <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    12. <property name="viewClass"
    13. value="org.springframework.web.servlet.view.JstlView">property>
    14. <property name="prefix" value="/"/>
    15. <property name="suffix" value=".jsp"/>
    16. bean>
    17. beans>

    结果:

     二、springMVC的工作原理

    1.浏览器发送请求到中央控制器(DispatcherServlet)

    2.中央控制器通过处理器映射器(HandlerMapping)找到处理适配器(HandlerAdapter)

    3.执行HandlerAdapter,返回值ModelAndView回传给中央控制器

    4.视图解析器(ViewResolve)会对返回值ModelAndView进行渲染,得到view视图对象

    5.最后中央控制器会将view视图对象返回给用户

     HelloController 

    1. package com.cdl.controller;
    2. import org.springframework.stereotype.Controller;
    3. import org.springframework.web.bind.annotation.RequestMapping;
    4. import org.springframework.web.servlet.ModelAndView;
    5. import javax.servlet.http.HttpServletRequest;
    6. /**
    7. * @author cdl
    8. * @site www.cdl.com
    9. * @create 2022-08-16 22:33
    10. *
    11. * @Controller 被其标记的类 会交给 spring进行管理
    12. */
    13. @Controller
    14. public class HelloController {
    15. //自定义mvc:浏览器发送请求 http://localhost:8080/hello.ation?mthodName=hello
    16. //springmvc:浏览器发送请求 http://localhost:8080/helloReq
    17. //此处是hello2的间写版本
    18. @RequestMapping("/helloReq")
    19. public String hello(){
    20. System.out.println("hello springmvc...");
    21. return "hello";
    22. }
    23. @RequestMapping("/hello2")
    24. public ModelAndView hello2(HttpServletRequest req){
    25. ModelAndView mv = new ModelAndView();
    26. mv.setViewName("hello");
    27. mv.addObject("msg","success...");
    28. return mv;
    29. }
    30. }

     hello.jsp

    1. <%--
    2. Created by IntelliJ IDEA.
    3. User: Lenovo
    4. Date: 2022/8/16
    5. Time: 22:45
    6. To change this template use File | Settings | File Templates.
    7. --%>
    8. <%@ page contentType="text/html;charset=UTF-8" language="java" %>
    9. <html>
    10. <head>
    11. <title>hellotitle>
    12. head>
    13. <body>
    14. spring mvc 你好
    15. ${msg}
    16. body>
    17. html>

    结果:

     三、crud之常用注释以及返回值

    BookMapper.xml

    1. "1.0" encoding="UTF-8" ?>
    2. mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
    3. <mapper namespace="com.cdl.ssm.mapper.BookMapper" >
    4. <resultMap id="BaseResultMap" type="com.cdl.ssm.model.Book" >
    5. <constructor >
    6. <idArg column="bid" jdbcType="INTEGER" javaType="java.lang.Integer" />
    7. <arg column="bname" jdbcType="VARCHAR" javaType="java.lang.String" />
    8. <arg column="price" jdbcType="REAL" javaType="java.lang.Float" />
    9. constructor>
    10. resultMap>
    11. <sql id="Base_Column_List" >
    12. bid, bname, price
    13. sql>
    14. <select id="selectByPrimaryKey" resultType="com.cdl.ssm.model.Book" parameterType="java.lang.Integer" >
    15. select
    16. <include refid="Base_Column_List" />
    17. from t_mvc_book
    18. where bid = #{bid,jdbcType=INTEGER}
    19. select>
    20. <select id="selectByIn" resultType="com.cdl.ssm.model.Book" parameterType="java.util.List" >
    21. select
    22. <include refid="Base_Column_List" />
    23. from t_mvc_book
    24. where bid in
    25. <foreach collection="bookIds" open="(" close=")" separator="," item="bid">
    26. #{bid}
    27. foreach>
    28. select>
    29. <delete id="deleteByPrimaryKey" parameterType="java.lang.Integer" >
    30. delete from t_mvc_book
    31. where bid = #{bid,jdbcType=INTEGER}
    32. delete>
    33. <insert id="insert" parameterType="com.cdl.ssm.model.Book" >
    34. insert into t_mvc_book (bid, bname, price
    35. )
    36. values (#{bid,jdbcType=INTEGER}, #{bname,jdbcType=VARCHAR}, #{price,jdbcType=REAL}
    37. )
    38. insert>
    39. <insert id="insertSelective" parameterType="com.cdl.ssm.model.Book" >
    40. insert into t_mvc_book
    41. <trim prefix="(" suffix=")" suffixOverrides="," >
    42. <if test="bid != null" >
    43. bid,
    44. if>
    45. <if test="bname != null" >
    46. bname,
    47. if>
    48. <if test="price != null" >
    49. price,
    50. if>
    51. trim>
    52. <trim prefix="values (" suffix=")" suffixOverrides="," >
    53. <if test="bid != null" >
    54. #{bid,jdbcType=INTEGER},
    55. if>
    56. <if test="bname != null" >
    57. #{bname,jdbcType=VARCHAR},
    58. if>
    59. <if test="price != null" >
    60. #{price,jdbcType=REAL},
    61. if>
    62. trim>
    63. insert>
    64. <update id="updateByPrimaryKeySelective" parameterType="com.cdl.ssm.model.Book" >
    65. update t_mvc_book
    66. <set >
    67. <if test="bname != null" >
    68. bname = #{bname,jdbcType=VARCHAR},
    69. if>
    70. <if test="price != null" >
    71. price = #{price,jdbcType=REAL},
    72. if>
    73. set>
    74. where bid = #{bid,jdbcType=INTEGER}
    75. update>
    76. <update id="updateByPrimaryKey" parameterType="com.cdl.ssm.model.Book" >
    77. update t_mvc_book
    78. set bname = #{bname,jdbcType=VARCHAR},
    79. price = #{price,jdbcType=REAL}
    80. where bid = #{bid,jdbcType=INTEGER}
    81. update>
    82. <select id="selectBooksLike1" resultType="com.cdl.ssm.model.Book" parameterType="java.lang.String">
    83. select * from t_mvc_book where bname like #{bname}
    84. select>
    85. <select id="selectBooksLike2" resultType="com.cdl.ssm.model.Book" parameterType="java.lang.String">
    86. select * from t_mvc_book where bname like '${bname}'
    87. select>
    88. <select id="selectBooksLike3" resultType="com.cdl.ssm.model.Book" parameterType="java.lang.String">
    89. select * from t_mvc_book where bname like concat('%',#{bname},'%')
    90. select>
    91. <select id="list1" resultMap="BaseResultMap">
    92. select * from t_mvc_book
    93. select>
    94. <select id="list2" resultType="com.cdl.ssm.model.Book">
    95. select * from t_mvc_book
    96. select>
    97. <select id="list3" resultType="com.cdl.ssm.model.Book" parameterType="com.cdl.ssm.model.vo.BookVo">
    98. select * from t_mvc_book where bid in
    99. <foreach collection="bookIds" open="(" close=")" separator="," item="bid">
    100. #{bid}
    101. foreach>
    102. select>
    103. <select id="list4" resultType="java.util.Map">
    104. select * from t_mvc_book
    105. select>
    106. <select id="list5" resultType="java.util.Map" parameterType="java.util.Map">
    107. select * from t_mvc_book where bid = #{bid}
    108. select>
    109. <select id="listPager" resultType="java.util.Map" parameterType="java.util.Map">
    110. select * from t_mvc_book where bname like concat(concat('%',#{bname}),'%')
    111. select>
    112. <select id="list6" resultType="com.cdl.ssm.model.Book" parameterType="com.cdl.ssm.model.vo.BookVo">
    113. select * from t_mvc_book
    114. <where>
    115. <if test="null != min and min != ''">
    116. if>
    117. <if test="null != max and max != ''">
    118. price ]]>
    119. if>
    120. where>
    121. select>
    122. <select id="list7" resultType="com.cdl.ssm.model.Book" parameterType="com.cdl.ssm.model.vo.BookVo">
    123. select * from t_mvc_book
    124. <where>
    125. <if test="null != min and min != ''">
    126. and #{min} < price
    127. if>
    128. <if test="null != max and max != ''">
    129. and #{max} > price
    130. if>
    131. where>
    132. select>
    133. mapper>

    BookMapper 

    1. package com.cdl.ssm.mapper;
    2. import com.cdl.ssm.model.Book;
    3. import com.cdl.ssm.model.vo.BookVo;
    4. import org.apache.ibatis.annotations.Param;
    5. import org.springframework.stereotype.Repository;
    6. import java.util.List;
    7. import java.util.Map;
    8. @Repository
    9. public interface BookMapper {
    10. int deleteByPrimaryKey(Integer bid);
    11. int insert(Book record);
    12. int insertSelective(Book record);
    13. Book selectByPrimaryKey(Integer bid);
    14. int updateByPrimaryKeySelective(Book record);
    15. int updateByPrimaryKey(Book record);
    16. // 通过in关键字进行查询,讲解foreach标签的使用
    17. // 如果说参数是非实体类(book,Order,....),那么记得加上注解 @param,bookIds是对应collection属性的
    18. List selectByIn(@Param("bookIds") List bookIds);
    19. List selectBooksLike1(@Param("bname") String bname);
    20. List selectBooksLike2(@Param("bname") String bname);
    21. List selectBooksLike3(@Param("bname") String bname);
    22. // list1 list2的结论是对于单表查询而言,可以用它resultType/resultMap接收,但是多表必须用resultMap接收
    23. List list1();
    24. List list2();
    25. // 如果要传入多个查询参数,必须以对象的方式进行传递
    26. List list3(BookVo vo);
    27. // 如果是返回一条数据,那么返回值Map
    28. // 如果是多条数据,那么返回值List
    29. List list4();
    30. Map list5(Map map);
    31. // 利用第三方插件进行分页
    32. List listPager(Map map);
    33. List list6(BookVo bookVo);
    34. List list7(BookVo bookVo);
    35. }

     BookBiz 

    1. package com.cdl.biz;
    2. import com.cdl.ssm.model.Book;
    3. import com.cdl.ssm.model.vo.BookVo;
    4. import com.cdl.util.PageBean;
    5. import java.util.List;
    6. import java.util.Map;
    7. /**
    8. * @author cdl
    9. * @site www.cdl.com
    10. * @create 2022-08-10 22:00
    11. * ctrl + t 跳到接口的实现类
    12. */
    13. public interface BookBiz {
    14. int deleteByPrimaryKey(Integer bid);
    15. int insert(Book record);
    16. int insertSelective(Book record);
    17. Book selectByPrimaryKey(Integer bid);
    18. int updateByPrimaryKeySelective(Book record);
    19. int updateByPrimaryKey(Book record);
    20. List selectByIn(List bookIds);
    21. public List selectBooksLike1(String bname);
    22. public List selectBooksLike2(String bname);
    23. public List selectBooksLike3(String bname);
    24. List list1();
    25. List list2();
    26. List list3(BookVo vo);
    27. List list4();
    28. Map list5(Map map);
    29. List listPager(PageBean pageBean, Map map);
    30. List list6(BookVo vo);
    31. List list7(BookVo vo);
    32. }

     BookBizImpl 

    1. package com.cdl.biz.impl;
    2. import com.cdl.biz.BookBiz;
    3. import com.cdl.ssm.mapper.BookMapper;
    4. import com.cdl.ssm.model.Book;
    5. import com.cdl.ssm.model.vo.BookVo;
    6. import com.cdl.util.PageBean;
    7. import org.springframework.beans.factory.annotation.Autowired;
    8. import org.springframework.stereotype.Service;
    9. import java.util.List;
    10. import java.util.Map;
    11. /**
    12. * @author cdl
    13. * @site www.cdl.com
    14. * @create 2022-08-10 22:49
    15. */
    16. @Service
    17. public class BookBizImpl implements BookBiz {
    18. @Autowired
    19. private BookMapper bookMapper;
    20. //alt+insert 快速提供set/get/toString/构造方法
    21. //alt+enter 快速构建实现类,填充代码的前半部分 Ctrl+1
    22. // public BookMapper getBookMapper() {
    23. // return bookMapper;
    24. // }
    25. //
    26. // public void setBookMapper(BookMapper bookMapper) {
    27. // this.bookMapper = bookMapper;
    28. // }
    29. @Override
    30. public int deleteByPrimaryKey(Integer bid) {
    31. return bookMapper.deleteByPrimaryKey(bid);
    32. }
    33. @Override
    34. public int insert(Book record) {
    35. return bookMapper.insert(record);
    36. }
    37. @Override
    38. public int insertSelective(Book record) {
    39. return bookMapper.insertSelective(record);
    40. }
    41. @Override
    42. public Book selectByPrimaryKey(Integer bid) {
    43. return bookMapper.selectByPrimaryKey(bid);
    44. }
    45. @Override
    46. public int updateByPrimaryKeySelective(Book record) {
    47. return bookMapper.updateByPrimaryKeySelective(record);
    48. }
    49. @Override
    50. public int updateByPrimaryKey(Book record) {
    51. return bookMapper.updateByPrimaryKey(record);
    52. }
    53. @Override
    54. public List selectByIn(List bookIds) {
    55. return bookMapper.selectByIn(bookIds);
    56. }
    57. @Override
    58. public List listPager(PageBean pageBean, Map map){
    59. return bookMapper.listPager(map);
    60. }
    61. // public List listPager(Map map, PageBean pageBean) {
    62. // pageHelper分页插件相关代码
    63. // if(pageBean != null && pageBean.isPagination()){
    64. // PageHelper.startPage(pageBean.getPage(),pageBean.getRows());
    65. // }
    66. //
    67. // List maps=bookMapper.listPager(map);
    68. //
    69. // if(pageBean != null && pageBean.isPagination()){
    70. 处理查询结果的前提,是需要分页的
    71. // PageInfo info=new PageInfo(maps);
    72. // pageBean.setTotal(info.getTotal()+"");
    73. // }
    74. //
    75. // return maps;
    76. // }
    77. @Override
    78. public List list6(BookVo vo) {
    79. return bookMapper.list6(vo);
    80. }
    81. @Override
    82. public List list7(BookVo vo) {
    83. return bookMapper.list7(vo);
    84. }
    85. @Override
    86. public List selectBooksLike1(String bname) {
    87. return bookMapper.selectBooksLike1(bname);
    88. }
    89. @Override
    90. public List selectBooksLike2(String bname) {
    91. return bookMapper.selectBooksLike2(bname);
    92. }
    93. @Override
    94. public List selectBooksLike3(String bname) {
    95. return bookMapper.selectBooksLike3(bname);
    96. }
    97. @Override
    98. public List list1() {
    99. return bookMapper.list1();
    100. }
    101. @Override
    102. public List list2() {
    103. return bookMapper.list2();
    104. }
    105. @Override
    106. public List list3(BookVo vo) {
    107. return bookMapper.list3(vo);
    108. }
    109. @Override
    110. public List list4() {
    111. return bookMapper.list4();
    112. }
    113. @Override
    114. public Map list5(Map map) {
    115. return bookMapper.list5(map);
    116. }
    117. public BookBizImpl() {
    118. super();
    119. }
    120. }

     BookController 

    1. package com.cdl.controller;
    2. import com.cdl.biz.BookBiz;
    3. import com.cdl.ssm.model.Book;
    4. import com.cdl.util.PageBean;
    5. import org.springframework.beans.factory.annotation.Autowired;
    6. import org.springframework.stereotype.Controller;
    7. import org.springframework.web.bind.annotation.PathVariable;
    8. import org.springframework.web.bind.annotation.RequestMapping;
    9. import javax.servlet.http.HttpServletRequest;
    10. import java.util.HashMap;
    11. import java.util.List;
    12. import java.util.Map;
    13. /**
    14. * @author cdl
    15. * @site www.cdl.com
    16. * @create 2022-08-17 1:04
    17. *
    18. * @RequestMapping 加在类上面 称窄化路径 相当于包的概念
    19. */
    20. @Controller
    21. @RequestMapping("/book")
    22. public class BookController {
    23. @Autowired
    24. private BookBiz bookBiz;
    25. // http://localhost:8080/book/list
    26. @RequestMapping("/list")
    27. public String hello(HttpServletRequest req){
    28. System.out.println("hello springmvc...");
    29. PageBean pageBean = new PageBean();
    30. pageBean.setRequest(req);
    31. Map map = new HashMap();
    32. String bname = req.getParameter("bname");
    33. map.put("bname",bname);
    34. List maps = this.bookBiz.listPager(pageBean, map);
    35. req.setAttribute("lst",maps);
    36. return "hello";
    37. }
    38. @RequestMapping("/add")
    39. public String add(Book book){
    40. this.bookBiz.insertSelective(book);
    41. return "hello";
    42. }
    43. @RequestMapping("/edit")
    44. public String edit(Book book){
    45. this.bookBiz.insertSelective(book);
    46. return "hello";
    47. }
    48. @RequestMapping("/del/{bid}")
    49. public String del(@PathVariable("bid") Integer bid){
    50. this.bookBiz.deleteByPrimaryKey(bid);
    51. return "hello";
    52. }
    53. }

    index.jsp

    1. <%--
    2. Created by IntelliJ IDEA.
    3. User: Lenovo
    4. Date: 2022/8/17
    5. Time: 1:35
    6. To change this template use File | Settings | File Templates.
    7. --%>
    8. <%@ page contentType="text/html;charset=UTF-8" language="java" %>
    9. <html>
    10. <head>
    11. <title>Titletitle>
    12. head>
    13. <body>
    14. <a href="${pageContext.request.contextPath}/book/list">查询所有a>
    15. <a href="${pageContext.request.contextPath}/book/add?bid=2&bname=sb&bprice=9.9">新增a>
    16. <a href="${pageContext.request.contextPath}/book/edit?bid=2&bname=suibian&bprice=9.8">修改a>
    17. <a href="${pageContext.request.contextPath}/book/del/2">删除a>
    18. body>
    19. html>

     index.jsp

    1. <%--
    2. Created by IntelliJ IDEA.
    3. User: Lenovo
    4. Date: 2022/8/17
    5. Time: 1:35
    6. To change this template use File | Settings | File Templates.
    7. --%>
    8. <%@ page contentType="text/html;charset=UTF-8" language="java" %>
    9. <html>
    10. <head>
    11. <title>Titletitle>
    12. head>
    13. <body>
    14. <a href="${pageContext.request.contextPath}/book/list?bname=圣墟">查询所有a>
    15. <a href="${pageContext.request.contextPath}/book/add?bid=2&bname=sb&bprice=9.9">新增a>
    16. <a href="${pageContext.request.contextPath}/book/edit?bid=2&bname=suibian&bprice=9.8">修改a>
    17. <a href="${pageContext.request.contextPath}/book/del/2">删除a>
    18. body>
    19. html>

     hello.jsp

    1. <%--
    2. Created by IntelliJ IDEA.
    3. User: Lenovo
    4. Date: 2022/8/16
    5. Time: 22:45
    6. To change this template use File | Settings | File Templates.
    7. --%>
    8. <%@ page contentType="text/html;charset=UTF-8" language="java" %>
    9. <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
    10. <html>
    11. <head>
    12. <title>hellotitle>
    13. head>
    14. <body>
    15. spring mvc 你好
    16. ${msg}
    17. <hr>
    18. ${lst}
    19. <c:forEach items="${lst}" var="l">
    20. ${l.bid} : ${l.bname}
    21. c:forEach>
    22. body>
    23. html>

     点击新增

     注意:但是在刷新页面的时候就会报错

    理由:因为a标签默认转发

    解决:改成重定向

    修改BookController

    1. package com.cdl.controller;
    2. import com.cdl.biz.BookBiz;
    3. import com.cdl.ssm.model.Book;
    4. import com.cdl.util.PageBean;
    5. import org.springframework.beans.factory.annotation.Autowired;
    6. import org.springframework.stereotype.Controller;
    7. import org.springframework.web.bind.annotation.PathVariable;
    8. import org.springframework.web.bind.annotation.RequestMapping;
    9. import javax.servlet.http.HttpServletRequest;
    10. import java.util.HashMap;
    11. import java.util.List;
    12. import java.util.Map;
    13. /**
    14. * @author cdl
    15. * @site www.cdl.com
    16. * @create 2022-08-17 1:04
    17. *
    18. * @RequestMapping 加在类上面 称窄化路径 相当于包的概念
    19. */
    20. @Controller
    21. @RequestMapping("/book")
    22. public class BookController {
    23. @Autowired
    24. private BookBiz bookBiz;
    25. // http://localhost:8080/book/list
    26. @RequestMapping("/list")
    27. public String hello(HttpServletRequest req){
    28. System.out.println("hello springmvc...");
    29. PageBean pageBean = new PageBean();
    30. pageBean.setRequest(req);
    31. Map map = new HashMap();
    32. String bname = req.getParameter("bname");
    33. map.put("bname",bname);
    34. List maps = this.bookBiz.listPager(pageBean, map);
    35. req.setAttribute("lst",maps);
    36. return "hello";
    37. }
    38. @RequestMapping("/add")
    39. public String add(Book book){
    40. this.bookBiz.insertSelective(book);
    41. return "redirect:/book/hello";
    42. }
    43. @RequestMapping("/edit")
    44. public String edit(Book book){
    45. this.bookBiz.insertSelective(book);
    46. return "redirect:/book/hello";
    47. }
    48. @RequestMapping("/del/{bid}")
    49. public String del(@PathVariable("bid") Integer bid){
    50. this.bookBiz.deleteByPrimaryKey(bid);
    51. return "redirect:/book/hello";
    52. }
    53. }
     
    

     可见可以用了

    常用注解

    @Controller :被其标记的类会交给spring管理

    @PathVariable:路径传参

    @RequestMapping(@getMapping【查询】 @PostMapping【修改】 @DeleMapping【删除】 @PutMapping 【新增】的总称) 加在类上称窄化路径相当于包的概念

     返回值得处理

    "hello" 转发到hello.jsp

    "redirect:/hello"重定向到../hello

     静态资源

     

     

     

     

  • 相关阅读:
    Dapr 不是服务网格,只是我长的和他很像
    从零开始学习软件测试-第43天笔记
    vscode + mingw + cmake C++配置管理项目
    十六.镜头知识之工业镜头的质量判断因素
    【编程之路】面试必刷TOP101:动态规划(72-77,Python实现)
    java图像化界面GUI(swing)学习讲解
    高阶数据结构:二叉搜索树
    SAP系统里的统驭科目
    EasyAR使用
    Haproxy集群
  • 原文地址:https://blog.csdn.net/weixin_62735525/article/details/126370162