在Spring-Boot中引入service

在XXXApplication的同级目录下,添加service文件夹,并在其下添加impl子文件夹。

设该service用于与DAO层交互来操作student表。

 

一.   在service下添加一个interface,其名称为IStudentService。前面的I代表interface。添加的其他interface同理。

为其定义一个get函数,用于获取Student对象:

package com.template.service;

import com.template.model.Student;

public interface IStudentService {
    Student get(String id);
}

二.   在service/impl文件夹下,添加一个类,其名称为StudentServiceImpl。后面的Impl代表实现。该类的类名与1中的interface名是对应的。

然后使用@Override重载1中的接口:

package com.template.service.impl;

import com.template.model.Student;
import com.template.service.IStudentService;
import org.springframework.stereotype.Service;

@Service("StudentService")
public class StudentServiceImpl implements IStudentService {

    @Override
    public Student get(String id) {
        return null;
    };
}

这样,service就实现了。

三.   调用

在Controller中,使用@Autowired引入service对象:

@Autowired
private IStudentService sudentService;

注意这里引入的类型是interface

然后调用其函数:

Student student = sudentService.get("201801");

即可。

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
PageHelper 是一个 MyBatis 分页插件,可以帮助开发者在 MyBatis 方便地实现分页功能。而 pagehelper-spring-boot-starter 是一个 PageHelper 在 Spring Boot 的集成,使用起来非常方便。以下是使用 pagehelper-spring-boot-starter 实现分页的步骤: 1. 引入依赖 在 pom.xml 文件添加以下依赖: ``` <dependency> <groupId>com.github.pagehelper</groupId> <artifactId>pagehelper-spring-boot-starter</artifactId> <version>1.2.10</version> </dependency> ``` 2. 配置 PageHelper 在 application.properties 文件添加以下配置: ``` # 分页插件配置 pagehelper.helperDialect=mysql pagehelper.reasonable=true pagehelper.supportMethodsArguments=true pagehelper.params=count=countSql ``` 其,helperDialect 为数据库方言,reasonable 为是否开启合理化分页,supportMethodsArguments 为是否支持 Mapper 方法参数为 Page 类型,params 为分页参数映射。 3. 使用分页 在 Mapper 接口的方法添加 Page 参数,即可实现分页查询: ``` List<User> getUsers(@Param("name") String name, Page<User> page); ``` 在 Service调用该方法,并传入 Page 参数即可实现分页查询: ``` Page<User> page = PageHelper.startPage(pageNum, pageSize); List<User> users = userMapper.getUsers(name, page); ``` 其,pageNum 和 pageSize 分别为当前页码和每页显示数量。PageHelper.startPage 方法会自动拦截下一次查询,并将查询转为分页查询。查询结果会封装在 Page 对象,可以通过该对象获取分页信息和查询结果。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值