SpringBoot的MyBatis的業務類
阿新 • • 發佈:2018-11-19
一 AreaService介面
package com.imooc.demo.service; import java.util.List; import com.imooc.demo.entity.Area; public interface AreaService { /** * 獲取區域列表 * * @return */ List<Area> getAreaList(); /** * 通過區域Id獲取區域資訊 * * @param areaId * @return */ Area getAreaById(int areaId); /** * 增加區域資訊 * * @param area * @return */ boolean addArea(Area area); /** * 修改區域資訊 * * @param area * @return */ boolean modifyArea(Area area); /** * 刪除區域資訊 * * @param area * @return */ boolean deleteArea(int areaId); }
二 AreaServiceImpl實現類
package com.imooc.demo.service.impl; import java.util.Date; import java.util.List; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import com.imooc.demo.dao.AreaDao; import com.imooc.demo.entity.Area; import com.imooc.demo.service.AreaService; @Service public class AreaServiceImpl implements AreaService { @Autowired private AreaDao areaDao; @Override public List<Area> getAreaList() { // 返回所有的區域資訊 return areaDao.queryArea(); } @Override public Area getAreaById(int areaId) { return areaDao.queryAreaById(areaId); } @Transactional @Override public boolean addArea(Area area) { // 空值判斷,主要是判斷areaName不為空 if (area.getAreaName() != null && !"".equals(area.getAreaName())) { // 設定預設值 area.setCreateTime(new Date()); area.setLastEditTime(new Date()); try { int effectedNum = areaDao.insertArea(area); if (effectedNum > 0) { return true; } else { throw new RuntimeException("新增區域資訊失敗!"); } } catch (Exception e) { throw new RuntimeException("新增區域資訊失敗:" + e.toString()); } } else { throw new RuntimeException("區域資訊不能為空!"); } } @Transactional @Override public boolean modifyArea(Area area) { // 空值判斷,主要是areaId不為空 if (area.getAreaId() != null && area.getAreaId() > 0) { // 設定預設值 area.setLastEditTime(new Date()); try { // 更新區域資訊 int effectedNum = areaDao.updateArea(area); if (effectedNum > 0) { return true; } else { throw new RuntimeException("更新區域資訊失敗!"); } } catch (Exception e) { throw new RuntimeException("更新區域資訊失敗:" + e.toString()); } } else { throw new RuntimeException("區域資訊不能為空!"); } } @Transactional @Override public boolean deleteArea(int areaId) { if (areaId > 0) { try { // 刪除區域資訊 int effectedNum = areaDao.deleteArea(areaId); if (effectedNum > 0) { return true; } else { throw new RuntimeException("刪除區域資訊失敗!"); } } catch (Exception e) { throw new RuntimeException("刪除區域資訊失敗:" + e.toString()); } } else { throw new RuntimeException("區域Id不能為空!"); } } }
三 事務處理
package com.imooc.demo.config.service; import javax.sql.DataSource; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.Configuration; import org.springframework.jdbc.datasource.DataSourceTransactionManager; import org.springframework.transaction.PlatformTransactionManager; import org.springframework.transaction.annotation.EnableTransactionManagement; import org.springframework.transaction.annotation.TransactionManagementConfigurer; /** * 對標spring-service裡面的transactionManager * 繼承TransactionManagementConfigurer是因為開啟annotation-driven * * @author xiangze * */ @Configuration // 首先使用註解 @EnableTransactionManagement 開啟事務支援後 // 在Service方法上添加註解 @Transactional 便可 @EnableTransactionManagement public class TransactionManagementConfiguration implements TransactionManagementConfigurer { @Autowired // 注入DataSourceConfiguration裡邊的dataSource,通過createDataSource()獲取 private DataSource dataSource; @Override /** * 關於事務管理,需要返回PlatformTransactionManager的實現 */ public PlatformTransactionManager annotationDrivenTransactionManager() { return new DataSourceTransactionManager(dataSource); } }