使用maven插件自动生成对应的实体类、mapper、xml文件
1 maven 依赖配置: 2 3 <!-- sql server --> 4 <dependency> 5 <groupId>com.microsoft.sqlserver</groupId> 6 <artifactId>mssql-jdbc</artifactId> 7 <version>6.1.0.jre8</version> 8 </dependency> 9 <!-- mysql --> 10 <dependency> 11 <groupId>mysql</groupId> 12 <artifactId>mysql-connector-java</artifactId> 13 <scope>runtime</scope> 14 </dependency> 15 16 <dependency> 17 <groupId>org.mybatis</groupId> 18 <artifactId>mybatis</artifactId> 19 <version>3.4.4</version> 20 </dependency> 21 22 <dependency> 23 <groupId>org.mybatis</groupId> 24 <artifactId>mybatis-spring</artifactId> 25 <version>1.3.1</version> 26 </dependency> 27 <dependency> 28 <groupId>org.mybatis.spring.boot</groupId> 29 <artifactId>mybatis-spring-boot-starter</artifactId> 30 <version>1.1.1</version> 31 </dependency> 32 33 maven 构建所需插件 34 <build> 35 <plugins> 36 <plugin> 37 <groupId>org.springframework.boot</groupId> 38 <artifactId>spring-boot-maven-plugin</artifactId> 39 <configuration> 40 <fork>true</fork> 41 </configuration> 42 </plugin> 43 <!-- mybatis generator 自动生成代码插件 --> 44 <plugin> 45 <groupId>org.mybatis.generator</groupId> 46 <artifactId>mybatis-generator-maven-plugin</artifactId> 47 <version>1.3.2</version> 48 <configuration> 49 <configurationFile>src/main/resources/generatorConfig.xml</configurationFile> 50 <overwrite>true</overwrite> 51 <verbose>true</verbose> 52 </configuration> 53 </plugin> 54 <plugin> 55 <groupId>org.apache.maven.plugins</groupId> 56 <artifactId>maven-compiler-plugin</artifactId> 57 <version>3.1</version> 58 </plugin> 59 <plugin> 60 <groupId>org.apache.maven.plugins</groupId> 61 <artifactId>maven-surefire-plugin</artifactId> 62 <version>2.18.1</version> 63 <configuration> 64 <skipTests>true</skipTests> 65 </configuration> 66 </plugin> 67 <plugin> 68 <groupId>org.apache.maven.plugins</groupId> 69 <artifactId>maven-clean-plugin</artifactId> 70 <version>2.6.1</version> 71 </plugin> 72 </plugins> 73 <finalName>springboot-test</finalName> 74 <!-- 指定生成文件存放的目录 --> 75 <resources> 76 <resource> 77 <directory>src/main/java</directory> 78 <includes> 79 <include>**/*.xml</include> 80 </includes> 81 <filtering>false</filtering> 82 </resource> 83 <resource> 84 <directory>src/main/resources</directory> 85 <filtering>true</filtering> 86 <includes> 87 <include>**/*.xml</include> 88 <include>application.properties</include> 89 <include>banner.txt</include> 90 <include>META-INF/app.properties</include> 91 <!--<include>application-${profileActive}.properties</include>--> 92 </includes> 93 <excludes> 94 <exclude>generatorConfig.xml</exclude> 95 </excludes> 96 </resource> 97 </resources> 98 </build>
generatorConfig.xml文件配置,该文件在resource目录下
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 <!-- 修改成自己本地的maven仓库地址,只需要替换红色部分即可(D:\maven\repository) --> 7 <classPathEntry location="D:\maven\repository\com\microsoft\sqlserver\mssql-jdbc\6.1.0.jre8\mssql-jdbc-6.1.0.jre8.jar" /> 8 <context defaultModelType="flat" id="write" targetRuntime="MyBatis3"> 9 <plugin type="org.mybatis.generator.plugins.ToStringPlugin"/> 10 <plugin type="org.mybatis.generator.plugins.SerializablePlugin" /> 11 <commentGenerator> 12 <property name="suppressAllComments" value="false"/> 13 <property name="suppressDate" value="false"/> 14 </commentGenerator> 15 <!-- 这里是sqlserver 数据库 --> 16 <jdbcConnection driverClass="com.microsoft.sqlserver.jdbc.SQLServerDriver" 17 connectionURL="jdbc:sqlserver://ip:1433;DatabaseName=test" 18 userId="sa" 19 password="123456"> 20 </jdbcConnection> 21 22 <javaTypeResolver> 23 <property name="trimStrings" value="true" /> 24 <property name="forceBigDecimals" value="false" /> 25 </javaTypeResolver> 26 27 <!--生成model 指定生成后类的存放目录--> 28 <javaModelGenerator targetPackage="com.example.model.po.base.mbg" 29 targetProject="${generator.path}/src/main/java"> 30 <property name="enableSubPackages" value="true"/> 31 </javaModelGenerator> 32 33 <!--生成xml文件 指定生成后的xml 文件存放的目录--> 34 <sqlMapGenerator targetPackage="com.example.dal.mappers.base.mbg" 35 targetProject="${generator.path}/src/main/java"> 36 <property name="enableSubPackages" value="true"/> 37 <property name="trimStrings" value="true"/> 38 </sqlMapGenerator> 39 40 <!--生成mappers 指定生成 后mappers 接口存放的目录--> 41 <javaClientGenerator targetPackage="com.example.dal.dao.base.mbg" 42 targetProject="${generator.path}/src/main/java" 43 type="XMLMAPPER"> 44 <property name="enableSubPackages" value="true"/> 45 <property name="exampleMethodVisibility" value="public"/> 46 <property name="methodNameCalculator" value="default"/> 47 </javaClientGenerator> 48 49 <!-- tableName:指的是你数据库中表的名称;domainObjectName:指的是生成实体类对应的名称 --> 50 <!-- enableSelectByExample="false" 、selectByExampleQueryId="false" 设置不生成 example --> 51 <table tableName="NP_Mng_RuleHistory" domainObjectName="MngRuleHistory" 52 enableCountByExample="false" 53 enableUpdateByExample="false" 54 enableDeleteByExample="false" 55 enableSelectByExample="false" 56 selectByExampleQueryId="false"> 57 <property name="useActualColumnNames" value="true" /> 58 <!-- column:指的是sql语句是否设置自动增长 --> 59 <generatedKey column="id" identity="true" sqlStatement="SqlServer"/> 60 </table> 61 62 </context> 63 </generatorConfiguration>