SpringBoot第二天
#今天是学习SpringBoot的第二天,来总结一下今天所学习的东西吧。
首先说一下swagger2文档,编写和维护接口文档是每个程序员的职责,前面我们已经写好的接口现在需要提供一份文档,这样才能方便调用者使用。
考虑到编写接口文档是一个非常枯燥的工作,我们采用Swagger2这套自动化文档工具来生成文档,它可以轻松的整合到Spring Boot中,并与Spring MVC程序配合组织出强大RESTful API文档。
- 第一导入相关的依赖
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.7.0</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.7.0</version>
</dependency>
创建swagger的配置类
package com.offcn.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;
@Configuration
@EnableSwagger2
public class SwaggerConfig {
@Bean
public Docket createRestApi() {
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
.select()
.apis(RequestHandlerSelectors.basePackage("com.lqf.controller"))
.paths(PathSelectors.any())
.build();
}
private ApiInfo apiInfo() {
return new ApiInfoBuilder()
.title("标题")
.description("描述")
.termsOfServiceUrl("连接地址")
.contact("Sunny")
.version("1.0")
.build();
}
}
- 修改Controller增加文档注释
-在这里我们通过@Apioperation注解来给API添加说明
-@ApiImplicitParams 、@ApiImplicitParam 注解来各参数添加说明
/**
* 更新指定id用户信息
* @param id
* @param user
* @return
*/
@PutMapping("/{id}")
@ApiOperation(value="更新指定id用户信息", notes="根据id更新用户信息")
@ApiImplicitParams({
@ApiImplicitParam(name = "id", value = "用户ID", required = true, dataType = "Long"),
@ApiImplicitParam(name = "user", value = "用户详细实体user", required = true, dataType = "User")
})
public String updateUser(@PathVariable("id") Long id,User user) {
user.setId(id);
userRepository.saveAndFlush(user);
return "success";
}
/***
* 删除指定id用户
* @param id
* @return
*/
@DeleteMapping("/{id}")
@ApiOperation(value="删除指定id用户信息", notes="根据id删除用户信息")
@ApiImplicitParam(name = "id", value = "用户id", required = true, dataType = "Long")
public String deleteUser(@PathVariable("id") Long id) {
userRepository.deleteById(id);
return "success";
}
配置好以上之后重启启动类,访问地址:http://localhost:8080/swagger-ui.html
使用SpringBoot整合Mybatis
1.springBoot 开启Mybatis支持
首先还是添加依赖(Mybatis、Mysqljdbc、druid)
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.0.0</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.10</version>
</dependency>
2.修改Springboot配置文件application.yml文件
#配置数据源
spring:
datasource:
url: jdbc:mysql://localhost:3306/test?serverTimezone=GMT%2B8
username: root //数据库用户名
password: 123 //数据库密码
driver-class-name: com.mysql.jdbc.Driver
#springboot整合mybatis
mybatis:
mapper-locations: classpath:mapper/*.xml
type-aliases-package: com.offcn.po
3.创建一个实体类,这里我们就不在多说了,要注意的是字段名要和数据库中表的表名对应上
并且给实体类的属性getter、setter和tostring方法。
4.创建Mybatis Mapper 的映射文件
你可以将映射文件和Mapper文件放在同一路径下,不过会出现问题,这里我们要在pom.xml配置文件中添加一个配置
<resources>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.properties</include>
<include>**/*.xml</include>
</includes>
<filtering>false</filtering>
</resource>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>**/*.*</include>
</includes>
<filtering>false</filtering>
</resource>
</resources>
或者你也可以把映射文件放到resources/mapper目录下,这两种方法都可以
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.offcn.mapper.UserMapper">
<select id="getUserList" resultType="com.offcn.pojo.MUser">
select * from user
</select>
</mapper>
5.创建Controller,来编写接口,
@RestController
public class UserController {
@Autowired
private UserDao userDao;
@Resource
private UserMapper userMapper;
@RequestMapping("/userList")
public List<MUser> getUserList3(){
List<MUser> userList = userMapper.getUserList();
return userList;
}
}
最后进行测试,这里可能出现的问题就是,忘记了在启动类中添加Mapper的扫描包,这里我们用一个注解来解决
@SpringBootApplication
@MapperScan("com.offcn.mapper") //这个MapperScan注解就是用来配置扫描的
public class HelloApplication {
public static void main(String[] args) {
SpringApplication.run(HelloApplication.class,args);
}
}
使用SpringBoot整合Spring Daya JPA 页面yml
首先,我们先来分析一下,我们的需求是 :
查询数据库–>数据–>展示到页面上
1.创建数据库,创建表,没有表怎么在数据库中查数据
2.使用持久层框架 Spring data jpa
3.返回json数据 使用jsp ,要返回到静态html页面 我们要使用freemarker
4.在页面展示中,我们使用静态页面来接收数据,因为springboot项目中不推荐使用jsp
使用步骤:
1.添加依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.10</version>
</dependency>
2.配置application.yml文件,增加jpa的配置,在这里一定要注意yml文件的书写格式(冒号 空格)
spring:
datasource:
url: jdbc:mysql://localhost:3306/springboot-001
type: com.alibaba.druid.pool.DruidDataSource
username: root
password: 123
driver-class-name: com.mysql.jdbc.Driver
jpa:
hibernate:
ddl-auto: update
show-sql: true
3.创建实体类,要给出属性的setter、getter和tostring方法
4.创建模板文件,保存位置resources/templates目录下,文件名后缀.flt
<html>
<head>
<title>spring boot</title>
</head>
<body>
<table border="1px">
<thead>
<!--tr>th*3-->
<tr>
<th>id</th>
<th>用户名</th>
<th>密码</th>
</tr>
</thead>
<tbody>
<#list userList as user> //ftl文件特有的遍历循环
//userList是要遍历的数据,//user另起的别名
<tr>
<td>${user.uid}</td>
<td>${user.uname}</td>
<td>${user.pwd}</td>
</tr>
</#list>
</tbody>
</table>
</body>
</html>
5.创建controller,编写接口,将数据传递给模板,然后在页面中显示。
@RequestMapping("/list1")
public String getUserList(Model model){
List<User> userList = userDao.findAll();
model.addAttribute("userList",userList);
return "user";
}
总结一下,今天所学的内容不多,但是要细心,毕竟刚接触好多东西不够了解,很容易犯错误,比如依赖导错了,注解忘记加了,所以一定要细心。