Mybatis-plus的代码生成器(逆向工程)以及解决mysql的Cannot resolve class or package ‘mysql‘


一、创建远程仓库 mp-generator

仓库地址:https://gitee.com/zhuang-rui-boy/mp-generator.git

二、创建springboot项目

1、从远程仓库拉取项目
2、创建模块

如果是web项目的话:

三、修改pom.xml并引入依赖

为了方便springboot项目的依赖版本的管理,将pom.xml文件进行修改

1、起初的pom.xml文件
<<?xml version="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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.6.6</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.zhuang.mall</groupId>
    <artifactId>mall-product</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>mall-product</name>
    <description>商城的商品服务</description>
    <properties>
        <java.version>1.8</java.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

删掉parent模块,增加以下的配置:

2、properties模块中添加<spring-boot.version>2.6.6</spring-boot.version>
<!--   <properties>里增加springboot的版本 -->
    <properties>
        <java.version>1.8</java.version>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <spring-boot.version>2.6.6</spring-boot.version>
    </properties>
3、 增加的dependencyManagement模块
    <!--   增加的<dependencyManagement>对springboot的所有依赖版本进行控制,实际上与<parent>作用是一样的,只是这种更加方便-->
    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-dependencies</artifactId>
                <version>${spring-boot.version}}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>
4、增加插件
    <!--   增加maven-compiler-plugin插件,防止对java程序的编译版本不是jdk1.8,可能变成jdk1.5版本-->
           <plugin>
               <groupId>org.apache.maven.plugins</groupId>
               <artifactId>maven-compiler-plugin</artifactId>
               <version>3.5.1</version>
               <configuration>
                   <source>1.8</source>
                   <target>1.8</target>
                   <encoding>UTF-8</encoding>
               </configuration>
           </plugin>
        
5、引入单元测试
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
            <scope>test</scope>
        </dependency>
5、修改后的pom.xml文件
<?xml version="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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
<!--    <parent>-->
<!--        <groupId>org.springframework.boot</groupId>-->
<!--        <artifactId>spring-boot-starter-parent</artifactId>-->
<!--        <version>2.6.6</version>-->
<!--        <relativePath/> &lt;!&ndash; lookup parent from repository &ndash;&gt;-->
<!--    </parent>-->
    <groupId>com.zhuang.mall</groupId>
    <artifactId>mall-product</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>mall-product</name>
    <description>商城的商品服务</description>
    <properties>
        <java.version>1.8</java.version>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <spring-boot.version>2.6.6</spring-boot.version>
    </properties>
    <dependencies>
    <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>
    
<!--    增加的<dependencyManagement>对springboot的所有依赖版本进行控制-->
    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-dependencies</artifactId>
                <version>${spring-boot.version}}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <build>
        <plugins>
                    <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.5.1</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                    <encoding>UTF-8</encoding>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

四、修改父项目

点击右侧的maven,可以看出没有父项目mp-generator,原因是没有pom.xml文件。

1、父项目添加pom.xml文件

首先复制子模块的pom.xml文件,粘贴到父项目中。

2、修改pom.xml文件

删除多余的模块、修改父项目的名称、打包方式以及引入子模块


<?xml version="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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.zhuang.mpgenerator</groupId>
    <artifactId>mp-generator</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>mp-generator</name>
    <description>商城的聚合服务</description>
    <!--    打包方式为pom-->
    <packaging>pom</packaging>

<!--    将子模块引入-->
    <modules>
        <module>mall-product</module>
    </modules>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.5.1</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                    <encoding>UTF-8</encoding>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

3、在maven中引入父项目

点击右侧maven中的"+"号,找到父项目mp-generator的配pom.xml置文件,并打开。

可以看出引进了父项目并定义为root,管理子模块。

五、添加renren-generator与renren-fast模块

进入gitee官网,搜索人人开源,找到人人开源/renren-generator人人开源/renren-fast

下载下来,复制粘贴到项目中。并在父项目的pom.xml文件中,添加module为renren-generator和renren-fast。

六、添加mybatis-plus依赖与mysql数据库驱动

1、在mall-product模块中的pom.xml文件中,添加依赖
        <!--  mybatis-plus-->
        <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus-boot-starter</artifactId>
            <version>3.2.0</version>
        </dependency>
        <!--    导入mysql驱动    -->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>8.0.25</version>
        </dependency>

2、在resource下新建application.yml文件
server:
  port: 1010

spring:
  datasource:
    username: root
    password: 1234
    url: jdbc:mysql://10.211.55.4:3306/gulimall_pms
    driver-class-name: com.mysql.cj.jdbc.Driver
  application:
    name: mall-product
    
mybatis-plus:
  mapper-locations: classpath*:/mapper/**/*.xml
  global-config:
    db-config:
      id-type: auto

七、添加依赖出错!!!

注意:本人一开始配置时driver-class-name: com.mysql.cj.jdbc.Driver爆红
提示Cannot resolve class or package 'mysql'

遇到上述问题的解决方法:
点击File-Project Structure,查看里面的Modulsmall-product的依赖,如下图

情形一:
如果里面没有mysql依赖,说明没有导入jar包,就尝试点击右侧mavenreload project,如果还是没有,就看下面Libraries里有没有mysql jar包。如果没有的话,就将mysql jar包maven仓库里导入到Libraries中。
如下图,我遇到的问题是Libraries里有mysql的jar包的

那么我们就在Modules的dependencies里点击下方的"+",选择Libraries里的mysql jar包。就好了。

情形二:
可能Modules里有mysql的jar包,它的scopeRuntime,那么需要将其改为Compile,就OK了。

八、更改renren-generator的配置文件

首先,打开renren-generator的application.properties文件,
添加需要代码生成的包和数据库中的表。

修改的地方为:

#修改1
mainPath=com.zhuang
#\u5305\u540D
#修改2
package=com.zhuang.mall
moduleName=product
#\u4F5C\u8005
#修改3
author=mrzhuang
#Email
#修改4
email=862627527@qq.com
#\u8868\u524D\u7F00(\u7C7B\u540D\u4E0D\u4F1A\u5305\u542B\u8868\u524D\u7F00)
#数据库中表的前缀
#修改5
tablePrefix=pms_

然后,打开renren-generatorapplication.yml文件
添加mysql的数据源配置,端口port为81。

server:
  port: 81

# mysql
spring:
  datasource:
    type: com.alibaba.druid.pool.DruidDataSource
    #MySQL配置
    driverClassName: com.mysql.cj.jdbc.Driver
    url: jdbc:mysql://10.211.55.4:3306/gulimall_pms?useUnicode=true&characterEncoding=UTF-8&useSSL=false&serverTimezone=Asia/Shanghai
    username: root
    password: 1234

注意:为了避免mall-product项目里shiro依赖没有引入的影响,两种方案:第一就是引入shiro的依赖包,第二种方法就是在renren-generator的src/main/resources/template/Controller.java.vm里,将import 的shiro删除,并将@RequiresPermissions注释掉。

九、新建mall-common的maven模块

新建的mall-common模块的里面需要的包都在renrne-fastsrc/main/java/io/renren/common下。

新建一个mall-commonmaven项目,并先将renren-fast中的src/main/java/io/renren/common/utils下的RPageUtilsQueryConstant类工具类复制粘贴到
mall-common的src/main/java/com/zhuang/common/utils下,然后按着爆红的提示,从renren-fast中copy相应的包和类。

十、运行renren-generator项目

1、访问http://localhost:81

可以看出,运行成功,并成功连接上了mysql数据库的表。

2、生成代码

将表全部选中,点击生成代码按钮。会生成一个名为renren的压缩包,进行解压,将其中的main文件夹复制粘贴到mall-product项目里。


粘贴后:

十一、测试

进入mall-product 的src/main/java/com/zhuang/mall/product/controller/CategoryController.java类下

浏览器上执行http://localhost:1010/product/category/list
结果如下:

{"msg":"success","code":200,"page":{"totalCount":0,"pageSize":10,"totalPage":0,"currPage":1,"list":[{"catId":1,"name":"图书、音像、电子书刊","parentCid":0,"catLevel":1,"showStatus":1,"sort":0,"icon":"WE","productUnit":"W","productCount":0},{"catId":2,"name":"手机","parentCid":0,"catLevel":1,"showStatus":1,"sort":2,"icon":null,"productUnit":null,"productCount":0},{"catId":3,"name":"家用电器","parentCid":0,"catLevel":1,"showStatus":1,"sort":3,"icon":null,"productUnit":null,"productCount":0},{"catId":4,"name":"数码","parentCid":0,"catLevel":1,"showStatus":1,"sort":4,"icon":null,"productUnit":null,"productCount":0},{"catId":5,"name":"家居家装","parentCid":0,"catLevel":1,"showStatus":1,"sort":5,"icon":null,"productUnit":null,"productCount":0},{"catId":6,"name":"电脑办公","parentCid":0,"catLevel":1,"showStatus":1,"sort":6,"icon":null,"productUnit":null,"productCount":0},{"catId":7,"name":"厨具","parentCid":0,"catLevel":1,"showStatus":1,"sort":7,"icon":null,"productUnit":null,"productCount":0},{"catId":8,"name":"个护化妆","parentCid":0,"catLevel":1,"showStatus":1,"sort":8,"icon":null,"productUnit":null,"productCount":0},{"catId":9,"name":"服饰内衣","parentCid":0,"catLevel":1,"showStatus":1,"sort":9,"icon":null,"productUnit":null,"productCount":0},{"catId":10,"name":"钟表","parentCid":0,"catLevel":1,"showStatus":0,"sort":0,"icon":null,"productUnit":null,"productCount":0},{"catId":11,"name":"鞋靴","parentCid":0,"catLevel":1,"showStatus":1,"sort":10,"icon":null,"productUnit":null,"productCount":0},{"catId":12,"name":"母婴","parentCid":0,"catLevel":1,"showStatus":1,"sort":11,"icon":null,"productUnit":null,"productCount":0},{"catId":13,"name":"礼品箱包","parentCid":0,"catLevel":1,"showStatus":1,"sort":12,"icon":null,"productUnit":null,"productCount":0},{"catId":14,"name":"食品饮料、保健食品","parentCid":0,"catLevel":1,"showStatus":1,"sort":13,"icon":null,"productUnit":null,"productCount":0},{"catId":15,"name":"珠宝","parentCid":0,"catLevel":1,"showStatus":1,"sort":14,"icon":null,"productUnit":null,"productCount":0},{"catId":16,"name":"汽车用品","parentCid":0,"catLevel":1,"showStatus":1,"sort":15,"icon":null,"productUnit":null,"productCount":0},{"catId":17,"name":"运动健康","parentCid":0,"catLevel":1,"showStatus":1,"sort":16,"icon":null,"productUnit":null,"productCount":0},{"catId":18,"name":"玩具乐器","parentCid":0,"catLevel":1,"showStatus":1,"sort":17,"icon":null,"productUnit":null,"productCount":0},{"catId":19,"name":"彩票、旅行、充值、票务","parentCid":0,"catLevel":1,"showStatus":1,"sort":18,"icon":null,"productUnit":null,"productCount":0},{"catId":20,"name":"生
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值