使用SpringBoot开发的书签管理应用

采用SpringBoot+MyBatis开发的书签管理应用,其主要目的是解除用户对固定浏览器书签的依赖,使之可以在任意浏览器上访问自定义书签。

Java 内容管理系统

详细介绍

关于index.html

index.html是采用SpringBoot+MyBatis开发的书签管理应用,其主要目的是解除用户对固定浏览器书签的依赖,使之可以在任意浏览器上访问自定义书签。


开发环境

  • SpringBoot 2.1.8.RELEASE
  • JDK 8
  • MySQL 8.0

功能说明

  • 书签展示

    1569672586104

  • 书签管理

    1569672648116

代码说明

  • sql建库【sql语句位于根目录下的bootmark.sql】

    1569672731876


  • application.yaml

    spring:
      datasource:
        #   数据源基本配置
        username: username
        password: password!   # 1.配置生成的password
        driver-class-name: com.mysql.cj.jdbc.Driver
        url: jdbc:mysql://ip:port/bookmark?serverTimezone=UTC
        type: com.alibaba.druid.pool.DruidDataSource
      thymeleaf:
        cache: false
    

    主要是数据源配置【注:8.0驱动名改为com.mysql.cj.jdbc.Driver】


  • 前端实现

    1569672895887

    • 前端页面分为展示页和编辑页,分别对应index.html和operation.html.

    • 使用Thymeleaf表达式和BootStrap框架。

    • 其中BootStrap出于对模态框的支持,选用3.x.

    • 后端使用MyBatis注解开发


部署说明

  • 服务器端安装JRE
  • 部署SpringBoot应用

更新记录


v1.1 add Security

  • 描述:

使用Spring Security添加编辑页面的访问控制


  • 功能说明

    点击编辑页面弹出账户密码登录界面

    1569768803196

    账户密码在application.yaml中配置

    1569768838938


  • 代码说明

    配置SecurityConfig

    @Configuration
    public class MySecurityConfig  extends WebSecurityConfigurerAdapter {
    
        @Override
        protected void configure(HttpSecurity http) throws Exception {
            http.authorizeRequests()
                    .antMatchers("/operation/**").hasRole("admin")
                    .and()
                    .formLogin()
                    .loginProcessingUrl("/login").permitAll()
                    .and()
                    .csrf()
                    .disable();
        }
    
    }
    

访问/operation/**的URL需要admin角色权限,之前在yaml中定义

【注】

  • 所有的页面URL添加前缀operation
  • 添加security依赖

v1.2 集成Swagger,优化API

pom依赖

<dependency>
    <groupId>io.springfox</groupId>
    <artifactId>springfox-swagger2</artifactId>
    <version>2.9.2</version>
</dependency>

<dependency>
    <groupId>io.springfox</groupId>
    <artifactId>springfox-swagger-ui</artifactId>
    <version>2.9.2</version>
</dependency>

添加配置类

@Configuration
@EnableSwagger2
public class SwaggerConfig {
    @Bean
    public Docket api() {
        return new Docket(DocumentationType.SWAGGER_2)
                .select()
                .apis(RequestHandlerSelectors.any())
                .paths(PathSelectors.any())
                .build();
    }
}

添加Controller接口注释

@Api(tags = "书签操作相关接口")
@Controller
public class WebsiteController {

    @Autowired
    WebsiteMapper websiteMapper;

    @ApiOperation("获取书签信息,跳转到首页")
    @GetMapping("/")
    public String getWebsites(Model model)
    {
        model.addAttribute("kindInfo",websiteMapper.getKinds());
        model.addAttribute("websiteInfo",websiteMapper.getWebsites());
        return "index";
    }

    @ApiOperation("获取书签信息,跳转到操作页")
    @GetMapping("/operation")
    public String save(Model model)
    {
        model.addAttribute("kindInfo",websiteMapper.getKinds());
        model.addAttribute("websiteInfo",websiteMapper.getWebsites());
        return "operation";
    }

    /**
     * 分类操作接口
     */

    @ApiOperation("添加分类")
    @GetMapping("/operation/kind/add")
    public String addKind(Kind kind)
    {
        System.out.println("添加Website:"+kind);
        websiteMapper.insertKind(kind);
        return "redirect:/operation";
    }

    @ApiOperation("更新分类")
    @GetMapping("/operation/kind/update")
    public String updateWebsite(Kind kind)
    {
        System.out.println("更新类别:"+kind);
        websiteMapper.updateKind(kind);
        return "redirect:/operation";
    }

    @ApiOperation("删除分类")
    @GetMapping("/operation/kind/delete")
    public String deleteKindById(Integer id)
    {
        System.out.println("删除类别:"+id);
        websiteMapper.deleteKindById(id);
        websiteMapper.deleteWebsiteByKindID(id);
        return "redirect:/operation";
    }

    /**
     * 书签操作接口
     */

    @ApiOperation("添加书签")
    @GetMapping("/operation/website/add")
    public String addWebsiteByKindID(Integer id)
    {
        System.out.println("添加Website:"+id);
        websiteMapper.insertWebsite(id);
        return "redirect:/operation";
    }

    @ApiOperation("更新书签")
    @GetMapping("/operation/website/update")
    public String updateWebsite(Website website)
    {
        System.out.println("更新网址"+website);
        websiteMapper.updateWebsite(website);
        return "redirect:/operation";
    }

    @ApiOperation("删除书签")
    @GetMapping("/operation/website/delete")
    public String deleteWebsite(Integer id)
    {
        System.out.println("删除网址"+id);
        websiteMapper.deleteWebsiteByID(id);
        return "redirect:/operation";
    }
}

访问http://localhost:8081/swagger-ui.html

1570084840170