SSM实现简单的CRUD之Service层

前言

开始Service层的编码之前,我们首先需要进行Dao层编码之后的思考:在Dao层我们只完成了针对表的相关操作包括写了接口方法和映射文件中的sql语句,并没有编写逻辑的代码,例如对多个Dao层方法的拼接,当我们用户成功秒杀商品时我们需要进行商品的减库存操作和增加用户明细等等,这些逻辑我们都需要在Service层完成。但是这个CRUD并不需要什么很难的逻辑只是一个简单的增删查改,所以在这个CRUD的Service层会很简单,接下来我们便进行Service层代码的编写。

Service接口设计

在cn.hfbin.crud包下创建一个service包用于存放我们的Service

创建EmployeeService类,里面的方法应该是按”使用者”(程序员)的角度去设计,EmployeeService.java,代码如下:

package cn.hfbin.crud.service;

import cn.hfbin.crud.bean.Employee;
import cn.hfbin.crud.bean.EmployeeExample;
import cn.hfbin.crud.bean.EmployeeExample.Criteria;
import cn.hfbin.crud.dao.EmployeeMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class EmployeeService {

    @Autowired
    EmployeeMapper employeeMapper;

    /**
     * 查询所有员工
     * @return
     */
    public List<Employee> getAll() {
        // TODO Auto-generated method stub
        return employeeMapper.selectByExampleWithDept(null);
    }

    /**
     * 员工保存
     * @param employee
     *
     */
    public void saveEmp(Employee employee) {
        // TODO Auto-generated method stub
        employeeMapper.insertSelective(employee);
    }

    /**
     * 检验用户名是否可用
     * 
     * @param empName
     * @return  true:代表当前姓名可用   fasle:不可用
     */
    public boolean checkUser(String empName) {
        // TODO Auto-generated method stub
        EmployeeExample example = new EmployeeExample();
        //这个我相信学过Hibernate 的同学都知道这个用法 不难 你可以理解为在where 加入条件
        Criteria criteria = example.createCriteria();
        //andEmpNameEqualTo根据词意来理解这个相当于 传入的这个empName与数据库的empName进行对比 不存在返回 0 ,存在返回其他
        criteria.andEmpNameEqualTo(empName);
        long count = employeeMapper.countByExample(example);
        return count == 0;
    }
    /**
     * 按照员工id查询员工
     * @param id
     * @return
     */
    public Employee getEmp(Integer id) {
        // TODO Auto-generated method stub
        Employee employee = employeeMapper.selectByPrimaryKey(id);
        return employee;
    }

    /**
     * 员工更新
     * @param employee
     */
    public void updateEmp(Employee employee) {
        // TODO Auto-generated method stub
        employeeMapper.updateByPrimaryKeySelective(employee);
    }

    /**
     * 员工删除
     * @param id
     */
    public void deleteEmp(Integer id) {
        // TODO Auto-generated method stub
        employeeMapper.deleteByPrimaryKey(id);
    }


    /**
     * 批量删除员工
     * @param ids
     */
    public void deleteBatch(List<Integer> ids) {
        // TODO Auto-generated method stub
        EmployeeExample example = new EmployeeExample();
        Criteria criteria = example.createCriteria();
        //delete from xxx where emp_id in(1,2,3)
        criteria.andEmpIdIn(ids);
        employeeMapper.deleteByExample(example);
    }

}

在这个EmployeeService.java 类做一个说明上面我们用到了EmployeeExample下面对xxxExample做一下说明:

mybatis的逆向工程中会生成实例及实例对应的example,example用于添加条件,相当where后面的部分

xxxExample example = new xxxExample();
Criteria criteria = new example.createCriteria();

xxxExample 方法说明:

example.setOrderByClause(“字段名 ASC”);    添加升序排列条件,DESC为降序
example.setDistinct(false)  去除重复,boolean型,true为选择不重复的记录。
criteria.andXxxIsNull   添加字段xxx为null的条件
criteria.andXxxIsNotNull    添加字段xxx不为null的条件
criteria.andXxxEqualTo(value)   添加xxx字段等于value条件
criteria.andXxxNotEqualTo(value)    添加xxx字段不等于value条件
criteria.andXxxGreaterThan(value)   添加xxx字段大于value条件
criteria.andXxxGreaterThanOrEqualTo(value)  添加xxx字段大于等于value条件
criteria.andXxxLessThan(value)  添加xxx字段小于value条件
criteria.andXxxLessThanOrEqualTo(value)     添加xxx字段小于等于value条件
criteria.andXxxIn(List<?>)  添加xxx字段值在List<?>条件
criteria.andXxxNotIn(List<?>)   添加xxx字段值不在List<?>条件
criteria.andXxxLike(“%”+value+”%”)  添加xxx字段值为value的模糊查询条件
criteria.andXxxNotLike(“%”+value+”%”)   添加xxx字段值不为value的模糊查询条件
criteria.andXxxBetween(value1,value2)   添加xxx字段值在value1和value2之间条件
criteria.andXxxNotBetween(value1,value2)    添加xxx字段值不在value1和value2之间条件

以EmployeeService.java 里面用到的举例

EmployeeExample example = new EmployeeExample();
Criteria criteria = example.createCriteria();
criteria.andEmpNameEqualTo(empName);
long count = employeeMapper.countByExample(example);

相当于:

select * from tbl_emp where emp_name= 'hfbin'

DepartmentService.java,代码如下:

package cn.hfbin.crud.service;

import cn.hfbin.crud.bean.Department;
import cn.hfbin.crud.dao.DepartmentMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class DepartmentService {

    @Autowired
    private DepartmentMapper departmentMapper;

    //获取所有部门
    public List<Department> getDepts() {
        // TODO Auto-generated method stub
        List<Department> list = departmentMapper.selectByExample(null);
        return list;
    }

}
测试Service

在这我只抽取一两个进行测试其它测试需要你们自己动手了

package cn.hfbin.crud.test;


import cn.hfbin.crud.service.EmployeeService;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations={"classpath:applicationContext.xml"})
public class ServicceTest {

    @Autowired
    EmployeeService employeeService;

    /**
     * 测试用户名是否可用
     * 查询id为4416的信息
     */
    @Test
    public void text01(){
        System.out.println("返回true或false----"+employeeService.checkUser("hfbin"));
        System.out.println("返回id为4416的数据"+employeeService.getEmp(4416));
    }
}

结果如下图:

这里写图片描述

好到这service层到此结束!!! 有没有发现很简单

下一章进行Web层的开发

SSM整合 http://blog.youkuaiyun.com/qq_33524158/article/details/78360268

MyBatis-逆向工程讲解 http://blog.youkuaiyun.com/qq_33524158/article/details/78442664

SSM实现简单的CRUD之DAO层 http://blog.youkuaiyun.com/qq_33524158/article/details/78442771

SSM实现简单的CRUD之Service层 http://blog.youkuaiyun.com/qq_33524158/article/details/78449897

SSM实现简单的CRUD之Web层http://blog.youkuaiyun.com/qq_33524158/article/details/78462932

项目源码地址:https://github.com/hfbin/SSM-crud

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值