springboot+mybatis 利用插件生成代码

首先在pom.xml 找到插件栏  plugins 

然后我们按照下面引入插件:

<?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.5.5</version>        <relativePath/> <!-- lookup parent from repository -->    </parent>    <groupId>com.bihu.study</groupId>    <artifactId>demo</artifactId>    <version>0.0.1-SNAPSHOT</version>    <name>demo</name>    <description>HGYstudySpringBoot</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><!--        不需要版本号 因为是用的是SpringBoot自带的插件 parent中可以看到版本号!-->        <dependency>            <groupId>org.springframework.boot</groupId>            <artifactId>spring-boot-devtools</artifactId>        </dependency><!--    添加mySql    -->        <dependency>            <groupId>mysql</groupId>            <artifactId>mysql-connector-java</artifactId>            <version>5.1.6</version>        </dependency>        <!--    添加myBatis    -->        <dependency>            <groupId>org.mybatis.spring.boot</groupId>            <artifactId>mybatis-spring-boot-starter</artifactId>            <version>2.1.3</version>        </dependency>    </dependencies>    <build>        <plugins>            <plugin>                <groupId>org.springframework.boot</groupId>                <artifactId>spring-boot-maven-plugin</artifactId>            </plugin>            <!-- 引入mybatis generator 插件 -->            <plugin>                <groupId>org.mybatis.generator</groupId>                <artifactId>mybatis-generator-maven-plugin</artifactId>                <version>1.4.0</version>                                <configuration>                    <!--配置文件位置[待会要新建]-->                    <configurationFile>src/main/resources/generator-config.xml</configurationFile>                    <overwrite>true</overwrite>                    <verbose>true</verbose>                </configuration>                <dependencies>                    <dependency>                        <groupId>mysql</groupId>                        <artifactId>mysql-connector-java</artifactId>                        <version>5.1.6</version>                    </dependency>                </dependencies>            </plugin>        </plugins>    </build></project>

pow.xml

自己找里面的 plugins 中的 第二个  plugin  按照里面代码配:

 

 

 

然后创建对应的配置文件 文件里面写:

 

<?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="Mysql" targetRuntime="MyBatis3" defaultModelType="flat">        <!-- 自动检查关键字,为关键字增加反引号 -->        <property name="autoDelimitKeywords" value="true"/>        <property name="beginningDelimiter" value="`"/>        <property name="endingDelimiter" value="`"/>        <!--覆盖生成XML文件-->        <plugin type="org.mybatis.generator.plugins.UnmergeableXmlMappersPlugin" />        <!-- 生成的实体类添加toString()方法 -->        <plugin type="org.mybatis.generator.plugins.ToStringPlugin"/>        <!-- 不生成注释 -->        <commentGenerator>            <property name="suppressAllComments" value="true"/>        </commentGenerator>        <jdbcConnection driverClass="com.mysql.jdbc.Driver"                        connectionURL="jdbc:mysql://localhost:3306/sb?useUnicode=true&amp;characterEncoding=utf-8"                        userId="root"                        password="root">        </jdbcConnection>        <!-- domain类的位置 -->        <javaModelGenerator targetProject="src\main\java"                            targetPackage="com.bihu.study.Bean"/>        <!-- mapper xml的位置 -->        <sqlMapGenerator targetProject="src\main\resources"                         targetPackage="mapper"/>        <!-- mapper类的位置 -->        <javaClientGenerator targetProject="src\main\java"                             targetPackage="com.bihu.study.Mapper"                             type="XMLMAPPER"/>        <!--<table tableName="demo" domainObjectName="Demo"/>-->        <!--<table tableName="ebook"/>-->        <!--<table tableName="category"/>-->        <!--<table tableName="doc"/>-->        <!--<table tableName="content"/>-->        <!--<table tableName="user"/>-->        <table tableName="ebook_snapshot"/>    </context></generatorConfiguration>

  generator-config.xml

 

你要改 就跟着注释改即可。

 

然后你配置运行命令,添加maven 的 然后输入:

 

 

然后你运行 一下命令 就会生成对应代码 最后你创建 接口 、Service 、 contorller 测试即可。