欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

spring-boot 代码生成工具

程序员文章站 2022-06-07 19:13:59
...
Mybatis Auto Code Generation Mapper/Service/Domain/SQL.xml, 主要是针对某个数据库所有表[参考配置文件ConfigConstants], 统一生成Domain/Service/Mapper,以及SQL.xml文件, 如:针对t_customer, 通过该应用生成CustonerInfo.java, CustomerSerivce.java/CustomerServiceImpl.java, CustomerMapper.java 以及 Customer-mapper.xml文件. 小工具类

1.Project run MainTest
@RunWith(SpringRunner.class)
@SpringBootTest(classes=SimpleApplication.class)
public class MainTest {
    Logger logger = LoggerFactory.getLogger(getClass());
    @Autowired
    private TableService tableService;

    @Test
    public void main() {
        long startTime = System.currentTimeMillis();
        logger.info("...........start application.........");

        tableService.run();

        logger.info("...end application...Time: {}",(System.currentTimeMillis()-startTime));
    }
}

2.Generate Code Config
public interface ConfigConstants {
    /**定义Schema*/
    String SCHEMA = "longchou-loan";

    /**移除表前缀*/
    String REMOVE_TABLE_PREFIX = "t_";

    /**Domain后缀, CustomerInfo*/
    String DOMAIN_SUFFIX = "";

    /**生成文件路径*/
    String FILE_PATH = "E:\\gitwork\\code-generation\\src\\main\\java";

    /**MyBatis SQL生成文件路径*/
    String SQL_PATH = "E:\\gitwork\\code-generation\\src\\main\\resources\\mappers";

    /**生成Mapper.xml后缀名字, EX: customer-mapper.xml*/
    String SQL_MAPPER_SUFFIX = "-mapper";

    /**生成包名称*/
    String ROOT_PACKAGE = "com.lance.code";

    /**JavaBean包名称*/
    String DOMAIN_PACKAGE = "domain";

    /**service包名称*/
    String SERVICE_PACKAGE = "service";

    /**serviceImpl包名称*/
    String SERVICE_impl_PACKAGE = "serviceImpl";

    /**mapper包名称*/
    String MAPPER_PACKAGE = "mapper";
}

3.Add dependencies to pom
<parent>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-parent</artifactId>
   <version>1.4.0.RELEASE</version>
</parent>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- Mybatis -->
<dependency>
    <groupId>org.mybatis.spring.boot</groupId>
    <artifactId>mybatis-spring-boot-starter</artifactId>
    <version>1.1.1</version>
</dependency>
<!-- MYSQL -->
<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
</dependency>
<dependency>
    <groupId>com.alibaba</groupId>
    <artifactId>fastjson</artifactId>
    <version>1.2.12</version>
</dependency>
<dependency>
    <groupId>org.apache.commons</groupId>
    <artifactId>commons-lang3</artifactId>
    <version>3.4</version>
</dependency>

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-test</artifactId>
    <scope>test</scope>
</dependency>
<dependency>
    <groupId>junit</groupId>
    <artifactId>junit</artifactId>
    <scope>test</scope>
</dependency>


4.spring-boot config
# IDENTITY (ContextIdApplicationContextInitializer)
spring.application.index=Code.v1.1
spring.application.name=Code Boot

#MYBATIS
mybatis.type-aliases-package=com.lance.code.generation.domain
mybatis.mapper-locations=classpath*:/mapper/*Mapper.xml
mybatis.configuration.map-underscore-to-camel-case=true
mybatis.configuration.use-generated-keys=true
mybatis.configuration.default-fetch-size=100
mybatis.configuration.default-statement-timeout=30

spring.datasource.url=jdbc:mysql://localhost:3306/loan
spring.datasource.username=root
spring.datasource.password=123456
spring.datasource.driver-class-name=com.mysql.jdbc.Driver


5.项目完整代码示例
https://github.com/leelance/code-generation