SSH框架-Service层搭建

SSH框架-Service层搭建

1、定义服务层接口

       

package com.caicai.elec.service;

import com.caicai.elec.doamin.ElecText;

public interface IElecTextService {
       //定义服务名
       public final static String IELecTextservice="com.caicai.elec.service.impl.IElecTextServiceImpl";
       public void saveElecText(ElecText elecText);

}

 

2、实现服务层接口

   

package com.caicai.elec.service.impl;

import com.caicai.elec.dao.IElecTextDao;
import com.caicai.elec.doamin.ElecText;
import com.caicai.elec.service.IElecTextService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
@Transactional(readOnly = true)
//面向接口,所以这边需要将这个service注入进来。
@Service(IElecTextService.IELecTextservice)
public class IElecTextServiceImpl implements IElecTextService {
     //服务层与dao相关联
    //注入dao
    @Resource(name = IElecTextDao.serivicename)
    private IElecTextDao iElecTextDao;
    @Override
    //isolation:隔离级别  Isolation.DEFAULT 隔离级别默认
    //propagation 广播通知形式
    @Transactional(isolation = Isolation.DEFAULT,propagation = Propagation.REQUIRED,readOnly = false)
    public void saveElecText(ElecText elecText) {
           iElecTextDao.save(elecText);

    }



}

 

3、编写测试服务类

 

   

package junit;

import com.caicai.elec.dao.IElecTextDao;
import com.caicai.elec.doamin.ElecText;
import com.caicai.elec.service.IElecTextService;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import java.util.Date;
/*
* 项目只有在增、删、改的时候可写,其它都是只读
* 所以这边在业务层做了事务控制。
*
* */
public class TestService {
    @Test
    public void Test_ServiceSave(){
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("beans.xml");
        IElecTextService iElecTextService = (IElecTextService)applicationContext.getBean(IElecTextService.IELecTextservice);
        //实例化对象赋值
        ElecText elecText = new ElecText();
        elecText.setTextName("Test ServiceSave");
        elecText.setTextRemark("This is test Service");
        elecText.setTextDate(new Date());
        iElecTextService.saveElecText(elecText);

    }

}

 

文件架构

 

 

posted @ 2021-04-18 21:00  菜菜920  阅读(116)  评论(0编辑  收藏  举报