这个主要是直接通过插件运行方式就好了,不需要自己写java启动类,一般的需求都能满足,但有一个地方不太好,就是如果需要自定义Mysql数据库类型和java类型的映射关系(比如mysql数据库里的是tinyint类型,默认会被转换为java里的Byte类型,但我想转换为Integer类型),就不太好处理了。我写了一个java代码方式生成的,如果没有这种需求可以直接看本文,如果需要可以参考下(链接地址:https://blog.csdn.net/lichuangcsdn/article/details/80873737)
1、首先在IDEA中创建好一个Spring boot maven工程,在pom.xml添加以下插件
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<!-- mybatis生成代码插件-->
<plugin>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-maven-plugin</artifactId>
<version>1.3.2</version>
<configuration>
<!--允许移动生成的文件-->
<verbose>true</verbose>
<!--允许覆盖生成的文件-->
<overwrite>true</overwrite>
</configuration>
</plugin>
</plugins>
</build>
2、在resource目录下新建generatorConfig.properties和generatorConfig.xml
2.1、其中generatorConfig.properties的内容如下:
jdbc.driverLocation=/Users/lichuang/.m2/repository/mysql/mysql-connector-java/5.1.44/mysql-connector-java-5.1.44.jar
jdbc.driverClass=com.mysql.jdbc.Driver
jdbc.connectionURL=jdbc:mysql://192.168.22.10:3306/test-demo?useUnicode=true&characterEncoding=utf8&useSSL=false
jdbc.userId=root
jdbc.password=123456
2.2、generatorConfig.xml的内容如下:
<?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>
<properties resource="generatorConfig.properties"></properties>
<classPathEntry location="${jdbc.driverLocation}"></classPathEntry>
<context id="default" targetRuntime="MyBatis3">
<commentGenerator>
<property name="suppressDate" value="true"></property>
<property name="suppressAllComments" value="true"></property>
</commentGenerator>
<jdbcConnection
driverClass="${jdbc.driverClass}"
connectionURL="${jdbc.connectionURL}"
userId="${jdbc.userId}"
password="${jdbc.password}">
</jdbcConnection>
<javaTypeResolver>
<property name="forceBigDecimals" value="false"></property>
</javaTypeResolver>
<javaModelGenerator targetPackage="com.example.entity" targetProject="src/main/java">
<property name="enableSubPackages" value="false"></property>
<property name="constructorBased" value="true"></property>
<property name="trimStrings" value="true"></property>
<property name="immutable" value="false"></property>
</javaModelGenerator>
<sqlMapGenerator targetPackage="mapper" targetProject="src/main/resources">
<property name="enableSubPackages" value="false"></property>
</sqlMapGenerator>
<!-- type取值 ANNOTATEDMAPPER:表示通过注解方式,无XML生成;XMLMAPPER:表示通过XML方式-->
<javaClientGenerator targetPackage="com.example.dao" targetProject="src/main/java" type="XMLMAPPER">
<property name="enableSubPackages" value="true"></property>
</javaClientGenerator>
<table tableName="shop">
<property name="constructorBased" value="false"/>
<generatedKey column="shop_id" sqlStatement="MySql" identity="true"></generatedKey>
</table>
</context>
</generatorConfiguration>
3、手动创建相关的包路径
4、直接在IDEA中右边的maven projects会有个Plugins,运行mybatis-generator:generate即可
如果运行报错了,可以在运行设置中的命令行添加-X参数,查看详细的错误日志信息