Spring Boot 项目学习 (二) MySql + MyBatis 注解 + 分页控件 配置

0 引言

  本文主要在Spring Boot 基础项目的基础上,添加 Mysql 、MyBatis(注解方式)与 分页控件 的配置,用于协助完成数据库操作。

1 创建数据表

  这个过程就暂时省略了。

2 搭建 MyBatis 

2.1 修改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>

2.2 修改配置文件application.yml

  添加数据库连接

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

2.3 代码实现

  配置完,基本信息后,接下来就是代码实现方式

  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 添加服务层接口的实现(即业务逻辑层)

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

3 分页控件的配置

  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);
    }

  

猜你喜欢

转载自www.cnblogs.com/huanghzm/p/11051647.html