资讯

精准传达 • 有效沟通

从品牌网站建设到网络营销策划,从策略到执行的一站式服务

SpringBoot集成MyMatis-Generator的使用方法

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

10年积累的成都网站制作、网站设计经验,可以快速应对客户对网站的新想法和需求。提供各种问题对应的解决方案。让选择我们的客户得到更好、更有力的网络服务。我虽然不认识你,你也不认识我。但先网站设计后付款的网站建设流程,更有柯城免费网站建设让你可以放心的选择与我们合作。

SpringBoot集成MyMatis-Generator

1.使用Spring Initializr创建SpringBoot项目

POM依赖

    
        
            org.springframework.boot
            spring-boot-starter-web
        
        
            org.mybatis.spring.boot
            mybatis-spring-boot-starter
            2.1.3
        

        
            MySQL
            mysql-connector-java
            runtime
            5.1.40
        

        
            org.projectlombok
            lombok
            true
        
        
            org.springframework.boot
            spring-boot-starter-test
            test
            
                
                    org.junit.vintage
                    junit-vintage-engine
                
            
        
    

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


        
            
                org.springframework.boot
                spring-boot-maven-plugin
            
            
                org.mybatis.generator
                mybatis-generator-maven-plugin
                1.4.0
                
                    
                        mysql
                        mysql-connector-java
                        5.1.40
                    
                
                
                
                    
                    ${project.basedir}/src/main/resources/generatorConfig.xml
                    true
                    true
                
            
        
        
        
            
                src/main/java
                
                    **/*.xml
                
            
            
                src/main/resources
                
                    **/*.yml
                    **/*.properties
                
            
        
    

注意:

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

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

3.发布时为了将mapper包中sql映射文件xml 导入资源文件中,加入resources。因此导致重新了默认的资源配置,需要额外导入*.yml,*.properties

4. 创建生成器配置文件

路径:

src\main\resources\generatorConfig.xml

内容:





    
        
            

        

        
        

        
            
        

        
        
            
            
        

        
        
            
        

        
        
            
        

        

        
            
        
    

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 getList();
    }

    StudentServiceImpl

    @Service
    public class StudentServiceImpl implements StudentService {
        @Resource
        private StudentMapper studentMapper;
    
        @Override
        public List getList() {
            StudentExample studentExample=new StudentExample();
            List 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 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中引入

    
    
    	org.apache.ibatis
    	ibatis-core
    	3.0
    

    感谢各位的阅读,以上就是“SpringBoot集成MyMatis-Generator的使用方法”的内容了,经过本文的学习后,相信大家对SpringBoot集成MyMatis-Generator的使用方法这一问题有了更深刻的体会,具体使用情况还需要大家实践验证。这里是创新互联,小编将为大家推送更多相关知识点的文章,欢迎关注!


    分享标题:SpringBoot集成MyMatis-Generator的使用方法
    网址分享:http://cdkjz.cn/article/giehcj.html
    多年建站经验

    多一份参考,总有益处

    联系快上网,免费获得专属《策划方案》及报价

    咨询相关问题或预约面谈,可以通过以下方式与我们联系

    业务热线:400-028-6601 / 大客户专线   成都:13518219792   座机:028-86922220