快速构建一个权限项目(二)

好各位小伙伴们我们接着上一篇文章来叙述完善我们的项目,希望大家能够喜欢:

在上一篇文章中我们已经把前面基本的配置都配置好了,下面就来教大家一个我们这个项目的核心类去搭建mybatis吧,

首先我们需要一个工具generate,这个工具我们在网站上也能搜索到,但是我们这里接下来也会直接教大家使用.

首先我们要导入这个工具,这里呢不能上传文件所以我将以代码的形式展示给你们:

 首先我们的工具是这样一个节点展示的,其中最重要的是这generator.xml文件,文件内容如下:

<?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="E:\code\permission\generator\mysql-connector-java-5.1.34.jar" /> <!-- 1 -->
    <context id="DB2Tables" targetRuntime="MyBatis3">
        <commentGenerator>
            <property name="suppressAllComments" value="true" />
        </commentGenerator>
        <!-- 数据库链接URL、用户名、密码 -->
        <jdbcConnection driverClass="com.mysql.jdbc.Driver" connectionURL="jdbc:mysql://localhost:3306/diction?characterEncoding=utf8" userId="root" password="root">  <!-- 2 -->
        </jdbcConnection>
        <javaTypeResolver>
            <property name="forceBigDecimals" value="false" />
        </javaTypeResolver>
        <!-- 生成模型的包名和位置 --> <!-- 3 -->
        <javaModelGenerator targetPackage="cn.oyc.entity" targetProject="E:\code\permission\generator\src">
            <property name="enableSubPackages" value="true" />
            <property name="trimStrings" value="true" />
        </javaModelGenerator>
        <!-- 生成的映射文件包名和位置 --> <!-- 4 -->
        <sqlMapGenerator targetPackage="cn.oyc.mapper" targetProject="E:\code\permission\generator\src">
            <property name="enableSubPackages" value="true" />
        </sqlMapGenerator>
        <!-- 生成DAO的包名和位置 --> <!-- 5 -->
        <javaClientGenerator type="XMLMAPPER" targetPackage="cn.oyc.dao" targetProject="E:\code\permission\generator\src">
            <property name="enableSubPackages" value="true" />
        </javaClientGenerator>
        <!-- 要生成那些表(更改tableName和domainObjectName就可以) --><!-- 6 -->
        <table tableName="sys_user" domainObjectName="SysUser" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false" />
        <table tableName="sys_dept" domainObjectName="SysDept" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false" />
        <table tableName="sys_acl" domainObjectName="SysAcl" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false" />
        <table tableName="sys_acl_module" domainObjectName="SysAclModule" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false" />
        <table tableName="sys_role" domainObjectName="SysRole" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false" />
        <table tableName="sys_role_acl" domainObjectName="SysRoleAcl" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false" />
        <table tableName="sys_role_user" domainObjectName="SysRoleUser" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false" />
        <table tableName="sys_log" domainObjectName="SysLog" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false" />
    </context>
</generatorConfiguration>

 其中我们一定得注意我们的jar包在什么位置,然后去把路径更改过来,jar包的话可以去https://mvnrepository.com/工厂直接搜索下载

把这xml文件中的内容改成你自己的然后在我们的控制台直接用cmd命令去执行他

扫描二维码关注公众号,回复: 8765107 查看本文章

直接执行命令java -jar mybatis-generator-core-1.3.2.jar -configfile generator.xml -overwrite

之后他就会跟着你xml中你所需要的数据帮你生成了

 生成之后就会在你的src文件夹下面显示,然后你复制到你的项目src中就可以了.

然后快速搭建我们的mybatis就完成了,接下来就是我们的json处理请求了,一般我们都会通过json数据来告诉我们返回的结果

这里我们就创建一个jsonDate类,代码如下:

package cn.oyc.common;

import lombok.Getter;
import lombok.Setter;

import java.util.HashMap;
import java.util.Map;

@Getter
@Setter
public class JsonData {
    private boolean ret;

    private String msg; //如果是msg告诉后端为啥出了错误

    private Object data;

    public JsonData(boolean ref){
        this.ret = ref;
    }

    public static JsonData success(Object object,String msg){
        JsonData jsonData = new JsonData(true);
        jsonData.data = object;
        jsonData.msg = msg;
        return jsonData;
    }

    public static JsonData success(Object object){
        return new JsonData(true);
    }

    public static JsonData fail(String msg){
        JsonData jsonData = new JsonData(false);
        jsonData.msg = msg;
        return jsonData;
    }

}

猜你喜欢

转载自www.cnblogs.com/Myoyc/p/12227658.html