SpringBoot2.x集成mybatis-generator

简述

mybatis是一个半自动化的orm框架,就是mybaitis只支持数据库查出的数据映射到model类上,而实体到数据库的映射需要自己编写对应关系,mybatis非常灵活,可以随心所欲的编写自己的sql语句来实现复杂的数据库操作,但是需要一大堆的配置文件,以及各种mapper和dao和实体的关联,导致使用mybatis还是不够简洁,后来mybatis也发现了这个弊端,开发了mybatis-generator工具来自动化生成实体类、mapper配置文件、dao层代码来减轻开发工作量

实现集成

创建一个SpringBoot项目,并配置resources下配置文件

#数据库配置
spring:
  datasource:
    driver-class-name: com.mysql.jdbc.Driver
    url: jdbc:mysql://localhost:3306/demo
    username: root
    password: root
#mapper配置
mybatis:
  mapper-locations: classpath:mapper/*.xml

在pom.xml引入mybatis-generator-maven-plugin插件和依赖org.mybatis.generator

<dependency>
            <groupId>org.mybatis.generatorgroupId>
            <artifactId>mybatis-generator-coreartifactId>
            <version>1.3.5version>
        dependency>
<build>
        <plugins>
            <plugin>
                <groupId>org.springframework.bootgroupId>
                <artifactId>spring-boot-maven-pluginartifactId>
                <configuration>
                    <includeSystemScope>trueincludeSystemScope>
                configuration>
            plugin>

            <plugin>
                <groupId>org.mybatis.generatorgroupId>
                <artifactId>mybatis-generator-maven-pluginartifactId>
                <version>1.3.5version>
                <configuration>
                    <configurationFile>${basedir}/src/main/resources/generator/generatorConfig.xmlconfigurationFile>
                    <overwrite>trueoverwrite>
                    <verbose>trueverbose>
                    <skip>falseskip>
                configuration>
            plugin>
        plugins>
    build>

在resources下创建generator目录,在目录下创建配置文件generatorConfig.xml



<generatorConfiguration>
    
    <classPathEntry
            location="数据库的JDBC驱动位置 "/>
    <context id="DB2Tables" targetRuntime="MyBatis3">
        <commentGenerator>
            <property name="suppressDate" value="true"/>
            
            <property name="suppressAllComments" value="true"/>
        commentGenerator>
        
        <jdbcConnection driverClass="自填"
                        connectionURL="自填"
                        userId="自填" password="自填">
        jdbcConnection>
        <javaTypeResolver>
            <property name="forceBigDecimals" value="false"/>
        javaTypeResolver>
        
        <javaModelGenerator targetPackage="xxx.model" targetProject="src/main/java">
            <property name="enableSubPackages" value="true"/>
            <property name="trimStrings" value="true"/>
        javaModelGenerator>
        
        <sqlMapGenerator targetPackage="resources.mapper" targetProject="src/main">
            <property name="enableSubPackages" value="true"/>
        sqlMapGenerator>
        
        <javaClientGenerator type="XMLMAPPER" targetPackage="xxx.dao" targetProject="src/main/java">
            <property name="enableSubPackages" value="true"/>
        javaClientGenerator>
        

        <table tableName="" domainObjectName="" enableCountByExample="false"
               enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false"
               selectByExampleQueryId="false">
        table>
    context>
generatorConfiguration>

运行
1、项目下执行maven命令

mvn mybatis-generator:generate

2、maven插件目录下点击命令
SpringBoot2.x集成mybatis-generator_第1张图片

集成完成

你可能感兴趣的:(#,框架.java)