Summary of springboot project problems

1. Web access problems

First picture:

Operation: Access the url directly in the browser, and only return a string in the controller.

Question: It probably means that the mapping is wrong. In fact, nothing is scanned. In my project, each module is a jar, so it should be that beans, services, controllers and mappers are not introduced.

Solution: the above picture

There are still two steps, one is to add @service to the serviceImpl class, and the other is to add the dependent projects service and serviceImpl to the pom.xml of the web. If neither of them is added, an error will occur and can be tested.

After the above operations, it is found that there is still an error, that is because there is no index.html. If it is a controller, it returns a page, and changing @Controller to RestController will return a string.

Second, the problem of calling API

Operation: Call this method, the error is a null pointer, indicating line 36.

Problem: The configuration file (two java classes) of mapper.xml cannot be scanned.

Solution: Add "com.banry.pscm.tenant.persist.dao.config" to the startup method in the above figure, which is the commented sentence in the above figure.

 @Configuration
public class MyBatisConfig {
    @Bean
    @ConfigurationProperties(prefix = "spring.datasource")
    public DataSource dataSource(){
        return new org.apache.tomcat.jdbc.pool.DataSource();
    }

    @Bean(name = "sqlSessionFactory")
    public SqlSessionFactory sqlSessionFactoryBean() throws Exception {
        SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();
        sqlSessionFactoryBean.setDataSource(dataSource());

        PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();

        sqlSessionFactoryBean.setMapperLocations(resolver.getResources("classpath:com/banry/pscm/tenant/persist/mapper/*.xml"));

        return sqlSessionFactoryBean.getObject();
    }
}
MyBatisConfig
@Configuration
@AutoConfigureAfter(MyBatisConfig.class)
public class MyBatisMapperScannerConfig {
    public MapperScannerConfigurer mapperScannerConfigurer(){
        MapperScannerConfigurer mapperScannerConfigurer = new MapperScannerConfigurer();
        mapperScannerConfigurer.setSqlSessionFactoryBeanName("sqlSessionFactory");
        mapperScannerConfigurer.setBasePackage("com.banry.pscm.tenant.persist.dao");
        return mapperScannerConfigurer;
    }
}
MyBatisMapperScannerConfig

 

 3. Invalid sql in xml

Description of the problem: Test the controller. If the sql is written in the mapper.xml file, the binding is invalid. If the sql is written in the mapper with annotations, it is fine.

Solution: Step 1: Select the web project, right-click Run As > Run Configurations, find the project running under the Spring Boot App, and select the classpath on the right, as shown below:

Step 2: Copy the package where mapper.xml is located to the web, and that's it.

 

Finally: the name of the bean in the project is preferably the same as the database, and the name of the imported bean in mapper.xml should be the same as the automatically generated bean in the project. Because I encountered it, so I marked a sentence.

The solution to the above problem is only a rough solution, and other problems need to be analyzed in detail. This is just what I have encountered so far.

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324954634&siteId=291194637