SpringBoot整合Mybatis Generator自动生成代码

Mybatis是目前主流的ORM框架,相比于hibernate的全自动,它是半自动化需要手写sql语句、接口、实体对象,后来推出的Generator自动生成代码,可以帮我们提高开发效率。

本文目的:SpringBoot 整合 Mybatis Generator自动生成dao、entity、mapper.xml实现单表增删改查。

1.创建SpringBoot项目

File→New→Project… 选择Spring Initializr,选择JDK版本,默认初始化URL

SpringBoot整合Mybatis Generator自动生成代码_第1张图片

填写项目名称,java版本,其他描述信息

SpringBoot整合Mybatis Generator自动生成代码_第2张图片

选择项目存放路径

SpringBoot整合Mybatis Generator自动生成代码_第3张图片

选择web、mybatis、mysql依赖

SpringBoot整合Mybatis Generator自动生成代码_第4张图片

Next–>Finish完成项目创建

2. mybatis-generator-maven插件的配置

打开项目的pom.xml文件添加

			
				org.mybatis.generator
				mybatis-generator-maven-plugin
				
					true
					true
				
			

完整pom.xml



	4.0.0

	com.xyz
	mybatis
	0.0.1-SNAPSHOT
	jar

	mybatis
	Spring Boot 整合 Mybatis

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

	
		UTF-8
		UTF-8
		1.8
	

	
		
			org.springframework.boot
			spring-boot-starter-thymeleaf
		
		
			org.springframework.boot
			spring-boot-starter-web
		
		
			org.mybatis.spring.boot
			mybatis-spring-boot-starter
			1.3.2
		

		
			mysql
			mysql-connector-java
			runtime
		
		
			org.springframework.boot
			spring-boot-starter-test
			test
		
		
			com.github.pagehelper
			pagehelper-spring-boot-starter
			1.2.5
		
	

	
		
			
				org.springframework.boot
				spring-boot-maven-plugin
			
			
				org.mybatis.generator
				mybatis-generator-maven-plugin
				
					true
					true
				
			
		
	




3. 项目结构构建

在项目目录下(这里是mybatis)添加controller、service、dao、entity包,在resources下添加mapper包存放映射文件。

SpringBoot整合Mybatis Generator自动生成代码_第5张图片

4. application.yml配置

#端口号配置
server:
  port: 8088
spring:
#模板引擎配置
  thymeleaf:
    prefix: classpath:/templates/
    suffix: .html
    mode: HTML
    encoding: UTF-8
    cache: false
    servlet:
      content-type: text/html
#静态文件配置
  resources:
    static-locations: classpath:/static,classpath:/META-INF/resources,classpath:/templates/
#jdbc配置
  datasource:
    url: jdbc:mysql://localhost:3306/video?useUnicode=true&characterEncoding=utf8
    username: xyz
    password: xyz
    driver-class-name: com.mysql.jdbc.Driver
#mybatis配置
mybatis:
#映射文件路径
  mapper-locations: classpath:mapper/*.xml
#模型所在的保命
  type-aliases-package: com.xyz.mybatis.entity

5. generatorConfig.xml配置

在resources文件下创建generatorConfig.xml文件,配置如下:






    
    

    
    

        
        

        
        

        
        
        

        
        

        
        
        
        

        
        
            
             
        

        
        

        
        
            
            
        

        
        
            
            
            
            
        

        
        
            
        

        
        
            
        

        
        
        

注意:
classPathEntry location=“E:\IdeaProjects\mysql-connector-java-5.1.47.jar”,建议用5.X系列的,否则可能生成的接口会缺少

6. 在idea中添加一个mybatis generator maven插件启动选项,点击Run,选择Edit Configuration… 点击加号"+"添加,选择maven,填写名称(这里用mybatis generator),命令行:mybatis-generator:generate -e

SpringBoot整合Mybatis Generator自动生成代码_第6张图片

SpringBoot整合Mybatis Generator自动生成代码_第7张图片

SpringBoot整合Mybatis Generator自动生成代码_第8张图片

7. 选择 Mybatis Generator 启动,自动在dao、entity、mapper包下生成代码

SpringBoot整合Mybatis Generator自动生成代码_第9张图片

注意:
利用Mybatis Generator自动生成代码,对于已经存在的文件会存在覆盖和在原有文件上追加的可能性,不宜多次生成。如需重新生成,需要删除已生成的源文件。

到此这篇关于SpringBoot整合Mybatis Generator自动生成代码的文章就介绍到这了,更多相关SpringBoot Mybatis Generator自动生成代码内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

你可能感兴趣的:(SpringBoot整合Mybatis Generator自动生成代码)