其实他是看api文档用的 而不是做什么网站用的 但是玩嘛 随便玩了
最近在看深入理解springboot微服务这本书 里面整合的例子还是非常多的 但是没有深入到源码和底层架构 有点差强人意
创建一个springboot项目
其中在pom.xml文件中引入一下几个包 我都已经加上了关键的注释
4.0.0
org.springframework.boot
spring-boot-starter-parent
2.1.3.RELEASE
com.forezp
hello-world
0.0.1-SNAPSHOT
hello-world
Demo project for Spring Boot
1.8
org.springframework.boot
spring-boot-starter-web
org.springframework.boot
spring-boot-starter-test
test
org.springframework.boot
spring-boot-starter-actuator
org.springframework.boot
spring-boot-starter-remote-shell
1.5.19.RELEASE
org.springframework.boot
spring-boot-starter-data-jpa
mysql
mysql-connector-java
org.projectlombok
lombok
org.springframework.boot
spring-boot-starter-data-redis
io.springfox
springfox-swagger2
2.9.2
io.springfox
springfox-swagger-ui
2.9.2
org.springframework.cloud
spring-cloud-dependencies
RELEASE
org.springframework.boot
spring-boot-maven-plugin
之后在相应的层中建立文件 然后实现
首先最重要的当然是配置文件了
首先是程序的主配置文件 然后是总配置文件
package com.forezp.helloworld.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;
/**
* @Author: Pandy
* @Date: 2019/3/31 10:35
* @Version 1.0
*/
@Configuration
@EnableSwagger2
public class Swagger2 {
@Bean
public Docket createRestApi(){
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
.select()
.apis(RequestHandlerSelectors.basePackage("com.forezp.helloworld.controller"))
.paths(PathSelectors.any())
.build();
}
private ApiInfo apiInfo(){
return new ApiInfoBuilder()
.title("快速构建网站")
.description("简单优雅")
.termsOfServiceUrl("https://www.jianshu.com/u/1f10e87ec1d0")
.version("1.0")
.build();
}
}
my:
name: Pandy
age: 22
spring:
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/spring-cloud?useUnicode=true&characterEncoding=utf8&serverTimezone=UTC
username: root
password: root
jpa:
hibernate:
ddl-auto: create
show-sql: true
redis:
host: localhost
port: 6379
password:
database: 1
pool:
然后是在各个层的实现 dao层是接口的形式继承一个通用的接口就不用自己写底层的代码了 这样解释就像是对小学生一样
package com.forezp.helloworld.dao;
import com.forezp.helloworld.pojo.User;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
/**
* @Author: Pandy
* @Date: 2019/3/30 23:10
* @Version 1.0
*/
@Repository
public interface UserDao extends JpaRepository {
User findByUsername(String username);
}
然后是service层
package com.forezp.helloworld.service;
import com.forezp.helloworld.dao.UserDao;
import com.forezp.helloworld.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
/**
* @Author: Pandy
* @Date: 2019/3/30 23:11
* @Version 1.0
*/
@Service
public class UserService {
@Autowired
private UserDao userDao;
public User findUserByUsername(String username){
return userDao.findByUsername(username);
}
public List findAll(){
return userDao.findAll();
}
public User saveUser(User user){
return userDao.save(user);
}
public User findUserById(long id){
return userDao.findById(id).orElse(null);
//return fidOne(id);只能在低版本中使用 高版本是上面的用法
}
public User updateUser(User user){
return userDao.saveAndFlush(user);
}
public void deleteUser(long id){
userDao.deleteById(id);
//也可以delete(user); 书上写的是错的~~~
}
}
最重要的controller层 其中网站上显示的api 以及详细的信息都有写 然后具体实现框架中的代码已经能为你实现了
package com.forezp.helloworld.controller;
import com.forezp.helloworld.pojo.User;
import com.forezp.helloworld.service.UserService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;
import java.util.List;
/**
* @Author: Pandy
* @Date: 2019/3/30 23:12
* @Version 1.0
*/
@RequestMapping("/user")
@RestController
public class UserController {
@Autowired
private UserService userService;
@GetMapping("/${username}")
public User getUser(@PathVariable("username")String username){
return userService.findUserByUsername(username);
}
@ApiOperation(value="用户列表",notes = "用户列表")
@RequestMapping(value = {""},method = RequestMethod.GET)
public List getUsers(){
List users = userService.findAll();
return users;
}
@ApiOperation(value = "创建用户",notes = "创建用户")
@RequestMapping(value = "",method = RequestMethod.POST)
public User postUser(@RequestBody User user){
return userService.saveUser(user);
}
@ApiOperation(value = "获取用户的详细信息",notes = "根据url的id来获取详细的信息")
@RequestMapping(value = "/{id}",method = RequestMethod.GET)
public User getUser(@PathVariable Long id){
return userService.findUserById(id);
}
@ApiOperation(value = "更新信息",notes = "根据url的id来指定更新用户信息")
@RequestMapping(value = "/{id}",method = RequestMethod.PUT)
public User putUser(@PathVariable Long id,@RequestBody User user){
User u = new User();
u.setUsername(user.getUsername());
u.setPassword(user.getPassword());
u.setId(user.getId());
return userService.updateUser(u);
}
@ApiOperation(value = "删除用户",notes = "根据url的id来指定删除的用户")
@RequestMapping(value = "/{id}",method = RequestMethod.DELETE)
public String deleteUser(@PathVariable long id){
userService.deleteUser(id);
return "success";
}
@ApiIgnore
@RequestMapping(value = "/hi",method = RequestMethod.GET)
public String jsonTest(){
return "hello world";
}
}
完了...
最简单的增删改查功能玩玩~
然后是整合redis 监控 以及springboot其余的很多重要的功能 负载均衡 熔断 网关管理 等等都有介绍 这方面还是要多做项目进行深入理解
package com.forezp.helloworld.dao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Repository;
import java.util.concurrent.TimeUnit;
/**
* @Author: Pandy
* @Date: 2019/3/31 10:12
* @Version 1.0
*/
@Repository
public class RedisDao {
@Autowired
private StringRedisTemplate stringRedisTemplate;
//设值
public void setKey(String key,String value){
ValueOperations ops = stringRedisTemplate.opsForValue();
ops.set(key, value,1,TimeUnit.MINUTES);//1分钟过期
}
//取值
public String getValue(String key){
ValueOperations ops = this.stringRedisTemplate.opsForValue();
return ops.get(key);
}
}
package com.forezp.helloworld;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.swagger2.annotations.EnableSwagger2;
@RestController
@SpringBootApplication
@EnableSwagger2
public class HelloWorldApplication {
public static void main(String[] args) {
SpringApplication.run(HelloWorldApplication.class, args);
}
}
pojo
package com.forezp.helloworld.pojo;
import lombok.Data;
import javax.persistence.*;
/**
* @Author: Pandy
* @Date: 2019/3/30 23:06
* @Version 1.0
*/
@Entity
@Data
public class User {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@Column(nullable = false,unique = true)
private String username;
@Column
private String password;
}
很多配置文件失效 因为技术的更新换代太快 阅读官方文档和源代码的习惯一定要注意养成