springBoot-mybatis逆向工程

Javaweb开发之-mybatis逆向工程

    • springBoot+mybatis逆向工程
    • springBoot+mybatis逆向工程——错误注意1
    • springBoot+mybatis逆向工程——错误注意2
    • springBoot+mybatis逆向工程——错误注意3

springBoot+mybatis逆向工程

Intellij IDEA为例,先创建springboot工程。 然后添加如下所示的依赖。

  1. pom.xml文件
    任何一个maven项目启动的时候,第一步首先去遍历加载pom.xml文件,
    首先是标签内容,(常见错误–>错误注意1
    其次是标签内容,(常见错误–>错误注意2
    再次是挂载的标签的内容,位于标签内;(常见错误–>错误注意3


    4.0.0
   
        org.springframework.boot
        spring-boot-starter-parent
        2.0.4.RELEASE
         
    
    com.yiche.points
    order-v2
    1.0.0
    order-v2
    车币商城订单服务2.0
    
        UTF-8
        UTF-8
        1.8
    
    
        
        
            org.mybatis.spring.boot
            mybatis-spring-boot-starter
            1.3.0
        
        
        
            org.mybatis.generator
            mybatis-generator-core
            1.3.2
        
    
    
        
            
                org.springframework.boot
                spring-boot-maven-plugin
            
            
            
                org.mybatis.generator
                mybatis-generator-maven-plugin
                1.3.2
                
                    ${basedir}/src/main/resources/generatorConfig.xml
                    true
                    true
                
            
        
    


  1. application.yml文件中配置数据源
spring:
  datasource:
    driver-class-name: com.mysql.jdbc.Driver
    url: jdbc:mysql://192.168.15.115:3306/mall
    username: root
    password: 123456
server:
  port: 8080
  1. 在resources文件夹下创建generatorConfig.xml文件




    
    
    

    

        
        
            
            
            
            
        

        
        
        

        
        
        
            
            
        

        
        
            
            
            
            
        

        
        
            
            
        

        
        
            
            
        

        
        

 
        
        
        
        
            
            
        
  1. 启动逆向工程生成实体类和mapper文件
    使用maven配置生成代码
    命令行是mybatis-generator:generate -e
    在IntelliJ IDEA中的操作步骤如下:
    首先选择Run->Edit Configurations…
    然后点击左上角的“+”号,选择Maven
    最后在Working directory中填入你项目的根目录,然后在下面的Command line中填入mybatis-generator:generate -e。点击OK即可。
    如下图所示:
    springBoot-mybatis逆向工程_第1张图片

springBoot+mybatis逆向工程——错误注意1

很多互联网的朋友,在使用maven的构建项目的时候,对Maven的运行机制不了解;
例如:我所构建的项目是一个maven的聚合项目,他的父项目是一个类型为pom项目,不是jar或者war;
这时是不能用generator进行逆向生成的;
代码如下:



    4.0.0
    
        com.yiche.points
        mobile
        1.0.0
        
    
    com.yiche.points
    order-v2
    1.0.0
    order-v2
    车币商城订单服务2.0
    
        UTF-8
        UTF-8
        1.8
    
    
        
        
            org.mybatis.spring.boot
            mybatis-spring-boot-starter
            1.3.0
        
        
        
            org.mybatis.generator
            mybatis-generator-core
            1.3.2
        
    
    
        
            
                org.springframework.boot
                spring-boot-maven-plugin
            
            
            
                org.mybatis.generator
                mybatis-generator-maven-plugin
                1.3.2
                
                    ${basedir}/src/main/resources/generatorConfig.xml
                    true
                    true
                
            
        
    

这时generator启动报错,报错新如下(哈哈,这时细心的同学就发现内容变了):

[INFO] Error stacktraces are turned on.
[INFO] Scanning for projects...
[ERROR] [ERROR] Some problems were encountered while processing the POMs:
[FATAL] Non-resolvable parent POM for com.yiche.points:order-v2:1.0.0: Failure to find com.yiche.points:mobile:pom:1.0.0 in http://maven.bitautotech.com/repository/WP-Public/ was cached in the local repository, resolution will not be reattempted until the update interval of yiche-wp-public has elapsed or updates are forced and 'parent.relativePath' points at no local POM @ line 5, column 13
 @ 
[ERROR] The build could not read 1 project -> [Help 1]
org.apache.maven.project.ProjectBuildingException: Some problems were encountered while processing the POMs:
[FATAL] Non-resolvable parent POM for com.yiche.points:order-v2:1.0.0: Failure to find com.yiche.points:mobile:pom:1.0.0 in http://maven.bitautotech.com/repository/WP-Public/ was cached in the local repository, resolution will not be reattempted until the update interval of yiche-wp-public has elapsed or updates are forced and 'parent.relativePath' points at no local POM @ line 5, column 13

	at org.apache.maven.project.DefaultProjectBuilder.build(DefaultProjectBuilder.java:422)
	at org.apache.maven.graph.DefaultGraphBuilder.collectProjects(DefaultGraphBuilder.java:419)
	at org.apache.maven.graph.DefaultGraphBuilder.getProjectsForMavenReactor(DefaultGraphBuilder.java:410)
	at org.apache.maven.graph.DefaultGraphBuilder.build(DefaultGraphBuilder.java:83)
	at org.apache.maven.DefaultMaven.buildGraph(DefaultMaven.java:491)
	at org.apache.maven.DefaultMaven.doExecute(DefaultMaven.java:219)
	at org.apache.maven.DefaultMaven.doExecute(DefaultMaven.java:193)
	at org.apache.maven.DefaultMaven.execute(DefaultMaven.java:106)
	at org.apache.maven.cli.MavenCli.execute(MavenCli.java:863)
	at org.apache.maven.cli.MavenCli.doMain(MavenCli.java:288)
	at org.apache.maven.cli.MavenCli.main(MavenCli.java:199)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.lang.reflect.Method.invoke(Method.java:498)
	at org.codehaus.plexus.classworlds.launcher.Launcher.launchEnhanced(Launcher.java:289)
	at org.codehaus.plexus.classworlds.launcher.Launcher.launch(Launcher.java:229)
	at org.codehaus.plexus.classworlds.launcher.Launcher.mainWithExitCode(Launcher.java:415)
	at org.codehaus.plexus.classworlds.launcher.Launcher.main(Launcher.java:356)
	at org.codehaus.classworlds.Launcher.main(Launcher.java:47)
[ERROR]   
[ERROR]   The project com.yiche.points:order-v2:1.0.0 (/Users/liyang9/IdeaProjects/points-mobile/order-v2/pom.xml) has 1 error
[ERROR]     Non-resolvable parent POM for com.yiche.points:order-v2:1.0.0: Failure to find com.yiche.points:mobile:pom:1.0.0 in http://maven.bitautotech.com/repository/WP-Public/ was cached in the local repository, resolution will not be reattempted until the update interval of yiche-wp-public has elapsed or updates are forced and 'parent.relativePath' points at no local POM @ line 5, column 13 -> [Help 2]
org.apache.maven.model.resolution.UnresolvableModelException: Failure to find com.yiche.points:mobile:pom:1.0.0 in http://maven.bitautotech.com/repository/WP-Public/ was cached in the local repository, resolution will not be reattempted until the update interval of yiche-wp-public has elapsed or updates are forced
	at org.apache.maven.project.ProjectModelResolver.resolveModel(ProjectModelResolver.java:197)
	at org.apache.maven.project.ProjectModelResolver.resolveModel(ProjectModelResolver.java:246)
	at org.apache.maven.model.building.DefaultModelBuilder.readParentExternally(DefaultModelBuilder.java:1000)
	at org.apache.maven.model.building.DefaultModelBuilder.readParent(DefaultModelBuilder.java:800)
	at org.apache.maven.model.building.DefaultModelBuilder.build(DefaultModelBuilder.java:329)
	at org.apache.maven.project.DefaultProjectBuilder.build(DefaultProjectBuilder.java:469)
	at org.apache.maven.project.DefaultProjectBuilder.build(DefaultProjectBuilder.java:438)
	at org.apache.maven.project.DefaultProjectBuilder.build(DefaultProjectBuilder.java:401)
	at org.apache.maven.graph.DefaultGraphBuilder.collectProjects(DefaultGraphBuilder.java:419)
	at org.apache.maven.graph.DefaultGraphBuilder.getProjectsForMavenReactor(DefaultGraphBuilder.java:410)
	at org.apache.maven.graph.DefaultGraphBuilder.build(DefaultGraphBuilder.java:83)
	at org.apache.maven.DefaultMaven.buildGraph(DefaultMaven.java:491)
	at org.apache.maven.DefaultMaven.doExecute(DefaultMaven.java:219)
	at org.apache.maven.DefaultMaven.doExecute(DefaultMaven.java:193)
	at org.apache.maven.DefaultMaven.execute(DefaultMaven.java:106)
	at org.apache.maven.cli.MavenCli.execute(MavenCli.java:863)
	at org.apache.maven.cli.MavenCli.doMain(MavenCli.java:288)
	at org.apache.maven.cli.MavenCli.main(MavenCli.java:199)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.lang.reflect.Method.invoke(Method.java:498)
	at org.codehaus.plexus.classworlds.launcher.Launcher.launchEnhanced(Launcher.java:289)
	at org.codehaus.plexus.classworlds.launcher.Launcher.launch(Launcher.java:229)
	at org.codehaus.plexus.classworlds.launcher.Launcher.mainWithExitCode(Launcher.java:415)
	at org.codehaus.plexus.classworlds.launcher.Launcher.main(Launcher.java:356)
	at org.codehaus.classworlds.Launcher.main(Launcher.java:47)
Caused by: org.eclipse.aether.resolution.ArtifactResolutionException: Failure to find com.yiche.points:mobile:pom:1.0.0 in http://maven.bitautotech.com/repository/WP-Public/ was cached in the local repository, resolution will not be reattempted until the update interval of yiche-wp-public has elapsed or updates are forced
	at org.eclipse.aether.internal.impl.DefaultArtifactResolver.resolve(DefaultArtifactResolver.java:444)
	at org.eclipse.aether.internal.impl.DefaultArtifactResolver.resolveArtifacts(DefaultArtifactResolver.java:246)
	at org.eclipse.aether.internal.impl.DefaultArtifactResolver.resolveArtifact(DefaultArtifactResolver.java:223)
	at org.eclipse.aether.internal.impl.DefaultRepositorySystem.resolveArtifact(DefaultRepositorySystem.java:294)
	at org.apache.maven.project.ProjectModelResolver.resolveModel(ProjectModelResolver.java:193)
	... 26 more
Caused by: org.eclipse.aether.transfer.ArtifactNotFoundException: Failure to find com.yiche.points:mobile:pom:1.0.0 in http://maven.bitautotech.com/repository/WP-Public/ was cached in the local repository, resolution will not be reattempted until the update interval of yiche-wp-public has elapsed or updates are forced
	at org.eclipse.aether.internal.impl.DefaultUpdateCheckManager.newException(DefaultUpdateCheckManager.java:231)
	at org.eclipse.aether.internal.impl.DefaultUpdateCheckManager.checkArtifact(DefaultUpdateCheckManager.java:206)
	at org.eclipse.aether.internal.impl.DefaultArtifactResolver.gatherDownloads(DefaultArtifactResolver.java:585)
	at org.eclipse.aether.internal.impl.DefaultArtifactResolver.performDownloads(DefaultArtifactResolver.java:503)
	at org.eclipse.aether.internal.impl.DefaultArtifactResolver.resolve(DefaultArtifactResolver.java:421)
	... 30 more
[ERROR] 
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR] 
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/ProjectBuildingException
[ERROR] [Help 2] http://cwiki.apache.org/confluence/display/MAVEN/UnresolvableModelException

解决办法
把下面这段代码,

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

去替换掉parent标签的内容,等pojo和mapper生产完毕后,再把原来parent标签的内容换回来;

springBoot+mybatis逆向工程——错误注意2

一定要确保你标签的jar都是存在的,可应用的;
意思就是:
譬如,我们总习惯在工程中把版本号统一定义在父工程的pom.xml文件中,
这时,在子工程中,会因为缺少版本号,而报错,报jar找不到;
所以,一定要确保内的jar文件都可以从maven仓库down下来;

springBoot+mybatis逆向工程——错误注意3

同理,标签下的标签,也一样要确保你的挂载是存在的,可以down下来;
除此之外,还要确保一点,标签不要和父工程的pom.xml的标签冲突,如果你的父pom文件有挂载,这时你的generator逆向工程也是会失败的;
错误代码如下:
父工程(mobile)的pom.xml文件:



    4.0.0
    com.yiche.points
    mobile
    1.0.0
    
        product-api
        order-api
        order-rabbit-receiver
        mall-h5
        order-v2
        product-v2
        payment-api
    
    pom

    mobile
    points h5 website.

    
        org.springframework.boot
        spring-boot-starter-parent
        1.5.10.RELEASE
         
    
    
        UTF-8
        UTF-8
        1.8
        1.8
        1.8
        true
        Edgware.SR2
    
    
        
            org.springframework.boot
            spring-boot-starter-web
        
        
            org.springframework.boot
            spring-boot-starter-actuator
        
        
            org.springframework.cloud
            spring-cloud-starter-eureka
        
        
            org.springframework.cloud
            spring-cloud-starter-config
        
        
            org.springframework.cloud
            spring-cloud-starter-feign
        
        
            org.springframework.cloud
            spring-cloud-starter-sleuth
        
        
            org.springframework.cloud
            spring-cloud-sleuth-stream
        
        
            org.springframework.cloud
            spring-cloud-starter-stream-rabbit
        
        
            org.springframework.cloud
            spring-cloud-starter-bus-amqp
        
        
            org.springframework.boot
            spring-boot-starter-amqp
        
        
            org.springframework.boot
            spring-boot-starter-jdbc
        

        
            mysql
            mysql-connector-java
            runtime
        
        
            com.alibaba
            fastjson
            1.2.41
        

        
            com.alibaba
            druid
            1.0.29
        
        
            com.fasterxml.jackson.core
            jackson-databind
            2.8.10
        
        
            org.springframework.boot
            spring-boot-starter-test
            test
        
        
            com.yiche
            micro-common
            3.0.0
        
        
            ch.qos.logback
            logback-classic
            1.2.3
        
        
            net.logstash.logback
            logstash-logback-encoder
            5.2
        
        
        
            com.yiche.points
            utils
            1.0.0
        

        
        
            org.apache.commons
            commons-lang3
            3.7
        
        
            com.fasterxml.jackson.core
            jackson-annotations
            2.8.4
        
        
            org.hibernate
            hibernate-validator
        
        
            org.springframework.boot
            spring-boot-autoconfigure
        
        
            org.mybatis.spring.boot
            mybatis-spring-boot-starter
            1.3.0
        
        
            io.swagger
            swagger-annotations
            1.5.3
        
    
    
        
            
                org.springframework.cloud
                spring-cloud-dependencies
                ${spring-cloud.version}
                pom
                import
            
        
    
    
        
            yiche-wp
            yiche-wp
            http://maven.bitautotech.com/repository/WP/
        
        
            yiche-wp-snapshots
            yiche-wp-snapshots
            http://maven.bitautotech.com/repository/WP/
        
    
    
   
        
            
                org.mybatis.generator
                mybatis-generator-maven-plugin
                1.3.2
                
                    ${basedir}/src/main/resources/generator/generatorConfig.xml
                    true
                    true
                
            
            
                org.springframework.boot
                spring-boot-maven-plugin
            
        
    


这种挂载方式理想情况下,是对所有子项目都有效的;但是逆向工程的时候,会报你的generator挂载找不到;

正确的挂载方式,是挂载在子项目的pom.xml文件中!
此时,还要注意!如果你的父工程中有内容,一定要先注释掉;不然你子项目的挂载的mybatis-generator一样不会生效!

你可能感兴趣的:(JavaWeb开发)