溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

SpringBoot集成MyMatis-Generator的使用方法

發(fā)布時(shí)間:2021-07-29 19:09:40 來(lái)源:億速云 閱讀:262 作者:chen 欄目:大數(shù)據(jù)

這篇文章主要講解了“SpringBoot集成MyMatis-Generator的使用方法”,文中的講解內(nèi)容簡(jiǎn)單清晰,易于學(xué)習(xí)與理解,下面請(qǐng)大家跟著小編的思路慢慢深入,一起來(lái)研究和學(xué)習(xí)“SpringBoot集成MyMatis-Generator的使用方法”吧!

SpringBoot集成MyMatis-Generator

1.使用Spring Initializr創(chuàng)建SpringBoot項(xiàng)目

POM依賴

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>2.1.3</version>
        </dependency>

        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <scope>runtime</scope>
            <version>5.1.40</version>
        </dependency>

        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
    </dependencies>

2. 配置application.yml 

根據(jù)實(shí)際數(shù)據(jù)源配置

spring:
  datasource:
    driver-class-name: com.mysql.jdbc.Driver
    url: jdbc:mysql://localhost:3306/spring_mybatis_dljd?useUnicode=true&characterEncoding=UTF-8&zeroDateTimeBehavior=convertToNull&serverTimezone=GMT%2B8&useSSL=false
    username: root
    password: 123456

3. POM中增加插件坐標(biāo)

<build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
            <plugin>
                <groupId>org.mybatis.generator</groupId>
                <artifactId>mybatis-generator-maven-plugin</artifactId>
                <version>1.4.0</version>
                <dependencies>
                    <dependency>
                        <groupId>mysql</groupId>
                        <artifactId>mysql-connector-java</artifactId>
                        <version>5.1.40</version>
                    </dependency>
                </dependencies>
                <!--指定配置文件的路徑-->
                <configuration>
                    <!-- 配置文件路徑 -->
                    <configurationFile>${project.basedir}/src/main/resources/generatorConfig.xml</configurationFile>
                    <overwrite>true</overwrite>
                    <verbose>true</verbose>
                </configuration>
            </plugin>
        </plugins>
        <!--
        sql 映射文件 放在 代碼中時(shí)需要配置
        此處掃描 src/main/java 中所有xml文件發(fā)布時(shí)導(dǎo)入resources中。
        因?yàn)橛昧速Y源配置重寫(xiě)了系統(tǒng)默認(rèn)的配置,必須配置yml和properties文件
        -->
        <resources>
            <resource>
                <directory>src/main/java</directory>
                <includes>
                    <include>**/*.xml</include>
                </includes>
            </resource>
            <resource>
                <directory>src/main/resources</directory>
                <includes>
                    <include>**/*.yml</include>
                    <include>**/*.properties</include>
                </includes>
            </resource>
        </resources>
    </build>

注意:

1. 插件中使用的 mysql-connector-java 與application.yml 中保持一致

2.configurationFile 中指定了生成器配置文件 generatorConfig.xml的位置

3.發(fā)布時(shí)為了將mapper包中sql映射文件xml 導(dǎo)入資源文件中,加入resources。因此導(dǎo)致重新了默認(rèn)的資源配置,需要額外導(dǎo)入*.yml,*.properties

4. 創(chuàng)建生成器配置文件

路徑:

src\main\resources\generatorConfig.xml

內(nèi)容:

<?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>

    <context id="context" targetRuntime="MyBatis3">
        <commentGenerator>
            <property name="suppressAllComments" value="false"/>

        </commentGenerator>

        <!-- !!!! Database Configurations !!!! -->
        <jdbcConnection driverClass="com.mysql.jdbc.Driver"
                        connectionURL="jdbc:mysql://localhost:3306/spring_mybatis_dljd?useUnicode=true&amp;characterEncoding=UTF-8&amp;zeroDateTimeBehavior=convertToNull&amp;serverTimezone=GMT%2B8&amp;useSSL=false&amp;nullCatalogMeansCurrent=true"
                        userId="root"
                        password="123456"/>

        <javaTypeResolver>
            <property name="forceBigDecimals" value="false"/>
        </javaTypeResolver>

        <!-- !!!! Model Configurations !!!! -->
        <javaModelGenerator targetPackage="com.zhl.springmybatis.pojo" targetProject="src/main/java">
            <property name="enableSubPackages" value="false"/>
            <property name="trimStrings" value="true"/>
        </javaModelGenerator>

        <!-- !!!! Mapper XML Configurations !!!! -->
        <sqlMapGenerator targetPackage="com.zhl.springmybatis.mapper" targetProject="src/main/java">
            <property name="enableSubPackages" value="false"/>
        </sqlMapGenerator>

        <!-- !!!! Mapper Interface Configurations !!!! -->
        <javaClientGenerator targetPackage="com.zhl.springmybatis.mapper" targetProject="src/main/java"
                             type="XMLMAPPER">
            <property name="enableSubPackages" value="false"/>
        </javaClientGenerator>

        <!-- !!!! Table Configurations !!!! -->
<!--        <table tableName="users"

               enableCountByExample="false" enableDeleteByExample="false" enableSelectByExample="false"
               enableUpdateByExample="false"/>-->
        <table tableName="%">
            <generatedKey column="id" sqlStatement="Mysql"/>
        </table>
    </context>
</generatorConfiguration>

1. javaModelGenerator 實(shí)體和Example文件指定位置

2. sqlMapGenerator SQL映射文件*.Mapper.xml 所在位置

3. javaClientGenerator 接口文件所在位置

4. tableName="%" 為生成所有表

     jdbcConnection driverClass :需與application.yml中保持一致

    5. 測(cè)試生成結(jié)果

    5.1 點(diǎn)擊

    SpringBoot集成MyMatis-Generator的使用方法

    5.2 .  生成

    SpringBoot集成MyMatis-Generator的使用方法

    6. 在啟動(dòng)類中配置掃描接口與映射配置文件

    @SpringBootApplication@MapperScan("com.zhl.springmybatis.mapper")public class SpringMybatisApplication {public static void main(String[] args) {
            SpringApplication.run(SpringMybatisApplication.class, args);
        }
    
    }

    7. 對(duì)生成的實(shí)體增加toString()方便測(cè)試,生成的Mapper接口 Example不用動(dòng)

    @Data
    @ToString
    public class Student {....}

    9.編寫(xiě)Service及ServiceImpl

    StudentService:

    package com.zhl.springmybatis.service;
    
    import com.zhl.springmybatis.pojo.Student;
    
    import java.util.List;
    
    public interface StudentService {
        List<Student> getList();
    }

    StudentServiceImpl

    @Service
    public class StudentServiceImpl implements StudentService {
        @Resource
        private StudentMapper studentMapper;
    
        @Override
        public List<Student> getList() {
            StudentExample studentExample=new StudentExample();
            List<Student> list= studentMapper.selectByExample(studentExample);
            for (Student s:list) {
                System.out.println(s);
            }
    
            return list;
        }
    }

    10. 測(cè)試類

    這里引用 Mapper接口 使用 @Resource 避免出現(xiàn)警告紅線。

    @SpringBootTest
    class SpringMybatisApplicationTests {
    
        @Resource
        private StudentMapper studentMapper;
        @Test
        void contextLoads() {
        }
    
        @Test
        public void GetList(){
            StudentExample studentExample=new StudentExample();
            List<Student> list= studentMapper.selectByExample(studentExample);
            for (Student s:list) {
                System.out.println(s);
            }
    
    
        }
    
    }

    11. 測(cè)試結(jié)果

    SpringBoot集成MyMatis-Generator的使用方法

    12.  解決dtd文件紅標(biāo)問(wèn)題

    12.1.根據(jù)約束文件地址下載文件到本地

    mybatis-3-mapper.dtd

    mybatis-generator-config_1_0.dtd

    12.2.  IDEA中設(shè)置

    路徑 File | Settings | Languages & Frameworks | Schemas and DTDs

    URI         為 網(wǎng)絡(luò)地址

    Location 為 本地文件地址

    SpringBoot集成MyMatis-Generator的使用方法

    13. 編譯時(shí)問(wèn)題 程序包org.apache.ibatis.annotations不存在

    pom中引入

    <!-- https://mvnrepository.com/artifact/org.apache.ibatis/ibatis-core -->
    <dependency>
    	<groupId>org.apache.ibatis</groupId>
    	<artifactId>ibatis-core</artifactId>
    	<version>3.0</version>
    </dependency>

    感謝各位的閱讀,以上就是“SpringBoot集成MyMatis-Generator的使用方法”的內(nèi)容了,經(jīng)過(guò)本文的學(xué)習(xí)后,相信大家對(duì)SpringBoot集成MyMatis-Generator的使用方法這一問(wèn)題有了更深刻的體會(huì),具體使用情況還需要大家實(shí)踐驗(yàn)證。這里是億速云,小編將為大家推送更多相關(guān)知識(shí)點(diǎn)的文章,歡迎關(guān)注!

    向AI問(wèn)一下細(xì)節(jié)

    免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

    AI