Java Web项目的层次结构及常见分包
Web项目中的层次
- Controller层:表现层(视图)层。用来显示数据和接收用户数据
- Service层:业务逻辑层,用来处理页面。先写接口,后写实现类
- Dao层:持久层(数据访问层)。用来操作数据库
项目中常见的分包
1.controller包
向用户展现的功能,实现用户交互。
public class UserController {public UserService userService;/*** 用户管理列表页*/@RequestMapping(value="/list",method=RequestMethod.GET)public ModelAndView list(ModelAndView model){model.setViewName("user/user_list");return model;}/*** 获取用户列表*/@RequestMapping(value="/get_list",method=RequestMethod.POST)@ResponseBodypublic Map<String, Object> getList(@RequestParam(value="username",required=false,defaultValue="") String username,Page page){Map<String, Object> ret = new HashMap<String, Object>();Map<String, Object> queryMap = new HashMap<String, Object>();queryMap.put("username", "%"+username+"%");queryMap.put("offset", page.getOffset());queryMap.put("pageSize", page.getRows());ret.put("rows", userService.findList(queryMap));ret.put("total", userService.getTotal(queryMap));return ret;}/*** 编辑用户操作*/@RequestMapping(value="/delete",method=RequestMethod.POST)@ResponseBodypublic Map<String, String> delete(@RequestParam(value="ids[]",required=true) Long[] ids){Map<String, String> ret = new HashMap<String, String>();if(ids == null){ret.put("type", "error");ret.put("msg", "请选择要删除的数据!");return ret;}String idsString = "";for(Long id:ids){idsString += id + ",";}idsString = idsString.substring(0,idsString.length()-1);if(userService.delete(idsString) <= 0){ret.put("type", "error");ret.put("msg", "删除失败!");return ret;}ret.put("type", "success");ret.put("msg", "修改成功!");return ret;}/*** 编辑用户操作*/@RequestMapping(value="/edit",method=RequestMethod.POST)@ResponseBodypublic Map<String, String> edit(User user){Map<String, String> ret = new HashMap<String, String>();if(user == null){ret.put("type", "error");ret.put("msg", "数据出错");return ret;}if(StringUtils.isEmpty(user.getUsername())){ret.put("type", "error");ret.put("msg", "用户名不能为空!");return ret;}if(StringUtils.isEmpty(user.getPassword())){ret.put("type", "error");ret.put("msg", "密码不能为空!");return ret;}User existUser = userService.findByUserName(user.getUsername());if(existUser != null){if(user.getId() != existUser.getId()){ret.put("type", "error");ret.put("msg", "该用户名已经存在!");return ret;}}if(userService.edit(user) <= 0){ret.put("type", "error");ret.put("msg", "修改失败!");return ret;}ret.put("type", "success");ret.put("msg", "修改成功!");return ret;}/*** 添加用户操作*/@RequestMapping(value="/add",method=RequestMethod.POST)@ResponseBodypublic Map<String, String> add(User user){Map<String, String> ret = new HashMap<String, String>();if(user == null){ret.put("type", "error");ret.put("msg", "数据出错");return ret;}if(StringUtils.isEmpty(user.getUsername())){ret.put("type", "error");ret.put("msg", "用户名不能为空!");return ret;}if(StringUtils.isEmpty(user.getPassword())){ret.put("type", "error");ret.put("msg", "密码不能为空!");return ret;}User existUser = userService.findByUserName(user.getUsername());if(existUser != null){ret.put("type", "error");ret.put("msg", "该用户名已经存在!");return ret;}if(userService.add(user) <= 0){ret.put("type", "error");ret.put("msg", "添加失败!");return ret;}ret.put("type", "success");ret.put("msg", "添加成功!");return ret;}}
2.service包
用来处理页面。先写接口,后写实现类(用service.impl包)
- 接口
public interface UserService {public User findByUserName(String username);public int add(User user);public int edit(User user);public int delete(String ids);public List<User> findList(Map<String,Object> queryMap);public int getTotal(Map<String,Object> queryMap);
}
- 实现类
public class UserServiceImpl implements UserService{@Autowiredprivate UserDao userDao;@Overridepublic User findByUserName(String username) {// TODO Auto-generated method stubreturn userDao.findByUserName(username);}@Overridepublic int add(User user) {// TODO Auto-generated method stubreturn userDao.add(user);}@Overridepublic List<User> findList(Map<String,Object> queryMap) {// TODO Auto-generated method stubreturn userDao.findList(queryMap);}@Overridepublic int getTotal(Map<String, Object> queryMap) {// TODO Auto-generated method stubreturn userDao.getTotal(queryMap);}@Overridepublic int edit(User user) {// TODO Auto-generated method stubreturn userDao.edit(user);}@Overridepublic int delete(String ids) {// TODO Auto-generated method stubreturn userDao.delete(ids);}}
3.dao包
操作数据库,实现数据的增删改查
public interface UserDao {public User findByUserName(String username);public int add(User user);public int edit(User user);public int delete(String ids);public List<User> findList(Map<String,Object> queryMap);public int getTotal(Map<String,Object> queryMap);
}
4.entity包
用来编写实体类(模型),entity包中类的数据类型,变量名必须和数据库表的数据类型属性名相对应。
public class User {private Long id;//用户id,主键、自增private String username;//用户名private String password;//密码public Long getId() {return id;}public void setId(Long id) {this.id = id;}public String getUsername() {return username;}public void setUsername(String username) {this.username = username;}public String getPassword() {return password;}public void setPassword(String password) {this.password = password;}}
5.util包
用来存放工具类。
如字符串的处理,日期类的处理
public class StringUtil {/*** 将给定的list按照指定的分隔符分割成字符串返回*/public static String joinString(List<Long> list,String split){String ret = "";for(Long l:list){ret += l + split;}if(!"".equals(ret)){ret = ret.substring(0,ret.length() - split.length());}return ret;}public static String generateSn(String prefix,String suffix){return prefix + new Date().getTime() + suffix;}
}