SpringBoot集成MyMatis-Generator的使用方法

发布时间:2021-07-29 19:09:40 作者:chen
来源:亿速云 阅读:263

这篇文章主要讲解了“SpringBoot集成MyMatis-Generator的使用方法”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“SpringBoot集成MyMatis-Generator的使用方法”吧!

SpringBoot集成MyMatis-Generator

1.使用Spring Initializr创建SpringBoot项目

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 

根据实际数据源配置

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中增加插件坐标

<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 映射文件 放在 代码中时需要配置
        此处扫描 src/main/java 中所有xml文件发布时导入resources中。
        因为用了资源配置重写了系统默认的配置,必须配置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.发布时为了将mapper包中sql映射文件xml 导入资源文件中,加入resources。因此导致重新了默认的资源配置,需要额外导入*.yml,*.properties

4. 创建生成器配置文件

路径:

src\main\resources\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>

    <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 实体和Example文件指定位置

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

3. javaClientGenerator 接口文件所在位置

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

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

    5. 测试生成结果

    5.1 点击

    SpringBoot集成MyMatis-Generator的使用方法

    5.2 .  生成

    SpringBoot集成MyMatis-Generator的使用方法

    6. 在启动类中配置扫描接口与映射配置文件

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

    7. 对生成的实体增加toString()方便测试,生成的Mapper接口 Example不用动

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

    9.编写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. 测试类

    这里引用 Mapper接口 使用 @Resource 避免出现警告红线。

    @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. 测试结果

    SpringBoot集成MyMatis-Generator的使用方法

    12.  解决dtd文件红标问题

    12.1.根据约束文件地址下载文件到本地

    mybatis-3-mapper.dtd

    mybatis-generator-config_1_0.dtd

    12.2.  IDEA中设置

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

    URI         为 网络地址

    Location 为 本地文件地址

    SpringBoot集成MyMatis-Generator的使用方法

    13. 编译时问题 程序包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的使用方法”的内容了,经过本文的学习后,相信大家对SpringBoot集成MyMatis-Generator的使用方法这一问题有了更深刻的体会,具体使用情况还需要大家实践验证。这里是亿速云,小编将为大家推送更多相关知识点的文章,欢迎关注!

    推荐阅读:
    1. SpringBoot集成Graylog
    2. SpringBoot如何集成swagger

    免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

    spring boot spring

    上一篇:docker中如何安装vim配置国内镜像源

    下一篇:Linux常见的问题有哪些

    相关阅读

    您好,登录后才能下订单哦!

    密码登录
    登录注册
    其他方式登录
    点击 登录注册 即表示同意《亿速云用户服务条款》