使用Intellij Idea创建spring boot+mybatis+MyBatis Generator

打开idea->new->project
1.选择Spring initializr,其他默认,点击下一步


使用Intellij Idea创建spring boot+mybatis+MyBatis Generator_第1张图片

2.项目配置


使用Intellij Idea创建spring boot+mybatis+MyBatis Generator_第2张图片

Group:对应pom文件中的groupId,项目组织的唯一标识,对应Java包的结构
Artifact:对应pom文件的artifactId,项目唯一标识,对应项目名称
Type:Maven构建的,选择第一个Maven Project
Packaging:打包类型,打包成Jar文件
Java Version:jdk版本,选择1.8
Language:开发语言,选择Java
Version:项目版本,对应pom文件的version
name:项目名称
Description:项目描述,对应pom文件的description
Package:包名
3.选择spring boot版本


使用Intellij Idea创建spring boot+mybatis+MyBatis Generator_第3张图片

4.填写项目名称,项目存放目录,点击Finish完成


使用Intellij Idea创建spring boot+mybatis+MyBatis Generator_第4张图片

5.删除下面三个无关的文件


使用Intellij Idea创建spring boot+mybatis+MyBatis Generator_第5张图片

6.最后的项目结构


使用Intellij Idea创建spring boot+mybatis+MyBatis Generator_第6张图片

查看自动生成的pom.xml文件



    4.0.0

    com.example
    demo
    0.0.1-SNAPSHOT
    jar

    demo
    Demo project for Spring Boot

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

    
        UTF-8
        UTF-8
        1.8
    

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

        
            org.springframework.boot
            spring-boot-starter-test
            test
        
    

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




7.新建一个controller测试

@RestController
public class HelloController {
    @RequestMapping(value = "/hello")
    public String hello(){
        return "hello spring boot";
    }
}

@RestController的意思就是controller里面的方法都一json格式输出
8.启动DemoApplication


9.到浏览器中输入http://localhost:8080/hello


至此一个spring boot项目创建完毕
10.接下来在pom.xml文件中加入mysql,mybatis和mybaits generator依赖



    4.0.0

    com.dwzzb
    dwzzb
    0.0.1-SNAPSHOT
    jar

    dwzzb
    dwzzb web project

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

    
        UTF-8
        UTF-8
        1.8
    

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

        
            org.springframework.boot
            spring-boot-starter-test
            test
        

        
            org.mybatis.spring.boot
            mybatis-spring-boot-starter
            1.1.1
        
        
            mysql
            mysql-connector-java
            5.1.21
        
    

    
        
            
                org.springframework.boot
                spring-boot-maven-plugin
            
            
            
                org.mybatis.generator
                mybatis-generator-maven-plugin
                1.3.2
                
                    true
                    true
                
                
                    
                        mysql
                        mysql-connector-java
                        5.1.21
                    
                
            
        
    




11.在配置文件application.properties中加入数据库的配置信息

spring.datasource.url=jdbc:mysql://localhost:3306/dbname
spring.datasource.username=root
spring.datasource.password=123456
spring.datasource.driver-class-name=com.mysql.jdbc.Driver

12.创建generatorConfig.xml文件




    
    

    
    

    

        
        
            
            
        

        
        
        


        
        
            
        


        
        

            
            
            
            
            
            
            
            
        

        
        
            
        

        
        
            
        


        

注意注释的部分,这里原本是需要数据库驱动的绝对地址,有些配置是这样写的

 
    

这边已经把对jar包的依赖的classPathEntry配置移到pom中,上面有完整的pom配置。
13.添加一个“RUN运行”,使用maven运行mybatis-generator-maven-plugin插件
点击菜单run中的Edit Configurations,然后点“+”,添加maven,设置如下

使用Intellij Idea创建spring boot+mybatis+MyBatis Generator_第7张图片

14.在run菜单中选择generator


点击绿色小箭头运行

"C:\Program Files\Java\jdk1.8.0_111\bin\java" -Dmaven.multiModuleProjectDirectory=D:\Project\JavaWeb\dwzzb_backend "-Dmaven.home=D:\Program_Files\JetBrains\IntelliJ IDEA 2017.1.2\plugins\maven\lib\maven3" "-Dclassworlds.conf=D:\Program_Files\JetBrains\IntelliJ IDEA 2017.1.2\plugins\maven\lib\maven3\bin\m2.conf" "-javaagent:D:\Program_Files\JetBrains\IntelliJ IDEA 2017.1.2\lib\idea_rt.jar=60424:D:\Program_Files\JetBrains\IntelliJ IDEA 2017.1.2\bin" -Dfile.encoding=UTF-8 -classpath "D:\Program_Files\JetBrains\IntelliJ IDEA 2017.1.2\plugins\maven\lib\maven3\boot\plexus-classworlds-2.5.2.jar" org.codehaus.classworlds.Launcher -Didea.version=2017.1.2 -s D:\Program_Files\apache-maven-3.5.0\conf\maven-repo-settings-ali.xml mybatis-generator:generate -e
[INFO] Error stacktraces are turned on.
[INFO] Scanning for projects...
Downloading: http://maven.aliyun.com/nexus/content/groups/public/org/springframework/boot/spring-boot-maven-plugin/1.5.6.RELEASE/spring-boot-maven-plugin-1.5.6.RELEASE.pom
Downloaded: http://maven.aliyun.com/nexus/content/groups/public/org/springframework/boot/spring-boot-maven-plugin/1.5.6.RELEASE/spring-boot-maven-plugin-1.5.6.RELEASE.pom (0 B at 0.0 KB/sec)
Downloading: http://maven.aliyun.com/nexus/content/groups/public/org/springframework/boot/spring-boot-tools/1.5.6.RELEASE/spring-boot-tools-1.5.6.RELEASE.pom
Downloaded: http://maven.aliyun.com/nexus/content/groups/public/org/springframework/boot/spring-boot-tools/1.5.6.RELEASE/spring-boot-tools-1.5.6.RELEASE.pom (0 B at 0.0 KB/sec)
Downloading: http://maven.aliyun.com/nexus/content/groups/public/org/springframework/boot/spring-boot-maven-plugin/1.5.6.RELEASE/spring-boot-maven-plugin-1.5.6.RELEASE.jar
Downloaded: http://maven.aliyun.com/nexus/content/groups/public/org/springframework/boot/spring-boot-maven-plugin/1.5.6.RELEASE/spring-boot-maven-plugin-1.5.6.RELEASE.jar (0 B at 0.0 KB/sec)
[INFO]                                                                         
[INFO] ------------------------------------------------------------------------
[INFO] Building demo 0.0.1-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO] 
[INFO] --- mybatis-generator-maven-plugin:1.3.2:generate (default-cli) @ dwzzb ---
[INFO] Connecting to the Database
[INFO] Introspecting table advertisement
log4j:WARN No appenders could be found for logger (org.mybatis.generator.internal.db.DatabaseIntrospector).
log4j:WARN Please initialize the log4j system properly.
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.
[INFO] Generating Record class for table advertisement
[INFO] Generating Mapper Interface for table advertisement
[INFO] Generating SQL Map for table advertisement
[INFO] Saving file AdvertisementMapper.xml
[INFO] Saving file Advertisement.java
[INFO] Saving file AdvertisementMapper.java
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 2.850 s
[INFO] Finished at: 2017-08-12T08:55:22+08:00
[INFO] Final Memory: 15M/166M
[INFO] ------------------------------------------------------------------------

Process finished with exit code 0

看到Build success之后,你会看到generator帮你生成好了dao,model和mapper文件

使用Intellij Idea创建spring boot+mybatis+MyBatis Generator_第8张图片
image.png

完!

你可能感兴趣的:(使用Intellij Idea创建spring boot+mybatis+MyBatis Generator)