java代码生成器按照模板_Java代码生成器

"本文介绍了一个基于GitHub的全自动代码生成器mybatis-generator项目,该项目可以帮助开发者自定义代码模板,快速生成MyBatis相关的Model、Query、Dao、Service及Mapper.xml文件。用户可以根据需求修改resources下的模版,配置数据库连接信息,并通过application-${name}
摘要由CSDN通过智能技术生成

全自动代码生成器mybatis-generator

项目地址

https://github.com/GitHub-Laziji/mybatis-generator

代码模版位于resources下, 可根据需要自行修改

第一个模版是根据 commons-mybatis 编写的, 若不适合可以自行修改模版或选择其他模版

配置文件

在resources下创建application-${name}.yml文件, ${name}随意, 例如: application-example.yml, 可创建多个

配置文件内容如下, 填入数据库配置, 以及生成代码的包名

模版文件映射用于自定义生成文件的包格式以及文件名

动态属性包含

{packageFilePath} 包文件路径 例如: com/xxx/xxx

{className} 类名 由表名改为驼峰命名法得来

{suffix} 类名后缀 DO或VO

一般按以下配置即可

现在项目中有两套模版template.path 可以选mybatis 或 mybatis-default

也可以自行扩展

spring:

datasource:

url: jdbc:mysql://xxx.xxx.xxx.xxx:3306/xxxx?characterEncoding=utf-8

username: xxxxxx

password: xxxxxx

generator:

package: com.xxx.xxx

template:

path: mybatis

mapping: |

Model.java.vm: main/java/{packageFilePath}/database/model/{className}.java

Query.java.vm: main/java/{packageFilePath}/database/query/{className}Query.java

Dao.java.vm: main/java/{packageFilePath}/database/dao/{className}.java

Service.java.vm: main/java/{packageFilePath}/database/service/{className}Service.java

Mapper.xml.vm: main/resources/mapper/{className}Mapper.xml

使用

在test文件下创建测试类

@ActiveProfiles(“example”)中填入刚才配置文件名的name

tableNames需要生成的表, 可以多个

zipPath 代码导出路径

调用generatorService.generateZip传入参数可以是表名数组String[]或者TableItem[]

运行测试方法

package pg.laziji.generator;

import org.junit.Test;

import org.junit.runner.RunWith;

import org.springframework.boot.test.context.SpringBootTest;

import org.springframework.test.context.ActiveProfiles;

import org.springframework.test.context.junit4.SpringRunner;

import pg.laziji.generator.mybatis.GeneratorService;

import javax.annotation.Resource;

import java.io.IOException;

@ActiveProfiles(“example”)

@RunWith(SpringRunner.class)

@SpringBootTest

public class ExampleTest {

@Resource

private GeneratorService generatorService;

@Test

public void test(){

String zipPath = “/home/code.zip”;

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值