spring boot中使用generator插件自动生成代码

简介: spring boot中使用generator插件自动生成代码

#使用方法:使用generator插件可以帮助我们生成model,mapper等文件,因而简化开发流程,加快开发速度,以下是使用方法:

##在pom.xml中添加插件

<! - 代码自动生成插件 - > 
      <! - 使用完必须注释掉下面这个插件 - > 
      <plugin> 
      <groupId> org.mybatis.generator </ groupId> 
      <artifactId> mybatis-generator-maven-plugin </ artifactId> 
      <version > 1.3.5 </ version> 
      <configuration> 
      <verbose> true </ verbose> 
      <overwrite> true </ overwrite> 
      </ configuration> 
      <executions> 
      <execution> 
      <id>生成MyBatis构件</ id> 
      <goals> 
      <goal>生成</ goal> 
      </ goals>
      </ execution> 
      </ executions> 
      <dependencies>
      <dependency>
      <groupId>org.mybatis.generator</groupId>
      <artifactId>mybatis-generator-core</artifactId>
      <version>1.3.5</version>
      </dependency>
      </dependencies>
      </plugin>
<!-- 注释 -->
    <commentGenerator>
        <!-- 是否生成注释代时间戳 -->
        <property name="suppressDate" value="true"/>
        <!-- 是否去除自动生成的注释 true:是 : false:否 -->
        <property name="suppressAllComments" value="true"/>
    </commentGenerator>
    <!-- JDBC连接 -->
    <jdbcConnection
            driverClass="${spring.datasource.driver-class-name}"
            connectionURL="${spring.datasource.url}"
            userId="${spring.datasource.username}"
            password="${spring.datasource.password}">
    </jdbcConnection>
    <!-- 非必需,类型处理器,在数据库类型和java类型之间的转换控制-->
    <!-- 默认false,把JDBC DECIMAL 和 NUMERIC 类型解析为 Integer,为 true时把JDBC DECIMAL 和
     NUMERIC 类型解析为java.math.BigDecimal -->
    <javaTypeResolver>
        <!-- 是否使用bigDecimal, false可自动转化以下类型(Long, Integer, Short, etc.) -->
        <property name="forceBigDecimals" value="false" />
    </javaTypeResolver>
    <!-- 生成实体类地址 -->
    <javaModelGenerator targetPackage="com.example.myproject.model" targetProject="src/main/java">
        <!-- 从数据库返回的值被清理前后的空格 -->
        <property name="trimStrings" value="true" />
        <!-- enableSubPackages:是否让schema作为包的后缀 -->
        <property name="enableSubPackages" value="false" />
    </javaModelGenerator>
    <!-- 生成mapper xml文件 -->
    <sqlMapGenerator targetPackage="mapper"  targetProject="src/main/resources">
        <!-- enableSubPackages:是否让schema作为包的后缀 -->
        <property name="enableSubPackages" value="false" />
    </sqlMapGenerator>
    <!-- 生成mapper xml对应Client-->
    <javaClientGenerator targetPackage="com.example.myproject.mapper" targetProject="src/main/java" type="XMLMAPPER">
        <!-- enableSubPackages:是否让schema作为包的后缀 -->
        <property name="enableSubPackages" value="false" />
    </javaClientGenerator>
    <!-- 配置表信息 -->
    <!-- schema即为数据库名 tableName为对应的数据库表 domainObjectName是要生成的实体类 enable*ByExample
        是否生成 example类 -->
    <table schema="user" tableName="user"
    domainObjectName="User" enableCountByExample="false"
    enableDeleteByExample="false" enableSelectByExample="false"
    enableUpdateByExample="false">
    </table>
    <!--<table schema="blog" tableName="article"-->
    <!--domainObjectName="Article" enableCountByExample="false"-->
    <!--enableDeleteByExample="false" enableSelectByExample="false"-->
    <!--enableUpdateByExample="false">-->
    <!--</table>-->
    <!--<table schema="blog" tableName="document"-->
    <!--domainObjectName="Document" enableCountByExample="false"-->
    <!--enableDeleteByExample="false" enableSelectByExample="false"-->
    <!--enableUpdateByExample="false">-->
    <!--</table>-->
    <!--<table schema="blog" tableName="tag"-->
    <!--domainObjectName="Tag" enableCountByExample="false"-->
    <!--enableDeleteByExample="false" enableSelectByExample="false"-->
    <!--enableUpdateByExample="false">-->
    <!--</table>-->
</context>

```

在application.properties中添加以下部分

一定要注意jdbc.location中的路径一定不要包含中文

#数据库设置

spring.datasource.url=jdbc:mysql://localhost:3306/user?characterEncoding=UTF-8&useUnicode=true&useSSL=false
spring.datasource.username=root
spring.datasource.password=123456
spring.datasource.driver-class-name=com.mysql.jdbc.Driver

Mybatis 配置

mybatis.typeAliasesPackage=com.example.myproject.domain
mybatis.mapperLocations=classpath:mapper/*.xml
jdbc.location=F:/MyDownloads/mysql-connector-java-5.1.39-bin.jar

最后点击run->edit configuration->点击+号新建一个maven

点击apply,ok,然后点击run generator运行即可(一定要自己提前建立好对应的文件夹)

目录
相关文章
|
2月前
|
安全 Java 应用服务中间件
Spring Boot + Java 21:内存减少 60%,启动速度提高 30% — 零代码
通过调整三个JVM和Spring Boot配置开关,无需重写代码即可显著优化Java应用性能:内存减少60%,启动速度提升30%。适用于所有在JVM上运行API的生产团队,低成本实现高效能。
221 3
|
1月前
|
人工智能 监控 Java
零代码改造 + 全链路追踪!Spring AI 最新可观测性详细解读
Spring AI Alibaba 通过集成 OpenTelemetry 实现可观测性,支持框架原生和无侵入探针两种方式。原生方案依赖 Micrometer 自动埋点,适用于快速接入;无侵入探针基于 LoongSuite 商业版,无需修改代码即可采集标准 OTLP 数据,解决了原生方案扩展性差、调用链易断链等问题。未来将开源无侵入探针方案,整合至 AgentScope Studio,并进一步增强多 Agent 场景下的观测能力。
1197 28
|
26天前
|
安全 Java 测试技术
《深入理解Spring》单元测试——高质量代码的守护神
Spring测试框架提供全面的单元与集成测试支持,通过`@SpringBootTest`、`@WebMvcTest`等注解实现分层测试,结合Mockito、Testcontainers和Jacoco,保障代码质量,提升开发效率与系统稳定性。
|
5月前
|
监控 Java 数据安全/隐私保护
阿里面试:SpringBoot启动时, 如何执行扩展代码?你们项目 SpringBoot 进行过 哪些 扩展?
阿里面试:SpringBoot启动时, 如何执行扩展代码?你们项目 SpringBoot 进行过 哪些 扩展?
|
2月前
|
安全 IDE Java
Spring 的@FieldDefaults和@Data:Lombok 注解以实现更简洁的代码
本文介绍了如何在 Spring 应用程序中使用 Project Lombok 的 `@Data` 和 `@FieldDefaults` 注解来减少样板代码,提升代码可读性和可维护性,并探讨了其适用场景与限制。
114 0
Spring 的@FieldDefaults和@Data:Lombok 注解以实现更简洁的代码
|
3月前
|
人工智能 监控 安全
Spring AOP切面编程颠覆传统!3大核心注解+5种通知类型,让业务代码纯净如初
本文介绍了AOP(面向切面编程)的基本概念、优势及其在Spring Boot中的使用。AOP作为OOP的补充,通过将横切关注点(如日志、安全、事务等)与业务逻辑分离,实现代码解耦,提升模块化程度、可维护性和灵活性。文章详细讲解了Spring AOP的核心概念,包括切面、切点、通知等,并提供了在Spring Boot中实现AOP的具体步骤和代码示例。此外,还列举了AOP在日志记录、性能监控、事务管理和安全控制等场景中的实际应用。通过本文,开发者可以快速掌握AOP编程思想及其实践技巧。
|
4月前
|
安全 Java Nacos
0代码改动实现Spring应用数据库帐密自动轮转
Nacos作为国内被广泛使用的配置中心,已经成为应用侧的基础设施产品,近年来安全问题被更多关注,这是中国国内软件行业逐渐迈向成熟的标志,也是必经之路,Nacos提供配置加密存储-运行时轮转的核心安全能力,将在应用安全领域承担更多职责。
|
5月前
|
Java 调度 流计算
基于Java 17 + Spring Boot 3.2 + Flink 1.18的智慧实验室管理系统核心代码
这是一套基于Java 17、Spring Boot 3.2和Flink 1.18开发的智慧实验室管理系统核心代码。系统涵盖多协议设备接入(支持OPC UA、MQTT等12种工业协议)、实时异常检测(Flink流处理引擎实现设备状态监控)、强化学习调度(Q-Learning算法优化资源分配)、三维可视化(JavaFX与WebGL渲染实验室空间)、微服务架构(Spring Cloud构建分布式体系)及数据湖建设(Spark构建实验室数据仓库)。实际应用中,该系统显著提升了设备调度效率(响应时间从46分钟降至9秒)、设备利用率(从41%提升至89%),并大幅减少实验准备时间和维护成本。
306 0
|
1月前
|
JavaScript Java 关系型数据库
基于springboot的项目管理系统
本文探讨项目管理系统在现代企业中的应用与实现,分析其研究背景、意义及现状,阐述基于SSM、Java、MySQL和Vue等技术构建系统的关键方法,展现其在提升管理效率、协同水平与风险管控方面的价值。