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);
    }
相关文章
相关标签/搜索