UserService實(shí)現(xiàn)(注意編程思維的養(yǎng)成)
根據(jù)面向接口編程的思維來講芋哭,在Service
中核心是實(shí)現(xiàn)Dao
層,并調(diào)用Dao
層恐似。
UserDao
層通過單元測(cè)試了,現(xiàn)在中心就應(yīng)該放在業(yè)務(wù)邏輯的實(shí)現(xiàn)便锨,畢竟數(shù)據(jù)持久化已經(jīng)實(shí)現(xiàn)了。
從服務(wù)端程序的角度看來我碟,用戶的主要業(yè)務(wù)有注冊(cè)放案、登錄、注銷登錄矫俺、注銷帳號(hào)等等吱殉,這里我們先拿注冊(cè)來說事。
用戶注冊(cè)流程分析(用戶角度):
填寫帳號(hào)相關(guān)信息
提交注冊(cè)信息
服務(wù)器返回是否注冊(cè)成功
用戶注冊(cè)流程分析(服務(wù)器角度):
收到用戶注冊(cè)請(qǐng)求
解包數(shù)據(jù)→封裝到UserBean
解包數(shù)據(jù)失敗(請(qǐng)求信息異常)恳守,返回錯(cuò)誤提示信息
針對(duì)具體的用戶信息檢查是否符合標(biāo)準(zhǔn)不符合檢查標(biāo)準(zhǔn)考婴,返回對(duì)應(yīng)的錯(cuò)誤提示
通過檢查贩虾,調(diào)用Dao
檢查是否存在同樣的用戶數(shù)據(jù)庫(kù)已經(jīng)存在相同的用戶信息催烘,不能重復(fù)添加,返回錯(cuò)誤提示信息
不存在同樣的用戶缎罢,添加新用戶伊群,并返回成功的提示信息
流程圖反映如下:

ssm框架用戶行為解析流程圖
自定義異常
新建一個(gè)與dao
,domain
同級(jí)的包exception
來存放自定義的異常
所有的自定義異常都要繼承于Exception
UserAireadyExistException
用戶已經(jīng)存在異常
public UserAireadyExistException(String s) {
super(s);
}
public UserAireadyExistException(String message, Throwable cause) {
super(message, cause);
}
下面的UserCanNotBeNullException
(用戶為空異常)策精,UserNameCanNotBeNullException
(用戶名為空異常)舰始,UserPwdCanNotBeNullException
(用戶密碼為空)異常代碼相類似
還有個(gè)其他異常
public class OtherThingsException extends Exception {
public OtherThingsException(String message) {
super(message);
}
public OtherThingsException(Exception e){
this(e.getMessage());
}
public OtherThingsException(String message, Throwable cause) {
super(message, cause);
}
}
可能會(huì)拋出的異常寫完了,下面來實(shí)現(xiàn)用戶注冊(cè)的Service
:
首先還是先寫個(gè)BaseService
接口咽袜,用泛型解耦
public interface BaseService<T> {
void add(T t) throws Exception;
}
用戶注冊(cè)接口
不同對(duì)象的業(yè)務(wù)體系不同丸卷,BaseService
并不能完全替代不同對(duì)象的具體行為表現(xiàn)
UserService
public interface UserService extends BaseService<User>{
void add(User user) throws Exception;
User findUser(User user) throws Exception;
}
實(shí)例化用戶注冊(cè)接口UserServiceImpl
在service
包下創(chuàng)建一個(gè)子包,叫serviceImpl
询刹,用來存放業(yè)務(wù)層接口實(shí)現(xiàn)類
UserServiceImpl
@Service("userService")
public class UserServiceImpl implements UserService{
@Autowired
private UserDao userDao;
/**
* 添加用戶谜嫉,一般來說需要檢查用戶為空、用戶名為空凹联、密碼為空
*/
public void add(User user) throws UserCanNotBeNullException, UserNameCanNotBeNullException, UserPwdCanNotBeNullException, UserAireadyExistException, OtherThingsException {
//先檢查用戶是否存在
if (null == user) {
//拋出用戶為空的自定義異常
throw new UserCanNotBeNullException("User can not be Null");
}
//用戶名不能為空檢查
if (StringUtils.isEmpty(user.getLoginId())) {
//拋出用戶名為空的自定義異常
throw new UserNameCanNotBeNullException("User name can not be Null");
}
//用戶密碼不能為空檢查
if (StringUtils.isEmpty(user.getPwd())) {
//拋出用戶密碼為空的自定義異常
throw new UserPwdCanNotBeNullException("User name can not be Null");
}
//由于我這個(gè)是管理系統(tǒng)沐兰,根據(jù)業(yè)務(wù)需求來說,我們的用戶基本信息都是不能為空的
//基本信息包括:姓名蔽挠、年齡住闯、用戶名、密碼澳淑、性別比原、手機(jī)號(hào),年齡大于18
if ( StringUtils.isEmpty(user.getSex())
|| user.getAge() < 18
|| StringUtils.isEmpty(user.getCellNumber())) {
//其他綜合異常
throw new OtherThingsException("Some use's base info can not be null");
}
//已經(jīng)存在相同用戶
if (null != userDao.findOneById(user.getLoginId())) {
//存在相同的用戶異常
throw new UserAireadyExistException("Register User Failed杠巡,Because the user Aiready exist");
}
int result = 0; //受影響的行數(shù)默認(rèn)為0
try {
result = userDao.add(user);
} catch (Exception e) {
System.out.println("添加用戶失敗,用戶已經(jīng)存在");
//其他用戶添加失敗異常
throw new OtherThingsException(e);
}
if (result > 0)
System.out.println("添加用戶成功");
}
/**
* 查找用戶
*
* @param user 用戶bean
* @throws Exception
*/
public User findUser(User user) throws Exception {
return userDao.findOneById(user.getLoginId());
}
/**
* 用于更新sessionId
*/
public void updateLoginSession(String sessionId, String loginId) {
userDao.updateLoginSession(sessionId, loginId);
}
}
在這里我們用到了StringUtils這個(gè)JAVA的工具類
http://www.reibang.com/p/2eb9e42ecf44
寫完每個(gè)Service
后量窘,都需要針對(duì)具體的對(duì)象的行為進(jìn)行單元測(cè)試,UserServiceTest
代碼如下:
同樣忽孽,要繼承基礎(chǔ)測(cè)試類
public class UserServiceTest extends BaseTest{
@Autowired
private UserServiceImpl userService;
//此處直接使用UserService的實(shí)現(xiàn)類绑改,主要是方便拋出異常谢床,然后異常出現(xiàn)時(shí)候可以針對(duì)性的處理
@Test
public void testAdd() {
User user = new User();
user.setLoginId("20171");
user.setName("意識(shí)流1");
user.setPwd("123456");
user.setSex("不知道");
user.setDuty("老大");
user.setCellNumber("12345678910");
user.setAge(10);
try {
userService.add(user);
} catch (UserCanNotBeNullException e) {
//用戶不能為空異常拋出
e.printStackTrace();
} catch (UserNameCanNotBeNullException e) {
//用戶名不能為空
e.printStackTrace();
} catch (UserPwdCanNotBeNullException e) {
//用戶密碼不能為空
e.printStackTrace();
} catch (UserAireadyExistException e) {
//用戶存在拋出
e.printStackTrace();
} catch (OtherThingsException e) {
//其他綜合異常或是不能處理的異常
e.printStackTrace();
}
}
@Test
public void testFindUser() throws Exception {
User user = new User();
user.setLoginId("20171");
User result = null; //受影響的行數(shù)默認(rèn)為0
try {
result = userService.findUser(user);
} catch (Exception e) {
e.printStackTrace();
System.out.println("查找用戶失敗");
}
if (null!=result)
System.out.println("查找用戶成功\n"+result.toString());
}
}
同樣的厘线,我們的Service的測(cè)試代碼執(zhí)行后识腿,我們可以在mysql中看到具體的數(shù)據(jù)變化
主要參考于大牛Clone丶記憶的SSM集成之路