在平时的开发中会涉及到实体类、mapper接口以及mapper.xml文件的编写或者生成。为了我们更加高效的开发,这些工作我们只需要集成一些现有的生成器就可以简单的实现
生成实体model、mapper接口、mapper.xml
首先配置maven插件
<!--添加到<build> <plugins> 下面-->
<plugin>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-maven-plugin</artifactId>
<!--<version>${mabatis-maven-plugin}</version>-->
<version>1.3.7</version>
<configuration>
<configurationFile>
${basedir}/src/main/resources/generator/generatorConfig.xml
</configurationFile>
<overwrite>true</overwrite>
<verbose>true</verbose>
</configuration>
<dependencies>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.16</version>
</dependency>
<dependency>
<groupId>tk.mybatis</groupId>
<artifactId>mapper</artifactId>
<version>${mapper.version}</version>
</dependency>
</dependencies>
</plugin>
然后添加generatorConfig.xml文件。我们需要在resources文件夹下面创建文件夹generator,然后在generator文件夹下面创建文件。generatorConfig.xml文件内容如下:
<!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="generator/config.properties"/>
<context id="Mysql" targetRuntime="MyBatis3Simple" defaultModelType="flat">
<property name="beginningDelimiter" value="`"/>
<property name="endingDelimiter" value="`"/>
<plugin type="tk.mybatis.mapper.generator.MapperPlugin">
<property name="mappers" value="tk.mybatis.mapper.common.Mapper"/>
<property name="caseSensitive" value="true"/>
</plugin>
<jdbcConnection driverClass="${jdbc.driverClass}"
connectionURL="${jdbc.url}"
userId="${jdbc.user}"
password="${jdbc.password}">
</jdbcConnection>
<!--实体-->
<javaModelGenerator targetPackage="com.chuxin.usercenter.dao.model.${moduleName}"
targetProject="src/main/java"/>
<!--mapper.xml-->
<sqlMapGenerator targetPackage="mapper"
targetProject="src/main/resources"/>
<!--mapper接口-->
<javaClientGenerator targetPackage="com.chuxin.usercenter.dao.mapper"
targetProject="src/main/java"
type="XMLMAPPER"/>
<!--为哪张表生成上面的三类文件-->
<table tableName="${tableName}">
<generatedKey column="id" sqlStatement="JDBC"/>
</table>
</context>
</generatorConfiguration>
接下来添加代码生成器连接的数据库信息以及我们自定义的定位符。在resources下的generator文件夹下创建config.properties文件,文件内容如下:
jdbc.driverClass=com.mysql.cj.jdbc.Driver
#nullCatalogMeansCurrent=true该参数是解决生成自己数据中的user表,而不是mysql里面的user表
jdbc.url=jdbc:mysql://localhost:3306/user_center?nullCatalogMeansCurrent=true&serverTimezone=UTC
jdbc.user=root
jdbc.password=123456
#模块名称
moduleName=user
#表名
tableName=user
这样配置就完成了,然后在idea中找到如下图的文件,双击启动运行即可生成
image.png
启动时遇到的问题以及解决方式:
image.png
完整的错误信息如下:
[ERROR] Failed to execute goal org.mybatis.generator:mybatis-generator-maven-plugin:1.3.7:generate (default-cli) on project user-center: The server time zone value '�й���ʱ��' is unrecognized or represents more than one time zone. You must configure either the server or JDBC driver (via the serverTimezone configuration property) to use a more specifc time zone value if you want to utilize time zone support. -> [Help 1]
[ERROR]
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR]
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoExecutionException
解决方法:在config.properties文件中的url后面加上
image.png&serverTimezone=UTC
即可解决问题
可以参考官方文档:官方文档
网友评论