SpringBoot 集成MyBatis Generator 代码生成器

符号 阅读:613 2021-03-31 18:16:10 评论:0

项目结构:

boot-shrio 根目录pom.xml 文件

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 
  <modelVersion>4.0.0</modelVersion> 
  <groupId>com.zzg</groupId> 
  <artifactId>boot-shrio</artifactId> 
  <version>0.0.1-SNAPSHOT</version> 
  <packaging>pom</packaging> 
   
  <parent> 
		<groupId>org.springframework.boot</groupId> 
		<artifactId>spring-boot-starter-parent</artifactId> 
		<version>2.1.2.RELEASE</version> 
	</parent> 
	 
	<properties> 
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> 
        <encoding>UTF-8</encoding> 
        <java.version>1.8</java.version> 
        <maven.compiler.source>1.8</maven.compiler.source> 
        <maven.compiler.target>1.8</maven.compiler.target> 
    </properties> 
     
	<dependencies> 
		<!--web 模块依赖 --> 
		<dependency> 
			<groupId>org.springframework.boot</groupId> 
			<artifactId>spring-boot-starter-web</artifactId> 
		</dependency> 
		<!--web 热部署 --> 
		<dependency> 
			<groupId>org.springframework.boot</groupId> 
			<artifactId>spring-boot-devtools</artifactId> 
			<optional>true</optional> 
		</dependency> 
		<!--web 单元测试 --> 
		<dependency> 
			<groupId>org.springframework.boot</groupId> 
			<artifactId>spring-boot-starter-test</artifactId> 
			<scope>test</scope> 
		</dependency> 
		<!--common-lang 常用工具包 --> 
		<dependency> 
			<groupId>commons-lang</groupId> 
			<artifactId>commons-lang</artifactId> 
			<version>2.6</version> 
		</dependency> 
		<!--commons-codec 加密工具包 --> 
		<dependency> 
			<groupId>commons-codec</groupId> 
			<artifactId>commons-codec</artifactId> 
			<version>1.10</version> 
		</dependency> 
	</dependencies> 
  <modules> 
  	<module>boot-shrio-api</module> 
  	<module>boot-shrio-dao</module> 
  	<module>boot-shrio-service</module> 
  	<module>boot-shrio-controller</module> 
  </modules> 
</project>

boot-shrio-dao集成mybatis-generator 插件,pom.xml 文件如下:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 
  <modelVersion>4.0.0</modelVersion> 
  <parent> 
    <groupId>com.zzg</groupId> 
    <artifactId>boot-shrio</artifactId> 
    <version>0.0.1-SNAPSHOT</version> 
  </parent> 
  <artifactId>boot-shrio-dao</artifactId> 
   
  <!--集成mybatis-generator 自动生成组件  --> 
  <build> 
		<plugins> 
			<plugin> 
				<groupId>org.mybatis.generator</groupId> 
				<artifactId>mybatis-generator-maven-plugin</artifactId> 
				<version>1.3.7</version> 
				<dependencies> 
					<dependency> 
						<groupId>mysql</groupId> 
						<artifactId>mysql-connector-java</artifactId> 
						<version>8.0.12</version> 
					</dependency> 
					<dependency> 
						<groupId>org.mybatis.generator</groupId> 
						<artifactId>mybatis-generator-core</artifactId> 
						<version>1.3.7</version> 
					</dependency> 
				</dependencies> 
				<executions> 
					<execution> 
						<id>Generate MyBatis Artifacts</id> 
						<phase>package</phase> 
						<goals> 
							<goal>generate</goal> 
						</goals> 
					</execution> 
				</executions> 
				<configuration> 
					<!--允许移动生成的文件 --> 
					<verbose>true</verbose> 
					<!-- 是否覆盖 --> 
					<overwrite>true</overwrite> 
					<!-- 自动生成的配置 --> 
					<configurationFile>src/main/resources/generatorConfig.xml</configurationFile> 
				</configuration> 
			</plugin> 
		</plugins> 
	</build> 
</project>

在boot-shrio-dao 资源文件路径(src/main/resource)下配置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> 
 
    <!--defaultModelType="flat" 大数据字段,不分表 --> 
    <context id="Mysql" targetRuntime="MyBatis3Simple" defaultModelType="flat"> 
        <property name="autoDelimitKeywords" value="true" /> 
        <property name="beginningDelimiter" value="`" /> 
        <property name="endingDelimiter" value="`" /> 
        <property name="javaFileEncoding" value="utf-8" /> 
        <plugin type="org.mybatis.generator.plugins.SerializablePlugin" /> 
        <plugin type="org.mybatis.generator.plugins.ToStringPlugin" /> 
 
        <!-- 注释 --> 
        <commentGenerator > 
            <property name="suppressAllComments" value="true"/><!-- 是否取消注释 --> 
            <property name="suppressDate" value="true" /> <!-- 是否生成注释代时间戳--> 
        </commentGenerator> 
         
        <!--数据库链接地址账号密码--> 
        <jdbcConnection driverClass="com.mysql.cj.jdbc.Driver" 
                        connectionURL="jdbc:mysql://127.0.0.1:3306/boot_shrio?serverTimezone=UTC" 
                        userId="root" 
                        password="123456"> 
        </jdbcConnection> 
         
        <!-- 类型转换 --> 
        <javaTypeResolver> 
            <!-- 是否使用bigDecimal, false可自动转化以下类型(Long, Integer, Short, etc.) --> 
            <property name="forceBigDecimals" value="false"/> 
        </javaTypeResolver> 
 
        <!--生成Model类存放位置--> 
        <javaModelGenerator targetPackage="com.zzg.entity" targetProject="src/main/java"> 
            <property name="enableSubPackages" value="true"/> 
            <property name="trimStrings" value="true"/> 
        </javaModelGenerator> 
 
        <!-- 生成mapxml文件 --> 
        <sqlMapGenerator targetPackage="mapper" targetProject="src/main/resources" > 
            <property name="enableSubPackages" value="false" /> 
        </sqlMapGenerator> 
 
        <!-- 生成mapxml对应client,也就是接口dao --> 
        <javaClientGenerator targetPackage="com.zzg.mapper" targetProject="src/main/java" type="XMLMAPPER" > 
            <property name="enableSubPackages" value="false" /> 
        </javaClientGenerator> 
 
        <table tableName="u_permission" domainObjectName="Permission" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"> 
            <generatedKey column="id" sqlStatement="Mysql" identity="true" /> 
        </table> 
        <table tableName="u_role" domainObjectName="Role" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"> 
            <generatedKey column="id" sqlStatement="Mysql" identity="true" /> 
        </table> 
         <table tableName="u_role_permission" domainObjectName="RolePermission" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"> 
        </table> 
        <table tableName="u_user" domainObjectName="User" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"> 
            <generatedKey column="id" sqlStatement="Mysql" identity="true" /> 
        </table> 
         <table tableName="u_user_role" domainObjectName="UserRole" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"> 
        </table> 
    </context> 
</generatorConfiguration>

执行mybatis-generator相关指令,生成文件

(1)如果是在eclipse 中,选择项目或者pom.xml文件,

鼠标单击右键先择Run AS——>Maven Build… ——>

在Goals框中输入:mybatis-generator:generate 

截图如下:

代码生成效果截图:

标签:Spring Boot
声明

1.本站遵循行业规范,任何转载的稿件都会明确标注作者和来源;2.本站的原创文章,请转载时务必注明文章作者和来源,不尊重原创的行为我们将追究责任;3.作者投稿可能会经我们编辑修改或补充。

关注我们

一个IT知识分享的公众号