MyBatis自动生成Dao层

MyBatis自动生成Dao层

 

MyBatis自动生成Dao层,从数据库的表映射到Java的数据层。包括 Mapper接口的定义,Mapper文件中的sql脚本以及接口中用到的对象

参考地址:

http://mybatis.org/generator/running/runningWithMaven.html

http://mybatis.org/generator/configreference/xmlconfig.html

 

新建Maven项目(我的是基于SpringBoot)

1.配置pom.xml  主要添加build那一块



    4.0.0
    
        org.springframework.boot
        spring-boot-starter-parent
        2.2.5.RELEASE
         
    
    com.imodule.product
    product
    0.0.1-SNAPSHOT
    product
    Demo project for Spring Boot

    
        UTF-8
        0.0.1-SNAPSHOT
        2.3.0

    

    
        
            junit
            junit
            test
        
  
        
            org.springframework.boot
            spring-boot-autoconfigure
        

        
            org.springframework.boot
            spring-boot-starter-test
            test
            
                
                    jul-to-slf4j
                    org.slf4j
                
                
                    log4j-to-slf4j
                    org.apache.logging.log4j
                
            
        

    

    
        
            
                org.springframework.boot
                spring-boot-maven-plugin
            

            
            
                org.mybatis.generator
                mybatis-generator-maven-plugin
                1.4.0
                
                    
                    src/main/resources/generatorConfig.xml
                    true
                    true
                
                
                    
                        Generate MyBatis Artifacts
                        
                            generate
                        
                    
                
                
                    
                        org.mybatis.generator
                        mybatis-generator-core
                        1.4.0
                    
                

            
        
    

  

2.生成dao的配置文件 generatorConfig.xml 

根据需求更改数据库配置信息,驱动包信息,目录数据 以及表信息即可






    
    

    

        
        
            
            
        

        
        
        

        
        
            
            
            
            
            
            
            
            
        

        
        
            
        

        
        
            
        

        
        

 

生成步骤:

IDEA- 点击Maven - Plugins - 选中 Mybatis-generator 双击即可

MyBatis自动生成Dao层_第1张图片

 

控制台输出日志:

MyBatis自动生成Dao层_第2张图片

 

 

如果出现以下类似错误

[WARNING] Table configuration with catalog null, schema null, and table XXX did not resolve to any tables

请注意表明的大小写是否与数据库一致 ,我将表名改为大写就都欧克啦!!!

 

 生成前的目录结构:

MyBatis自动生成Dao层_第3张图片

 

 

生成后的目录 (红色框框标记处均为自动生成的)

MyBatis自动生成Dao层_第4张图片

 

到这里就欧克啦,后期要使用的时候  maven 打包一下即可啦,然后在别的项目直接引用即可!

这里有个不好的点是所有的表要一一配置,我再康康有没有啥更快捷的办法,能对整个数据库自动生成的哈哈哈哈 找到了我再来更新~

 

你可能感兴趣的:(MyBatis自动生成Dao层)