分享 ibatis与hibernate结合使用(应用篇二)

10年前
1、服务层接口
import java.sql.SQLException;    import com.nalike.system.model.Log;    public interface LogService {     public void addLog(Log log) throws SQLException ;  }  

 

2、服务层接口的实现类:

import java.sql.SQLException;  import javax.annotation.Resource;  import org.springframework.stereotype.Service;  import com.nalike.system.dao.LogDao;  import com.nalike.system.model.Log;    @Service  public class LogServiceImpl implements LogService{     @Resource   private LogDao logDao ;      public void addLog(Log log) throws SQLException {    logDao.addLog(log) ;   }    }

3、配置测试基类:

import org.junit.runner.RunWith;  import org.springframework.test.context.ContextConfiguration;  import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;  import org.springframework.test.context.transaction.TransactionConfiguration;  import org.springframework.transaction.annotation.Transactional;    @RunWith(SpringJUnit4ClassRunner.class)  @ContextConfiguration(locations = "/resource/applicationContext.xml")  @TransactionConfiguration(transactionManager="transactionManager",defaultRollback=false)  @Transactional  public class BaseTest {    }

4、写个简单的测试插入一条记录:

import javax.annotation.Resource;  import org.junit.Test;  import com.nalike.base.BaseTest;  import com.nalike.system.model.Log;    public class LogTest extends BaseTest{   @Resource   private LogService logService ;   @Test   public void saveLog(){    Log log = new Log() ;    log.setContent("保存日志成功!") ;    log.setType(1) ;    log.setIp("10.1.5.8") ;    log.setCreateTime("2011-07-07") ;    log.setId(1) ;    try {     logService.addLog(log) ;     System.out.println("日志保存成功!") ;    } catch (Exception e) {     e.printStackTrace();     System.out.println("保存异常回滚!") ;    }           }        }