栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java > SpringBoot

SpringBoot使用Mybatis-Generator

SpringBoot 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

SpringBoot使用Mybatis-Generator

本文介绍如何将Maven和Mybatis-Generator配合使用。

image

简介

Mybatis-Generator是Mybatis提供的一个便捷型插件,自动可以为项目生产对应的实体类,Mapper,dao层。

官网文档:http://www.mybatis.org/generator/index.html

入门案例

本文使用SpringBoot结合Mybatis-Generator插件使用,数据库Mysql。

新建项目

新建一个SpringBoot项目。

依赖文件

在项目pom文件中,引入Mybatis-Generator插件,并且引入Mybatis和Mysql依赖。完整pom代码如下:


    4.0.0

    com.dalaoyang
    springboot_generator
    0.0.1-SNAPSHOT
    jar

    springboot_generator
    springboot_generator

    
        org.springframework.boot
        spring-boot-starter-parent
        1.5.15.RELEASE
         
    

    
        UTF-8
        UTF-8
        1.8
    

    
        
            org.springframework.boot
            spring-boot-starter-test
            test
        
        
            org.springframework.boot
            spring-boot-starter-web
        
        
            org.mybatis.spring.boot
            mybatis-spring-boot-starter
            1.3.1
        

        
            org.springframework.boot
            spring-boot-devtools
            runtime
        
        
            mysql
            mysql-connector-java
            runtime
        
    

    
        
            
                org.mybatis.generator
                mybatis-generator-maven-plugin
                1.3.2
                
                    
                        mybatis-generator
                        deploy
                        
                            generate
                        
                    
                
                
                    
                    src/main/resources/mybatis-generator/generatorConfig.xml
                    true
                    true
                
                
                    
                        mysql
                        mysql-connector-java
                        5.1.46
                    
                    
                        org.mybatis.generator
                        mybatis-generator-core
                        1.3.2
                    
                
            
            
                org.springframework.boot
                spring-boot-maven-plugin
                
                    exec
                
            
        
    
配置Mybatis-Generator配置

在pom文件中配置的Mybatis-Generator 工具配置文件的位置新建一个generatorConfig.xml,(本文案例配置的位置是src/main/resources/mybatis-generator/generatorConfig.xml),配置文件代码如下,具体配置需要自行修改至自己的项目:


    
    
    
    
    

    
    
    
        
        
        
        
        
        
        

        
        
        
        
        

        

        
        
            
             
        

        
        
        
        
            
            
        

        
        
            
            
        
        
        
            
        
        
        
            
        
        
        
            
            
            
        
        
            
            
            
        
    
配置application.properties

配置项目的application.properties,其中数据库信息,Mapper地址之前都有过介绍,具体SpringBoot-Mybatis配置可以参考:
《SpringBoot+Mybatis+MySql学习》

本文配置如下:

## mapper xml 文件地址mybatis.mapper-locations=classpath*:mapper
    int deleteByPrimaryKey(Long id);    
    int insert(User record);    
    User selectByPrimaryKey(Long id);    
    List selectAll();    
    int updateByPrimaryKey(User record);
}

UserMapper.xml代码如下:


  
    
    
    
    
  
  
    
    delete from user
    where id = #{id,jdbcType=BIGINT}  
  
    
    
      SELECT LAST_INSERT_ID()    
    insert into user (user_name, user_password)
    values (#{userName,jdbcType=VARCHAR}, #{userPassword,jdbcType=VARCHAR})  
  
    
    update user
    set user_name = #{userName,jdbcType=VARCHAR},
      user_password = #{userPassword,jdbcType=VARCHAR}
    where id = #{id,jdbcType=BIGINT}  
  
    
    select id, user_name, user_password
    from user
    where id = #{id,jdbcType=BIGINT}  
  
    
    select id, user_name, user_password
    from user  



作者:dalaoyang
链接:https://www.jianshu.com/p/a8bfc14a3534


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/235721.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号