SpringBoot简单整合Mybatis、并使用Mybatis-Generator自动生成所需代码

下面我们在上次博客中搭建的简单Spring Boot web环境基础上来进行整合mybatis,并且使用Mybatis-Generator自动生成所需代码。
大家在自己练习过程中如果遇到问题可以看看我下一篇博客,我把我出现的所有问题都写在了里面,希望可以帮到大家!

  • 一、在pom.xml中添加对应的依赖

    
         org.springframework.boot
         spring-boot-starter-jdbc
     
     
         mysql
         mysql-connector-java
         runtime
     
     
         org.mybatis.spring.boot
         mybatis-spring-boot-starter
         1.3.2
     
     
         org.mybatis.generator
         mybatis-generator-core
         1.3.5
      
    
  • 二、整理配置文件

     #数据源
     spring.datasource.url=
     jdbc:mysql://127.0.0.1:3306/Mysql?useUnicode=true&characterEncoding=utf-8
     spring.datasource.username=root
     spring.datasource.password=123
     spring.datasource.driver-class-name=com.mysql.jdbc.Driver
     #整合mybatis
     mybatis.mapper-locations=classpath:/mapper/**.xml
    
  • 三、在resources目录下新建generator的配置文件,mybatis-
    generator.xml 注意修改代码中的对应路径改为自己所用的路径

     
    
        
        
        
            
            
            
            
                
            
            
            
                
                
            
            
            
                
            
            
            
                
            
            
            
  • 四、在对应文件夹下创建子文件夹用来分类存放代码
    注意在图中的文件夹已有我写好的代码,你们建好应该是空的
    SpringBoot简单整合Mybatis、并使用Mybatis-Generator自动生成所需代码_第1张图片

  • 五、在utils包下新建Generator.java
    同样的道理,记得替换里面的路径为自己的

    public class Generator {
             public static void main(String[] args) throws Exception{
                 List warnings = new ArrayList();
                 boolean overwrite = true;
                 File configFile = new File("C:/App/IDEA/springBootTest/src/main/resources/mybatis-generator.xml");
                 ConfigurationParser cp = new ConfigurationParser(warnings);
                 Configuration config = cp.parseConfiguration(configFile);
                 DefaultShellCallback callback = new DefaultShellCallback(overwrite);
                 MyBatisGenerator myBatisGenerator = new MyBatisGenerator(config, callback, warnings);
                 myBatisGenerator.generate(null);
             }
         }
    
  • 六、创建数据库方便进行连接测试
    SpringBoot简单整合Mybatis、并使用Mybatis-Generator自动生成所需代码_第2张图片

  • 七、运行Generator.java,在对应的文件夹中产生所需要的代码,如果能顺利产生代码就说明数据库连接成功,而且代码生成成功!

  • 八、在controller包下新建PersonController类,在service包下新建PersonService接口,在serviceImpl包下新建PersonServiceImpl类实现PersonService接口。并写一个html页面用来显示数据 具体代码如下:
    SpringBoot简单整合Mybatis、并使用Mybatis-Generator自动生成所需代码_第3张图片
    SpringBoot简单整合Mybatis、并使用Mybatis-Generator自动生成所需代码_第4张图片
    SpringBoot简单整合Mybatis、并使用Mybatis-Generator自动生成所需代码_第5张图片
    在页面里用的是thymeleaf,在pom.xml中已经引入的对应的依赖
    SpringBoot简单整合Mybatis、并使用Mybatis-Generator自动生成所需代码_第6张图片
    九、运行SpringBootTestApplication
    SpringBoot简单整合Mybatis、并使用Mybatis-Generator自动生成所需代码_第7张图片

  • 十、成功图示
    SpringBoot简单整合Mybatis、并使用Mybatis-Generator自动生成所需代码_第8张图片

好啦!至此完美结束!

(__) 嘻嘻……其实中间出了好多问题,下面我就在下一篇博客中把我中间的产生的问题给大家说一下,看有没有同样的问题!

你可能感兴趣的:(SpringBoot简单整合Mybatis、并使用Mybatis-Generator自动生成所需代码)