• springboot视图渲染技术


    一、Freemarker入门

    新建一个springboot项目
    在这里插入图片描述
    配置freemarker
    在这里插入图片描述
    在这里插入图片描述
    在这里插入图片描述
    在这里插入图片描述

    二、Freemarker的基本语法

    1、application.yml

    mybatis:
        mapper-locations: classpath:mappers/*xml
        type-aliases-package: com.xnx.spboot04.model
    server:
        port: 8080
        servlet:
            context-path: /spboot04
    spring:
        application:
            name: spboot04
        datasource:
            driver-class-name: com.mysql.jdbc.Driver
            name: defaultDataSource
            password: 123456
            url: jdbc:mysql://localhost:3306/t280?useUnicode=true&characterEncoding=UTF-8
            username: root
        freemarker:
            cache: false
            charset: utf-8
            expose-request-attributes: true
            expose-session-attributes: true
            suffix: .ftl
            template-loader-path: classpath:/templates/
        mvc:
            static-path-pattern: /static/**
    #    resources:
    #        static-locations: classpath:/static/# Ӧ�÷��� WEB ���ʶ˿�
    pagehelper:
        reasonable: true
        supportMethodsArguments: true
        page-size-zero: true
        helper-dialect: mysql
    logging:
        level:
            com.xnx.spboot04: debug
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35

    2、IndexController

    package com.xnx.spboot04.controller;
    
    import com.xnx.spboot04.entity.User;
    import lombok.AllArgsConstructor;
    import lombok.Data;
    import lombok.NoArgsConstructor;
    import org.springframework.stereotype.Controller;
    import org.springframework.ui.Model;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.servlet.ModelAndView;
    
    import java.util.Arrays;
    
    /**
     * @author xnx
     * @create 2022-11-01 17:45
     */
    @Controller
    public class IndexController {
    //    @RequestMapping("/")
    //    public ModelAndView index(){
            springmvc
    //        ModelAndView mv = new ModelAndView();
    //        mv.addObject("uname","aa");
    //        mv.setViewName("index");
    //        System.out.println("come in");
    //        return mv;
    //    }
    
        @RequestMapping("/")
        public String index(Model model){
            model.addAttribute("sex","mimi");
    //        model.addAttribute("uname","aa");
            model.addAttribute("users", Arrays.asList(new User(1,"zs"),
                    new User(2,"ls"),new User(3,"ww")));
            model.addAttribute("arr",new Integer[]{3,4,5,6,7,8,9});
            System.out.println("come in");
            return "index";
        }
    }
    
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42

    3、head.ftl

    <#assign ctx>
        ${springMacroRequestContext.contextPath}
    
    
    <#global ctx2>
        ${springMacroRequestContext.contextPath}
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    4、index.ftl

    
    
    
        
        
    
    
    

    取值

    1.1提供默认值

    <#--当接收的参数不为空会报错。这个问题是需要避免的--> <#--$
    {uname}--> <#--{uname !}--> ${uname ! 'bb'}

    1.2对null先进行判断

    1)exist用在逻辑判断 <#if uname?exists> uname}
    2)??判断对象是否为空 springboot项目需要对查询的结果进行遍历,那么遍历之前必须判空 <#if uname??> $
    {uname}
    3)if_exist打印东西 $
    {uname?if_exists}

    条件

    <#if sex="nv"> 女 <#elseif sex="nan"> 男 <#else> 未知

    循环

    1)取出数组中的元素 <#list arr as a> $
    {a} /
    2)取出集合中的对象 <#list users as a> $
    {a.id}:${a.name}

    4.include

    <#include '/head.ftl'>

    5.局部变量/全局变量

    $
    {ctx} : ${ctx2}
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54

    5、测试

    在这里插入图片描述

    三、Freemarker综合案例

    1、PagerAspect

    package com.xnx.spboot04.aspect;
    
    import com.github.pagehelper.PageHelper;
    import com.github.pagehelper.PageInfo;
    import com.xnx.spboot04.util.PageBean;
    import org.aspectj.lang.ProceedingJoinPoint;
    import org.aspectj.lang.annotation.Around;
    import org.aspectj.lang.annotation.Aspect;
    import org.springframework.stereotype.Component;
    
    import java.util.List;
    
    @Component
    @Aspect
    public class PagerAspect {
    
        /**
         * *:返回值类型
         * *..:无限包
         * *Service:以Service结尾接口名
         * *Pager:以Pager方法
         * 只要同时匹配上诉四个条件,就会被列为目标对象
         * 上述配置要生效,代理注解不能少
         * @param args
         * @return
         * @throws Throwable
         */
        @Around("execution(* *..*Biz.*Pager(..))")
        public Object invoke(ProceedingJoinPoint args) throws Throwable {
            Object[] params = args.getArgs();
            PageBean pageBean = null;
            for (Object param : params) {
                if(param instanceof PageBean){
                    pageBean = (PageBean)param;
                    break;
                }
            }
    
            if(pageBean != null && pageBean.isPagination())
                PageHelper.startPage(pageBean.getPage(),pageBean.getRows());
    
    //        执行目标方法
            Object list = args.proceed(params);
    
            if(null != pageBean && pageBean.isPagination()){
                PageInfo pageInfo = new PageInfo((List) list);
                pageBean.setTotal(pageInfo.getTotal()+"");
            }
            return list;
        }
    
    
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54

    2、ClazzBizImpl

    package com.xnx.spboot04.biz.impl;
    
    import com.xnx.spboot04.biz.ClazzBiz;
    import com.xnx.spboot04.mapper.ClazzMapper;
    import com.xnx.spboot04.model.Clazz;
    import com.xnx.spboot04.util.PageBean;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Service;
    
    import java.util.List;
    import java.util.Map;
    
    @Service
    public class ClazzBizImpl implements ClazzBiz {
        @Autowired
        private ClazzMapper clazzMapper;
        @Override
        public int deleteByPrimaryKey(Integer cid) {
            System.out.println("不做任何操作...");
            return clazzMapper.deleteByPrimaryKey(cid);
    //        return 0;
        }
    
        @Override
        public int insert(Clazz record) {
            return clazzMapper.insert(record);
        }
    
        @Override
        public int insertSelective(Clazz record) {
            return clazzMapper.insertSelective(record);
        }
    
        @Override
        public Clazz selectByPrimaryKey(Integer cid) {
            return clazzMapper.selectByPrimaryKey(cid);
        }
    
        @Override
        public int updateByPrimaryKeySelective(Clazz record) {
            return clazzMapper.updateByPrimaryKeySelective(record);
        }
    
        @Override
        public int updateByPrimaryKey(Clazz record) {
            return clazzMapper.updateByPrimaryKey(record);
        }
    
        @Override
        public List<Clazz> listPager(Clazz clazz, PageBean pageBean) {
            return clazzMapper.listPager(clazz);
        }
    
        @Override
        public List<Map> listMapPager(Clazz clazz, PageBean pageBean) {
            if(true)
                throw new RuntimeException("查询班级信息异常,异常存在于ClazzBizImpl.list。。。。");
            return clazzMapper.listMapPager(clazz);
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61

    3、ClazzBiz

    package com.xnx.spboot04.biz;
    
    import com.xnx.spboot04.model.Clazz;
    import com.xnx.spboot04.util.PageBean;
    import org.springframework.cache.annotation.CacheEvict;
    import org.springframework.cache.annotation.CachePut;
    
    import java.util.List;
    import java.util.Map;
    
    public interface ClazzBiz {
        int deleteByPrimaryKey(Integer cid);
    
        int insert(Clazz record);
    
        int insertSelective(Clazz record);
    
    //    xx=cache-cid:1
    //    key的作用改变原有的key生成规则
    //    @Cacheable(value = "xx",key = "'cid:'+#cid",condition = "#cid > 6")
        Clazz selectByPrimaryKey(Integer cid);
    
        int updateByPrimaryKeySelective(Clazz record);
    
        int updateByPrimaryKey(Clazz record);
    
        List<Clazz> listPager(Clazz clazz, PageBean pageBean);
        List<Map> listMapPager(Clazz clazz, PageBean pageBean);
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29

    4、ClazzController

    package com.xnx.spboot04.controller;
    
    import com.xnx.spboot04.biz.ClazzBiz;
    import com.xnx.spboot04.model.Clazz;
    import com.xnx.spboot04.util.FreemakerPageHelper;
    import com.xnx.spboot04.util.PageBean;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.http.HttpHeaders;
    import org.springframework.http.HttpStatus;
    import org.springframework.http.MediaType;
    import org.springframework.http.ResponseEntity;
    import org.springframework.stereotype.Controller;
    import org.springframework.validation.BindingResult;
    import org.springframework.validation.FieldError;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.multipart.MultipartFile;
    
    import javax.servlet.http.HttpServletRequest;
    import java.io.File;
    import java.util.HashMap;
    import java.util.List;
    import java.util.Map;
    
    @Controller
    @RequestMapping("/clz")
    public class ClazzController {
        @Autowired
        private ClazzBiz clazzBiz;
    //    list->clzList
    //    toList->重定向list->"redirect:/clz/list"
    //    toEdit->跳转到编辑界面->clzEdit
    //    Clazz : 以前是通过模型驱动接口封装,现在是直接在方法中接受参数即可
        @RequestMapping("/list")
        public String list(Clazz clazz, HttpServletRequest request){
            System.out.println("我添加了 新的代码,终于不重启了....");
            PageBean pageBean = new PageBean();
            pageBean.setRequest(request);
            List<Clazz> lst = this.clazzBiz.listPager(clazz, pageBean);
            request.setAttribute("lst",lst);
            request.setAttribute("pageBean",pageBean);
    
            request.setAttribute("pagestr", FreemakerPageHelper.toHTML(pageBean));
            return "clz/clzList";
        }
    
        @RequestMapping("/toEdit")
        public String toEdit(Clazz clazz, HttpServletRequest request){
            Integer cid = clazz.getCid();
    //        传递的id代表了修改,没传代表新增
            if(cid != null){
                List<Clazz> lst = this.clazzBiz.listPager(clazz, null);
                request.setAttribute("b",lst.get(0));
            }
            return "clz/clzEdit";
        }
    
        @RequestMapping("/add")
        public String add(Clazz clazz){
            this.clazzBiz.insertSelective(clazz);
            return "redirect:/clz/list";
        }
    
        /**
         * @Valid 是与实体类中 的服务端校验 注解配合使用的
         * BindingResult 存放了所有违背 校验的错误信息
         * @param clazz
         * @param bindingResult
         * @return
         */
        @RequestMapping("/valiAdd")
        public String valiAdd(Clazz clazz, BindingResult bindingResult,HttpServletRequest request){
            if (bindingResult.hasErrors()){
                Map msg = new HashMap();
    //            违背规则
                List<FieldError> fieldErrors = bindingResult.getFieldErrors();
                for (FieldError fieldError : fieldErrors) {
    //                cid : cid不能为空
                    System.out.println(fieldError.getField() + ":" + fieldError.getDefaultMessage());
    //                msg.put(cid,cid不能为空);
                    msg.put(fieldError.getField(),fieldError.getDefaultMessage());
                }
                request.setAttribute("msg",msg);
    //            如果出现了错误,应该将提示语显示在 表单提交元素后方
                return "clzEdit";
            }else {
                this.clazzBiz.insertSelective(clazz);
            }
            
            return "redirect:/clz/list";
        }
    
        @RequestMapping("/edit")
        public String edit(Clazz clazz){
            this.clazzBiz.updateByPrimaryKeySelective(clazz);
            return "redirect:/clz/list";
        }
    
        @RequestMapping("/del")
        public String del(Clazz clazz){
            this.clazzBiz.deleteByPrimaryKey(clazz.getCid());
            return "redirect:/clz/list";
        }
    
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105

    5、ClazzMapper

    package com.xnx.spboot04.mapper;
    
    import com.xnx.spboot04.model.Clazz;
    import org.springframework.stereotype.Repository;
    
    import java.util.List;
    import java.util.Map;
    
    @Repository
    public interface ClazzMapper {
        int deleteByPrimaryKey(Integer cid);
    
        int insert(Clazz record);
    
        int insertSelective(Clazz record);
    
        Clazz selectByPrimaryKey(Integer cid);
    
        List<Clazz> listPager(Clazz clazz);
    
        List<Map> listMapPager(Clazz clazz);
    
        int updateByPrimaryKeySelective(Clazz record);
    
        int updateByPrimaryKey(Clazz record);
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26

    6、Clazz

    package com.xnx.spboot04.model;
    
    /**
     * NotNull:针对的是基本数据类型
     *  @NotEmpty 作用于集合
     *  @NotBlank    作用于字符串
     */
    public class Clazz {
        protected Integer cid;
    
        protected String cname;
    
        protected String cteacher;
    
        protected String pic;
    
        public Clazz(Integer cid, String cname, String cteacher, String pic) {
            this.cid = cid;
            this.cname = cname;
            this.cteacher = cteacher;
            this.pic = pic;
        }
    
        public Clazz() {
            super();
        }
    
        public Integer getCid() {
            return cid;
        }
    
        public void setCid(Integer cid) {
            this.cid = cid;
        }
    
        public String getCname() {
            return cname;
        }
    
        public void setCname(String cname) {
            this.cname = cname;
        }
    
        public String getCteacher() {
            return cteacher;
        }
    
        public void setCteacher(String cteacher) {
            this.cteacher = cteacher;
        }
    
        public String getPic() {
            return pic;
        }
    
        public void setPic(String pic) {
            this.pic = pic;
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59

    7、FreemakerPageHelper

    package com.xnx.spboot04.util;
    
    import java.util.Map;
    import java.util.Map.Entry;
    import java.util.Set;
    
    /**
     * @author xnx
     * @create 2022-11-01 19:55
     */
    public class FreemakerPageHelper {
        public static String toHTML(PageBean pageBean) {
            StringBuffer sb = new StringBuffer();
    //		隐藏的form表单---这个就是上一次请求下次重新发的奥义所在
    //		上一次请求的URL
            sb.append("
    "); sb.append(" "); // 上一次请求的参数 Map<String, String[]> paramMap = pageBean.getParamMap(); if(paramMap != null && paramMap.size() > 0) { Set<Entry<String, String[]>> entrySet = paramMap.entrySet(); for (Entry<String, String[]> entry : entrySet) { // 参数名 String key = entry.getKey(); // 参数值 for (String value : entry.getValue()) { // 上一次请求的参数,再一次组装成了新的Form表单 // 注意:page参数每次都会提交,我们需要避免 if(!"page".equals(key)) { sb.append(" "); } } } } sb.append(""
    ); // 分页条 sb.append(""); // 分页执行的JS代码 sb.append(""); return sb.toString(); } }
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74

    8、PageBean

    package com.xnx.spboot04.util;
    
    import java.util.HashMap;
    import java.util.Map;
    
    import javax.servlet.http.HttpServletRequest;
    
    /**
     * 分页工具类
     *
     */
    public class PageBean {
    
    	private int page = 1;// 页码
    
    	private int rows = 10;// 页大小
    
    	private int total = 0;// 总记录数
    
    	private boolean pagination = true;// 是否分页
    	
    	private String url; //保存上一次请求的URL
    	
    	private Map<String,String[]> paramMap = new HashMap<>();// 保存上一次请求的参数
    	
    	/**
    	 * 初始化pagebean的,保存上一次请求的重要参数
    	 * @param req
    	 */
    	public void setRequest(HttpServletRequest req) {
    //		1.1	需要保存上一次请求的URL
    		this.setUrl(req.getRequestURL().toString());
    //		1.2	需要保存上一次请求的参数	bname、price
    		this.setParamMap(req.getParameterMap());
    //		1.3	需要保存上一次请求的分页设置	pagination
    		this.setPagination(req.getParameter("pagination"));
    //		1.4	需要保存上一次请求的展示条目数
    		this.setRows(req.getParameter("rows"));
    //		1.5  初始化请求的页码	page
    		this.setPage(req.getParameter("page"));
    	}
    	
    	public void setPage(String page) {
    		if(StringUtils.isNotBlank(page))
    			this.setPage(Integer.valueOf(page));
    	}
    
    	public void setRows(String rows) {
    		if(StringUtils.isNotBlank(rows))
    			this.setRows(Integer.valueOf(rows));
    	}
    
    	public void setPagination(String pagination) {
    //		只有在前台jsp填写了pagination=false,才代表不分页
    		if(StringUtils.isNotBlank(pagination))
    			this.setPagination(!"false".equals(pagination));
    	}
    
    
    	public String getUrl() {
    		return url;
    	}
    
    	public void setUrl(String url) {
    		this.url = url;
    	}
    
    	public Map<String, String[]> getParamMap() {
    		return paramMap;
    	}
    
    	public void setParamMap(Map<String, String[]> paramMap) {
    		this.paramMap = paramMap;
    	}
    
    
    
    	public PageBean() {
    		super();
    	}
    
    	public int getPage() {
    		return page;
    	}
    
    	public void setPage(int page) {
    		this.page = page;
    	}
    
    	public int getRows() {
    		return rows;
    	}
    
    	public void setRows(int rows) {
    		this.rows = rows;
    	}
    
    	public int getTotal() {
    		return total;
    	}
    
    	public void setTotal(int total) {
    		this.total = total;
    	}
    
    	public void setTotal(String total) {
    		this.total = Integer.parseInt(total);
    	}
    
    	public boolean isPagination() {
    		return pagination;
    	}
    
    	public void setPagination(boolean pagination) {
    		this.pagination = pagination;
    	}
    
    	/**
    	 * 获得起始记录的下标
    	 * 
    	 * @return
    	 */
    	public int getStartIndex() {
    		return (this.page - 1) * this.rows;
    	}
    	
    	/**
    	 * 最大页
    	 * @return
    	 */
    	public int maxPage() {
    //		total % rows == 0 ? total / rows : total / rows +1
    		return this.total % this.rows == 0 ? this.total / this.rows : this.total / this.rows + 1;
    	}
    	
    	/**
    	 * 下一页
    	 * @return
    	 */
    	public int nextPage() {
    //		如果当前页小于最大页,那就下一页为当前页+1;如果不小于,说明当前页就是最大页,那就无需+1
    		return this.page < this.maxPage() ? this.page + 1 : this.page;
    	}
    	
    	/**
    	 * 上一页
    	 * @return
    	 */
    	public int previousPage() {
    		return this.page > 1 ? this.page - 1 : this.page;
    	}
    
    	@Override
    	public String toString() {
    		return "PageBean [page=" + page + ", rows=" + rows + ", total=" + total + ", pagination=" + pagination + "]";
    	}
    
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
    • 117
    • 118
    • 119
    • 120
    • 121
    • 122
    • 123
    • 124
    • 125
    • 126
    • 127
    • 128
    • 129
    • 130
    • 131
    • 132
    • 133
    • 134
    • 135
    • 136
    • 137
    • 138
    • 139
    • 140
    • 141
    • 142
    • 143
    • 144
    • 145
    • 146
    • 147
    • 148
    • 149
    • 150
    • 151
    • 152
    • 153
    • 154
    • 155
    • 156
    • 157
    • 158
    • 159

    9、StringUtils

    package com.xnx.spboot04.util;
    
    public class StringUtils {
    	// 私有的构造方法,保护此类不能在外部实例化
    	private StringUtils() {
    	}
    
    	/**
    	 * 如果字符串等于null或去空格后等于"",则返回true,否则返回false
    	 * 
    	 * @param s
    	 * @return
    	 */
    	public static boolean isBlank(String s) {
    		boolean b = false;
    		if (null == s || s.trim().equals("")) {
    			b = true;
    		}
    		return b;
    	}
    	
    	/**
    	 * 如果字符串不等于null或去空格后不等于"",则返回true,否则返回false
    	 * 
    	 * @param s
    	 * @return
    	 */
    	public static boolean isNotBlank(String s) {
    		return !isBlank(s);
    	}
    
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33

    10、ClazzMapper.xml

    
    DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
    <mapper namespace="com.xnx.spboot04.mapper.ClazzMapper" >
      <resultMap id="BaseResultMap" type="com.xnx.spboot04.model.Clazz" >
        <constructor >
          <idArg column="cid" jdbcType="INTEGER" javaType="java.lang.Integer" />
          <arg column="cname" jdbcType="VARCHAR" javaType="java.lang.String" />
          <arg column="cteacher" jdbcType="VARCHAR" javaType="java.lang.String" />
          <arg column="pic" jdbcType="VARCHAR" javaType="java.lang.String" />
        constructor>
      resultMap>
      <sql id="Base_Column_List" >
        cid, cname, cteacher, pic
      sql>
      <select id="selectByPrimaryKey" resultMap="BaseResultMap" parameterType="java.lang.Integer" >
        select 
        <include refid="Base_Column_List" />
        from t_struts_class
        where cid = #{cid,jdbcType=INTEGER}
      select>
    
      <select id="listMapPager" resultType="java.util.Map" parameterType="Clazz" >
        select
        <include refid="Base_Column_List" />
        from t_struts_class
        <where>
          <if test="cname != null and cname != ''">
             and cname like CONCAT('%',#{cname},'%')
          if>
          <if test="cid != null and cid != ''">
            and cid = #{cid}
          if>
        where>
      select>
    
      <select id="listPager" resultType="com.zking.ssm.model.Clazz" parameterType="Clazz" >
        select
        <include refid="Base_Column_List" />
        from t_struts_class
        <where>
          <if test="cname != null and cname != ''">
            and cname like CONCAT('%',#{cname},'%')
          if>
          <if test="cid != null and cid != ''">
            and cid = #{cid}
          if>
        where>
      select>
    
      <delete id="deleteByPrimaryKey" parameterType="java.lang.Integer" >
        delete from t_struts_class
        where cid = #{cid,jdbcType=INTEGER}
      delete>
      <insert id="insert" parameterType="com.zking.ssm.model.Clazz" >
        insert into t_struts_class (cid, cname, cteacher, 
          pic)
        values (#{cid,jdbcType=INTEGER}, #{cname,jdbcType=VARCHAR}, #{cteacher,jdbcType=VARCHAR}, 
          #{pic,jdbcType=VARCHAR})
      insert>
      <insert id="insertSelective" parameterType="Clazz" >
        insert into t_struts_class
        <trim prefix="(" suffix=")" suffixOverrides="," >
          <if test="cid != null" >
            cid,
          if>
          <if test="cname != null" >
            cname,
          if>
          <if test="cteacher != null" >
            cteacher,
          if>
          <if test="pic != null" >
            pic,
          if>
        trim>
        <trim prefix="values (" suffix=")" suffixOverrides="," >
          <if test="cid != null" >
            #{cid,jdbcType=INTEGER},
          if>
          <if test="cname != null" >
            #{cname,jdbcType=VARCHAR},
          if>
          <if test="cteacher != null" >
            #{cteacher,jdbcType=VARCHAR},
          if>
          <if test="pic != null" >
            #{pic,jdbcType=VARCHAR},
          if>
        trim>
      insert>
      <update id="updateByPrimaryKeySelective" parameterType="Clazz" >
        update t_struts_class
        <set >
          <if test="cname != null" >
            cname = #{cname,jdbcType=VARCHAR},
          if>
          <if test="cteacher != null" >
            cteacher = #{cteacher,jdbcType=VARCHAR},
          if>
          <if test="pic != null" >
            pic = #{pic,jdbcType=VARCHAR},
          if>
        set>
        where cid = #{cid,jdbcType=INTEGER}
      update>
      <update id="updateByPrimaryKey" parameterType="Clazz" >
        update t_struts_class
        set cname = #{cname,jdbcType=VARCHAR},
          cteacher = #{cteacher,jdbcType=VARCHAR},
          pic = #{pic,jdbcType=VARCHAR}
        where cid = #{cid,jdbcType=INTEGER}
      update>
    mapper>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113

    11、clzEdit.ftl

    
    "en">
    
        "Content-Type" content="text/html; charset=UTF-8">
        班级的编辑界面
    
    
    <#include '/head.ftl'>
    <#if b??>
        <#--修改-->
        
    "${ctx }/clz/edit" method="post"> cid:"text" name="cid" value="${b.cid !}">
    cname:"text" name="cname" value="${b.cname !}">
    cteacher:"text" name="cteacher" value="${b.cteacher !}">
    "submit">
    <#else> <#--新增-->
    "${ctx }/clz/add" method="post"> cid:"text" name="cid" value="">
    cname:"text" name="cname" value="">
    cteacher:"text" name="cteacher" value="">
    "submit">
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27

    12、clzList.ftl

    
    
    
        
        
        
        班级列表
        
    
    
    <#include '/head.ftl'>
    <#--查询条件框-->
    
    新增
    <#--数据展示列表--> <#list lst as b>
    ID 班级名称 指导老师 操作
    ${b.cid !} ${b.cname !} ${b.cteacher !} 修改 删除
    $
    {pagestr !}
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69

    在这里插入图片描述
    在这里插入图片描述
    在这里插入图片描述

  • 相关阅读:
    InnoDB行格式(4)行溢出和溢出页
    DELPHI仿花生壳(外网穿透内网)
    python学习笔记之word文档提取
    C++字符串大小写转换
    Problem B: 排序二叉树
    Linux操作文档——Kubeadm部署k8s集群
    English语法_指示代词 this / these / that / those
    第三章 运算符与标识符与关键字
    红队专题-Cobalt strike从小白到飞升手册
    四书五经 中庸
  • 原文地址:https://blog.csdn.net/weixin_67677668/article/details/127640606