Home  >  Article  >  Java  >  MyBatis Generator configuration details and usage guide

MyBatis Generator configuration details and usage guide

PHPz
PHPzOriginal
2024-02-23 08:51:03903browse

MyBatis Generator配置详解与使用指南

MyBatis Generator is a powerful code generation tool that can help developers automatically generate Java Beans, Mapper interfaces and XML files corresponding to database tables. This article will introduce in detail how to configure and use MyBatis Generator, and provide specific code examples to help readers quickly get started with the tool.

1. Configure MyBatis Generator

  1. Add MyBatis Generator dependency in the project’s pom.xml file:

    <dependency>
     <groupId>org.mybatis.generator</groupId>
     <artifactId>mybatis-generator-core</artifactId>
     <version>1.4.0</version>
    </dependency>
  2. Create MyBatis Generator's configuration file (generatorConfig.xml), configure generation rules, database connection information, etc.:

    <?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="MyBatisGenerator" targetRuntime="MyBatis3">
         <commentGenerator>
             <property name="suppressDate" value="true"/>
             <property name="suppressAllComments" value="true"/>
         </commentGenerator>
    
         <jdbcConnection driverClass="com.mysql.jdbc.Driver"
                         connectionURL="jdbc:mysql://localhost:3306/test"
                         userId="root"
                         password="password"/>
         
         <javaModelGenerator targetPackage="com.example.model"
                             targetProject="src/main/java"/>
    
         <sqlMapGenerator targetPackage="mapper"
                           targetProject="src/main/resources"/>
    
         <javaClientGenerator type="XMLMAPPER"
                               targetPackage="com.example.mapper"
                               targetProject="src/main/java"/>
    
         <table tableName="user" domainObjectName="User"/>
    
     </context>
    </generatorConfiguration>
  3. Configure the Maven plug-in and execute MyBatis Generator:

    <plugin>
     <groupId>org.mybatis.generator</groupId>
     <artifactId>mybatis-generator-maven-plugin</artifactId>
     <version>1.4.0</version>
     <configuration>
         <configurationFile>src/main/resources/generatorConfig.xml</configurationFile>
         <overwrite>true</overwrite>
         <verbose>true</verbose>
     </configuration>
    </plugin>

2. Use MyBatis Generator

  1. to run the Maven plug-in to generate code:
    Execute the following command in the project root directory:

    mvn mybatis-generator:generate
  2. Automatically The generated file structure is as follows:
  3. src/main/java/com/example/model/User.java
  4. src/main/resources/mapper/UserMapper.xml
  5. src/main/java/com/example/mapper/UserMapper.java
  6. Use the generated Mapper interface:

    // 自动注入生成的Mapper接口
    @Autowired
    private UserMapper userMapper;
    
    // 调用Mapper接口方法
    User user = new User();
    user.setId(1);
    user.setName("Test");
    userMapper.insert(user);

Through the above configuration and usage method, Developers can quickly generate and use the Java Beans, Mapper interfaces and XML files corresponding to MyBatis, improving development efficiency and reducing duplication of work. I hope this article will help readers understand and use MyBatis Generator.

The above is the detailed content of MyBatis Generator configuration details and usage guide. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn