Spring Boot 項目學習 (二) MySql + MyBatis 註解 + 分頁控件 配置

引言

  本文主要在Spring Boot 基礎項目的基礎上,添加 Mysql 、MyBatis(註解方式)與 分頁控件 的配置,用於協助完成數據庫操做。html

系列文檔目錄

Spring Boot 項目學習 (一) 項目搭建java

Spring Boot 項目學習 (二) MySql + MyBatis 註解 + 分頁控件 配置mysql

Spring Boot 項目學習 (三) Spring Boot + Redis 搭建git

Spring Boot 項目學習 (四) Spring Boot整合Swagger2自動生成API文檔github

建立數據表

  這個過程就暫時省略了。web

搭建 MyBatis 

修改pom.xml,添加一下依賴

<!-- mybatis -->
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>${mybatis.version}</version>
        </dependency>
        <!--mysql-->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>${mysql.version}</version>
        </dependency>

修改配置文件application.yml

  添加數據庫鏈接spring

spring:
  datasource:
    url: jdbc:mysql://******:3306/***
    username: ********
    password: ********
    driver-class-name: com.mysql.jdbc.Driver

代碼實現

  配置完,基本信息後,接下來就是代碼實現方式sql

  1.1 添加服務層接口 Dao數據庫

package com.springboot.dao; import com.springboot.entity.Church; import org.apache.ibatis.annotations.Mapper; import org.apache.ibatis.annotations.Select; import java.util.List; @Mapper public interface ChurchDao { /** * 獲取指定教會/團契信息 * @param churchId 教會/團契標識 * @return 教會/團契對象 */ @Select("SELECT * FROM Church WHERE ChurchId = #{churchId}") Church get(String churchId); /** * 獲取教會/團契列表 * @return 教會/團契列表 */ @Select("SELECT * FROM Church") List<Church> list(); }
View Code

  1.2 添加服務層接口的實現(即業務邏輯層)apache

package com.springboot.service; import com.springboot.dao.ChurchDao; import com.springboot.entity.Church; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import java.util.List; @Service public class ChurchService implements ChurchDao { @Autowired private ChurchDao churchDao; /** * 獲取指定教會/團契信息 * @param churchId 教會標識 * @return 教會/團契信息 */
    public Church get(String churchId) { return churchDao.get(churchId); } /** * 獲取教會/團契列表 * @return 教會/團契列表 */
    public List<Church> list() { return churchDao.list(); } }
View Code

上述代碼中使用到了一些註解,咱們來看下這些註解表示什麼意思。

@Autowired
@Autowired顧名思義,就是自動裝配,其做用是爲了消除代碼Java代碼裏面的getter/setter與bean屬性中的property。固然,getter看我的需求,若是私有屬性須要對外提供的話,應當予以保留。

當Spring發現@Autowired註解時,將自動在代碼上下文中找到和其匹配(默認是類型匹配)的Bean,並自動注入到相應的地方去。

當@Autowired進行接口注入,若是實現類有多個該怎麼辦?此時能夠使用@Qualifier註解,如:@Qualifier("接口實現類的類名")

@Service

@Service註解,咱們在代碼中看到它帶了一個名稱areaService,若是隻有一個類實現了AreaService接口的話,你不帶名稱也是能夠的,可是若是有多個類實現了AreaService接口,那麼你就須要經過一個名稱來進行區分。

@Override
Java SE5新增長@Override註解,它並非關鍵字,可是能夠把它看成關鍵字使用。當你想要覆寫(重寫)某個方法時,能夠選擇添加這個註解,

  1.3 添加控制器ChurchController

package com.springboot.controller; import com.springboot.entity.Church; import com.springboot.service.ChurchService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; import java.util.List; @RestController @RequestMapping("/church") public class ChurchController { @Autowired private ChurchService churchService; // 獲取指定教會/團契信息
    @RequestMapping(value = "get", method = RequestMethod.GET) public String getChurch(@RequestParam("id") String churchId) { Church church = churchService.get(churchId); return church.toString(); } // 獲取教會/團契列表
    @RequestMapping(value = "list", method = RequestMethod.GET) public List<Church> list() { return churchService.list(); } }
View Code

分頁控件的配置

  1. 修改pom.xml,添加依賴

<!-- 分頁插件 -->
        <dependency>
            <groupId>com.github.pagehelper</groupId>
            <artifactId>pagehelper-spring-boot-starter</artifactId>
            <version>1.2.5</version>
        </dependency>

  2. 修改配置文件,添加基本配置

pagehelper:
  helper-dialect: mysql
  reasonable: true
  support-methods-arguments: true
  params: count==countSql
  page-size-zero: true

  3. 代碼實現

    根據不一樣的需求,返回的分頁對象PageInfo,能夠輸出不一樣的對象結構。

/**
     * 分頁獲取教會/團契列表
     * @param page 當前頁
     * @param pageSize 每頁顯示數
     * @return 教會/團契列表
     */
    public PageInfo<Church> list(int page, int pageSize) {
        PageHelper.startPage(page, pageSize);
        List<Church> list = churchDao.list();
        return new PageInfo<>(list);
    }
相關文章
相關標籤/搜索