一聚教程网:一个值得你收藏的教程网站

热门教程

springboot集成Mybatis代码示例

时间:2022-06-29 02:21:06 编辑:袖梨 来源:一聚教程网

本篇文章小编给大家分享一下springboot集成Mybatis代码示例,文章代码介绍的很详细,小编觉得挺不错的,现在分享给大家供大家参考,有需要的小伙伴们可以来看看。

springboot集成Mybatis

第一步:

添加Mybatis依赖



    org.mybatis.spring.boot
    mybatis-spring-boot-starter
    2.0.0

第二步:

添加mysql驱动

之所以没有版本号,因为它继承的是父工程的。当然你也可以自己指定一个版本号



    mysql
    mysql-connector-java
    

使用Mybatis提供的逆向工程 生成实体bean,映射文件,DAO接口

第一步:

在项目根目录创建GeneratorMapper.xml 文件,配置如下:




    
    
    
    
        
            
        
        
        
        
        
        
            
            
        
        
        
            
        
        
        
            
        
 
        
        

第二步

在pom.xml中配置如下:




    org.mybatis.generator
    mybatis-generator-maven-plugin
    1.3.7
    
        
            mysql
            mysql-connector-java
            8.0.15
        
    
    
        
        GeneratorMapper.xml
        true
        true
    

第三步

双击如下执行时,

这里有一个坑,我的在这里报错了,报错信息如下:

Failed to execute goal org.mybatis.generator:mybatis-generator-maven-plugin:1.3.7:generate (default-cli) on project sprint_boot_01: Communications link failure

百分之99是驱动和配置数据库信息中的connectionURL配置错了,我的解决方案:

driverClass="com.mysql.cj.jdbc.Driver"
connectionURL="jdbc:mysql://127.0.0.1:3306/java_pro?serverTimezone=Asia/Shanghai&useUnicode=true&characterEncoding=utf8&useSSL=false"

然后在双击执行,成功:

生成的目录如下: