service层调用dao层的时候,dao使用全局变量,还是局部变量
- package com.xiaozhi.service;
- import com.xiaozhi.dao.UserDao;
- import com.xiaozhi.dao.impl.UserDaoImpl;
- import com.xiaozhi.dao.impl.UserDaoMySqlImple;
- import com.xiaozhi.domain.User;
- import com.xiaozhi.exception.UserExistException;
- import com.xiaozhi.factory.ObjectFactory;
- import com.xiaozhi.utils.ServiceUtil;
- public class BusinessService
- {
- UserDao dao=ObjectFactory.getInstance().getUserDaoImpl();
- public void register(User user) throws UserExistException{
- if(!dao.find(user.getUsername())){
- // user.setId(ServiceUtils.generateID());//xml用来生成id
- user.setPassword(ServiceUtil.md5(user.getPassword()));
- dao.add(user);
- }else{
- throw new UserExistException();
- }
- }
-
- public User login(String username,String password){
- password=ServiceUtil.md5(password);
- User user=dao.find(username, password);
- return user;
- }
- }
复制代码 |
|