目录
springMVC: Spring Web MVC是一种基于Java的实现了MVC设计模式的、请求驱动类型的、轻量级Web框架。
org.springframework spring-webmvc ${spring.version}
pom.xml
- "1.0" encoding="UTF-8"?>
-
- <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/xsd/maven-4.0.0.xsd">
- <parent>
- <artifactId>ideamavenartifactId>
- <groupId>org.examplegroupId>
- <version>1.0-SNAPSHOTversion>
- parent>
- <modelVersion>4.0.0modelVersion>
-
- <artifactId>ssmartifactId>
- <packaging>warpackaging>
-
- <name>ssm Maven Webappname>
-
- <url>http://www.example.comurl>
-
- <properties>
- <project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
- <maven.compiler.source>1.8maven.compiler.source>
- <maven.compiler.target>1.8maven.compiler.target>
- <maven.compiler.plugin.version>3.7.0maven.compiler.plugin.version>
-
-
-
- <spring.version>5.0.2.RELEASEspring.version>
-
- <mybatis.version>3.4.5mybatis.version>
-
- <mysql.version>5.1.44mysql.version>
-
- <pagehelper.version>5.1.2pagehelper.version>
-
- <mybatis.spring.version>1.3.1mybatis.spring.version>
-
- <commons.dbcp2.version>2.1.1commons.dbcp2.version>
- <commons.pool2.version>2.4.3commons.pool2.version>
-
- <log4j2.version>2.9.1log4j2.version>
-
- <servlet.version>4.0.0servlet.version>
- <lombok.version>1.18.2lombok.version>
- properties>
-
-
- <dependencies>
- <dependency>
- <groupId>junitgroupId>
- <artifactId>junitartifactId>
- <version>4.12version>
- dependency>
-
- <dependency>
- <groupId>org.springframeworkgroupId>
- <artifactId>spring-contextartifactId>
- <version>${spring.version}version>
- dependency>
- <dependency>
- <groupId>org.springframeworkgroupId>
- <artifactId>spring-ormartifactId>
- <version>${spring.version}version>
- dependency>
- <dependency>
- <groupId>org.springframeworkgroupId>
- <artifactId>spring-txartifactId>
- <version>${spring.version}version>
- dependency>
- <dependency>
- <groupId>org.springframeworkgroupId>
- <artifactId>spring-aspectsartifactId>
- <version>${spring.version}version>
- dependency>
- <dependency>
- <groupId>org.springframeworkgroupId>
- <artifactId>spring-webartifactId>
- <version>${spring.version}version>
- dependency>
- <dependency>
- <groupId>org.springframeworkgroupId>
- <artifactId>spring-testartifactId>
- <version>${spring.version}version>
- dependency>
-
-
- <dependency>
- <groupId>org.mybatisgroupId>
- <artifactId>mybatisartifactId>
- <version>${mybatis.version}version>
- dependency>
-
- <dependency>
- <groupId>mysqlgroupId>
- <artifactId>mysql-connector-javaartifactId>
- <version>${mysql.version}version>
- dependency>
-
- <dependency>
- <groupId>com.github.pagehelpergroupId>
- <artifactId>pagehelperartifactId>
- <version>${pagehelper.version}version>
- dependency>
-
- <dependency>
- <groupId>org.mybatisgroupId>
- <artifactId>mybatis-springartifactId>
- <version>${mybatis.spring.version}version>
- dependency>
-
-
- <dependency>
- <groupId>org.apache.commonsgroupId>
- <artifactId>commons-dbcp2artifactId>
- <version>${commons.dbcp2.version}version>
- dependency>
- <dependency>
- <groupId>org.apache.commonsgroupId>
- <artifactId>commons-pool2artifactId>
- <version>${commons.pool2.version}version>
- dependency>
-
-
-
- <dependency>
- <groupId>org.apache.logging.log4jgroupId>
- <artifactId>log4j-coreartifactId>
- <version>${log4j2.version}version>
- dependency>
- <dependency>
- <groupId>org.apache.logging.log4jgroupId>
- <artifactId>log4j-apiartifactId>
- <version>${log4j2.version}version>
- dependency>
-
- <dependency>
- <groupId>org.apache.logging.log4jgroupId>
- <artifactId>log4j-webartifactId>
- <version>${log4j2.version}version>
- dependency>
-
-
- <dependency>
- <groupId>javax.servletgroupId>
- <artifactId>javax.servlet-apiartifactId>
- <version>${servlet.version}version>
- <scope>providedscope>
- dependency>
- <dependency>
- <groupId>org.projectlombokgroupId>
- <artifactId>lombokartifactId>
- <version>${lombok.version}version>
- <scope>providedscope>
- dependency>
-
- <dependency>
- <groupId>org.springframeworkgroupId>
- <artifactId>spring-webmvcartifactId>
- <version>${spring.version}version>
- dependency>
- dependencies>
-
-
- <build>
- <finalName>ssmfinalName>
-
-
- <resources>
-
- <resource>
- <directory>src/main/javadirectory>
- <includes>
- <include>**/*.xmlinclude>
- includes>
- resource>
-
- <resource>
- <directory>src/main/resourcesdirectory>
- <includes>
- <include>jdbc.propertiesinclude>
- <include>*.xmlinclude>
- includes>
- resource>
- resources>
-
-
- <pluginManagement>
- <plugins>
-
- <plugin>
- <groupId>org.apache.maven.pluginsgroupId>
- <artifactId>maven-compiler-pluginartifactId>
- <version>${maven.compiler.plugin.version}version>
- <configuration>
- <source>${maven.compiler.source}source>
- <target>${maven.compiler.target}target>
- <encoding>${project.build.sourceEncoding}encoding>
- configuration>
- plugin>
- <plugin>
- <groupId>org.mybatis.generatorgroupId>
- <artifactId>mybatis-generator-maven-pluginartifactId>
- <version>1.3.2version>
- <dependencies>
-
- <dependency>
- <groupId>mysqlgroupId>
- <artifactId>mysql-connector-javaartifactId>
- <version>${mysql.version}version>
- dependency>
- dependencies>
- <configuration>
- <overwrite>trueoverwrite>
- configuration>
- plugin>
-
-
-
-
-
- <plugin>
- <artifactId>maven-clean-pluginartifactId>
- <version>3.1.0version>
- plugin>
-
- <plugin>
- <artifactId>maven-resources-pluginartifactId>
- <version>3.0.2version>
- plugin>
- <plugin>
- <artifactId>maven-compiler-pluginartifactId>
- <version>3.8.0version>
- plugin>
- <plugin>
- <artifactId>maven-surefire-pluginartifactId>
- <version>2.22.1version>
- plugin>
- <plugin>
- <artifactId>maven-war-pluginartifactId>
- <version>3.2.2version>
- plugin>
- <plugin>
- <artifactId>maven-install-pluginartifactId>
- <version>2.5.2version>
- plugin>
- <plugin>
- <artifactId>maven-deploy-pluginartifactId>
- <version>2.8.2version>
- plugin>
- plugins>
- pluginManagement>
- build>
- project>
因为版本一样,所以之前的相同依赖不需要删除,若不同则要删除,否则会版本冲突
添加jstl依赖
jstl
jstl
1.2
taglibs
standard
1.1.2
1.扫描base-package
定义base-package
开启注释扫描
开启动态代理
2.配置视图解析器
本地资源视图解析器:InternalResourceViewResolver
多功能视图解析器
3.做静态资源映射
- "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"
- xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:aop="http://www.springframework.org/schema/aop"
- 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-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">
-
-
- <aop:aspectj-autoproxy/>
- <context:component-scan base-package="com.cdl.ssm"/>
-
-
-
-
- <mvc:annotation-driven>mvc:annotation-driven>
-
-
- <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
-
- <property name="viewClass"
- value="org.springframework.web.servlet.view.JstlView">property>
- <property name="prefix" value="/"/>
- <property name="suffix" value=".jsp"/>
- bean>
-
-
-
-
-
-
-
- beans>
spring与web容器的集成:监听器
springMVC和web容器的集成:servlet (DispatcherServlet)
- <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
- version="3.1">
- <display-name>Archetype Created Web Applicationdisplay-name>
- <context-param>
- <param-name>contextConfigLocationparam-name>
- <param-value>classpath:applicationContext.xmlparam-value>
- context-param>
-
- <listener>
- <listener-class>org.springframework.web.context.ContextLoaderListenerlistener-class>
- listener>
-
-
- <servlet>
- <servlet-name>SpringMVCservlet-name>
- <servlet-class>org.springframework.web.servlet.DispatcherServletservlet-class>
-
- <init-param>
- <param-name>contextConfigLocationparam-name>
- <param-value>/WEB-INF/springmvc-servlet.xmlparam-value>
- init-param>
- <load-on-startup>1load-on-startup>
-
- <async-supported>trueasync-supported>
- servlet>
- <servlet-mapping>
- <servlet-name>SpringMVCservlet-name>
- <url-pattern>/url-pattern>
- servlet-mapping>
- web-app>
-
-
applicationContext.xml
- "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" xmlns:tx="http://www.springframework.org/schema/tx"
- xmlns:aop="http://www.springframework.org/schema/aop"
- 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">
-
- <import resource="applicationContext-mybatis.xml">import>
- beans>
- package com.cdl.controller;
-
- import org.springframework.stereotype.Controller;
- import org.springframework.web.bind.annotation.RequestMapping;
-
- /**
- * @author cdl
- * @site www.cdl.com
- * @create 2022-08-16 22:33
- *
- * @Controller 被其标记的类 会交给 spring进行管理
- */
- @Controller
- public class HelloController {
-
- //自定义mvc:浏览器发送请求 http://localhost:8080/hello.ation?mthodName=hello
- //springmvc:浏览器发送请求 http://localhost:8080/helloReq
- @RequestMapping("/helloReq")
- public String hello(){
- System.out.println("hello springmvc...");
- return "hello";
- }
-
-
- }
报错:
翻译得:
解决:扫描有问题
springmvc-servlet.xml
- "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"
- xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:aop="http://www.springframework.org/schema/aop"
- 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-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">
-
-
- <aop:aspectj-autoproxy/>
- <context:component-scan base-package="com.cdl"/>
-
-
-
-
- <mvc:annotation-driven>mvc:annotation-driven>
-
-
- <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
-
- <property name="viewClass"
- value="org.springframework.web.servlet.view.JstlView">property>
- <property name="prefix" value="/"/>
- <property name="suffix" value=".jsp"/>
- bean>
-
-
-
-
-
-
-
- beans>
结果:
1.浏览器发送请求到中央控制器(DispatcherServlet)
2.中央控制器通过处理器映射器(HandlerMapping)找到处理适配器(HandlerAdapter)
3.执行HandlerAdapter,返回值ModelAndView回传给中央控制器
4.视图解析器(ViewResolve)会对返回值ModelAndView进行渲染,得到view视图对象
5.最后中央控制器会将view视图对象返回给用户
HelloController
- package com.cdl.controller;
-
- import org.springframework.stereotype.Controller;
- import org.springframework.web.bind.annotation.RequestMapping;
- import org.springframework.web.servlet.ModelAndView;
-
- import javax.servlet.http.HttpServletRequest;
-
- /**
- * @author cdl
- * @site www.cdl.com
- * @create 2022-08-16 22:33
- *
- * @Controller 被其标记的类 会交给 spring进行管理
- */
- @Controller
- public class HelloController {
-
- //自定义mvc:浏览器发送请求 http://localhost:8080/hello.ation?mthodName=hello
- //springmvc:浏览器发送请求 http://localhost:8080/helloReq
- //此处是hello2的间写版本
- @RequestMapping("/helloReq")
- public String hello(){
- System.out.println("hello springmvc...");
- return "hello";
- }
-
-
- @RequestMapping("/hello2")
- public ModelAndView hello2(HttpServletRequest req){
- ModelAndView mv = new ModelAndView();
- mv.setViewName("hello");
- mv.addObject("msg","success...");
- return mv;
- }
-
-
- }
hello.jsp
- <%--
- Created by IntelliJ IDEA.
- User: Lenovo
- Date: 2022/8/16
- Time: 22:45
- To change this template use File | Settings | File Templates.
- --%>
- <%@ page contentType="text/html;charset=UTF-8" language="java" %>
- <html>
- <head>
- <title>hellotitle>
- head>
- <body>
- spring mvc 你好
- ${msg}
- body>
- html>
结果:
BookMapper.xml
- "1.0" encoding="UTF-8" ?>
- mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
- <mapper namespace="com.cdl.ssm.mapper.BookMapper" >
- <resultMap id="BaseResultMap" type="com.cdl.ssm.model.Book" >
- <constructor >
- <idArg column="bid" jdbcType="INTEGER" javaType="java.lang.Integer" />
- <arg column="bname" jdbcType="VARCHAR" javaType="java.lang.String" />
- <arg column="price" jdbcType="REAL" javaType="java.lang.Float" />
- constructor>
- resultMap>
- <sql id="Base_Column_List" >
- bid, bname, price
- sql>
- <select id="selectByPrimaryKey" resultType="com.cdl.ssm.model.Book" parameterType="java.lang.Integer" >
- select
- <include refid="Base_Column_List" />
- from t_mvc_book
- where bid = #{bid,jdbcType=INTEGER}
- select>
-
-
- <select id="selectByIn" resultType="com.cdl.ssm.model.Book" parameterType="java.util.List" >
- select
- <include refid="Base_Column_List" />
- from t_mvc_book
- where bid in
- <foreach collection="bookIds" open="(" close=")" separator="," item="bid">
- #{bid}
- foreach>
- select>
-
-
-
- <delete id="deleteByPrimaryKey" parameterType="java.lang.Integer" >
- delete from t_mvc_book
- where bid = #{bid,jdbcType=INTEGER}
- delete>
- <insert id="insert" parameterType="com.cdl.ssm.model.Book" >
- insert into t_mvc_book (bid, bname, price
- )
- values (#{bid,jdbcType=INTEGER}, #{bname,jdbcType=VARCHAR}, #{price,jdbcType=REAL}
- )
- insert>
- <insert id="insertSelective" parameterType="com.cdl.ssm.model.Book" >
- insert into t_mvc_book
- <trim prefix="(" suffix=")" suffixOverrides="," >
- <if test="bid != null" >
- bid,
- if>
- <if test="bname != null" >
- bname,
- if>
- <if test="price != null" >
- price,
- if>
- trim>
- <trim prefix="values (" suffix=")" suffixOverrides="," >
- <if test="bid != null" >
- #{bid,jdbcType=INTEGER},
- if>
- <if test="bname != null" >
- #{bname,jdbcType=VARCHAR},
- if>
- <if test="price != null" >
- #{price,jdbcType=REAL},
- if>
- trim>
- insert>
- <update id="updateByPrimaryKeySelective" parameterType="com.cdl.ssm.model.Book" >
- update t_mvc_book
- <set >
- <if test="bname != null" >
- bname = #{bname,jdbcType=VARCHAR},
- if>
- <if test="price != null" >
- price = #{price,jdbcType=REAL},
- if>
- set>
- where bid = #{bid,jdbcType=INTEGER}
- update>
- <update id="updateByPrimaryKey" parameterType="com.cdl.ssm.model.Book" >
- update t_mvc_book
- set bname = #{bname,jdbcType=VARCHAR},
- price = #{price,jdbcType=REAL}
- where bid = #{bid,jdbcType=INTEGER}
- update>
-
-
- <select id="selectBooksLike1" resultType="com.cdl.ssm.model.Book" parameterType="java.lang.String">
- select * from t_mvc_book where bname like #{bname}
- select>
- <select id="selectBooksLike2" resultType="com.cdl.ssm.model.Book" parameterType="java.lang.String">
- select * from t_mvc_book where bname like '${bname}'
- select>
- <select id="selectBooksLike3" resultType="com.cdl.ssm.model.Book" parameterType="java.lang.String">
- select * from t_mvc_book where bname like concat('%',#{bname},'%')
- select>
-
- <select id="list1" resultMap="BaseResultMap">
- select * from t_mvc_book
- select>
- <select id="list2" resultType="com.cdl.ssm.model.Book">
- select * from t_mvc_book
- select>
- <select id="list3" resultType="com.cdl.ssm.model.Book" parameterType="com.cdl.ssm.model.vo.BookVo">
- select * from t_mvc_book where bid in
- <foreach collection="bookIds" open="(" close=")" separator="," item="bid">
- #{bid}
- foreach>
- select>
- <select id="list4" resultType="java.util.Map">
- select * from t_mvc_book
- select>
- <select id="list5" resultType="java.util.Map" parameterType="java.util.Map">
- select * from t_mvc_book where bid = #{bid}
- select>
-
- <select id="listPager" resultType="java.util.Map" parameterType="java.util.Map">
- select * from t_mvc_book where bname like concat(concat('%',#{bname}),'%')
- select>
-
-
- <select id="list6" resultType="com.cdl.ssm.model.Book" parameterType="com.cdl.ssm.model.vo.BookVo">
- select * from t_mvc_book
- <where>
- <if test="null != min and min != ''">
-
- if>
- <if test="null != max and max != ''">
- price ]]>
- if>
- where>
- select>
-
- <select id="list7" resultType="com.cdl.ssm.model.Book" parameterType="com.cdl.ssm.model.vo.BookVo">
- select * from t_mvc_book
- <where>
- <if test="null != min and min != ''">
- and #{min} < price
- if>
- <if test="null != max and max != ''">
- and #{max} > price
- if>
- where>
- select>
-
- mapper>
BookMapper
- package com.cdl.ssm.mapper;
-
- import com.cdl.ssm.model.Book;
- import com.cdl.ssm.model.vo.BookVo;
- import org.apache.ibatis.annotations.Param;
- import org.springframework.stereotype.Repository;
-
- import java.util.List;
- import java.util.Map;
-
- @Repository
- public interface BookMapper {
- int deleteByPrimaryKey(Integer bid);
-
- int insert(Book record);
-
- int insertSelective(Book record);
-
- Book selectByPrimaryKey(Integer bid);
-
- int updateByPrimaryKeySelective(Book record);
-
- int updateByPrimaryKey(Book record);
-
- // 通过in关键字进行查询,讲解foreach标签的使用
- // 如果说参数是非实体类(book,Order,....),那么记得加上注解 @param,bookIds是对应collection属性的
- List
selectByIn(@Param("bookIds") List bookIds); -
- List
selectBooksLike1(@Param("bname") String bname); - List
selectBooksLike2(@Param("bname") String bname); - List
selectBooksLike3(@Param("bname") String bname); -
- // list1 list2的结论是对于单表查询而言,可以用它resultType/resultMap接收,但是多表必须用resultMap接收
- List
list1(); - List
list2(); - // 如果要传入多个查询参数,必须以对象的方式进行传递
- List
list3(BookVo vo); - // 如果是返回一条数据,那么返回值Map
- // 如果是多条数据,那么返回值List
- List
- Map list5(Map map);
-
- // 利用第三方插件进行分页
- List
-
-
-
- List
list6(BookVo bookVo); -
-
-
- List
list7(BookVo bookVo); -
- }
BookBiz
- package com.cdl.biz;
-
- import com.cdl.ssm.model.Book;
- import com.cdl.ssm.model.vo.BookVo;
- import com.cdl.util.PageBean;
-
- import java.util.List;
- import java.util.Map;
-
- /**
- * @author cdl
- * @site www.cdl.com
- * @create 2022-08-10 22:00
- * ctrl + t 跳到接口的实现类
- */
- public interface BookBiz {
- int deleteByPrimaryKey(Integer bid);
-
- int insert(Book record);
-
- int insertSelective(Book record);
-
- Book selectByPrimaryKey(Integer bid);
-
- int updateByPrimaryKeySelective(Book record);
-
- int updateByPrimaryKey(Book record);
-
- List
selectByIn(List bookIds); -
- public List
selectBooksLike1(String bname); - public List
selectBooksLike2(String bname); - public List
selectBooksLike3(String bname); -
-
- List
list1(); - List
list2(); - List
list3(BookVo vo); - List
- Map list5(Map map);
-
-
- List
-
- List
list6(BookVo vo); - List
list7(BookVo vo); -
- }
BookBizImpl
- package com.cdl.biz.impl;
-
-
- import com.cdl.biz.BookBiz;
- import com.cdl.ssm.mapper.BookMapper;
- import com.cdl.ssm.model.Book;
- import com.cdl.ssm.model.vo.BookVo;
- import com.cdl.util.PageBean;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.stereotype.Service;
-
- import java.util.List;
- import java.util.Map;
-
- /**
- * @author cdl
- * @site www.cdl.com
- * @create 2022-08-10 22:49
- */
- @Service
- public class BookBizImpl implements BookBiz {
- @Autowired
- private BookMapper bookMapper;
-
- //alt+insert 快速提供set/get/toString/构造方法
- //alt+enter 快速构建实现类,填充代码的前半部分 Ctrl+1
- // public BookMapper getBookMapper() {
- // return bookMapper;
- // }
- //
- // public void setBookMapper(BookMapper bookMapper) {
- // this.bookMapper = bookMapper;
- // }
-
- @Override
- public int deleteByPrimaryKey(Integer bid) {
- return bookMapper.deleteByPrimaryKey(bid);
- }
-
- @Override
- public int insert(Book record) {
- return bookMapper.insert(record);
- }
-
- @Override
- public int insertSelective(Book record) {
- return bookMapper.insertSelective(record);
- }
-
- @Override
- public Book selectByPrimaryKey(Integer bid) {
- return bookMapper.selectByPrimaryKey(bid);
- }
-
- @Override
- public int updateByPrimaryKeySelective(Book record) {
- return bookMapper.updateByPrimaryKeySelective(record);
- }
-
- @Override
- public int updateByPrimaryKey(Book record) {
- return bookMapper.updateByPrimaryKey(record);
- }
-
- @Override
- public List
selectByIn(List bookIds) { - return bookMapper.selectByIn(bookIds);
- }
-
- @Override
- public List
- return bookMapper.listPager(map);
- }
- // public List
- // pageHelper分页插件相关代码
- // if(pageBean != null && pageBean.isPagination()){
- // PageHelper.startPage(pageBean.getPage(),pageBean.getRows());
- // }
- //
- // List
- //
- // if(pageBean != null && pageBean.isPagination()){
- 处理查询结果的前提,是需要分页的
- // PageInfo info=new PageInfo(maps);
- // pageBean.setTotal(info.getTotal()+"");
- // }
- //
- // return maps;
- // }
-
- @Override
- public List
list6(BookVo vo) { - return bookMapper.list6(vo);
- }
-
- @Override
- public List
list7(BookVo vo) { - return bookMapper.list7(vo);
- }
-
- @Override
- public List
selectBooksLike1(String bname) { - return bookMapper.selectBooksLike1(bname);
- }
-
- @Override
- public List
selectBooksLike2(String bname) { - return bookMapper.selectBooksLike2(bname);
- }
-
- @Override
- public List
selectBooksLike3(String bname) { - return bookMapper.selectBooksLike3(bname);
- }
-
- @Override
- public List
list1() { -
- return bookMapper.list1();
- }
-
-
-
- @Override
- public List
list2() { - return bookMapper.list2();
- }
-
- @Override
- public List
list3(BookVo vo) { - return bookMapper.list3(vo);
- }
-
- @Override
- public List
- return bookMapper.list4();
- }
-
- @Override
- public Map list5(Map map) {
- return bookMapper.list5(map);
- }
-
- public BookBizImpl() {
- super();
- }
-
- }
-
BookController
- package com.cdl.controller;
-
- import com.cdl.biz.BookBiz;
- import com.cdl.ssm.model.Book;
- import com.cdl.util.PageBean;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.stereotype.Controller;
- import org.springframework.web.bind.annotation.PathVariable;
- import org.springframework.web.bind.annotation.RequestMapping;
-
- import javax.servlet.http.HttpServletRequest;
- import java.util.HashMap;
- import java.util.List;
- import java.util.Map;
-
- /**
- * @author cdl
- * @site www.cdl.com
- * @create 2022-08-17 1:04
- *
- * @RequestMapping 加在类上面 称窄化路径 相当于包的概念
- */
- @Controller
- @RequestMapping("/book")
- public class BookController {
-
- @Autowired
- private BookBiz bookBiz;
-
- // http://localhost:8080/book/list
- @RequestMapping("/list")
- public String hello(HttpServletRequest req){
- System.out.println("hello springmvc...");
- PageBean pageBean = new PageBean();
- pageBean.setRequest(req);
- Map map = new HashMap();
- String bname = req.getParameter("bname");
- map.put("bname",bname);
- List
- req.setAttribute("lst",maps);
- return "hello";
- }
-
-
- @RequestMapping("/add")
- public String add(Book book){
- this.bookBiz.insertSelective(book);
- return "hello";
- }
-
- @RequestMapping("/edit")
- public String edit(Book book){
- this.bookBiz.insertSelective(book);
- return "hello";
- }
-
- @RequestMapping("/del/{bid}")
- public String del(@PathVariable("bid") Integer bid){
- this.bookBiz.deleteByPrimaryKey(bid);
- return "hello";
- }
-
- }
index.jsp
- <%--
- Created by IntelliJ IDEA.
- User: Lenovo
- Date: 2022/8/17
- Time: 1:35
- To change this template use File | Settings | File Templates.
- --%>
- <%@ page contentType="text/html;charset=UTF-8" language="java" %>
- <html>
- <head>
- <title>Titletitle>
- head>
- <body>
- <a href="${pageContext.request.contextPath}/book/list">查询所有a>
- <a href="${pageContext.request.contextPath}/book/add?bid=2&bname=sb&bprice=9.9">新增a>
- <a href="${pageContext.request.contextPath}/book/edit?bid=2&bname=suibian&bprice=9.8">修改a>
- <a href="${pageContext.request.contextPath}/book/del/2">删除a>
- body>
- html>
index.jsp
- <%--
- Created by IntelliJ IDEA.
- User: Lenovo
- Date: 2022/8/17
- Time: 1:35
- To change this template use File | Settings | File Templates.
- --%>
- <%@ page contentType="text/html;charset=UTF-8" language="java" %>
- <html>
- <head>
- <title>Titletitle>
- head>
- <body>
- <a href="${pageContext.request.contextPath}/book/list?bname=圣墟">查询所有a>
- <a href="${pageContext.request.contextPath}/book/add?bid=2&bname=sb&bprice=9.9">新增a>
- <a href="${pageContext.request.contextPath}/book/edit?bid=2&bname=suibian&bprice=9.8">修改a>
- <a href="${pageContext.request.contextPath}/book/del/2">删除a>
- body>
- html>
hello.jsp
- <%--
- Created by IntelliJ IDEA.
- User: Lenovo
- Date: 2022/8/16
- Time: 22:45
- To change this template use File | Settings | File Templates.
- --%>
- <%@ page contentType="text/html;charset=UTF-8" language="java" %>
- <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
- <html>
- <head>
- <title>hellotitle>
- head>
- <body>
- spring mvc 你好
- ${msg}
- <hr>
- ${lst}
- <c:forEach items="${lst}" var="l">
- ${l.bid} : ${l.bname}
- c:forEach>
-
- body>
- html>
点击新增
注意:但是在刷新页面的时候就会报错
理由:因为a标签默认转发
解决:改成重定向
修改BookController
- package com.cdl.controller;
-
- import com.cdl.biz.BookBiz;
- import com.cdl.ssm.model.Book;
- import com.cdl.util.PageBean;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.stereotype.Controller;
- import org.springframework.web.bind.annotation.PathVariable;
- import org.springframework.web.bind.annotation.RequestMapping;
-
- import javax.servlet.http.HttpServletRequest;
- import java.util.HashMap;
- import java.util.List;
- import java.util.Map;
-
- /**
- * @author cdl
- * @site www.cdl.com
- * @create 2022-08-17 1:04
- *
- * @RequestMapping 加在类上面 称窄化路径 相当于包的概念
- */
- @Controller
- @RequestMapping("/book")
- public class BookController {
-
- @Autowired
- private BookBiz bookBiz;
-
- // http://localhost:8080/book/list
- @RequestMapping("/list")
- public String hello(HttpServletRequest req){
- System.out.println("hello springmvc...");
- PageBean pageBean = new PageBean();
- pageBean.setRequest(req);
- Map map = new HashMap();
- String bname = req.getParameter("bname");
- map.put("bname",bname);
- List
- req.setAttribute("lst",maps);
- return "hello";
- }
-
-
- @RequestMapping("/add")
- public String add(Book book){
- this.bookBiz.insertSelective(book);
- return "redirect:/book/hello";
- }
-
- @RequestMapping("/edit")
- public String edit(Book book){
- this.bookBiz.insertSelective(book);
- return "redirect:/book/hello";
- }
-
- @RequestMapping("/del/{bid}")
- public String del(@PathVariable("bid") Integer bid){
- this.bookBiz.deleteByPrimaryKey(bid);
- return "redirect:/book/hello";
- }
-
- }
可见可以用了
@Controller :被其标记的类会交给spring管理
@PathVariable:路径传参
@RequestMapping(@getMapping【查询】 @PostMapping【修改】 @DeleMapping【删除】 @PutMapping 【新增】的总称) 加在类上称窄化路径相当于包的概念
"hello" 转发到hello.jsp
"redirect:/hello"重定向到../hello