优秀的编程知识分享平台

网站首页 > 技术文章 正文

玩转微服务-自动生成工具(微服务自动构建)

nanyue 2024-10-02 17:45:13 技术文章 8 ℃

大家好,结合上一篇介绍父包配置多个模块,为何要配置多个模块呢?

1. 方便解耦,独立运行;

2. 支持不同的数据源;

3. 按需依赖,比如子服务包yl-chis(服务)我只需要依赖parent-chis里面的mapper相关接口就可以了;如yl-report报表统计可能需要依赖多个父模块。

那model,mapper里面的代码都需要自己敲吗?可以自己敲,也可以通过mybatis的自动生成工具generatorConfiguration,下面就结合项目介绍下该工具的使用

1.首先在对应模块parent-public的pom中配置plugin

注意:这个plugin使用dependencies引入了mysql 的驱动和mybatis的相关jar包,这个不能省略

2.结合plugin配置指向的src/main/resources/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>
<!--指定驱动的路径-->
 <classPathEntry
 location="d:/mysql-connector-java-5.1.38.jar" />
 <context id="mybatisDemoForMysql" targetRuntime="MyBatis3">
 <plugin type="org.mybatis.generator.plugins.CaseInsensitiveLikePlugin"></plugin>
 <plugin type="org.mybatis.generator.plugins.SerializablePlugin"></plugin>
 <!-- Pagination -->
 <plugin type="com.gy.mybatisGenerator.plugin.MySQLPaginationPlugin"></plugin>
 <!-- 控制注释 -->
 <commentGenerator>
 <!-- 是否去除所有自动生成的注释文件 -->
 <property name="suppressAllComments" value="true" />
 <!-- 是否去除所有自动生成的文件的时间戳,默认为false -->
 <property name="suppressDate" value="true" />
 </commentGenerator>
 <!-- 指向数据库 -->
 <jdbcConnection driverClass="com.mysql.jdbc.Driver"
 connectionURL="jdbc:mysql://****:3306/public" userId=""
 password="">
 </jdbcConnection>
 <javaTypeResolver>
 <!-- 把jdbc中的decimal与numberic类型转化为integer类型 -->
 <property name="forceBigDecimals" value="false" />
 </javaTypeResolver>
 <!-- 数据库表对应的model -->
 <javaModelGenerator targetPackage="com.zw.basic.model"
 targetProject="../parent-common/src/main/java">
 <property name="enableSubPackages" value="true" />
 <property name="trimStrings" value="true" />
 </javaModelGenerator>
 <!-- 控制Model的xmlMapper文件 -->
 <sqlMapGenerator targetPackage="com.zw.parent.basic.mapper"
 targetProject="src/main/java">
 <property name="enableSubPackages" value="true" />
 </sqlMapGenerator>
 <!-- 控制mapper接口 -->
 <javaClientGenerator targetPackage="com.zw.parent.basic.mapper"
 type="XMLMAPPER" targetProject="src/main/java">
 <property name="enableSubPackages" value="true" />
 <property name="methodNameCalculator" value="extended" />
 </javaClientGenerator>
 <table tableName="tb_user" domainObjectName="User"
 enableCountByExample="true" enableUpdateByExample="true"
 enableDeleteByExample="true" enableSelectByExample="true"
 selectByExampleQueryId="true">
 <!-- 指定数据库自增字段 -->
 <generatedKey column="user_id" sqlStatement="Mysql"/>
 </table>
 </context>
</generatorConfiguration>

3.项目执行方式

在idea添加一个“Run运行”选项,使用maven运行mybatis-generator-maven-plugin插件

Step1:选择配置edit configuration

Step2:创建maven运行项

Step3:配置命令 mybatis-generator:generate -e

注意:

working directory要指向具体哪个模块的生成工具generate;

mybatis-generator:generate -e 其中-e为了让插件输出详细信息,方便定位问题;

4.点击运行看效果

己经达到效果,自动生成了model及mapper,另外generate生成工具只对单表生成,多表联合需要自己写

5.现在我们编译看效果

编译成功,己发布到本地库,现在子项目就自动依赖了

6.启动看效果

谢谢大家多多关注,想要代码及学习资料的关注私信我,谢谢!!!

Tags:

最近发表
标签列表