在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");

即可。

GitHub 加速计划 / sp / spring-boot
38
7
下载
spring-projects/spring-boot: 是一个用于简化Spring应用开发的框架。适合用于需要快速开发企业级Java应用的项目。特点是可以提供自动配置、独立运行和内置的Tomcat服务器,简化Spring应用的构建和部署。
最近提交(Master分支:1 个月前 )
f72da4c7 Previously, if Actuator was being used in a non-web app such that spring-web was not on the classpath, the app would fail to start if Jackson 2 was present. This occured as the auto-configuration for the EndpointJackson2ObjectMapper tried to use spring-web's Jackson2ObjectMapperBuilder that was not present. This commit updates the auto-configuration to back off when Jackson2ObjectMapperBuilder is absent, aligning it with the behavior of JacksonEndpointAutoConfiguration in 3.5. Fixes gh-47788 1 天前
08a1d664 * pr/47784: Fix JacksonTesterTestAutoConfiguration javadoc Closes gh-47784 1 天前
Logo

新一代开源开发者平台 GitCode,通过集成代码托管服务、代码仓库以及可信赖的开源组件库,让开发者可以在云端进行代码托管和开发。旨在为数千万中国开发者提供一个无缝且高效的云端环境,以支持学习、使用和贡献开源项目。

更多推荐