springboot整合mybatis开发


1创建项目,在启动类上添加映射扫描注解

2导入依赖,添加mybatis generator自动生成代码插件


			
				org.mybatis.generator
				mybatis-generator-maven-plugin
				1.3.2
				
					${project.basedir}/src/main/resources/generator/generatorConfig.xml
					true
					true
				
			

 3创建generatorConfig.xml文件

<?xml version="1.0" encoding="UTF-8"?>


    
    
    
        
            
            
            
        
        
        
        
        
            
        
        
        
            
            
        
        
        
            
        
        
        
            
        
        
        
        
        
        
        
        
        

  根据generatorConfig.xml文件生成的

模型:

映射文件:

DAO文件:

 4、自定义查询语句

 mapper

dao:

<?xml version="1.0" encoding="UTF-8" ?>


  
    
    
    
    
    
    
  
  
    id, host_id, collector_id, create_time, update_tine, state
  
  
    
  
  
  
    delete from tab_collection
    where id = #{id,jdbcType=BIGINT}
  
  
    insert into tab_collection (id, host_id, collector_id, 
      create_time, update_tine, state
      )
    values (#{id,jdbcType=BIGINT}, #{hostId,jdbcType=BIGINT}, #{collectorId,jdbcType=BIGINT}, 
      #{createTime,jdbcType=TIMESTAMP}, #{updateTine,jdbcType=TIMESTAMP}, #{state,jdbcType=BIGINT}
      )
  
  
    insert into tab_collection
    
      
        id,
      
      
        host_id,
      
      
        collector_id,
      
      
        create_time,
      
      
        update_tine,
      
      
        state,
      
    
    
      
        #{id,jdbcType=BIGINT},
      
      
        #{hostId,jdbcType=BIGINT},
      
      
        #{collectorId,jdbcType=BIGINT},
      
      
        #{createTime,jdbcType=TIMESTAMP},
      
      
        #{updateTine,jdbcType=TIMESTAMP},
      
      
        #{state,jdbcType=BIGINT},
      
    
  
  
    update tab_collection
    
      
        host_id = #{hostId,jdbcType=BIGINT},
      
      
        collector_id = #{collectorId,jdbcType=BIGINT},
      
      
        create_time = #{createTime,jdbcType=TIMESTAMP},
      
      
        update_tine = #{updateTine,jdbcType=TIMESTAMP},
      
      
        state = #{state,jdbcType=BIGINT},
      
    
    where id = #{id,jdbcType=BIGINT}
  
  
    update tab_collection
    set host_id = #{hostId,jdbcType=BIGINT},
      collector_id = #{collectorId,jdbcType=BIGINT},
      create_time = #{createTime,jdbcType=TIMESTAMP},
      update_tine = #{updateTine,jdbcType=TIMESTAMP},
      state = #{state,jdbcType=BIGINT}
    where id = #{id,jdbcType=BIGINT}
  

方便在Dao接口和Mappper XML文件之间进行切换,下载free mybatis plugins插件,安装步骤: