hjr-JAVA SpringMVC+Mybatis-generate 模型設計

控制器

接收參數可以用

 public WarpJson fun(@RequestBody ModelVo modelVo,@RequestParam("id") String projectId,Integer page, Integer size){
   ModelDTO modelDTO = mapper.query();
 }

此處 @RequestBody 和 @RequestParam 可以共存

@RequestBody 用postman測試需要 進入 body -> raw 然後輸入json,同時選擇JSON(application/json)

@RequestParam 直接輸入 key value形式的參數即可

ModelVo

此model 是前端傳遞複雜數據的模型,也可以使用model,但是那樣一般需要在model中加入分頁信息等參數,有時不需要完整的model時使用單獨的Vo具有更好的可維護性

WarpJson

此類爲返回值的包裝類,一般封裝了,返回編碼,錯誤與否,錯誤信息等

Integer page, Integer size

所有分頁插件都要傳遞的頁數 和 每頁顯示條數

一般後臺需要查詢兩次,一次分頁list,一次count全部數據條數

ModelDTO

一般繼承了基本model,可以拓展一些屬性,如page,size,查詢條件等

可以取代mybatis的resultMap

可以避免最後使用map獲取取到的返回結果

xxxExtMapper.xml 和 xxxExtMapper.class

爲mapper的拓展類,和拓展xml

因爲用mybatis-generate自動生成mybatis相關文件後會覆蓋已有文件

所以xxxExtMapper.class 繼承原有mapper.class,並在裏面寫相關拓展代碼

xxxExtMapper.xml 只需要把命名空間改爲xxxExtMapper.class

然後在服務層 注入Mapper的地方改爲注入xxxExtMapper即可

mybatis-generate

然後統一使用該工具生成dao mapper等文件

配置

<?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>
    <classPathEntry location="C:/Users/hjr/.m2/repository\mysql/mysql-connector-java/5.1.34/mysql-connector-java-5.1.34.jar" />//此處需要修改爲自己jar包路徑
    <context id="mysqlTables" targetRuntime="MyBatis3">
        <commentGenerator>
            <property name="suppressAllComments" value="true"/>
        </commentGenerator>

        <jdbcConnection driverClass="com.mysql.jdbc.Driver"
                        connectionURL="jdbc:mysql://"
                        userId="" password="">//此處爲數據庫地址賬號密碼
        </jdbcConnection>
        <javaTypeResolver>
            <property name="forceBigDecimals" value="false"/>
        </javaTypeResolver>

        <javaModelGenerator targetPackage="com.xxx.xxx" targetProject="src/main/java">//此處修改爲想要生成model的包路徑
            <property name="enableSubPackages" value="true"/>
            <property name="trimStrings" value="true"/>
        </javaModelGenerator>

        <sqlMapGenerator targetPackage="com.xxx.xxx" targetProject="src/main/java">//此處修改爲想要生成mapper的包路徑
            <property name="enableSubPackages" value="true"/>
        </sqlMapGenerator>

        <javaClientGenerator type="XMLMAPPER" targetPackage="com.xxx.xxx"//此處修改爲想要生成mapper的包路徑 targetProject="src/main/java">
            <property name="enableSubPackages" value="true"/>
        </javaClientGenerator>

//此處修改爲 表名和model
        <table  tableName="table_name" domainObjectName="TableName" >
            <generatedKey column="id" sqlStatement="MySql" identity="true"/>
        </table>
    </context>

</generatorConfiguration>



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章