mybatis automatically generates Mapper files and xml files

1. Introduction

mybatis-generator is a mybatis automatic code generation tool that can quickly generate mapper and xml files through configuration.

2. Configuration method

Add the plugin configuration in the project's pom file

<plugin>
    <groupId>org.mybatis.generator</groupId>
    <artifactId>mybatis-generator-maven-plugin</artifactId>
    <version>1.3.2</version>
    <configuration>
    <verbose>true</verbose>
    <overwrite>true</overwrite>
    </configuration>
</plugin>
在main的resource目录下创建generatorConfig.xml文件

The content in the configuration file is as follows, which can be modified as needed

<?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>
    <!--Import property configuration-->
    <properties resource="datasource.properties"></properties>
 
    <!-- Specify the database-driven jdbc driver jar package Location -->
    <classPathEntry location="${db.driverLocation}" />
 
    <!-- context is the main configuration information for reverse engineering -->
    <!-- id: give a name -->
    <!-- targetRuntime : set the generated file for that mybatis version -->
    <context id="default" targetRuntime="MyBatis3">
 
        <!--optional, designed to control comments when creating a class -->
        <commentGenerator>
            <property name="suppressDate" value="true" />
            <!-- whether to remove automatically generated comments true: yes: false: no -->
            <property name="suppressAllComments" value="true" />
        </ commentGenerator>
 
        <!--jdbc's database connection-->
        <jdbcConnection driverClass="${db.driverClassName}"
                        connectionURL="${db.url}"
                        userId="${db.username}"
                        password="${ db.password}">
        </jdbcConnection>
 
        <!--optional, type handler, conversion control between database types and java types-->
        <javaTypeResolver>
            <!-- By default, the decimal in the database, bigInt in Java corresponds to the BigDecimal class under sql -->
            <!-- Not double and long types -->
            <!-- Use common basic types instead of reference types under sql package-->
            <property name="forceBigDecimals" value="false" />
        </javaTypeResolver>
 
        <!-- targetPackage: the package where the generated entity class is located -->
        <!-- targetProject: The hard disk location where the generated entity class is located -->
        <javaModelGenerator targetPackage="com.mall.pojo"
                            targetProject=".\src\main\java">
            <!-- Whether to allow child Package -->
            <property name="enableSubPackages" value="false" />
            <!-- Whether to add a constructor to modal -->
            <property name="constructorBased" value="true" />
            <!-- No Clean up the blank characters on the left and right sides of the string queried from the database -->
            <property name="trimStrings" value="true" />
            <!-- To establish whether the modal object is immutable, the generated modal object will not have a setter method, only a construction method-->
            <property name="immutable" value="false" />
        </javaModelGenerator>
 
        <!-- targetPackage and targetProject: the package and location of the generated mapper file-->
        <sqlMapGenerator targetPackage="mappers"
                         targetProject=".\src\main\resource">
            <!-- A configuration for the database, whether to use schema as the name of the font package -->
            <property name="enableSubPackages" value="false" />
        </sqlMapGenerator>
 
        <!-- targetPackage and targetProject: the package and location of the generated interface file -->
        <javaClientGenerator type="XMLMAPPER"
                             targetPackage="com.mall.dao" targetProject=".\src\main\java">
            <!-- A configuration for the oracle database, whether to use schema as the subpackage name-->
            <property name="enableSubPackages" value="false" />
        </javaClientGenerator>
        <table tableName="user" domainObjectName="User" "
               enableCountByExample="false" enableUpdateByExample="false"
               enableDeleteByExample="false" enableSelectByExample="false"
               selectByExampleQueryId="false">
        </table>
    </context>
</generatorConfiguration>
Some configuration information in the generatorConfig.xml file needs to be add in config file

Create the datasource.properties file in the same directory

db.driverLocation=C:/Users/mysql/mysql-connector-java/5.1.6/mysql-connector-java-5.1.6.jar
db.driverClassName=com.mysql.jdbc.Driver
db.url=jdbc:mysql ://192.168.199.193:3306/mall?characterEncoding=utf-8
db.username=root
db.password=123456
The three lines of 192.168.199.193 in the configuration need to be replaced with the IP of the host where they are located, and mall with the database name

Configure the fourth and fifth lines to configure the username and password for the database connection respectively

After adding the configuration of the table to be generated in the generatorConfig.xml file

Double-click the configuration in the figure to automatically generate mapper and xml files

but! The above only exists under smooth conditions! ! ! Below I will talk about the pits I encountered and related solutions :)

Problem 1: Mybatis-generator cannot be found at all on the Maven Projects side and cannot be double-clicked

Solution: Open the pom file, create the plugins tag under the pluginManagement tag, and then move the mybatis-generator plugin configuration to the plugins tag, as shown in the figure

Question 2: An error is reported after double-clicking mybatis-generator

[ERROR] Error resolving version for plugin 'org.apache.maven.plugin:maven-compiler-plugin' from the repositories [local (C:\Users\.m2\repository), central (http://repo.maven.apache.org/maven2)]: Plugin not found in any plugin repository -> [Help 1]

Solution: add the version tag to the maven configuration in the pom file, as shown in the figure

Question 3: Error reporting

[ERROR] Failed to execute goal org.mybatis.generator:mybatis-generator-maven-plugin:1.3.2:generate (default-cli) on project mall: Execution default-cli of goal org.mybatis.generator:mybatis-generator-maven-plugin:1.3.2:generate failed: Cannot resolve classpath entry: C:Users.m2

Solution: Because of laziness, the db.driverLocation value in the datasource.properties file is copied and pasted directly in the folder, so the slashes of the path are all "\", just change it to "/", the above configuration The configuration content in the step is already correct

Question 4: This problem is the integration of the following errors

报错1:[ERROR] Failed to execute goal org.mybatis.generator:mybatis-generator-maven-plugin:1.3.2:generate (default-cli) on project mall: Access denied for user 'mall'@'219.143.190.211' (using password: YES) -> [Help 1]

报错2:[ERROR] Failed to execute goal org.mybatis.generator:mybatis-generator-maven-plugin:1.3.2:generate (default-cli) on project mall: null,  message from server: "Host 'zs-HP.lan' is not allowed to connect to this MySQL server" -> [Help 1]

报错3:[ERROR] Failed to execute goal org.mybatis.generator:mybatis-generator-maven-plugin:1.3.2:generate (default-cli) on project mall: Access denied for user 'root'@'%' to database 'mall' -> [Help 1]

报错4:[ERROR] Failed to execute goal org.mybatis.generator:mybatis-generator-maven-plugin:1.3.2:generate (default-cli) on project mall: SELECT command denied to user 'root'@'zs-HP.lan' for table 'cart' -> [Help 1]

报错5:[ERROR] Failed to execute goal org.mybatis.generator:mybatis-generator-maven-plugin:1.3.2:generate (default-cli) on project mall: Access denied for user 'root'@'zs-HP.lan' (using password: YES) -> [Help 1]

The above errors are basically caused by the same type of problems, as long as you follow the steps below, you can basically avoid them.

1. Find the my.ini file in the mysql bin directory, and add skip-grant-tables under [mysql] to skip the password

2, mysql open mysql remote access permission (cmd run as administrator)

Log in to mysql: mysql -uroot -ppwd
View user table:
mysql> use mysql
Database changed
mysql> select host,user,password,Grant_priv,Super_priv from user;
+-------------+ ---------+--------------------------------------+- -------------+------------+
| host | user | password | Grant_priv | Super_priv
+-------------- +---------+-------------------------------------- ----+------------+--------
| LocalHost | root | *FED29C14B2E900D70B11B1F1B370F953BA51| N | Y         
+------------ --+---------+-------------------------------------- -+------------+------------+
1 row in set (0.00 sec)
Change localhost to %. Modify it to % to indicate that all hosts can access the database through the root user.
       命令:mysql> update user set host = '%',Grant_priv='Y', Super_priv='Y'  where user = 'root';

       Look at the table again: mysql>use mysql

       Check if it has been modified

       finally! ! ! Enter the command: mysql> FLUSH PRIVILEGES; refresh the previously modified content

       This works! ! ! ! !

3. Modify database table permissions

If the mysql client (I am using Navicate) selects the right-click on the form machine to find the option to set permissions, then select the corresponding operation.

If the right button does not set the permission option, you need to add a new user, the configuration is as follows

Then double-click the management user to configure permissions

 

Completing the above steps can basically solve most of the errors reported. If there are other problems, I have not encountered them yet, please Baidu :)

Above, thank you~
—————————————————
Explanation: If only the insert statement is generated

https://www.jianshu.com/p/dbeeac29ff27

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324093563&siteId=291194637