mybatis-generator插件

转载:https://blog.csdn.net/viczking/article/details/80965296 

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

1.创建Maven(webapp)项目


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

mysql-connector的坐标

  1.  
    <!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
  2.  
    <dependency>
  3.  
    <groupId>mysql </groupId>
  4.  
    <artifactId>mysql-connector-java </artifactId>
  5.  
    <version>5.1.31 </version>
  6.  
    </dependency>
  7.  
     

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

  1.  
    <!--放在finalName标签的下面-->
  2.  
    <plugins>
  3.  
    <!--mybatis-generator插件-->
  4.  
    <plugin>
  5.  
    <!--Mybatis-generator插件,用于自动生成Mapper和POJO-->
  6.  
    <groupId>org.mybatis.generator </groupId>
  7.  
    <artifactId>mybatis-generator-maven-plugin </artifactId>
  8.  
    <version>1.3.2 </version>
  9.  
    <configuration>
  10.  
    <!--配置文件的位置 一定要改成配置文件的位置-->
  11.  
    <configurationFile>src/main/resources/generatorConfig.xml </configurationFile>
  12.  
    <verbose>true </verbose>
  13.  
    <overwrite>true </overwrite>
  14.  
    </configuration>
  15.  
    <executions>
  16.  
    <execution>
  17.  
    <id>Generate MyBatis Artifacts </id>
  18.  
    <goals>
  19.  
    <goal>generate </goal>
  20.  
    </goals>
  21.  
    </execution>
  22.  
    </executions>
  23.  
    <dependencies>
  24.  
    <dependency>
  25.  
    <groupId>org.mybatis.generator </groupId>
  26.  
    <artifactId>mybatis-generator-core </artifactId>
  27.  
    <version>1.3.2 </version>
  28.  
    </dependency>
  29.  
    </dependencies>
  30.  
    </plugin>
  31.  
    </plugins>

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

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

test-jdbc.properties

扫描二维码关注公众号,回复: 10162666 查看本文章
  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.  
    <?xml version="1.0" encoding="UTF-8"?>
  2.  
    <!DOCTYPE generatorConfiguration
  3.  
    PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN"
  4.  
    "http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd">
  5.  
    <generatorConfiguration>
  6.  
    <properties resource="test-jdbc.properties"/>
  7.  
    <!-- 数据库驱动 -->
  8.  
    <classPathEntry location="C:\Users\GaoHang\.m2\repository\mysql\mysql-connector-java\5.1.31\mysql-connector-java-5.1.31.jar" />
  9.  
    <context id="DB2Tables" targetRuntime="MyBatis3">
  10.  
    <commentGenerator>
  11.  
    <property name="suppressDate" value="true" />
  12.  
    <!-- 是否去除自动生成的注释 true:是 : false:否 -->
  13.  
    <property name="suppressAllComments" value="true" />
  14.  
    </commentGenerator>
  15.  
    <!--数据库链接URL,用户名、密码 -->
  16.  
    <jdbcConnection driverClass="${jdbc.driver}"
  17.  
    connectionURL= "${jdbc.url}" userId= "${jdbc.username}" password= "${jdbc.password}">
  18.  
    </jdbcConnection>
  19.  
    <!-- 默认false,把JDBC DECIMAL 和 NUMERIC 类型解析为 Integer true,把JDBC DECIMAL 和
  20.  
    NUMERIC 类型解析为java.math.BigDecimal -->
  21.  
    <javaTypeResolver>
  22.  
    <property name="forceBigDecimals" value="false" />
  23.  
    </javaTypeResolver>
  24.  
    <!-- 生成模型的包名和位置 -->
  25.  
    <javaModelGenerator targetPackage="com.test.pojo"
  26.  
    targetProject= "src/main/java">
  27.  
    <!-- enableSubPackages:是否让schema作为包的后缀 -->
  28.  
    <property name="enableSubPackages" value="true" />
  29.  
    <!-- 从数据库返回的值被清理前后的空格 -->
  30.  
    <property name="trimStrings" value="true" />
  31.  
    </javaModelGenerator>
  32.  
    <!-- 生成映射文件的包名和位置XML文件 -->
  33.  
    <sqlMapGenerator targetPackage="com.test.dao"
  34.  
    targetProject= "src/main/java">
  35.  
    <property name="enableSubPackages" value="true" />
  36.  
    </sqlMapGenerator>
  37.  
    <!-- 生成DAO的包名和位置 -->
  38.  
    <javaClientGenerator type="XMLMAPPER"
  39.  
    targetPackage= "com.test.dao" targetProject= "src/main/java">
  40.  
    <property name="enableSubPackages" value="true" />
  41.  
    </javaClientGenerator>
  42.  
    <!-- 要生成哪些表 -->
  43.  
    <!-- tableName:用于自动生成代码的数据库表;domainObjectName:对应于数据库表的javaBean类名 -->
  44.  
    <!--book_info数据库表明--> <!--别名Book_Info pojo(实体类明)-->
  45.  
    <table tableName="studentno"
  46.  
    domainObjectName= "StudentNo"
  47.  
    enableCountByExample= "false"
  48.  
    enableUpdateByExample= "false"
  49.  
    enableDeleteByExample= "false"
  50.  
    enableSelectByExample= "false"
  51.  
    selectByExampleQueryId= "false"> </table>
  52.  
    <table tableName="classno" domainObjectName="ClassNo"
  53.  
    enableCountByExample= "false" enableUpdateByExample= "false"
  54.  
    enableDeleteByExample= "false" enableSelectByExample= "false"
  55.  
    selectByExampleQueryId= "false"> </table>
  56.  
    </context>
  57.  
    </generatorConfiguration>

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

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

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

猜你喜欢

转载自www.cnblogs.com/lzmrex/p/12568666.html