IDEA中mybatis-generator插件的使用
阅读原文时间:2021年04月21日阅读:1

 mybatis-generator插件可自动生成实体类和mapper还有xml配置文件。在IDEA中只需修改插件中的generatorConfig.xml文件,然后运行配置文件就可以得到说需要的类,接口,xml文件。

1.创建Maven(webapp)项目

2.在pom.xml中加入包,以及插件

mysql-connector的坐标

  1. <dependency>

  2. <groupId>mysql </groupId>

  3. <artifactId>mysql-connector-java </artifactId>

  4. <version>5.1.31 </version>

  5. </dependency>

插件:放在finalName标签下面,否则插件不会显示,需要自己配置。generatorConfig.xml配置文件的路径要加上。

  1. <plugins>

  2. <plugin>

  3. <groupId>org.mybatis.generator </groupId>

  4. <artifactId>mybatis-generator-maven-plugin </artifactId>

  5. <version>1.3.2 </version>

  6. <configuration>

  7. <configurationFile>src/main/resources/generatorConfig.xml </configurationFile>

  8. <verbose>true </verbose>

  9. <overwrite>true </overwrite>

  10. </configuration>

  11. <executions>

  12. <execution>

  13. <id>Generate MyBatis Artifacts </id>

  14. <goals>

  15. <goal>generate </goal>

  16. </goals>

  17. </execution>

  18. </executions>

  19. <dependencies>

  20. <dependency>

  21. <groupId>org.mybatis.generator </groupId>

  22. <artifactId>mybatis-generator-core </artifactId>

  23. <version>1.3.2 </version>

  24. </dependency>

  25. </dependencies>

  26. </plugin>

  27. </plugins>

如果在右边可以看到mybatis-generator,说明插件安装好了,紧接着添加及修改配置文件

3.添加generatorConfig.xml(插件对应的配置文件)test-jdbc.properties(数据库相关信息)

test-jdbc.properties

  1. jdbc.driver=com.mysql.jdbc.Driver

  2. jdbc.url=jdbc:mysql://localhost:3306/bookdb

  3. jdbc.username=root

  4. jdbc.password=123456

generatorConfig.xml

  1. <!DOCTYPE generatorConfiguration

  2. PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN"

  3. "http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd">

  4. <generatorConfiguration>

  5. <commentGenerator>

  6. </commentGenerator>

  7. <jdbcConnection driverClass="${jdbc.driver}"

  8. connectionURL= "${jdbc.url}" userId= "${jdbc.username}" password= "${jdbc.password}">

  9. </jdbcConnection>

  10. <!-- 默认false,把JDBC DECIMAL 和 NUMERIC 类型解析为 Integer true,把JDBC DECIMAL 和

  11. NUMERIC 类型解析为java.math.BigDecimal -->

  12. <javaTypeResolver>

  13. </javaTypeResolver>

  14. <javaModelGenerator targetPackage="com.test.pojo"

  15. targetProject= "src/main/java">

  16. </javaModelGenerator>

  17. <sqlMapGenerator targetPackage="com.test.dao"

  18. targetProject= "src/main/java">

  19. </sqlMapGenerator>

  20. <javaClientGenerator type="XMLMAPPER"

  21. targetPackage= "com.test.dao" targetProject= "src/main/java">

  22. </javaClientGenerator>

  23. <table tableName="studentno"

  24. domainObjectName= "StudentNo"

  25. enableCountByExample= "false"

  26. enableUpdateByExample= "false"

  27. enableDeleteByExample= "false"

  28. enableSelectByExample= "false"

  29. selectByExampleQueryId= "false"> </table>

  30. <table tableName="classno" domainObjectName="ClassNo"

  31. enableCountByExample= "false" enableUpdateByExample= "false"

  32. enableDeleteByExample= "false" enableSelectByExample= "false"

  33. selectByExampleQueryId= "false"> </table>

  34. </context>

  35. </generatorConfiguration>

只需将test-jdbc.properties文件改成自己数据库对应的位置,generatorConfig.xml更改下面四个位置

4.运行右侧Maven项目中的mybatis-generator

这样就说明自动生成成功了,然后可以加入mybatis包以及mybatis配置文件,在配置文件中注册所需要的Mapper对应的xml文件,就可以使用mybatis了。

手机扫一扫

移动阅读更方便

阿里云服务器
腾讯云服务器
七牛云服务器