电子商务型网站建设免费自建网站
news/
2025/10/4 19:13:12/
文章来源:
电子商务型网站建设,免费自建网站,怎么改网站上的logo,wordpress短链Java Web项目的层次结构及常见分包
Web项目中的层次 ControllerServiceDaoController层#xff1a;表现层#xff08;视图#xff09;层。用来显示数据和接收用户数据Service层#xff1a;业务逻辑层#xff0c;用来处理页面。先写接口#xff0c;后写实现类Dao层#…Java Web项目的层次结构及常见分包
Web项目中的层次
ControllerServiceDaoController层表现层视图层。用来显示数据和接收用户数据Service层业务逻辑层用来处理页面。先写接口后写实现类Dao层持久层数据访问层。用来操作数据库
项目中常见的分包
1.controller包 向用户展现的功能实现用户交互。
public class UserController {public UserService userService;/*** 用户管理列表页*/RequestMapping(value/list,methodRequestMethod.GET)public ModelAndView list(ModelAndView model){model.setViewName(user/user_list);return model;}/*** 获取用户列表*/RequestMapping(value/get_list,methodRequestMethod.POST)ResponseBodypublic MapString, Object getList(RequestParam(valueusername,requiredfalse,defaultValue) String username,Page page){MapString, Object ret new HashMapString, Object();MapString, Object queryMap new HashMapString, 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,methodRequestMethod.POST)ResponseBodypublic MapString, String delete(RequestParam(valueids[],requiredtrue) Long[] ids){MapString, String ret new HashMapString, 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,methodRequestMethod.POST)ResponseBodypublic MapString, String edit(User user){MapString, String ret new HashMapString, 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,methodRequestMethod.POST)ResponseBodypublic MapString, String add(User user){MapString, String ret new HashMapString, 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 ListUser findList(MapString,Object queryMap);public int getTotal(MapString,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 ListUser findList(MapString,Object queryMap) {// TODO Auto-generated method stubreturn userDao.findList(queryMap);}Overridepublic int getTotal(MapString, 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 ListUser findList(MapString,Object queryMap);public int getTotal(MapString,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(ListLong 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;}
}
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/927460.shtml
如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!