实用的Mybatis generator 自动生成工具

日常开发或接私活以后,有时候我们需要快速创建项目进行开发,除了需要项目框架模板(如SSM,SSH,Spring boot等框架模板),我们还需要快速的生成一些简易代码.
     

Mybatis generator代码生成器,根据数据表名称生成对应的Model、Mapper、Service、Controller简化开发。

     

        此版本mybatis gennerator无需配置任何配置文件,只需添加maven依赖后,执行test类就能生成,且生成的mapper接口简单易用.是目前配置较为简单,方法非常实用的,是目前较为优秀的mabatis自动生成工具.

    ****此种方发生成代码无需单独建立工程,而是将代码生成器放在项目的测试包下****

     因此生产时,在建表之后,可以边开发边根据新的业务需求随时生成新的代码.如下图所示,test包下的CodeGenerator是生成代码的核心类.

   

以下是使用步骤:

  1.  添加maven依赖:  

主要有3个依赖要添加: mybatis自动生成工具(org.mtbatis.generator),mybatis工具包(tk.mybatis),代码生成器依赖(org.freemarker)

<!--mybatis自动生成工具-->
    <dependency>
      <groupId>org.mybatis.generator</groupId>
      <artifactId>mybatis-generator-core</artifactId>
      <version>1.3.5</version>
      <scope>test</scope>
    </dependency>

    <!-- mybatis 工具包-->
    <dependency>
      <groupId>tk.mybatis</groupId>
      <artifactId>mapper</artifactId>
      <version>3.4.2</version>
    </dependency>

    <!--代码生成器依赖-->
    <dependency>
      <groupId>org.freemarker</groupId>
      <artifactId>freemarker</artifactId>
      <version>2.3.23</version>
      <scope>test</scope>
    </dependency>

  2.    在工程的core包下放入2个接口(自动生成工具的核心继承接口,类名为Mapper和Service),这两个类的内容不能变,名称可以根据CodeGenerator类中的配置动态变化

      代码如下:

package com.zt.core;

import tk.mybatis.mapper.common.BaseMapper;
import tk.mybatis.mapper.common.ConditionMapper;
import tk.mybatis.mapper.common.IdsMapper;
import tk.mybatis.mapper.common.special.InsertListMapper;

/**
 * 定制版MyBatis Mapper插件接口,如需其他接口参考官方文档自行添加。
 */
public interface Mapper<T>
        extends
        BaseMapper<T>,
        ConditionMapper<T>,
        IdsMapper<T>,
        InsertListMapper<T>{
}
package com.zt.core;

import tk.mybatis.mapper.entity.Condition;

import java.util.List;

public interface Service<T> {

    long count(T model);

    long countByCondition(Condition condition);

    void insert(T model);

    void insertSelective(T model);

    void insertList(List<T> models);

    void deleteByPrimaryKey(Object id);

    void deleteByIds(String ids);

    void deleteByCondition(Condition condition);

    void updateByCondition(T model, Condition condition);

    void updateByConditionSelective(T model, Condition condition);

    void updateByPrimaryKey(T model);

    void updateByPrimaryKeySelective(T model);

    T selectByPrimaryKey(Object id);

    T selectOne(T model);

    List<T> selectAll();

    T selectFirst(T model);

    T selectFirstByCondition(Condition condition);

    List<T> select(T model);

    List<T> selectForStartPage(T model, Integer pageNum, Integer pageSize);

    List<T> selectByIds(String ids);

    List<T> selectByCondition(Condition condition);

    List<T> selectByConditionForStartPage(Condition condition, Integer pageNum, Integer pageSize);
}

   3.在test包的资源文件夹下(resources)添加模板.如下图

 

 以下是模板资源内容(创建同名目录和文件,将代码复制进去即可)

controller.ftl:

package ${basePackage}.web;

import ${basePackage}.core.BasePageSearch;s
import ${basePackage}.dto.IDValid;
import ${basePackage}.result.PageResult;
import ${basePackage}.result.Result;
import ${basePackage}.result.ResultCode;
import ${basePackage}.model.${modelNameUpperCamel};
import ${basePackage}.service.${modelNameUpperCamel}Service;
import org.springframework.web.bind.annotation.*;
import com.wordnik.swagger.annotations.Api;
import com.wordnik.swagger.annotations.ApiOperation;
import tk.mybatis.mapper.entity.Condition;

import javax.annotation.Resource;
import java.util.List;

/**
* Created by ${author} on ${date}.
*/
@RestController
@RequestMapping("${baseRequestMapping}")
@Api(value = "xx", description = "xx")
public class ${modelNameUpperCamel}Controller {

    @Resource
    private ${modelNameUpperCamel}Service services;

    @ApiOperation(value = "获取xx列表")
    @RequestMapping(value = "/get_list", method = RequestMethod.POST)
    @ResponseBody
    public Object get_list(@RequestBody BasePageSearch<${modelNameUpperCamel}> modelValid) {
	Condition condition = new Condition(${modelNameUpperCamel}.class);
	Condition.Criteria criteria = condition.createCriteria();
	//        condition.setOrderByClause("ID DESC");

    ${modelNameUpperCamel} model = modelValid.getSearch() == null?new ${modelNameUpperCamel}():modelValid.getSearch();

	//模型判断是否等于场景使用比如 ID = 10
	//        List<${modelNameUpperCamel}> lists = services.selectForStartPage(model,modelValid.getPage(),modelValid.getLimit());
		//        long total = services.count(model);

		List<${modelNameUpperCamel}> lists = services.selectByConditionForStartPage(condition,modelValid.getPage(),modelValid.getLimit());
			long total = services.countByCondition(condition);

			return new Result(ResultCode.SUCCESS,lists,new PageResult(modelValid.getPage(),total));
    }

    @ApiOperation(value = "获取单条xx")
    @RequestMapping(value = "/get_one", method = RequestMethod.POST)
    @ResponseBody
    public Object get_one(@RequestBody IDValid valid) {
        ${modelNameUpperCamel} model = services.selectByPrimaryKey(valid.getId());
        return new Result(ResultCode.SUCCESS,model);
        }

        @ApiOperation(value = "新增单条xx")
        @RequestMapping(value = "/add_one", method = RequestMethod.POST)
        @ResponseBody
        public Object add_one(@RequestBody ${modelNameUpperCamel} model) {
        services.insertSelective(model);
        return new Result(ResultCode.SUCCESS,model.getId());
    }

    @ApiOperation(value = "新增多条xx")
    @RequestMapping(value = "/add_multiple", method = RequestMethod.POST)
    @ResponseBody
    public Object add_multiple(@RequestBody List<${modelNameUpperCamel}> model) {
        services.insertList(model);
        return new Result(ResultCode.SUCCESS);
        }

        @ApiOperation(value = "修改单条xx")
        @RequestMapping(value = "/edit_one", method = RequestMethod.POST)
        @ResponseBody
        public Object edit_one(@RequestBody ${modelNameUpperCamel} model) {
        services.updateByPrimaryKeySelective(model);
        return new Result(ResultCode.SUCCESS);
        }

        @ApiOperation(value = "删除单条xx")
        @RequestMapping(value = "/delete_one", method = RequestMethod.POST)
        @ResponseBody
        public Object delete_one(@RequestBody IDValid valid) {
        services.deleteByPrimaryKey(valid.getId());
        return new Result(ResultCode.SUCCESS);
        }

        @ApiOperation(value = "删除多条xx")
        @RequestMapping(value = "/delete_multiple", method = RequestMethod.POST)
        @ResponseBody
        //ids 1,2,3,4
        public Object delete_multiple(@RequestBody String ids) {
        services.deleteByIds(ids);
        return new Result(ResultCode.SUCCESS);
    }


}

service.ftl:

package ${basePackage}.service;
import ${basePackage}.model.${modelNameUpperCamel};
import ${basePackage}.core.Service;


/**
 * Created by ${author} on ${date}.
 */
public interface ${modelNameUpperCamel}Service extends Service<${modelNameUpperCamel}> {

}

service-impl.ftl

package ${basePackage}.service.impl;

import ${basePackage}.dao.${modelNameUpperCamel}Mapper;
import ${basePackage}.model.${modelNameUpperCamel};
import ${basePackage}.service.${modelNameUpperCamel}Service;
import ${basePackage}.core.BaseService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;


/**
 * Created by ${author} on ${date}.
 */
@Service
@Transactional
public class ${modelNameUpperCamel}ServiceImpl extends BaseService<${modelNameUpperCamel}> implements ${modelNameUpperCamel}Service {
    @Resource
    private ${modelNameUpperCamel}Mapper ${modelNameLowerCamel}Mapper;

}

4.在maven工程测试包下添加核心生成类CodeGenerator.java,执行此类的main方法,根据配置,就能生成数据表对应的Model,Mapper,Service,Controller等.代码如下(注:此类中有我的私有方类StringUtils,注意替换成您自己的),此类中的数据库和文件地址保证正确,否则代码生成失败!



import com.google.common.base.CaseFormat;
import com.zt.utils.StringUtils;
import freemarker.template.TemplateExceptionHandler;
import org.mybatis.generator.api.MyBatisGenerator;
import org.mybatis.generator.config.*;
import org.mybatis.generator.internal.DefaultShellCallback;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * 代码生成器,根据数据表名称生成对应的Model、Mapper、Service、Controller简化开发。
 */
public class CodeGenerator {

	public static final String BASE_PACKAGE = "com.zt";//项目基础包名称,根据自己的项目修改

	/*生成文件地址配置*/
	public static final String MODEL_PACKAGE = BASE_PACKAGE + ".model";//生成的Model类所在包
	public static final String MAPPER_PACKAGE = BASE_PACKAGE + ".dao";//生成的Mapper所在包
	public static final String SERVICE_PACKAGE = BASE_PACKAGE + ".service";//生成的Service所在包
	public static final String SERVICE_IMPL_PACKAGE = SERVICE_PACKAGE + ".impl";//生成的ServiceImpl所在包
	public static final String CONTROLLER_PACKAGE = BASE_PACKAGE + ".controller";//生成的Controller所在包

	public static final String MAPPER_INTERFACE_REFERENCE = BASE_PACKAGE + ".core.Mapper";//Mapper插件基础接口的完全限定名(第二步提到的核心继承接口Mapper)

	/*数据库配置*/
	private static final String JDBC_URL = "jdbc:mysql://106.14.168.164:3306/zt";//数据库url
	private static final String JDBC_USERNAME = "root";
	private static final String JDBC_PASSWORD = "123456";
	private static final String JDBC_DIVER_CLASS_NAME = "com.mysql.jdbc.Driver";

	private static final String PROJECT_PATH = System.getProperty("user.dir");//项目在硬盘上的基础路径
	private static final String TEMPLATE_FILE_PATH = PROJECT_PATH + "/src/test/resources/generator/template";//模板位置

	private static final String JAVA_PATH = "/src/main/java"; //java文件路径
	private static final String RESOURCES_PATH = "/src/main/resources";//资源文件路径

	private static final String PACKAGE_PATH_SERVICE = packageConvertPath(SERVICE_PACKAGE);//生成的Service存放路径
	private static final String PACKAGE_PATH_SERVICE_IMPL = packageConvertPath(SERVICE_IMPL_PACKAGE);//生成的Service实现存放路径
	private static final String PACKAGE_PATH_CONTROLLER = packageConvertPath(CONTROLLER_PACKAGE);//生成的Controller存放路径

	private static final String AUTHOR = "KingZS";//@author
	private static final String DATE = new SimpleDateFormat("yyyy/MM/dd").format(new Date());//@date

	/*main函数入口,放入表名运行即可生成代码*/
	public static void main(String[] args) {
		genCode("program");

		//genCodeByCustomModelName("输入表名","输入自定义Model名称");
	}

	/**
	 * 通过数据表名称生成代码,Model 名称通过解析数据表名称获得,下划线转大驼峰的形式。
	 * 如输入表名称 "t_user_detail" 将生成 TUserDetail、TUserDetailMapper、TUserDetailService ...
	 * @param tableNames 数据表名称...
	 */
	public static void genCode(String... tableNames) {
		for (String tableName : tableNames) {
			genCodeByCustomModelName(tableName, null);
		}
	}

	/**
	 * 通过数据表名称,和自定义的 Model 名称生成代码
	 * 如输入表名称 "t_user_detail" 和自定义的 Model 名称 "User" 将生成 User、UserMapper、UserService ...
	 * @param tableName 数据表名称
	 * @param modelName 自定义的 Model 名称
	 */
	public static void genCodeByCustomModelName(String tableName, String modelName) {
		genModelAndMapper(tableName, modelName);

//		genService(tableName, modelName);
//		genController(tableName, modelName);

	}


	public static void genModelAndMapper(String tableName, String modelName) {
		Context context = new Context(ModelType.FLAT);
		context.setId("Potato");
		context.setTargetRuntime("MyBatis3Simple");
		context.addProperty(PropertyRegistry.CONTEXT_BEGINNING_DELIMITER, "`");
		context.addProperty(PropertyRegistry.CONTEXT_ENDING_DELIMITER, "`");

		JDBCConnectionConfiguration jdbcConnectionConfiguration = new JDBCConnectionConfiguration();
		jdbcConnectionConfiguration.setConnectionURL(JDBC_URL);
		jdbcConnectionConfiguration.setUserId(JDBC_USERNAME);
		jdbcConnectionConfiguration.setPassword(JDBC_PASSWORD);
		jdbcConnectionConfiguration.setDriverClass(JDBC_DIVER_CLASS_NAME);
		context.setJdbcConnectionConfiguration(jdbcConnectionConfiguration);

		PluginConfiguration pluginConfiguration = new PluginConfiguration();
		pluginConfiguration.setConfigurationType("tk.mybatis.mapper.generator.MapperPlugin");
		pluginConfiguration.addProperty("mappers", MAPPER_INTERFACE_REFERENCE);
		context.addPluginConfiguration(pluginConfiguration);

		JavaModelGeneratorConfiguration javaModelGeneratorConfiguration = new JavaModelGeneratorConfiguration();
		javaModelGeneratorConfiguration.setTargetProject(PROJECT_PATH + JAVA_PATH);
		javaModelGeneratorConfiguration.setTargetPackage(MODEL_PACKAGE);
		context.setJavaModelGeneratorConfiguration(javaModelGeneratorConfiguration);

		SqlMapGeneratorConfiguration sqlMapGeneratorConfiguration = new SqlMapGeneratorConfiguration();
		sqlMapGeneratorConfiguration.setTargetProject(PROJECT_PATH + RESOURCES_PATH);
		sqlMapGeneratorConfiguration.setTargetPackage("Mapping");
		context.setSqlMapGeneratorConfiguration(sqlMapGeneratorConfiguration);

		JavaClientGeneratorConfiguration javaClientGeneratorConfiguration = new JavaClientGeneratorConfiguration();
		javaClientGeneratorConfiguration.setTargetProject(PROJECT_PATH + JAVA_PATH);
		javaClientGeneratorConfiguration.setTargetPackage(MAPPER_PACKAGE);
		javaClientGeneratorConfiguration.setConfigurationType("XMLMAPPER");
		context.setJavaClientGeneratorConfiguration(javaClientGeneratorConfiguration);

		TableConfiguration tableConfiguration = new TableConfiguration(context);
		tableConfiguration.setTableName(tableName);
		if (StringUtils.isNotEmpty(modelName))tableConfiguration.setDomainObjectName(modelName);
		tableConfiguration.setGeneratedKey(new GeneratedKey("id", "Mysql", true, null));
		context.addTableConfiguration(tableConfiguration);

		List<String> warnings;
		MyBatisGenerator generator;
		try {
			Configuration config = new Configuration();
			config.addContext(context);
			config.validate();

			boolean overwrite = true;
			DefaultShellCallback callback = new DefaultShellCallback(overwrite);
			warnings = new ArrayList<String>();
			generator = new MyBatisGenerator(config, callback, warnings);
			generator.generate(null);
		} catch (Exception e) {
			throw new RuntimeException("生成Model和Mapper失败", e);
		}

		if (generator.getGeneratedJavaFiles().isEmpty() || generator.getGeneratedXmlFiles().isEmpty()) {
			throw new RuntimeException("生成Model和Mapper失败:" + warnings);
		}
		if (StringUtils.isEmpty(modelName)) modelName = tableNameConvertUpperCamel(tableName);
		System.out.println(modelName + ".java 生成成功");
		System.out.println(modelName + "Mapper.java 生成成功");
		System.out.println(modelName + "Mapper.xml 生成成功");
	}

	public static void genService(String tableName, String modelName) {
		try {
			freemarker.template.Configuration cfg = getConfiguration();

			Map<String, Object> data = new HashMap<>();
			data.put("date", DATE);
			data.put("author", AUTHOR);
			String modelNameUpperCamel = StringUtils.isEmpty(modelName) ? tableNameConvertUpperCamel(tableName) : modelName;
			data.put("modelNameUpperCamel", modelNameUpperCamel);
			data.put("modelNameLowerCamel", tableNameConvertLowerCamel(tableName));
			data.put("basePackage", BASE_PACKAGE);

			File file = new File(PROJECT_PATH + JAVA_PATH + PACKAGE_PATH_SERVICE + modelNameUpperCamel + "Service.java");
			if (!file.getParentFile().exists()) {
				file.getParentFile().mkdirs();
			}
			cfg.getTemplate("service.ftl").process(data,
					new FileWriter(file));
			System.out.println(modelNameUpperCamel + "Service.java 生成成功");

			File file1 = new File(PROJECT_PATH + JAVA_PATH + PACKAGE_PATH_SERVICE_IMPL + modelNameUpperCamel + "ServiceImpl.java");
			if (!file1.getParentFile().exists()) {
				file1.getParentFile().mkdirs();
			}
			cfg.getTemplate("service-impl.ftl").process(data,
					new FileWriter(file1));
			System.out.println(modelNameUpperCamel + "ServiceImpl.java 生成成功");
		} catch (Exception e) {
			throw new RuntimeException("生成Service失败", e);
		}
	}

	public static void genController(String tableName, String modelName) {
		try {
			freemarker.template.Configuration cfg = getConfiguration();

			Map<String, Object> data = new HashMap<>();
			data.put("date", DATE);
			data.put("author", AUTHOR);
			String modelNameUpperCamel = StringUtils.isEmpty(modelName) ? tableNameConvertUpperCamel(tableName) : modelName;
			data.put("baseRequestMapping", modelNameConvertMappingPath(modelNameUpperCamel));
			data.put("modelNameUpperCamel", modelNameUpperCamel);
			data.put("modelNameLowerCamel", CaseFormat.UPPER_CAMEL.to(CaseFormat.LOWER_CAMEL, modelNameUpperCamel));
			data.put("basePackage", BASE_PACKAGE);

			File file = new File(PROJECT_PATH + JAVA_PATH + PACKAGE_PATH_CONTROLLER + modelNameUpperCamel + "Controller.java");
			if (!file.getParentFile().exists()) {
				file.getParentFile().mkdirs();
			}
			//cfg.getTemplate("controller-restful.ftl").process(data, new FileWriter(file));
			cfg.getTemplate("controller.ftl").process(data, new FileWriter(file));

			System.out.println(modelNameUpperCamel + "Controller.java 生成成功");
		} catch (Exception e) {
			throw new RuntimeException("生成Controller失败", e);
		}

	}

	private static freemarker.template.Configuration getConfiguration() throws IOException {
		freemarker.template.Configuration cfg = new freemarker.template.Configuration(freemarker.template.Configuration.VERSION_2_3_23);
		cfg.setDirectoryForTemplateLoading(new File(TEMPLATE_FILE_PATH));
		cfg.setDefaultEncoding("UTF-8");
		cfg.setTemplateExceptionHandler(TemplateExceptionHandler.IGNORE_HANDLER);
		return cfg;
	}

	private static String tableNameConvertLowerCamel(String tableName) {
		return CaseFormat.LOWER_UNDERSCORE.to(CaseFormat.LOWER_CAMEL, tableName.toLowerCase());
	}

	private static String tableNameConvertUpperCamel(String tableName) {
		return CaseFormat.LOWER_UNDERSCORE.to(CaseFormat.UPPER_CAMEL, tableName.toLowerCase());

	}

	private static String tableNameConvertMappingPath(String tableName) {
		tableName = tableName.toLowerCase();//兼容使用大写的表名
		return "/" + (tableName.contains("_") ? tableName.replaceAll("_", "/") : tableName);
	}

	private static String modelNameConvertMappingPath(String modelName) {
		String tableName = CaseFormat.UPPER_CAMEL.to(CaseFormat.LOWER_UNDERSCORE, modelName);
		return tableNameConvertMappingPath(tableName);
	}

	private static String packageConvertPath(String packageName) {
		return String.format("/%s/", packageName.contains(".") ? packageName.replaceAll("\\.", "/") : packageName);
	}

}

以下代码部分是配置生成的内容(Model,Mapper,Service,Controller)如下图配置,执行以后只生成Model类和xxxMapper.xml文件

此工具目前博主依旧在用,如有改进意见请君提出!

  • 7
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 12
    评论
### 回答1: MyBatis Generator 是一个可以自动化生成MyBatis代码的工具,包括 Mapper 接口、Mapper XML 文件以及 POJO 类等。如果你想要生成 Mapper XML 文件,可以按照以下步骤进行操作: 1. 配置 MyBatis Generator 插件:在 pom.xml 文件中添加以下依赖: ```xml <dependency> <groupId>org.mybatis.generator</groupId> <artifactId>mybatis-generator-core</artifactId> <version>1.4.0</version> </dependency> ``` 2. 编写 MyBatis Generator 配置文件:创建一个名为 generatorConfig.xml 的文件,并按照以下格式进行配置: ```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> <context id="context1"> <!-- 配置数据库连接信息 --> <jdbcConnection driverClass="com.mysql.jdbc.Driver" connectionURL="jdbc:mysql://localhost:3306/test" userId="root" password="root"> </jdbcConnection> <!-- 配置生成的 Java 模型类的包名和位置 --> <javaModelGenerator targetPackage="com.example.model" targetProject="src/main/java"> <property name="enableSubPackages" value="true" /> <property name="trimStrings" value="true" /> </javaModelGenerator> <!-- 配置生成的 Mapper 接口的包名和位置 --> <sqlMapGenerator targetPackage="com.example.mapper" targetProject="src/main/resources"> <property name="enableSubPackages" value="true" /> </sqlMapGenerator> <!-- 配置生成的 Mapper XML 文件的位置 --> <javaClientGenerator type="XMLMAPPER" targetPackage="com.example.mapper" targetProject="src/main/java"> <property name="enableSubPackages" value="true" /> </javaClientGenerator> <!-- 配置要生成的表 --> <table tableName="user" domainObjectName="User" /> </context> </generatorConfiguration> ``` 其中,`jdbcConnection` 标签用于配置数据库连接信息,`javaModelGenerator` 标签用于配置生成的 Java 模型类的包名和位置, `sqlMapGenerator` 标签用于配置生成的 Mapper 接口的包名和位置,`javaClientGenerator` 标签用于配置生成的 Mapper XML 文件的位置,`table` 标签用于配置要生成的表。 3. 运行 MyBatis Generator:在工程根目录下执行以下命令: ``` mvn mybatis-generator:generate ``` 执行命令后,MyBatis Generator自动生成 Mapper XML 文件,并放置在 `target/generated-sources/mybatis-generator` 目录下。 注意:在执行命令之前,需要先将 Maven 的 settings.xml 文件中的本地仓库地址修改为你自己的仓库地址,否则可能会出现找不到 MyBatis Generator 插件的情况。 ### 回答2: MyBatis Generator是一个在Java项目中自动生成MyBatis数据库访问层代码的工具。它能够根据数据库表结构自动生成包含增删改查等基本方法的Mapper接口和对应的XML映射文件。 生成Mapper接口和XML映射文件的过程非常简单。首先需要配置MyBatis Generator的生成规则,配置文件中包括数据库连接信息以及要生成的表名等。其次,可以使用命令行或者Maven插件来执行生成命令,工具会连接数据库读取表信息,并据此生成对应的Mapper接口和XML映射文件。 生成的Mapper接口中包含了常见的数据库操作方法,如插入insert、删除delete、更新update和查询select。这些方法会在对应的XML映射文件中被实现,通过SQL语句与数据库进行交互。可以根据需要自定义SQL语句,也可以使用MyBatis提供的动态SQL特性来灵活地构建查询条件。 生成的XML映射文件中,会将Mapper接口中定义的方法与对应的SQL语句进行映射。映射时需要指定SQL语句的类型,如insert、delete、update或select,并在对应的标签中编写具体的SQL语句。可以使用占位符来引用Mapper接口方法中的参数,从而实现动态SQL的拼接。 总之,使用MyBatis Generator可以方便地自动生成Mapper接口和XML映射文件,减少了手动编写代码的工作量,提高了开发效率。同时,生成的代码结构清晰,易于维护和扩展。 ### 回答3: MyBatis Generator是一个用于自动生成持久层代码的工具,它可以根据数据库表结构自动生成代码,包括mapper.xml文件中的方法。 MyBatis Generator生成mapper.xml文件中的方法需要指定数据表的信息,比如表名、列名、数据类型等。通常情况下,我们需要先创建一个配置文件,指定数据库的连接信息和要生成代码的目标文件路径等。 在配置文件中,可以通过配置表名的规则、列名的规则以及其他定制化的配置项,来指定生成代码的方式和风格。配置完成后,执行生成器程序,它会根据配置文件中的规则和信息自动读取数据库表结构,并生成对应的mapper.xml文件。 生成的mapper.xml文件会包含CRUD操作的基础方法,比如插入、删除、更新、查询等,这些方法的具体实现会根据数据库表结构自动生成。 生成的方法会根据表结构的主键、索引等信息生成对应的条件查询方法,方便根据特定的条件进行查询操作。 此外,MyBatis Generator还可以生成动态SQL查询的方法,可以根据不同的查询条件生成不同的查询语句,方便根据不同的需求进行灵活的查询。 总而言之,通过配置文件和执行生成器程序,可以使用MyBatis Generator自动化生成mapper.xml文件中的方法,从而减少手工编写重复繁琐的代码工作,提高开发效率。
评论 12
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值