首先肯定要有mybatis的依赖
<!--mybatis spring--> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>1.3.2</version> </dependency>
然后是要mybatis生成代码器核心依赖
<!--mybatis生成代码控件--> <dependency> <groupId>org.mybatis.generator</groupId> <artifactId>mybatis-generator-core</artifactId> <version>1.3.2</version> </dependency>
之后需要MySQL的依赖
<!--MySQL依赖--> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> </dependency>
之后需要在build>plugins下加入一个plugin
<!--mybatis generator plugin--> <plugin> <groupId>org.mybatis.generator</groupId> <artifactId>mybatis-generator-maven-plugin</artifactId> <version>1.3.2</version> <executions> <execution> <id>Generate MyBatis Files</id> <goals> <goal>generate</goal> </goals> <phase>generate-sources</phase> <configuration> <verbose>true</verbose> <overwrite>true</overwrite> </configuration> </execution> </executions> <dependencies> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.38</version> </dependency> <dependency> <groupId>org.mybatis.generator</groupId> <artifactId>mybatis-generator-core</artifactId> <version>1.3.5</version> </dependency> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>3.4.2</version> </dependency> </dependencies> </plugin> <!--/mybatis generator plugin-->
点击Apply。
<?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"> <!--避免生成重复代码的插件--> <plugin type="com.tansuo365.test1.util.OverIsMergeablePlugin" /> <!--是否在代码中显示注释--> <commentGenerator> <property name="suppressDate" value="true" /> <property name="suppressAllComments" value="true" /> </commentGenerator> <!--数据库链接地址账号密码--> <jdbcConnection driverClass="com.mysql.cj.jdbc.Driver" connectionURL="jdbc:mysql://localhost:3306/tansuodb" userId="root" password="xxxxxxxx"> </jdbcConnection> <!--不知道做什么用的。。。反正贴上来了~--> <javaTypeResolver> <property name="forceBigDecimals" value="false"/> </javaTypeResolver> <!--生成bean类存放位置--> <javaModelGenerator targetPackage="com.tansuo365.test1.bean" targetProject="src"> <property name="enableSubPackages" value="true"/> <property name="trimStrings" value="true"/> </javaModelGenerator> <!--生成xml映射文件存放位置--> <sqlMapGenerator targetPackage="classpath:mappings" targetProject="resources"> <property name="enableSubPackages" value="true"/> </sqlMapGenerator> <!--生成mapper类存放位置--> <javaClientGenerator type="XMLMAPPER" targetPackage="com.tansuo365.test1.mapper" targetProject="src"> <property name="enableSubPackages" value="true"/> </javaClientGenerator> <!--生成对应表及类名--> <table tableName="petroleum_coke_tbl" domainObjectName="PetroleumCoke" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="true" selectByExampleQueryId="false"> <property name="my.isgen.usekeys" value="true"/> <property name="useActualColumnNames" value="true"/> <generatedKey column="id" sqlStatement="JDBC"/> </table> <!-- <table tableName="role" domainObjectName="Role" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="true" selectByExampleQueryId="false"> <property name="my.isgen.usekeys" value="true"/> <property name="useActualColumnNames" value="true"/> <generatedKey column="id" sqlStatement="JDBC"/> </table> <table tableName="permission" domainObjectName="Permission" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="true" selectByExampleQueryId="false"> <property name="my.isgen.usekeys" value="true"/> <property name="useActualColumnNames" value="true"/> <generatedKey column="id" sqlStatement="JDBC"/> </table> <table tableName="user_role" domainObjectName="UserRole" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="true" selectByExampleQueryId="false"> <property name="my.isgen.usekeys" value="true"/> <property name="useActualColumnNames" value="true"/> <generatedKey column="id" sqlStatement="JDBC"/> </table> <table tableName="role_permission" domainObjectName="RolePermission" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="true" selectByExampleQueryId="false"> <property name="my.isgen.usekeys" value="true"/> <property name="useActualColumnNames" value="true"/> <generatedKey column="id" sqlStatement="JDBC"/> </table>--> </context> </generatorConfiguration>
具体也可以参考https://www.jianshu.com/p/310c299846fc