美文网首页
集成逆向工程-异常处理-阅读数功能-(6)

集成逆向工程-异常处理-阅读数功能-(6)

作者: 弹钢琴的崽崽 | 来源:发表于2020-03-08 08:14 被阅读0次

1. 集成Mybatis-Generator

1.1 导入依赖

<plugin>
    <groupId>org.mybatis.generator</groupId>
    <artifactId>mybatis-generator-maven-plugin</artifactId>
    <version>1.3.7</version>
    <dependencies>
        <dependency>
            <groupId>com.h2database</groupId>
            <artifactId>h2</artifactId>
            <version>1.4.199</version>
        </dependency>
    </dependencies>
</plugin>

1.2 建立配置文件

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE generatorConfiguration
        PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN"
        "http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd">

<generatorConfiguration>

    <context id="DB2Tables" targetRuntime="MyBatis3">

        <plugin type="org.mybatis.generator.plugins.RowBoundsPlugin"></plugin>
        <jdbcConnection driverClass="org.h2.Driver"
                        connectionURL="jdbc:h2:E:\Users\IdeaProjects\community"
                        userId="sa"
                        password="123">
        </jdbcConnection>

        <javaTypeResolver>
            <property name="forceBigDecimals" value="false"/>
        </javaTypeResolver>

        <javaModelGenerator targetPackage="life.guohui.community.model" targetProject="src\main\java">
            <property name="enableSubPackages" value="true"/>
            <property name="trimStrings" value="true"/>
        </javaModelGenerator>

        <sqlMapGenerator targetPackage="mapper" targetProject="src\main\resources">
            <property name="enableSubPackages" value="true"/>
        </sqlMapGenerator>

        <javaClientGenerator type="XMLMAPPER" targetPackage="life.guohui.community.mapper"
                             targetProject="src/main/java">
            <property name="enableSubPackages" value="true"/>
        </javaClientGenerator>
        <table tableName="user" domainObjectName="User"></table>
        <table tableName="question" domainObjectName="Question"></table>
    </context>
</generatorConfiguration>

1.3 扫描mapper文件添加注解

1.4 配置文件中扫描xml文件和mapper接口

# 扫描mapper.xml和mapper接口模型
mybatis.type-aliases-package=life.guohui.community.mapper
mybatis.mapper-locations=classpath:mapper/*.xml

1.5 逆向工程配置文件中引入分页插件

1.6 需要了解的操作

a. 修改方法

updateByExampleSelective

只修改参数1中已经赋值的字段,参数2是传入条件

b. 分页方法

selectByExampleWithRowbounds

public PaginationDTO list(Integer page, Integer size) {
    PaginationDTO paginationDTO = new PaginationDTO();
    Integer totalPage;
    //拿到总数
    Integer totalCount = (int)questionMapper.countByExample(new QuestionExample());
    if(totalCount % size == 0){totalPage = totalCount/size;}else{totalPage = totalCount/size + 1;}
    if(page<1){ page = 1;}
    if(page>totalPage){page = totalPage;}
    paginationDTO.setPagination(totalPage,page);
    Integer offset = size * (page - 1);
    List<Question> questions = questionMapper.selectByExampleWithRowbounds(new QuestionExample(), new RowBounds(offset, size));
    List<QuestionDTO> questionDTOList = new ArrayList<>();
    for(Question question : questions){
        User user = userMapper.selectByPrimaryKey(question.getCreator());
        QuestionDTO questionDTO = new QuestionDTO();
        BeanUtils.copyProperties(question,questionDTO);
        questionDTO.setUser(user);
        questionDTOList.add(questionDTO);
    }
    paginationDTO.setQuestions(questionDTOList);
    return paginationDTO;
}

2. 异常处理

2.1 添加error.html

<!DOCTYPE HTML>
<html xmlns:th="http://www.thymeleaf.org">
......
    <body>

        <div th:insert="~{navigation :: nav}"></div>
        <div class="jumbotron" style="min-height: 300px;">
            <div class="col-lg-6 col-md-12 col-sm-12 col-xs-12"><h1>出错啦!!!</h1>
                <p th:text="${message}">服务太热啦,要不然稍等下再来试试~</p>
                <p><a class="btn btn-primary btn-lg" href="/" role="button">回到主页</a></p>
            </div>
        </div>
    </body>
</html>

2.2 添加css

2.3 自定义异常

package life.guohui.community.exception;

public class CustomizeException extends RuntimeException {
    private String message;

    public CustomizeException(ICustomizeErrorCode errorCode) {
        this.message = errorCode.getMessage();
    }

    public CustomizeException(String message) {
        this.message = message;
    }

    @Override
    public String getMessage() {
        return message;
    }
}

2.4 监听器advice

package life.guohui.community.advice;
@ControllerAdvice
public class CustomizeExceptionHandler {

    ModelAndView handle(HttpServletRequest request, Throwable e, Model model){
        if(e instanceof CustomizeException){
            model.addAttribute("message",e.getMessage());
        }else{
            model.addAttribute("message","服务过热,稍后试试!!!");
        }

        return new ModelAndView("error");
    }
}

2.4 创建异常信息接口

2.5 创建枚举实现异常信息接口

package life.guohui.community.exception;
public enum  CustomizeErrorCode implements ICustomizeErrorCode{
    QUESTION_NOT_FOUND("你找的问题都不在了,要不要换一个试试?");
    private String message;

    CustomizeErrorCode(String message) {
        this.message = message;
    }

    @Override
    public String getMessage() {
        return message;
    }
}

2.6 创建Controller实现errorController

package life.guohui.community.controller;
@Controller
@RequestMapping("${server.error.path:${error.path:/error}}")
public class CustomizeErrorController implements ErrorController {
    @Override
    public String getErrorPath() {
        return "error";
    }
    @RequestMapping(produces = MediaType.TEXT_HTML_VALUE)
    public ModelAndView errorHtml(HttpServletRequest request, Model model) {
        HttpStatus status = getStatus(request);

        if (status.is4xxClientError()) {
            model.addAttribute("message", "你这个请求错了吧,要不然换个姿势?");
        }
        if (status.is5xxServerError()) {
            model.addAttribute("message", "服务冒烟了,要不然你稍后再试试!!!");
        }

        return new ModelAndView("error");
    }

    private HttpStatus getStatus(HttpServletRequest request) {
        Integer statusCode = (Integer) request
                .getAttribute("javax.servlet.error.status_code");
        if (statusCode == null) {
            return HttpStatus.INTERNAL_SERVER_ERROR;
        }
        try {
            return HttpStatus.valueOf(statusCode);
        } catch (Exception ex) {
            return HttpStatus.INTERNAL_SERVER_ERROR;
        }
    }
}

2.7 在QuestionService中使用

3. 阅读功能

3.1 点击文章标题后阅读数+1

Controller中增加阅读

3.2 Mapper.xml文件中增加方法

<update id="incView" parameterType="life.guohui.community.model.Question">
    update QUESTION
    set
    VIEW_COUNT = VIEW_COUNT + #{viewCount,jdbcType=INTEGER}
    WHERE id = #{id}
</update>

3.3 Service中使用

public void inView(Integer id) {
    Question question = new Question();
    question.setId(id);
    question.setViewCount(1);
    questionMapper.incView(question);
}

相关文章

  • 集成逆向工程-异常处理-阅读数功能-(6)

    1. 集成Mybatis-Generator 1.1 导入依赖 1.2 建立配置文件 1.3 扫描mapper文件...

  • mybats逆向工程中使用in数据过长抛出异常问题。

    当你使用逆向工程一次in的list过长。超过一千,就会抛出异常。 超级坑,发现异常之后让我修复。关键都是用逆向工程...

  • mybatis逆向工程配置及异常处理

    使用逆向工程自动生成实体类及mapper 配置 1.新增逆向工程的包 2.加入mybatis-generator自...

  • iOS逆向工程

    iOS逆向工程 什么是iOS逆向工程? iOS应用逆向工程,是指从目标应用的界面及功能表现入手,使用不同的工具和理...

  • Mybatis----(3)

    主要内容1、mybatis逆向工程 今天get到新技能Mybatis的逆向工程,这个功能很好用,跟大家分享一下。 ...

  • 9.14商学院-电吹风的反面,是吸尘器

    如何训练逆向思维?六种方式:结构逆向,功能逆向,状态逆向,原理逆向,序位逆向,方向逆向,通过以上6种方式,可以很好...

  • 第1章 关于逆向工程

    1. 逆向工程 逆向工程(Reverse Engineering,简称PE). 2. 代码逆向工程 代码逆向工程(...

  • spring-mybatis逆向工程文件配置

    使用Spring MVC + Spring + MyBatis完成crud功能 MyBatis 逆向工程使用 在 ...

  • ssm

    工程目录结构 mybatis逆向工程 逆向工程配置文件 generatorConfig.xml文件 逆向工程代码 ...

  • (一)了解逆向

    一、什么是逆向工程? 软件的逆向工程指的是通过分析一个程序或系统的功能、结构或行为,将它的技术实现或设计细节推导出...

网友评论

      本文标题:集成逆向工程-异常处理-阅读数功能-(6)

      本文链接:https://www.haomeiwen.com/subject/gnanrhtx.html