目录

1.首先可能是springboot静态资源访问不到

2.下面是整和mybatis的时候mapper 文件产生的问题,不映射的问题,检查一下方面,如果还出现错误,我就无能为力了

1.首先你的mapper.xml 的namespace 一定要对应到类 ,我写是时候对应到包,我就错了,好气气

2.@MapperScan("com.example.demo.mapper")记得加上这个 ,@mapper记得也要加上,

3.一定要指定一下mapper 的路径

4,。如果还不行的话,加上这个东西,在pom 文件下

5.检查一下依赖

3.springboot 中test测试类自动注入进不来的问题:


1.首先可能是springboot静态资源访问不到

这个springboot 2.0以上出现的问题,拦截器会拦截静态资源,

在自动注入的时候,覆盖了路径,需要重写

package com.ak.springboot02.config;

import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;

@Configuration
public class MyMvcConfig extends WebMvcConfigurationSupport {

    protected void addViewControllers(ViewControllerRegistry registry) {
        registry.addViewController("/").setViewName("login");
    }

    @Override
    protected void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/**")
                .addResourceLocations("classpath:/resources/")
                .addResourceLocations("classpath:/static/")
                .addResourceLocations("classpath:/public/");

        super.addResourceHandlers(registry);
    }

}

 

 

2.下面是整和mybatis的时候mapper 文件产生的问题,不映射的问题,检查一下方面,如果还出现错误,我就无能为力了

 

映射产生的问题

1.首先你的mapper.xml 的namespace 一定要对应到类 ,我写是时候对应到包,我就错了,好气气

然后呢你的xml 文件要放在 包下面扫描的时候就不会出现问题

 

2.@MapperScan("com.example.demo.mapper")记得加上这个 ,@mapper记得也要加上,

package com.example.demo;

import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.ServletComponentScan;

@SpringBootApplication
@MapperScan("com.example.demo.mapper")
public class DemoApplication {

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

}

3.一定要指定一下mapper 的路径

mybatis.mapper-locations=classpath*:com/example/demo/mapper/*.xml

4,。如果还不行的话,加上这个东西,在pom 文件下

<build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>


        <resources>
            <resource>
                <directory>src/main/resources</directory>
            </resource>
            <resource>
                <directory>src/main/java</directory>
                <filtering>true</filtering>
                <includes>
                    <include>**/*.xml</include>
                </includes>
            </resource>
        </resources>

    </build>

5.检查一下依赖

<dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>org.webjars</groupId>
            <artifactId>jquery</artifactId>
            <version>3.4.1</version>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>
        <dependency>
            <groupId>org.thymeleaf</groupId>
            <artifactId>thymeleaf-spring5</artifactId>
        </dependency>
        <dependency>
            <groupId>org.thymeleaf.extras</groupId>
            <artifactId>thymeleaf-extras-java8time</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>

        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-jdbc</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-jdbc</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.data</groupId>
            <artifactId>spring-data-jdbc</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-configuration-processor</artifactId>
        </dependency>

    </dependencies>

 

3.springboot 中test测试类自动注入进不来的问题:

@RunWith(SpringRunner.class)
@SpringBootTest
public class Springboot02ApplicationTests {

    @Autowired
    DataSource dataSource;

    @Autowired
    int k1;

    @Test
    public void contextLoads() throws Exception{

        System.out.println(k1);
//        System.out.println(dataSource.getClass());
//
//        Connection connection=dataSource.getConnection();
//
//
//        System.out.println(connection);
//
//        connection.close();
    }

}

上面一直报错 ,说自动注入不进来?

package com.test1.demo1;

import com.example.demo.DemoApplication;
import com.example.demo.mapper.StudentMapper;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import javax.sql.DataSource;

@RunWith(SpringRunner.class)
@SpringBootTest(classes = DemoApplication.class)
public class Demo1ApplicationTests {


    @Autowired
    private StudentMapper studentMapper;


    @Test
    public void contextLoads() {

       System.out.println(studentMapper.getStudentBySno(1));

    }

}

这样子就好了,

 

修改

@SpringBootTest(classes = DemoApplication.class)

这样子就好了。

以上就是昨天学习springboot 出现的一系列问题,今天继续写哈哈。

遨游在报错的海洋中