spring boot2.1.1 集成tk mybatis

 

 

此文章内容来源  https://gitee.com/free/Mapper/wikis/1.3-spring-boot?sort_id=208198

环境:

  1. 开发IDE:idea
  2. spring boot:2.1.1 release
  3. tk.mybatis 4
  4. mysql 5
  5. druid 1.0.25
  • 使用spring boot 初始化项目

    pom.xml



    4.0.0
    
        org.springframework.boot
        spring-boot-starter-parent
        2.1.1.RELEASE
         
    
    com.zjchsoft.bi
    tkmb
    1.0.0-SNAPSHOT
    tkmb
    Demo project for Spring Boot

    
        1.8
    

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

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

        
            com.alibaba
            druid-spring-boot-starter
            1.1.10
        

        
            com.alibaba
            druid
            1.0.25
        

        
        
        
            tk.mybatis
            mapper
            4.0.0
        


        
            com.github.pagehelper
            pagehelper
            4.2.1
        


        
            mysql
            mysql-connector-java
            5.1.29
        

        
        
            tk.mybatis
            mapper-spring-boot-starter
            2.1.2
        



    

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

            
                org.mybatis.generator
                mybatis-generator-maven-plugin
                1.3.2
                
                    ${basedir}/src/main/resources/generator/generatorConfig.xml
                    true
                    true
                
                
                    
                        mysql
                        mysql-connector-java
                        5.1.29
                    
                    
                        tk.mybatis
                        mapper
                        4.0.0
                    
                
            
        
    


  • 配置mappers
     
    spring:
      datasource:
        type: com.alibaba.druid.pool.DruidDataSource
        url: jdbc:mysql://localhost:3306/test
        username: root
        password:
        driver-class-name: com.mysql.jdbc.Driver
    mapper:
      mappers:
        - tk.mybatis.mapper.common.Mapper
        - tk.mybatis.mapper.common.Mapper2
      not-empty: true
    # 自定义xml文件时引入,否则可以不引入
    mybatis:
      mapperLocations: classpath:mapper/*.xml

     
  • 配置扫描包路径
package com.zjchsoft.bi.tkmb;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
@tk.mybatis.spring.annotation.MapperScan(basePackages = "com.zjchsoft.bi.tkmb.mapper")
public class TkmbApplication {

    public static void main(String[] args) {
        SpringApplication.run(TkmbApplication.class, args);
    }

}

  •  添加自定义查询
     
    
    
    
      
        
        
        
        
        
      
      
            id, user_name, password, sex
        
      
    

     

package com.zjchsoft.bi.tkmb.mapper;

import com.zjchsoft.bi.tkmb.entity.User;
import tk.mybatis.mapper.common.Mapper;
//不用添加注解,starter会自动扫描继承Mapper的类文件
public interface UserMapper extends Mapper {
    //自定义查询
    public User selectById2(int id);
}

 

使用控制层进行访问
 

package com.zjchsoft.bi.tkmb.controller;

import com.zjchsoft.bi.tkmb.entity.User;
import com.zjchsoft.bi.tkmb.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/user")
public class Usercontroller {

    @Autowired
    UserMapper userMapper;

    @GetMapping("/all")
    public List getUser(){
        return userMapper.selectAll();
    }

    @GetMapping("/get/{id}")
    public User getUser(@PathVariable("id") int id){
        return userMapper.selectById2(id);
    }
}
  • 启动项目进行访问

    spring boot2.1.1 集成tk mybatis_第1张图片

    spring boot2.1.1 集成tk mybatis_第2张图片

你可能感兴趣的:(spring,boot)