Sprint Boot项目搭建STEP3:集成代码生成工具

1.pom.xml添加(mybatis generator 自动生成代码插件plugin引入和generatorConfig.xml配置 dependency依赖),这里注意plugin和dependency的区别。

在这里插入图片描述

<!-- mybatis代码自动生成generatorConfig.xml配置 -->
		<dependency>
            <groupId>org.mybatis.generator</groupId>
            <artifactId>mybatis-generator-core</artifactId>
            <version>1.3.2</version>
        </dependency>
<!-- mybatis generator 自动生成代码插件 -->
			<plugin>
                <groupId>org.mybatis.generator</groupId>
                <artifactId>mybatis-generator-maven-plugin</artifactId>
                <version>1.3.2</version>
                <configuration>
                    <configurationFile>${basedir}/src/main/resources/generatorConfig.xml</configurationFile>
                    <overwrite>true</overwrite>
                    <verbose>true</verbose>
                </configuration>
            </plugin>

2.创建generatorConfig.xml文件。

在resources目录下新建一个generator目录,再创建一个generatorStudy.xml文件

generatorStudy.xml文件如下:注: 这里要跟配置文件名一致,里面的配置引用要跟配置文件里定义的一致。

<?xml version="1.0" encoding="UTF-8"?>

<!-- ~ The MIT License (MIT) ~ ~ Copyright (c) 2014 [email protected] ~ ~ 
	Permission is hereby granted, free of charge, to any person obtaining a copy 
	~ of this software and associated documentation files (the "Software"), to 
	deal ~ in the Software without restriction, including without limitation 
	the rights ~ to use, copy, modify, merge, publish, distribute, sublicense, 
	and/or sell ~ copies of the Software, and to permit persons to whom the Software 
	is ~ furnished to do so, subject to the following conditions: ~ ~ The above 
	copyright notice and this permission notice shall be included in ~ all copies 
	or substantial portions of the Software. ~ ~ THE SOFTWARE IS PROVIDED "AS 
	IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR ~ IMPLIED, INCLUDING BUT NOT 
	LIMITED TO THE WARRANTIES OF MERCHANTABILITY, ~ FITNESS FOR A PARTICULAR 
	PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE ~ AUTHORS OR COPYRIGHT 
	HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER ~ LIABILITY, WHETHER IN 
	AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, ~ OUT OF OR IN CONNECTION 
	WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN ~ THE SOFTWARE. -->

<!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="application.properties" />

	   <context id="Mysql" targetRuntime="MyBatis3Simple"
        defaultModelType="flat">
        <property name="beginningDelimiter" value="`" />
        <property name="endingDelimiter" value="`" />

        <plugin type="${mapper.plugin}">
            <property name="mappers" value="${mapper.Mapper}" />
        </plugin>

        <jdbcConnection driverClass="${study.driverClassName}"
            connectionURL="${study.url}" userId="${study.username}" password="${study.password}">
        </jdbcConnection>

        <javaModelGenerator targetPackage="com.zhy.study.model"
            targetProject="${generator.basePath}" />

        <sqlMapGenerator targetPackage="com.zhy.study.xml"
            targetProject="${generator.basePath}" />

        <javaClientGenerator targetPackage="com.zhy.study.mapper"
            targetProject="${generator.basePath}"
            type="XMLMAPPER" />

        <table tableName="student" domainObjectName="student">
         
        </table>
    </context>
</generatorConfiguration>

在application.properties里补充定义generator.basePath,即为文件生成的本地路径。
在这里插入图片描述

#自定我生成文件路径
generator.basePath=D\:\\study\\src

在application.properties里补充定义 m a p p e r . p l u g i n {mapper.plugin}和 {mapper.Mapper}
在这里插入图片描述

mapper.plugin=tk.mybatis.mapper.generator.MapperPlugin
mapper.Mapper=tk.mybatis.mapper.common.Mapper

3.和project同级创建generator文件夹,然后创建启动文件MybaitisGen.java

在这里插入图片描述

package com.zhy.generator;

import java.io.File;
import java.io.IOException;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import org.mybatis.generator.api.MyBatisGenerator;
import org.mybatis.generator.config.Configuration;
import org.mybatis.generator.config.xml.ConfigurationParser;
import org.mybatis.generator.exception.InvalidConfigurationException;
import org.mybatis.generator.exception.XMLParserException;
import org.mybatis.generator.internal.DefaultShellCallback;

public class MybaitisGen {
	public static void main(String[] args) {
		List<String> warnings = new ArrayList<String>();
		boolean overwrite = true;
		String genCfg = "/generator/generatorStudy.xml";
		File configFile = new File(MybaitisGen.class.getResource(genCfg).getFile());
		ConfigurationParser cp = new ConfigurationParser(warnings);
		Configuration config = null;
		try {
			config = cp.parseConfiguration(configFile);
		} catch (IOException e) {
			e.printStackTrace();
		} catch (XMLParserException e) {
			e.printStackTrace();
		}
		DefaultShellCallback callback = new DefaultShellCallback(overwrite);
		MyBatisGenerator myBatisGenerator = null;
		try {
			myBatisGenerator = new MyBatisGenerator(config, callback, warnings);
		} catch (InvalidConfigurationException e) {
			e.printStackTrace();
		}
		try {
			myBatisGenerator.generate(null);
		} catch (SQLException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} catch (InterruptedException e) {
			e.printStackTrace();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}

注意:这里指向xml配置文件的名字要对应配置文件generatorStudy.xml

4.右键MybaitisGen.java执行 run as >> Java Application

没有生成文件夹和文件,注:要先再本地建立generator.basePath定义的文件目录。
在这里插入图片描述
再次执行:正常生成。拷贝到项目里。
在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/weixin_43733834/article/details/88793508
今日推荐