一、实验介绍

1.1 实验内容

本节课程主要利用 Spring 框架实现 Service 层。

1.2 实验知识点

Spring 框架

1.3 实验环境

JDK1.8

Eclipse JavaEE

二、实验步骤

在项目 hrms 的目录 src/main/java 下新建包 com.shiyanlou.service,作为 Servcie 层接口的包,新建包 com.shiyanlou.service.impl 作为 Servcie 层实现的包。

2.1 AdminService 接口与实现

在包 com.shiyanlou.service 下建一个 AdminService.java 接口文件,代码如下:

package com.shiyanlou.service;

import java.util.List;

import java.util.Map;

import com.shiyanlou.domain.Admin;

public interface AdminService {

/** 登录

*

* @param admin

* @return

*/

public Admin login(Admin admin);

/** 根据条件查询管理员

*

* @param map

* @return

*/

public List findAdmins(Map map);

/** 根据条件查询管理员人数

*

* @param map

* @return

*/

public Integer getCount(Map map);

/** 添加管理员

*

* @param admin

* @return

*/

public Integer addAdmin(Admin admin);

/** 修改管理员

*

* @param admin

* @return

*/

public Integer updateAdmin(Admin admin);

/** 删除管理员

*

* @param id

* @return

*/

public Integer deleteAdmin(Integer id);

}

在包 com.shiyanlou.service.impl 下建一个类 AdminServiceImpl 实现上述的接口,代码如下:

package com.shiyanlou.service.impl;

import java.util.List;

import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.shiyanlou.dao.AdminDao;

import com.shiyanlou.domain.Admin;

import com.shiyanlou.service.AdminService;

@Service("adminService")

public class AdminServiceImpl implements AdminService {

// 自动注入 DAO 对象

@Resource

private AdminDao adminDao;

// 实现接口中的方法

@Override

public Admin login(Admin admin) {

return adminDao.login(admin);

}

@Override

public List findAdmins(Map map) {

return adminDao.findAdmins(map);

}

@Override

public Integer getCount(Map map) {

return adminDao.getCount(map);

}

@Override

public Integer addAdmin(Admin admin) {

return adminDao.addAdmin(admin);

}

@Override

public Integer updateAdmin(Admin admin) {

return adminDao.updateAdmin(admin);

}

@Override

public Integer deleteAdmin(Integer id) {

return adminDao.deleteAdmin(id);

}

}

2.2 PostService 接口与实现

在包 com.shiyanlou.service 下建一个 PostService.java 接口文件,代码如下:

package com.shiyanlou.service;

import java.util.List;

import java.util.Map;

import com.shiyanlou.domain.Post;

public interface PostService {

/** 根据条件查询公告

*

* @param map

* @return

*/

public ListfindPosts(Map map);

/** 根据条件查询公告数量

*

* @param map

* @return

*/

public Integer getCount(Map map);

/** 添加公告

*

* @param post

* @return

*/

public Integer addPost(Post post);

/** 修改公告

*

* @param post

* @return

*/

public Integer updatePost(Post post);

/** 删除公告

*

* @param id

* @return

*/

public Integer deletePost(Integer id);

/** 根据 ID 查询公告信息

*

* @param id

* @return

*/

public Post getPostById(Integer id);

}

在包 com.shiyanlou.service.impl 下建一个类 PostServiceImpl 实现上述的接口,代码如下:

package com.shiyanlou.service.impl;

import java.util.List;

import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.shiyanlou.dao.PostDao;

import com.shiyanlou.domain.Post;

import com.shiyanlou.service.PostService;

@Service("postService")

public class PostServiceImpl implements PostService {

// 自动注入 DAO 对象

@Resource

private PostDao postDao;

// 实现接口中的方法

@Override

public List findPosts(Map map) {

return postDao.findPosts(map);

}

@Override

public Integer getCount(Map map) {

return postDao.getCount(map);

}

@Override

public Integer addPost(Post post) {

return postDao.addPost(post);

}

@Override

public Integer updatePost(Post post) {

return postDao.updatePost(post);

}

@Override

public Integer deletePost(Integer id) {

return postDao.deletePost(id);

}

@Override

public Post getPostById(Integer id) {

return postDao.getPostById(id);

}

}

2.3 DepartmentService 接口与实现

在包 com.shiyanlou.service 下建一个 DepartmentService.java 接口文件,代码如下:

package com.shiyanlou.service;

import java.util.List;

import java.util.Map;

import com.shiyanlou.domain.Department;

public interface DepartmentService {

/** 根据条件查询部门

*

* @param map

* @return

*/

public List findDepartments(Map map);

/** 根据条件查询部门数量

*

* @param map

* @return

*/

public Integer getCount(Map map);

/** 添加部门

*

* @param department

* @return

*/

public Integer addDepartment(Department department);

/** 修改部门

*

* @param department

* @return

*/

public Integer updateDepartment(Department department);

/** 删除部门

*

* @param id

* @return

*/

public Integer deleteDepartment(Integer id);

}

在包 com.shiyanlou.service.impl 下建一个类 DepartmentServiceImpl 实现上述的接口,代码如下:

package com.shiyanlou.service.impl;

import java.util.List;

import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.shiyanlou.dao.DepartmentDao;

import com.shiyanlou.domain.Department;

import com.shiyanlou.service.DepartmentService;

@Service("departmentService")

public class DepartmentServiceImpl implements DepartmentService {

// 自动注入 DAO 对象

@Resource

private DepartmentDao departmentDao;

// 实现接口中的方法

@Override

public List findDepartments(Map map) {

return departmentDao.findDepartments(map);

}

@Override

public Integer getCount(Map map) {

return departmentDao.getCount(map);

}

@Override

public Integer addDepartment(Department department) {

return departmentDao.addDepartment(department);

}

@Override

public Integer updateDepartment(Department department) {

return departmentDao.updateDepartment(department);

}

@Override

public Integer deleteDepartment(Integer id) {

Integer flag = null;

// 如果删除关联外键的行记录,抛出异常

try {

flag = departmentDao.deleteDepartment(id);

} catch (Exception e) {

throw new RuntimeException();

}

return flag;

}

}

2.4 PositionService 接口与实现

在包 com.shiyanlou.service 下建一个 PositionService.java 接口文件,代码如下:

package com.shiyanlou.service;

import java.util.List;

import java.util.Map;

import com.shiyanlou.domain.Position;

public interface PositionService {

/** 根据条件查询职位

*

* @param map

* @return

*/

public List findPositions(Map map);

/** 根据条件查询职位数量

*

* @param map

* @return

*/

public Integer getCount(Map map);

/** 添加职位

*

* @param position

* @return

*/

public Integer addPosition(Position position);

/* 修改职位

*

* @param position

* @return

*/

public Integer updatePosition(Position position);

/** 删除职位

*

* @param id

* @return

*/

public Integer deletePosition(Integer id);

}

在包 com.shiyanlou.service.impl 下建一个类 PositionServiceImpl 实现上述的接口,代码如下:

package com.shiyanlou.service.impl;

import java.util.List;

import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.shiyanlou.dao.PositionDao;

import com.shiyanlou.domain.Position;

import com.shiyanlou.service.PositionService;

@Service("positionService")

public class PositionServiceImpl implements PositionService {

// 自动注入 DAO 对象

@Resource

private PositionDao positionDao;

// 实现接口中的方法

@Override

public List findPositions(Map map) {

return positionDao.findPositions(map);

}

@Override

public Integer getCount(Map map) {

return positionDao.getCount(map);

}

@Override

public Integer addPosition(Position position) {

return positionDao.addPosition(position);

}

@Override

public Integer updatePosition(Position position) {

return positionDao.updatePosition(position);

}

@Override

public Integer deletePosition(Integer id) {

Integer flag = null;

// 如果删除关联外键的行记录,抛出异常

try {

flag = positionDao.deletePosition(id);

} catch (Exception e) {

throw new RuntimeException();

}

return flag;

}

}

2.5 EmployeeService 接口与实现

在包 com.shiyanlou.service 下建一个 EmployeeService.java 接口文件,代码如下:

package com.shiyanlou.service;

import java.util.List;

import java.util.Map;

import com.shiyanlou.domain.Employee;

import com.shiyanlou.domain.Post;

public interface EmployeeService {

/** 根据条件查询员工

*

* @param map

* @return

*/

public ListfindEmployees(Map map);

/** 根据条件查询员工人数

*

* @param map

* @return

*/

public Integer getCount(Map map);

/** 添加员工

*

* @param employee

* @return

*/

public Integer addEmployee(Employee employee);

/** 修改员工

*

* @param employee

* @return

*/

public Integer updateEmployee(Employee employee);

/** 删除员工

*

* @param id

* @return

*/

public Integer deleteEmployee(String id);

}

在包 com.shiyanlou.service.impl 下建一个类 EmployeeServiceImpl 实现上述的接口,代码如下:

package com.shiyanlou.service.impl;

import java.util.List;

import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.shiyanlou.dao.EmployeeDao;

import com.shiyanlou.domain.Employee;

import com.shiyanlou.domain.Post;

import com.shiyanlou.service.EmployeeService;

@Service("employeeService")

public class EmployeeServiceImpl implements EmployeeService {

// 自动注入 DAO 对象

@Resource

private EmployeeDao employeeDao;

// 实现接口中的方法

@Override

public List findEmployees(Map map) {

return employeeDao.findEmployees(map);

}

@Override

public Integer getCount(Map map) {

return employeeDao.getCount(map);

}

@Override

public Integer addEmployee(Employee employee) {

Integer flag = null;

// 如果插入记录主键重复,抛出异常

try {

flag = employeeDao.addEmployee(employee);

} catch (Exception e) {

throw new RuntimeException();

}

return flag;

}

@Override

public Integer updateEmployee(Employee employee) {

return employeeDao.updateEmployee(employee);

}

@Override

public Integer deleteEmployee(String id) {

return employeeDao.deleteEmployee(id);

}

}

三、实验总结

到这里我们就完成了 Service 层的代码实现,下一节我们将完成 MyBatis 和 Spring 的整合及测试。

java中的service层教程_Service 层实现相关推荐

  1. Java中注解学习系列教程-2

    简介 在上一篇文章中,我们讲解了:Java中注解的定义.JDK中内置注解.第三方注解.本文是注解系列教程中的第二篇.来看看今天主要内容:1:注解的分类2:元注解说明3:自定义注解声明一:注解的分类注解 ...

  2. Java中注解学习系列教程-1

    前言: 我们为什么要学习注解? 注解是我们的编程更简洁明了.spring boot的流行也带动了注解.spring boot中就使用了大量的注解来减少配置.我们在使用spring boot开发时候,感 ...

  3. java中min用法,java11教程--类MinguoDate用法

    民国历法系统的约会. 该日历系统主要用于中华民国,通常称为台湾. 对齐日期使得0001-01-01 (Minguo)是1912-01-01 (ISO) . 在MinguoDate实例上使用身份敏感操作 ...

  4. Java中Stream详细使用教程

    1.java8中Stream流以及lambda的使用: stream可以将需要处理的集合元素看做流操作,是结合对象功能的一个增强: lambda表达式与stream结合使用,可以更好的对集合进行遍历. ...

  5. java中web service的几种实现方式(自用)

    https://www.cnblogs.com/liubin1988/p/8995444.html 转载于:https://www.cnblogs.com/chyxOne/p/9673126.html

  6. java excel 注解_Java中注解学习系列教程-5 excel导出优化初级阶段

    在上一篇文章中,我们实现了使用自定义注解导出Excel的小案例.但是有问题的.我们发现,如果对象属性中包含了date类型或者使用了枚举类.这个时候就会出问题.我们来看看问题所在: 是不是已经看到了结果 ...

  7. 分层设计 --java中的几种包

    对于刚接触包分层的同学,下面简单介绍一下java中各个层次: Modle 模型层 :存放你的实体类 dao:主要做数据库的交互工作,具体的增删改查等方法,操作数据库的:这里也可以存放查询所有的信息接口 ...

  8. Java中的生成器设计模式

    Java 中的 Builder设计模式是一种创建模式,即用于创建对象,类似于 工厂方法设计模式 ,这也是创建设计模式. 在学习任何设计模式之前,我建议先找出特定设计模式要解决的问题. 众所周知, 必要 ...

  9. @excel 注解_Java中注解学习系列教程-3

    本文是<Java中注解学习系列教程>第三篇文章 在前两篇中我们学习了注解的定义.JDK内置注解.注解分类及自定义注解的写法. 本文咱们将学习: ​ 1:自定义注解一些说明 2:自定义注解怎 ...

最新文章

  1. 当对象或对象属性为空时,如何安全给对象或对象属性添加默认值
  2. java调优 视频_Java优化高性能高并发+高并发程序设计视频教程
  3. 图片/容器/字体 透明度[opacity:0.4; filter:alpha(opacity=40)]
  4. nonlocal python3_Python3中 对local和nonlocal 关键字的认识
  5. JSP页面Table的数据绑定
  6. [分际]如何使用EVENTLOG类操作日志
  7. wpf首次项目开发技术总结wpf页面
  8. qt爬取网页信息_豆瓣TOP250数据爬取
  9. Java 9 JShell示例:集合静态工厂方法
  10. DYNAMIC_DOWNCAST STATIC_DOWNCAST IsKindOf
  11. 使用phaserjs开发简单h5小游戏
  12. 分组求和并排序python_python 实现分组求和与分组累加求和代码
  13. 固态硬盘能不能提高计算机速度,固态硬盘掉速如何解决?几招轻松提升SSD速度方法 (全文)...
  14. excel几个数相加等于某个数_『excel表如何从一列数据中筛选出合计等于某数的某些数?』...
  15. HCL_路由器_三层交换
  16. The .NET Core SDK cannot be located.解决方法
  17. python小游戏——塔防小游戏代码开源
  18. mysql存储用户昵称_mysql 微信用户昵称 emoji 完整保存 - 人人都是架构师
  19. (四)Tensorboard学习——mnist_with_summaries.py
  20. 对拉格朗日乘数法的直观认识

热门文章

  1. AXI_Bus_Matrix_4x4 设计
  2. 计算机蓝屏的解决方法,电脑蓝屏是怎么回事,详细教您电脑蓝屏是怎么解决
  3. SpringMVC 框架基础
  4. FMI-人工智能大数据高峰论坛(深圳站)
  5. 怎么做服务器压力测试? 1
  6. ubuntu-16.04 详细安装教程(图文)附下载地址
  7. vxworks pci驱动解析
  8. 【Linux篇】第十七篇——信号量
  9. android计时器handler,用 Handler 轻松实现专属Android定时器
  10. 观战Retrofit开发中的哪点事