1.创建数据库
DROP TABLE IF EXISTS `movie`;
CREATE TABLE `movie` (`id` int(255) NOT NULL AUTO_INCREMENT,`name` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL,`author` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL,`score` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL,`title` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL,PRIMARY KEY (`id`) USING BTREE
)
INSERT INTO `movie` VALUES (1, '美丽人生', '罗伯托·贝尼尼', '9.6', '一个快乐的传说');
INSERT INTO `movie` VALUES (2, '放牛班的春天', ' 克里斯托夫·巴拉蒂', '9.3', ' 歌声伴我心');
INSERT INTO `movie` VALUES (3, '触不可及', '奥利维埃·纳卡什 / 埃里克·托莱达诺', '9.3', '闪亮人生');
INSERT INTO `movie` VALUES (4, '三傻大闹宝莱坞', '拉吉库马尔·希拉尼', '9.2', ': 三个傻瓜');
INSERT INTO `movie` VALUES (5, '怦然心动', '罗伯·莱纳', '9.1', '梧桐树之恋');
INSERT INTO `movie` VALUES (6, '绿皮书', '彼得·法雷里', '8.9', ' 绿簿旅友');
INSERT INTO `movie` VALUES (7, '功夫', ' 周星驰', '8.8', '功夫3D');
INSERT INTO `movie` VALUES (8, '悲伤逆流成河', '郭敬明', '8.8', '逆流成河');
INSERT INTO `movie` VALUES (9, '肖申克的救赎', '弗兰克·德拉邦特', '9.8', '月黑高飞');
INSERT INTO `movie` VALUES (14, '霸王别姬', '陈凯歌', '9.6', '再见,我的妾');
INSERT INTO `movie` VALUES (15, '泰坦尼克号', ' 詹姆斯·卡梅隆', '9.4', '铁达尼号');
INSERT INTO `movie` VALUES (16, '这个杀手不太冷', '吕克·贝松', '9.4', '终极追杀令');
INSERT INTO `movie` VALUES (17, '千与千寻', '宫崎骏', '9.4', '神隐少女');
2.新建一个名为springboot-swagger的springboot工程
3.配置yml
server:port: 8004
spring:datasource:url: jdbc:mysql:///你的数据库名?serverTimezone=UTCdriver-class-name: com.mysql.cj.jdbc.Driverusername: 你的数据库用户名password: 你的数据库密码mvc:pathmatch:matching-strategy: ant_path_matcher
mybatis:configuration:log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
4.导入依赖
<!--swagger依赖--><dependency><groupId>io.springfox</groupId><artifactId>springfox-swagger2</artifactId><version>2.9.2</version></dependency><!--swagger ui--><dependency><groupId>io.springfox</groupId><artifactId>springfox-swagger-ui</artifactId><version>2.9.2</version></dependency>
5.配置swagger
@Configuration
@EnableSwagger2
public class Swagger2Config {/*** 创建API应用* apiInfo() 增加API相关信息* 通过select()函数返回一个ApiSelectorBuilder实例,用来控制哪些接口暴露给Swagger来展现,* 指定扫描的包路径来定义指定要建立API的目录。* @return*/@Beanpublic Docket coreApiConfig(){return new Docket(DocumentationType.SWAGGER_2).apiInfo(adminApiInfo()).groupName("adminApi").select().apis(RequestHandlerSelectors.basePackage("com.xulu"))//只显示admin下面的路径//.paths(Predicates.and(PathSelectors.regex("/admin/.*"))).build();}private ApiInfo adminApiInfo(){return new ApiInfoBuilder().title("电影案例--api文档").description("测试电影的功能").version("1.0").contact(new Contact("Xiao Xulu","http://baidu.com","1435533194@qq.com")).build();}
}
6.创建实体类
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Movie {private Integer id;private String name;private String author;private String score;private String title;
}
7.创建数据层
@Mapper
public interface MovieDao {/*** 查询所有*/@Select("select * from movie")public List<Movie> findAll();/*** 根据id查询*/@Select("select * from movie where id=#{id}")public Movie findById(Integer id);/*** 新增*/@Insert("insert into movie(name,author,score,title) " +"values(#{name},#{author},#{score},#{title})")public int add(Movie movie);/*** 修改*/@Update("update movie set name=#{name},author=#{author}," +"score=#{score},title=#{title} where id=#{id}")public int update(Movie movie);/*** 删除*/@Delete("delete from movie where id=#{id}")public int delete(Integer id);/*** 分页*/@Select("select * from movie limit #{current},#{size}")public List<Movie> findPage(@Param("current") Integer current,@Param("size")Integer size);/*** 条件*/@Select("select * from movie where name like #{name}")public List<Movie> findQuery(String name);}
8.创建服务接口类
public interface MovieService {/*** 查询所有*/public List<Movie> findAll();/*** 根据id查询*/public Movie findById(Integer id);/*** 新增*/public int add(Movie movie);/*** 修改*/public int update(Movie movie);/*** 删除*/public int delete(Integer id);/*** 分页*/public List<Movie> findPage(Integer current, Integer size);/*** 条件*/public List<Movie> findQuery(String name);
}
9.创建服务接口实现类
@Service
public class MovieServiceImpl implements MovieService {@Resourceprivate MovieDao movieDao;/*** 查询所有*/@Overridepublic List<Movie> findAll() {return movieDao.findAll();}/*** 根据id查询*/@Overridepublic Movie findById(Integer id) {return movieDao.findById(id);}/*** 新增*/@Overridepublic int add(Movie movie) {return movieDao.add(movie);}/*** 更新*/@Overridepublic int update(Movie movie) {return movieDao.update(movie);}/*** 删除*/@Overridepublic int delete(Integer id) {return movieDao.delete(id);}/*** 分页*/@Overridepublic List<Movie> findPage(Integer current, Integer size) {return movieDao.findPage(current, size);}/*** 条件*/@Overridepublic List<Movie> findQuery(String name) {String query="%" + name + "%";return movieDao.findQuery(query);}
}
10.创建控制层
@RestController
@Api(tags = "电影控制层")
public class MovieController {@Autowiredprivate MovieService movieService;/*** 查询所有*/@ApiOperation("查询所有")@GetMapping("/findAll")public List<Movie> findAll(){List<Movie> movieList = movieService.findAll();return movieList;}/*** 根据id查询*/@ApiOperation("根据id查询")@GetMapping("/findById/{id}")public Movie findById(@PathVariable Integer id){Movie movie = movieService.findById(id);return movie;}/*** 新增*/@ApiOperation("新增")@PostMapping("/save")public int save(@RequestBody Movie movie){int count = movieService.add(movie);return count;}/*** 修改*/@ApiOperation("修改")@PostMapping("/update")public int updateById( @RequestBody Movie movie){int count = movieService.update(movie);return count;}/*** 修改*/@ApiOperation("修改")@DeleteMapping("/delete/{id}")public int updateById(@PathVariable Integer id){int count = movieService.delete(id);return count;}/*** 分页查询*/@ApiOperation("分页查询")@GetMapping("/findPage/{current}/{size}")public List<Movie> findPage(@PathVariable int current,@PathVariable int size){List<Movie> movieList = movieService.findPage(current, size);return movieList;}/*** 条件查询*/@ApiOperation("条件查询")@GetMapping("/findQuery/{name}")public List<Movie> findQuery(@PathVariable String name){List<Movie> movieList = movieService.findQuery(name);return movieList;}}
11.启动测试
@SpringBootApplication
@EnableSwagger2//开启swagger
public class SpringbootSwaggerApplication {public static void main(String[] args) {SpringApplication.run(SpringbootSwaggerApplication.class, args);}}
访问http://localhost:8004/swagger-ui.html
查询所有
根据id查询
新增
修改
删除
分页查询
条件查询