springBoot自动集成mybatis-Generator(springboot学习篇)

1.pom.xml中添加依赖

<plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
            <!--添加mybatis generator maven插件-->
            <plugin>
                <groupId>org.mybatis.generator</groupId>
                <artifactId>mybatis-generator-maven-plugin</artifactId>
                <version>1.3.5</version>
                <configuration>
                    <!--generatorConfig.xml位置-->
                    <configurationFile>src/main/resources/mybatis-generator/generatorConfig.xml</configurationFile>
                    <verbose>true</verbose>
                    <overwrite>true</overwrite>
                </configuration>
                <executions>
                    <execution>
                        <id>Generate MyBatis Artifacts</id>
                        <goals>
                            <goal>generate</goal>
                        </goals>
                        <phase>generate-sources</phase>
                    </execution>
                </executions>
                <!--此处必须添加mysql驱动包-->
                <dependencies>
                    <dependency>
                        <groupId>mysql</groupId>
                        <artifactId>mysql-connector-java</artifactId>
                        <scope>runtime</scope>
                        <version>5.1.46</version>
                    </dependency>
                </dependencies>
            </plugin>
2.在/srcmain/resources下创建mybatis-generator文件.文件夹内创建generatorConfig.xml文件和mybatisGeneratorinit.properties文件

generatorConfig.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>
    <!--执行generator插件生成文件的命令: call mvn mybatis-generator:generate -e -->
    <!-- 引入配置文件 -->
    <properties resource="mybatis-generator/mybatisGeneratorinit.properties"/>
    <!--classPathEntry:数据库的JDBC驱动,换成你自己的驱动位置 可选 -->
    <!--<classPathEntry location="E:\mybatis\mysql-connector-java-5.1.24-bin.jar" /> -->

    <!-- 一个数据库一个context -->
    <!--defaultModelType="flat" 大数据字段,不分表 -->
    <context id="MysqlTables" targetRuntime="MyBatis3Simple" defaultModelType="flat">
        <!-- 自动识别数据库关键字,默认false,如果设置为true,根据SqlReservedWords中定义的关键字列表;
        一般保留默认值,遇到数据库关键字(Java关键字),使用columnOverride覆盖 -->
        <property name="autoDelimitKeywords" value="true" />
        <!-- 生成的Java文件的编码 -->
        <property name="javaFileEncoding" value="utf-8" />
        <!-- beginningDelimiter和endingDelimiter:指明数据库的用于标记数据库对象名的符号,比如ORACLE就是双引号,MYSQL默认是`反引号; -->
        <property name="beginningDelimiter" value="`" />
        <property name="endingDelimiter" value="`" />

        <!-- 格式化java代码 -->
        <property name="javaFormatter" value="org.mybatis.generator.api.dom.DefaultJavaFormatter"/>
        <!-- 格式化XML代码 -->
        <property name="xmlFormatter" value="org.mybatis.generator.api.dom.DefaultXmlFormatter"/>
        <plugin type="org.mybatis.generator.plugins.SerializablePlugin" />

        <plugin type="org.mybatis.generator.plugins.ToStringPlugin" />

        <!-- 注释 -->
        <commentGenerator >
            <property name="suppressAllComments" value="true"/><!-- 是否取消注释 -->
            <property name="suppressDate" value="true" /> <!-- 是否生成注释代时间戳-->
        </commentGenerator>

        <!-- jdbc连接 -->
        <jdbcConnection driverClass="${jdbc_driver}" connectionURL="${jdbc_url}" userId="${jdbc_user}" password="${jdbc_password}" />
        <!-- 类型转换 -->
        <javaTypeResolver>
            <!-- 是否使用bigDecimal, false可自动转化以下类型(Long, Integer, Short, etc.) -->
            <property name="forceBigDecimals" value="false"/>
        </javaTypeResolver>

        <!-- 生成实体类地址 -->
        <javaModelGenerator targetPackage="com.dx.entity" targetProject="${models}" >
            <property name="enableSubPackages" value="false"/>
            <property name="trimStrings" value="true"/>
        </javaModelGenerator>

        <!-- 生成mapxml文件 -->
        <sqlMapGenerator targetPackage="mapper" targetProject="${resources}" >
            <property name="enableSubPackages" value="false" />
        </sqlMapGenerator>

        <!-- 生成mapxml对应client,也就是接口dao -->
        <javaClientGenerator targetPackage="com.dx.mapper" targetProject="${dao}" type="XMLMAPPER" >
            <property name="enableSubPackages" value="true" />
        </javaClientGenerator>

        <!-- table可以有多个,每个数据库中的表都可以写一个table,tableName表示要匹配的数据库表,也可以在tableName属性中通过使用%通配符来匹配所有数据库表,只有匹配的表才会自动生成文件 -->
        <!--如果想生成一个表则tableName="table_name"-->
        <table tableName="%"
               enableCountByExample="true"
               enableUpdateByExample="true"
               enableDeleteByExample="true"
               enableSelectByExample="true"
               selectByExampleQueryId="true">
            <property name="useActualColumnNames" value="false" />
            <!-- 数据库表主键 -->
            <generatedKey column="id" sqlStatement="Mysql" identity="true" />
        </table>
    </context>
</generatorConfiguration>

以上文件修改生成的文件地址就可以了,如图

image-20210108161918748 style="zoom:75%;

mybatisGeneratorinit.properties 的内容

jdbc_driver=com.mysql.jdbc.Driver
jdbc_url=jdbc:mysql://localhost:3306/partys?useUnicode=true&characterEncoding=utf-8&useSSL=true
jdbc_user=root
jdbc_password=root
models=E:\\java\\party_spring\\src\\main\\java
resources=E:\\java\\party_spring\\src\\main\\resources
dao=E:\\java\\party_spring\\src\\main\\java

顺带把application.yml的一起给你们吧

server:
  port: 8081
  servlet:
    context-path: /
    session:
      timeout: 7200
spring:
  datasource:
    driverClassName: com.mysql.jdbc.Driver
    url: jdbc:mysql://localhost:3306/message
    jdbc:
    username: root
    password: root
  thymeleaf:
    cache: true
    prefix: classpath:/templates/views/
    suffix: .html
    mode: HTML5
    encoding: UTF-8

application.properties

#mysql drive
spring.datasource.driverClassName = com.mysql.jdbc.Driver
spring.datasource.url = jdbc:mysql://localhost:3306/zentao?useUnicode=true&characterEncoding=utf-8
spring.datasource.username = root
spring.datasource.password =root
# view
#spring.mvc.view.prefix=classpath:/templates/
#spring.mvc.view.suffix=.html
#
spring.thymeleaf.cache=true
spring.thymeleaf.prefix=classpath:/templates/views/
spring.thymeleaf.suffix=.html
spring.thymeleaf.mode=HTML5
spring.thymeleaf.encoding=UTF-8

修改之后点击右上角Maven调出Plugins/mybatis-generator/mybatis-generator:generate执行即可

image-20210108163528543
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Spring Boot + MyBatis + Vue 是一种经典的 Web 开发技术栈,常用于后端服务与前端页面的搭建。 其中,Spring Boot 是一个快速开发框架,可以快速构建后端服务;MyBatis 是一个优秀的 ORM 框架,可以方便地操作数据库;Vue 是一个流行的前端框架,可以快速构建单页面应用。 基于 Spring Boot + MyBatis + Vue 的开发流程一般如下: 1. 使用 Spring Initializr 创建一个 Spring Boot 项目,添加 MyBatis 和相关依赖。 2. 配置 MyBatis 数据库连接信息、Mapper 映射文件等。 3. 编写 Controller 层,处理请求并返回数据。 4. 编写 Service 层,实现业务逻辑,并调用 Mapper 层操作数据库。 5. 编写 Vue 前端页面,使用 axios 等库发送请求,并处理响应数据。 6. 后端服务与前端页面进行集成,前端页面可以通过 Ajax 等方式调用后端服务。 7. 部署项目到服务器上,测试和运行。 在开发过程中,可以使用一些常用的工具和框架来提高开发效率和代码质量,例如: 1. 使用 MyBatis Generator 自动生成 Mapper 映射文件和 Model 类。 2. 使用 Swagger UI 自动生成 API 文档,方便前端调用接口。 3. 使用 Vue CLI 脚手架快速搭建前端项目,并集成 Element UI 等组件库。 4. 使用 Git 进行版本控制,便于多人协作和代码管理。 综上所述,基于 Spring Boot + MyBatis + Vue 的技术栈可以快速构建 Web 应用程序,提高开发效率和代码质量。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值