日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > c/c++ >内容正文

c/c++

spring+springMVC+mybatis的整合 part5

發(fā)布時間:2023/12/31 c/c++ 27 豆豆
生活随笔 收集整理的這篇文章主要介紹了 spring+springMVC+mybatis的整合 part5 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

UserService實現(xiàn)(注意編程思維的養(yǎng)成)

根據面向接口編程的思維來講,在Service中核心是實現(xiàn)Dao層,并調用Dao層。
UserDao層通過單元測試了,現(xiàn)在中心就應該放在業(yè)務邏輯的實現(xiàn),畢竟數據持久化已經實現(xiàn)了。
從服務端程序的角度看來,用戶的主要業(yè)務有注冊、登錄、注銷登錄、注銷帳號等等,這里我們先拿注冊來說事。

用戶注冊流程分析(用戶角度):
填寫帳號相關信息
提交注冊信息
服務器返回是否注冊成功

用戶注冊流程分析(服務器角度):
收到用戶注冊請求
解包數據→封裝到UserBean解包數據失敗(請求信息異常),返回錯誤提示信息
針對具體的用戶信息檢查是否符合標準不符合檢查標準,返回對應的錯誤提示
通過檢查,調用Dao檢查是否存在同樣的用戶數據庫已經存在相同的用戶信息,不能重復添加,返回錯誤提示信息
不存在同樣的用戶,添加新用戶,并返回成功的提示信息

流程圖反映如下:


ssm框架用戶行為解析流程圖

自定義異常
新建一個與dao,domain同級的包exception來存放自定義的異常
所有的自定義異常都要繼承于Exception
UserAireadyExistException 用戶已經存在異常

public UserAireadyExistException(String s) {super(s);}public UserAireadyExistException(String message, Throwable cause) {super(message, cause);}

下面的UserCanNotBeNullException (用戶為空異常),UserNameCanNotBeNullException(用戶名為空異常),UserPwdCanNotBeNullException(用戶密碼為空)異常代碼相類似

還有個其他異常

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);} }

可能會拋出的異常寫完了,下面來實現(xiàn)用戶注冊的Service:
首先還是先寫個BaseService接口,用泛型解耦

public interface BaseService<T> {void add(T t) throws Exception; }

用戶注冊接口
不同對象的業(yè)務體系不同,BaseService并不能完全替代不同對象的具體行為表現(xiàn)
UserService

public interface UserService extends BaseService<User>{void add(User user) throws Exception;User findUser(User user) throws Exception; }
實例化用戶注冊接口UserServiceImpl

在service包下創(chuàng)建一個子包,叫serviceImpl,用來存放業(yè)務層接口實現(xiàn)類
UserServiceImpl

@Service("userService") public class UserServiceImpl implements UserService{@Autowiredprivate 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");}//由于我這個是管理系統(tǒng),根據業(yè)務需求來說,我們的用戶基本信息都是不能為空的//基本信息包括:姓名、年齡、用戶名、密碼、性別、手機號,年齡大于18if ( StringUtils.isEmpty(user.getSex())|| user.getAge() < 18|| StringUtils.isEmpty(user.getCellNumber())) {//其他綜合異常throw new OtherThingsException("Some use's base info can not be null");}//已經存在相同用戶if (null != userDao.findOneById(user.getLoginId())) {//存在相同的用戶異常throw new UserAireadyExistException("Register User Failed,Because the user Aiready exist");}int result = 0; //受影響的行數默認為0try {result = userDao.add(user);} catch (Exception e) {System.out.println("添加用戶失敗,用戶已經存在");//其他用戶添加失敗異常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這個JAVA的工具類

http://www.jianshu.com/p/2eb9e42ecf44
寫完每個Service后,都需要針對具體的對象的行為進行單元測試,UserServiceTest代碼如下:
同樣,要繼承基礎測試類

public class UserServiceTest extends BaseTest{@Autowiredprivate UserServiceImpl userService;//此處直接使用UserService的實現(xiàn)類,主要是方便拋出異常,然后異常出現(xiàn)時候可以針對性的處理@Testpublic void testAdd() {User user = new User();user.setLoginId("20171");user.setName("意識流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();}}@Testpublic void testFindUser() throws Exception {User user = new User();user.setLoginId("20171");User result = null; //受影響的行數默認為0try {result = userService.findUser(user);} catch (Exception e) {e.printStackTrace();System.out.println("查找用戶失敗");}if (null!=result)System.out.println("查找用戶成功\n"+result.toString());} }

同樣的,我們的Service的測試代碼執(zhí)行后,我們可以在mysql中看到具體的數據變化

主要參考于大牛Clone丶記憶的SSM集成之路

總結

以上是生活随笔為你收集整理的spring+springMVC+mybatis的整合 part5的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。