Spring Cloud微服务代码生成(四)

385 阅读4分钟

持续创作,加速成长!这是我参与「掘金日新计划 · 6 月更文挑战」的第14天,点击查看活动详情

前言

因为咱们微服务框架的持久层映射框架选择的是MyBatis,MyBatis 免除了几乎所有的 JDBC 代码以及设置参数和获取结果集的工作。MyBatis 可以通过简单的 XML 或注解来配置和映射原始类型、接口和 Java POJO(Plain Old Java Objects,普通老式 Java 对象)为数据库中的记录。

基于传送的Spring MVC三层架构,开发过程中我们都需要编写数据操作层代码,也就是DAO层对数据库的增删改查。基于单表的增删改查代码往往是一些模式化的编写(创建映射对象POJO、创建mapper接口、创建mapper.xml映射文件),而这些代码的编写会因为表的字段较多导致编写的复杂程度增加,因此MyBatis提供了一个代码生成器MyBatis Generator,可以根据表结构生成项目开发过程中需要的POJO实体类、mapper接口、mapper.xml映射文件。

因为开发过程中可能因为版本的迭代,导致数据库表的字段的变更,因此约定,自动生成的mapper.xml映射文件可以作为默认文件,自定义的SQL语句不在生成的mapper.xml文件中编写。如果需要自定义SQL语句可以复制一份mapper.xml文件,然后清空自动生成的SQL语句的内容,文件添加ext后缀,自定义的SQL语句就在自定义的mapper文件中编写,这样表结构变化的时候,我们重新生成一份代码即可,不会对自定义的SQL产生影响。这是个人经验,不适用于所有情况,具体根据各自的项目情况而定

资源准备

  • mybatis-generator-core的jar包,mybatis-generator-core-1.3.2.jar

  • mysql连接驱动jar包,mysql-connector-java-5.1.34.jar

  • 配置文件generator.xml 点击获取相关资源

修改配置文件

需要修改:

  • jdbcConnection标签的connectionURLuserIdpassword属性。

  • javaModelGenerator标签的targetPackage(实体类在项目的package)、targetProject(实体类文件的存放目录,目录需手动提前创建)属性。

  • sqlMapGenerator标签的targetPackage(xml映射文件的目录名)、targetProject(xml映射文件的存放目录,目录需手动提前创建)属性。

  • javaClientGeneratorb标签targetPackage(mapper接口的package)、targetProject(mapper接口的存放目录,目录需手动提前创建)属性。

  • table标签的tableName(数据库表名)、domainObjectName(生成的实体类名)属性。

同时生成多个表的时候,可以配置多个table标签。

<?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="/Users/jielingyang/Documents/generator/mysql-connector-java-5.1.34.jar" /> 
	<!-- <classPathEntry location="C:\oracle\product\10.2.0\db_1\jdbc\lib\ojdbc14.jar" />-->
	<context id="DB2Tables" targetRuntime="MyBatis3">
		<commentGenerator>
			<property name="suppressAllComments" value="true" />
		</commentGenerator>
		<!-- 数据库链接URL、用户名、密码 -->
                <!-- mysql -->
		<jdbcConnection driverClass="com.mysql.jdbc.Driver" connectionURL="jdbc:mysql://localhost:3306/vip" userId="root" password="***">
                <!-- oracle -->
		<!--<jdbcConnection driverClass="oracle.jdbc.driver.OracleDriver" connectionURL="jdbc:oracle:thin:@localhost:1521:orcl" userId="msa" password="msa">-->
		</jdbcConnection>
		<javaTypeResolver>
			<property name="forceBigDecimals" value="false" />
		</javaTypeResolver>
		<!-- 生成模型的包名和位置 -->
		<javaModelGenerator targetPackage="com.vip.common.po" targetProject="/Users/jielingyang/Documents/generator/src/main">
			<property name="enableSubPackages" value="true" />
			<property name="trimStrings" value="true" />
		</javaModelGenerator>
		<!-- 生成的映射文件包名和位置 -->
		<sqlMapGenerator targetPackage="mapper" targetProject="/Users/jielingyang/Documents/generator/src/resources">
			<property name="enableSubPackages" value="true" />
		</sqlMapGenerator>
		<!-- 生成DAO的包名和位置 -->
		<javaClientGenerator type="XMLMAPPER" targetPackage="com.vip.calculate.biz.dao" targetProject="/Users/jielingyang/Documents/generator/src/main">
			<property name="enableSubPackages" value="true" />
		</javaClientGenerator>
		<!-- 要生成那些表(更改tableName和domainObjectName就可以) -->
		<table tableName="catvehiclex" domainObjectName="CatVehicleX" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false" >
			<property name="useActualColumnNames" value="true" />
		</table>
		<!-- <table tableName="gcd_manageruser" domainObjectName="ManagerUser" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false" /> -->
	</context>
</generatorConfiguration>

执行生成命令

执行生成命令,先在generator路径下创建/src/main/src/resources目录。

进入generatorm目录下执行

image.png

java -jar mybatis-generator-core-1.3.2.jar -configfile generator.xml -overwrite

以上的命令和路径完全是基于下载资源后没有修改文件名称,也没有改变资源文件夹里面的文件目录。建议首次使用不要进行任何文件的修改和路径的改变

完整目录结构

image.png

移动生成的代码

将生成的java文件移动到自己的项目中。

常见问题

问题一:MybatisGenerator生成****WithBLOBs.java文件。

原因: 数据库中出现text类型字段,MybatisGenerator会自动生成WithBLOBS.java存放该类型字段。数据库中varchar,text类型字段,在java中都是自动生成string类型属性,但是会通过WithBLOBS.java来进行区分。

解决方案:

<table schema="erpdb" tableName="t_supplier_category" domainObjectName="SupplierCategory"
       enableCountByExample="false" enableUpdateByExample="false"
       enableDeleteByExample="false" enableSelectByExample="false"
       selectByExampleQueryId="false">

  <!--以下为添加内容 -->
  <columnOverride column="detail_address" javaType="java.lang.String" jdbcType="VARCHAR" />
  <columnOverride column="supplier_introduction" javaType="java.lang.String" jdbcType="VARCHAR" />
  <columnOverride column="business_scope_introduction" javaType="java.lang.String" jdbcType="VARCHAR" />
</table>

或者可以修改表的字段类型

问题二:生成的实体类直接使用数据库表原字段,不进行驼峰转换。

原因:因为MyBatis Generator生成实体类时默认开启了下划线转驼峰,如果数据库表的字段没有下划线,那么数据库表的字段在实体类中是全小写,这个时候如果数据库表字段是驼峰命名规则。

解决方案:可以通过配置实现。MyBatis Generator配置文件--指定生成实体类使用实际的表列名作为实体类的属性名。

table标签下的设置属性useActualColumnNames用于指定生成实体类时是否使用实际的列名作为实体类的属性名,取值true或false。

  • true:MyBatis Generator会使用数据库中实际的字段名字作为生成的实体类的属性名。
  • false:这是默认值。如果设置为false,则MyBatis Generator会将数据库中实际的字段名字转换为Camel Case风格作为生成的实体类的属性名。
<!-- 要生成那些表(更改tableName和domainObjectName就可以) -->
<table tableName="catvehiclex" domainObjectName="CatVehicleX" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false" >
  <property name="useActualColumnNames" value="true" />
</table>

如果明确的使用columnOverride元素指定了字段对应的实体的属性名,那么useActualColumnNames会被忽略。

假设表有一个字段名为start_date,如果这个属性设置为true,则生成的实体类的属性名为start_date,生成的setter/getter为 setStart_date/getStart_date。如果useActualColumnNames设置为false,则生成的实体类的属性名为startDate,生成的setter/getter为setStartDate/getStartDate。

那为什么要在数据库表字段中使用Snake Case下划线风格呢?因为大部分数据库服务器对象的命名是不分大小写的,因此使用Snake Case命名风格还是十分有必要的。MyBatis Generator考虑的还真是仔细,将Snake Case转换为Camel Case以与Java风格保持一致。