接口编写

最简单的编写

屏幕快照 2019-02-01 下午2.34.44.png
屏幕快照 2019-02-01 下午2.35.26.png
屏幕快照 2019-02-01 下午2.36.09.png
屏幕快照 2019-02-01 下午2.37.37.png
屏幕快照 2019-02-01 下午2.38.01.png
屏幕快照 2019-02-01 下午2.57.59.png

新建 DemoRestController

@RestController
public class DemoRestController {

    @RequestMapping("/hello") 
    public String hello() {
        return "Hello World!";
    }
}

pom.xml 添加依赖


            org.springframework.boot
            spring-boot-starter-web
 
屏幕快照 2019-02-01 下午3.07.57.png

点击右上角运行

验证
在浏览器或者 postman 输入 http://localhost:8080/hello,输出"Hello World!"

数据操作层框架 Mybatis

http://www.spring4all.com/article/145

自动生成Mybatis 各种采坑

Q:
Description:
Field cityMapper in com.simple.helloworld.service.impl.CityServiceImpl required a bean of type 'com.simple.helloworld.mapper.CityMapper' that could not be found.
The injection point has the following annotations:
- @org.springframework.beans.factory.annotation.Autowired(required=true)
Action:
Consider defining a bean of type 'com.simple.helloworld.mapper.CityMapper' in your configuration.

A:
添加包路径 @MapperScan("com.simple.helloworld.mapper")``, scanBasePackages={"com.simple.helloworld.mapper"}

@SpringBootApplication(exclude = DataSourceAutoConfiguration.class ,scanBasePackages={"com.simple.helloworld.mapper"})
@MapperScan("com.simple.helloworld.mapper")
public class HelloworldApplication {
    public static void main(String[] args) {
        SpringApplication.run(HelloworldApplication.class, args);
    }
}

Q:
Description:
Failed to configure a DataSource: 'url' attribute is not specified and no embedded datasource could be configured.
Reason: Failed to determine a suitable driver class
Action:
Consider the following:
If you want an embedded database (H2, HSQL or Derby), please put it on the classpath.
If you have database settings to be loaded from a particular profile you may need to activate it (no profiles are currently active).
A:
使用了自动配置的数据源,如generatorConfig.xml
添加过滤操作 @SpringBootApplication(exclude = DataSourceAutoConfiguration.class)

@SpringBootApplication(exclude = DataSourceAutoConfiguration.class ,scanBasePackages={"com.simple.helloworld.mapper"})
@MapperScan("com.simple.helloworld.mapper")
public class HelloworldApplication {
    public static void main(String[] args) {
        SpringApplication.run(HelloworldApplication.class, args);
    }
}

Q:
Property 'sqlSessionFactory' or 'sqlSessionTemplate' are required

A:

@Service
public abstract class CityMapper extends SqlSessionDaoSupport {
    @Override
    @Autowired
    public void setSqlSessionFactory(SqlSessionFactory sqlSessionFactory) {
        super.setSqlSessionFactory(sqlSessionFactory);
    }
}
@SpringBootApplication(exclude = DataSourceAutoConfiguration.class ,scanBasePackages={"com.simple.helloworld.mapper"})
@MapperScan("com.simple.helloworld.mapper")
public class HelloworldApplication {
    public static void main(String[] args) {
        SpringApplication.run(HelloworldApplication.class, args);
    }
}

你可能感兴趣的:(接口编写)