Springboot + Mybatis(自动生成mapper) 浏览器访问报404问题

springBoot 整合Mybatis (mapper自动生成)

1、pom文件(以下标红位置是特别注意的  不导入mybatis依赖@MapperScan注解无法导包--会导致访问产生404)



    4.0.0
    
        org.springframework.boot
        spring-boot-starter-parent
        2.3.1.RELEASE
         
    
    com.dm
    myjob
    0.0.1-SNAPSHOT
    myjob
    Demo project for Spring Boot

    
        1.8
    

    
        
            org.springframework.boot
            spring-boot-starter-web
        
        
            org.springframework.boot
            spring-boot-starter
        
        
            org.springframework.boot
            spring-boot-starter-thymeleaf
        
        
            org.mybatis
            mybatis
            3.4.5
        
        
            org.mybatis.spring.boot
            mybatis-spring-boot-starter
            2.1.3
        
        
        
            mysql
            mysql-connector-java
            6.0.6
        
        
        
            com.alibaba
            druid
            1.1.10
        

        
            org.springframework.boot
            spring-boot-starter-test
            test
            
                
                    org.junit.vintage
                    junit-vintage-engine
                
            
        
    

    
        
            
                org.springframework.boot
                spring-boot-maven-plugin
                
                    exec
                
            
            
            
            org.mybatis.generator
            mybatis-generator-maven-plugin
            1.3.2
            
                
                ${basedir}/src/main/resources/generator/generatorConfig.xml
                true
                true
            
            
        
    


2、配置generatorConfig.xml文件


    
    ${basedir}/src/main/resources/generator/generatorConfig.xml
    true
    true

代码贴上




    
    
    
        
            
            
            
        
        
        
        
        
            
        
        
        
            
            
        
        
        
            
        
        
        
            
        
        
        test" domainObjectName="Dept" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false">

3、配置yml文件(yml文件与properties同样 写法比较简单 但是如果同时编辑properties的优先级高于yml)

server:
  port: 8082

spring:
  datasource:
    name: bootmybatisone
    url: jdbc:mysql://localhost:3306/bootmybatisone?serverTimezone=UTC&useUnicode=true&characterEncoding=utf-8&useSSL=false
    #jdbc:mysql://cdb-a1z7n8qw.bj.tencentcdb.com:10039/KY?serverTimezone=GMT%2B8&useSSL=false
    username: root
    password: root
    # 使用druid数据源
    type: com.alibaba.druid.pool.DruidDataSource
    #cdb-a1z7n8qw.bj.tencentcdb.com
    driver-class-name: com.mysql.jdbc.Driver
    filters: stat
    maxActive: 20
    initialSize: 1
    maxWait: 60000
    minIdle: 1
    timeBetweenEvictionRunsMillis: 60000
    minEvictableIdleTimeMillis: 300000
    validationQuery: select 'x'
    testWhileIdle: true
    testOnBorrow: false
    testOnReturn: false
    poolPreparedStatements: true
    maxOpenPreparedStatements: 20

mybatis:
  mapper-locations: classpath:mapping/*.xml  #classpath就是应用程序resources的路径
  type-aliases-package: com.dm.myjob

4、生成mapper

Springboot + Mybatis(自动生成mapper) 浏览器访问报404问题_第1张图片

Springboot + Mybatis(自动生成mapper) 浏览器访问报404问题_第2张图片

Springboot + Mybatis(自动生成mapper) 浏览器访问报404问题_第3张图片

Springboot + Mybatis(自动生成mapper) 浏览器访问报404问题_第4张图片

即可生成如下工程样子

Springboot + Mybatis(自动生成mapper) 浏览器访问报404问题_第5张图片

5、测试代码

controller层

package com.dm.myjob.controller;

import com.dm.myjob.mapper.Dept;
import com.dm.myjob.model.DeptMapper;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.List;

/* *
 * @ProjectName:    myjob
 * @ClassName:      Test
 * @Author:     Dm
 * @Date:    2020/6/23
 */
@Controller
@RequestMapping(value = "/test")
public class Test {
    @Resource
    private DeptMapper deptMapper;

    @ResponseBody
    @RequestMapping(value = "/select")
    public String select(){
        Dept list= deptMapper.selectByPrimaryKey(20);
        String s = "hello! ";
        if( list.getClass()!= null) {
            s += "Firstname:" + list.getFirstName()+ "LastName:" + list.getLastName();
            System.out.println("s = " + s);
        }
        return s;
    }
}

附带上mapper.xml文件




  
    
    
    
    
  
  
    insert into test (id, magic_id, first_name,
      last_name)
    values (#{id,jdbcType=INTEGER}, #{magicId,jdbcType=VARCHAR}, #{firstName,jdbcType=VARCHAR},
      #{lastName,jdbcType=VARCHAR})
  
  
  
    insert into test
    
      
        id,
      
      
        magic_id,
      
      
        first_name,
      
      
        last_name,
      
    
    
      
        #{id,jdbcType=INTEGER},
      
      
        #{magicId,jdbcType=VARCHAR},
      
      
        #{firstName,jdbcType=VARCHAR},
      
      
        #{lastName,jdbcType=VARCHAR},
      
    
  

启动类MyjobApplication

package com.dm.myjob;

import org.apache.ibatis.annotations.Mapper;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;

@SpringBootApplication
//@ComponentScan(basePackages = {"com.dm.myjob.model"})
@MapperScan("com.dm.myjob.model")
//这里需要注意到的是  如果不加@MapperScan注解  
//导致扫描不到 报错A component required a bean of type 'com.dm.myjob.model.DeptMapper' that could not be found.
//@ComponentScan 注解和@MapperScan作用类似 

@ComponentScan:此注解是用来管理容器中的bean,即是管理项目中类的依赖关系, 注意此注解并不创建类的实例; 默认情况下此注解扫描本工程下的所有包, 但是在springBoot的分布式中如果需要用到别的微服务工程中的实例,那么就要写为如下的形式。注意要加上本工程。 因为当使用 basePackages时,此注解就不会使用默认的扫描路径了
@ComponentScan(basePackages = { 
    "com.wisdombud.dth.boss.customer", "com.wisdombud.dth.boss.his",
"com.wisdombud.dth.boss.product.srv",
"com.wisdombud.dth.boss.product.mapper" })
@MapperScan: 此注解是扫描被此注解中声明的mapper路径下的*mapper.xml文件,读取文件配置,并根据此文件中的mapper接口路径创建bean实例。如下写法。

public class MyjobApplication {

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

6、访问(用postman客户端输入路径和端口可以看到结果)

Springboot + Mybatis(自动生成mapper) 浏览器访问报404问题_第6张图片

你可能感兴趣的:(Java学习)