MyBatis Generator is a code generation tool officially provided by MyBatis, which can help developers quickly generate Java Beans, Mapper interfaces and XML mapping files that conform to the database table structure. In the process of using MyBatis Generator for code generation, the setting of configuration parameters is crucial. This article will delve into the best practices of MyBatis Generator from the perspective of configuration parameters and provide readers with specific code examples.
Before using MyBatis Generator for code generation, you need to write a configuration file named generatorConfig.xml
to guide the generation of code. Behavior. The following is a simple configuration file example:
<?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="DB2Tables" targetRuntime="MyBatis3"> <!-- 数据库连接信息 --> <jdbcConnection driverClass="com.mysql.cj.jdbc.Driver" connectionURL="jdbc:mysql://localhost:3306/test" userId="root" password="123456"> </jdbcConnection> <!-- 实体类、Mapper接口、XML映射文件生成路径 --> <javaModelGenerator targetPackage="com.example.model" targetProject="src/main/java"> </javaModelGenerator> <sqlMapGenerator targetPackage="mapper" targetProject="src/main/resources"> </sqlMapGenerator> <!-- Mapper接口的生成 --> <javaClientGenerator type="XMLMAPPER" targetPackage="com.example.mapper" targetProject="src/main/java"> </javaClientGenerator> <!-- 数据库表及生成的代码配置 --> <table tableName="user" domainObjectName="User" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"> </table> </context> </generatorConfiguration>
In the above configuration file, we define the database connection information, generated entity class, Mapper interface and XML mapping file path, and set the database table to generate code and related configurations.
In the configuration file, there are some key configuration parameters that require our special attention. They play a decisive role in the effect and quality of the generated code. Let’s parse these parameters one by one:
jdbcConnection
jdbcConnection
node is used to configure database connection information, including database driver class, connection URL, username and password, etc. In actual applications, you need to make corresponding modifications according to your own database configuration.
javaModelGenerator
javaModelGenerator
The node is used to configure the package name and storage path of the generated entity class (Java Bean). By setting the targetPackage
and targetProject
parameters, we can specify the generation path of the entity class.
sqlMapGenerator
sqlMapGenerator
node is used to configure the package name and storage path of the generated XML mapping file. Similarly, we can specify the generation path of the XML mapping file by setting the targetPackage
and targetProject
parameters.
javaClientGenerator
javaClientGenerator
node is used to configure the package name and storage path of the generated Mapper interface. By setting the type
parameter to XMLMAPPER
, you can specify to generate a Mapper interface based on XML configuration.
table
table
The node is used to configure the database table information for generating code, including table name, generated entity class name, Whether to enable specific query methods, etc. You can control the behavior of generated code by setting different properties.
When using MyBatis Generator to generate code, we can follow the following best practices:
Try to avoid adding too many configurations to the configuration file, and you can flexibly adjust the scope and content of the generated code according to project needs. At the same time, configuration files with clear comments and simple structure help code maintenance and management.
In the table
node, you can choose whether to enable certain query methods according to specific needs to reduce the redundancy of generation code. For example, if you do not need to use the selectByExample
method, you can disable it by setting enableSelectByExample="false"
.
In addition to the default generation rules, we can also write custom plug-ins to extend the functionality of MyBatis Generator. By writing plug-ins, you can achieve a more flexible code generation strategy that better meets project needs.
The following is a complete example showing how to use MyBatis Generator to generate a simple User entity class and the corresponding Mapper interface and XML mapping file:
public class User { private Long id; private String username; private String password; // Getters and setters }
public interface UserMapper { int insert(User record); int deleteByPrimaryKey(Long id); int updateByPrimaryKey(User record); User selectByPrimaryKey(Long id); }
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <mapper namespace="com.example.mapper.UserMapper"> <resultMap id="BaseResultMap" type="User"> <id column="id" property="id" /> <result column="username" property="username" /> <result column="password" property="password" /> </resultMap> <insert id="insert" parameterType="User"> INSERT INTO user (id, username, password) VALUES (#{id}, #{username}, #{password}) </insert> <!-- 其他SQL语句 --> </mapper>
Through the introduction of this article, readers should have a deeper understanding of the configuration parameters of MyBatis Generator and understand the best practices. In actual projects, by setting configuration parameters appropriately and using custom plug-ins flexibly, the MyBatis Generator tool can be used more efficiently to generate code that meets project requirements. I hope this article will be helpful to readers when using MyBatis Generator.
The above is the detailed content of MyBatis Generator configuration parameter interpretation and best practices. For more information, please follow other related articles on the PHP Chinese website!