源码获取:博客首页 "资源" 里下载!
超市订单管理系统。主要功能包括订单管理、供应商管理、用户管理等功能;
1.运行环境:最好是java jdk 1.8,我们在这个平台上运行的。其他版本理论上也可以。
2.IDE环境:IDEA,Eclipse,Myeclipse都可以。推荐IDEA;
3.tomcat环境:Tomcat 7.x,8.x,9.x版本均可
4.硬件环境:windows 7/8/10 1G内存以上;或者 Mac OS;
5.是否Maven项目: 否;查看源码目录中是否包含pom.xml;若包含,则为maven项目,否则为非maven项目
6.数据库:MySql 5.7版本;
1. 后端:Spring SpringMVC MyBatis
2. 前端:JSP+bootstrap+jQuery
1. 使用Navicat或者其它工具,在mysql中创建对应名称的数据库,并导入项目的sql文件;
2. 将项目中applicationContext-jdbc.xml配置文件中的数据库配置改为自己的配置
3. 使用IDEA/Eclipse/MyEclipse导入项目,Eclipse/MyEclipse导入时,若为maven项目请选择maven;
若为maven项目,导入成功后请执行maven clean;maven install命令,配置tomcat,然后运行;
4. 运行项目,输入localhost:8080/ssm8/login 登录;其中ss8为项目名称,可自定义,但后面路径必须为/login;
@Controller
public class LoginServlet{
@Autowired
UserServiceImpl userService;
@Autowired
RoleServiceImpl roleService;
@Autowired
ProviderServiceImpl providerService;
@Autowired
BillServiceImpl billService;
@RequestMapping(value="/login.do",method = {RequestMethod.POST, RequestMethod.GET})
public String doPost(HttpServletRequest request, HttpServletResponse response, Model model){
System.out.println("login ============ " );
//获取用户名和密码
String userCode = request.getParameter("userCode");
String userPassword = request.getParameter("userPassword");
System.out.println(userCode+" "+userPassword);
//调用service方法,进行用户匹配
User user = userService.login(userCode,userPassword);
System.out.println(user);
if(null != user){//登录成功
//放入session
request.getSession().setAttribute(Constants.USER_SESSION, user);
//页面跳转(frame.jsp)
return "frame";
}else{
//页面跳转(login.jsp)带出提示信息--转发
model.addAttribute("error", "用户名或密码不正确");
return "login";
//.forward(request, response);
}
}
}
@Controller
public class UserServlet{
@Autowired
BillServiceImpl billService;
@Autowired
ProviderServiceImpl providerService;
@Autowired
UserServiceImpl userService;
@Autowired
RoleServiceImpl roleService;
@RequestMapping(value="/user.do",method = {RequestMethod.POST, RequestMethod.GET})
public String doPost(HttpSession session, @Valid User user, BindingResult bindingResult, HttpServletRequest request, HttpServletResponse response, @RequestParam(value="method",required = false) String method, Model model)
throws ServletException, IOException {
System.out.println("--------------->"+method);
if(method != null && method.equals("add")){
return this.add(request, response,user,bindingResult,session);
}else if(method != null && method.equals("query")){
return this.query(request, response);
}else if(method != null && method.equals("getrolelist")){
this.getRoleList(request, response);
}else if(method != null && method.equals("ucexist")){
this.userCodeExist(request, response);
}else if(method != null && method.equals("deluser")){
this.delUser(request, response);
}else if(method != null && method.equals("view")){
return this.getUserById(request, response,"userview",model);
}else if(method != null && method.equals("modify")){
return this.getUserById(request, response,"usermodify",model);
}else if(method != null && method.equals("modifyexe")){
return this.modify(request, response);
}else if(method != null && method.equals("pwdmodify")){
this.getPwdByUserId(request, response);
}else if(method != null && method.equals("savepwd")){
return this.updatePwd(request, response);
}
return "error";
}
private String updatePwd(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
Object o = request.getSession().getAttribute(Constants.USER_SESSION);
String newpassword = request.getParameter("newpassword");
boolean flag = false;
if(o != null && !StringUtils.isNullOrEmpty(newpassword)){
flag = userService.updatePwd(((User)o).getId(),newpassword);
if(flag){
request.setAttribute(Constants.SYS_MESSAGE, "修改密码成功,请退出并使用新密码重新登录!");
request.getSession().removeAttribute(Constants.USER_SESSION);//session注销
}else{
request.setAttribute(Constants.SYS_MESSAGE, "修改密码失败!");
}
}else{
request.setAttribute(Constants.SYS_MESSAGE, "修改密码失败!");
}
return "pwdmodify";
}
private void getPwdByUserId(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
Object o = request.getSession().getAttribute(Constants.USER_SESSION);
String oldpassword = request.getParameter("oldpassword");
Map resultMap = new HashMap();
if(null == o ){//session过期
resultMap.put("result", "sessionerror");
}else if(StringUtils.isNullOrEmpty(oldpassword)){//旧密码输入为空
resultMap.put("result", "error");
}else{
String sessionPwd = ((User)o).getUserPassword();
if(oldpassword.equals(sessionPwd)){
resultMap.put("result", "true");
}else{//旧密码输入不正确
resultMap.put("result", "false");
}
}
// response.setContentType("application/json");
request.setCharacterEncoding("utf-8");
response.setContentType("text/html;charset=utf-8");
PrintWriter outPrintWriter = response.getWriter();
outPrintWriter.write(JSONArray.toJSONString(resultMap));
outPrintWriter.flush();
outPrintWriter.close();
}
private String modify(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String id = request.getParameter("uid");
String userName = request.getParameter("userName");
String gender = request.getParameter("gender");
String birthday = request.getParameter("birthday");
String phone = request.getParameter("phone");
String address = request.getParameter("address");
String userRole = request.getParameter("userRole");
User user = new User();
user.setId(Integer.valueOf(id));
user.setUserName(userName);
user.setGender(Integer.valueOf(gender));
try {
user.setBirthday(new SimpleDateFormat("yyyy-MM-dd").parse(birthday));
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
user.setPhone(phone);
user.setAddress(address);
user.setUserRole(Integer.valueOf(userRole));
user.setModifyBy(((User)request.getSession().getAttribute(Constants.USER_SESSION)).getId());
user.setModifyDate(new Date());
if(userService.modify(user)){
return "redirect:/user.do?method=query";
// response.sendRedirect(request.getContextPath()+"/jsp/user.do?method=query");
}else{
return "usermodify";
// request.getRequestDispatcher("usermodify.jsp").forward(request, response);
}
}
private String getUserById(HttpServletRequest request, HttpServletResponse response, String url, Model model)
throws ServletException, IOException {
String id = request.getParameter("uid");
if(!StringUtils.isNullOrEmpty(id)){
//调用后台方法得到user对象
User user = userService.getUserById(id);
System.out.println("------------> "+user);
model.addAttribute("user", user);
return url;
// request.getRequestDispatcher(url).forward(request, response);
}
return "error";
}
private void delUser(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String id = request.getParameter("uid");
Integer delId = 0;
try{
delId = Integer.parseInt(id);
}catch (Exception e) {
// TODO: handle exception
delId = 0;
}
HashMap resultMap = new HashMap();
if(delId <= 0){
resultMap.put("delResult", "notexist");
}else{
if(userService.deleteUserById(delId)){
resultMap.put("delResult", "true");
}else{
resultMap.put("delResult", "false");
}
}
//把resultMap转换成json对象输出
// response.setContentType("application/json");
request.setCharacterEncoding("utf-8");
response.setContentType("text/html;charset=utf-8");
PrintWriter outPrintWriter = response.getWriter();
outPrintWriter.write(JSONArray.toJSONString(resultMap));
outPrintWriter.flush();
outPrintWriter.close();
}
private void userCodeExist(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
//判断用户账号是否可用
String userCode = request.getParameter("userCode");
HashMap resultMap = new HashMap();
if(StringUtils.isNullOrEmpty(userCode)){
//userCode == null || userCode.equals("")
resultMap.put("userCode", "exist");
}else{
User user = userService.selectUserCodeExist(userCode);
if(null != user){
resultMap.put("userCode","exist");
}else{
resultMap.put("userCode", "notexist");
}
}
//把resultMap转为json字符串以json的形式输出
//配置上下文的输出类型
// response.setContentType("application/json");
request.setCharacterEncoding("utf-8");
response.setContentType("text/html;charset=utf-8");
//从response对象中获取往外输出的writer对象
PrintWriter outPrintWriter = response.getWriter();
//把resultMap转为json字符串 输出
outPrintWriter.write(JSONArray.toJSONString(resultMap));
outPrintWriter.flush();//刷新
outPrintWriter.close();//关闭流
}
private void getRoleList(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
List roleList = null;
roleList = roleService.getRoleList();
//把roleList转换成json对象输出
// response.setContentType("application/json");
request.setCharacterEncoding("utf-8");
response.setContentType("text/html;charset=utf-8");
PrintWriter outPrintWriter = response.getWriter();
outPrintWriter.write(JSONArray.toJSONString(roleList));
outPrintWriter.flush();
outPrintWriter.close();
}
private String query(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
//查询用户列表
String queryUserName = request.getParameter("queryname");
String temp = request.getParameter("queryUserRole");
String pageIndex = request.getParameter("pageIndex");
int queryUserRole = 0;
List userList = null;
//设置页面容量
int pageSize = Constants.pageSize;
//当前页码
int currentPageNo = 1;
System.out.println("queryUserName servlet--------"+queryUserName);
System.out.println("queryUserRole servlet--------"+queryUserRole);
System.out.println("query pageIndex--------- > " + pageIndex);
if(queryUserName == null){
queryUserName = "";
}
if(temp != null && !temp.equals("")){
queryUserRole = Integer.parseInt(temp);
}
if(pageIndex != null){
try{
currentPageNo = Integer.valueOf(pageIndex);
}catch(NumberFormatException e){
return "error";
}
}
//总数量(表)
int totalCount = userService.getUserCount(queryUserName,queryUserRole);
//总页数
PageSupport pages=new PageSupport();
pages.setCurrentPageNo(currentPageNo);
pages.setPageSize(pageSize);
pages.setTotalCount(totalCount);
int totalPageCount = pages.getTotalPageCount();
//控制首页和尾页
if(currentPageNo < 1){
currentPageNo = 1;
}else if(currentPageNo > totalPageCount){
currentPageNo = totalPageCount;
}
userList = userService.getUserList(queryUserName,queryUserRole,currentPageNo, pageSize);
System.out.println("----------> "+userList.get(0).getAge());
request.setAttribute("userList", userList);
List roleList = null;
System.out.println(userList.get(0).getRole().getRoleName());
roleList = roleService.getRoleList();
System.out.println("===== "+roleList.get(0).getRoleCode()+"--------roleList:"+roleList.get(0).getRoleName());
request.setAttribute("roleList", roleList);
request.setAttribute("queryUserName", queryUserName);
request.setAttribute("queryUserRole", queryUserRole);
request.setAttribute("totalPageCount", totalPageCount);
request.setAttribute("totalCount", totalCount);
request.setAttribute("currentPageNo", currentPageNo);
return "userlist";
}
private String add(HttpServletRequest request, HttpServletResponse response, User user, BindingResult bindingResult, HttpSession session)
throws ServletException, IOException {
if(bindingResult.hasErrors()){
System.out.println("输入错误");
return "useradd";
}System.out.println("111");
user.setCreatedBy(((User)session.getAttribute(Constants.USER_SESSION)).getId());System.out.println("222");
user.setCreationDate(new Date());System.out.println("333");
if(userService.add(user)){return "redirect:/user.do?method=query";}System.out.println("444");
return "useradd";
}
}
@Controller
public class ProviderServlet {
@Autowired
BillServiceImpl billService;
@Autowired
ProviderServiceImpl providerService;
@Autowired
UserServiceImpl userService;
@RequestMapping(value="/provider.do",method = {RequestMethod.POST, RequestMethod.GET})
public String doPost(HttpServletRequest request, HttpServletResponse response, @RequestParam(value="method",required = false) String method)
throws ServletException, IOException {
if(method != null && method.equals("query")){
return this.query(request,response);
}else if(method != null && method.equals("add")){
return this.add(request,response);
}else if(method != null && method.equals("view")){
return this.getProviderById(request,response,"providerview");
}else if(method != null && method.equals("modify")){
return this.getProviderById(request,response,"providermodify");
}else if(method != null && method.equals("modifysave")){
return this.modify(request,response);
}else if(method != null && method.equals("delprovider")){
this.delProvider(request,response);
}
return "error";
}
private void delProvider(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String id = request.getParameter("proid");
HashMap resultMap = new HashMap();
if(!StringUtils.isNullOrEmpty(id)){
int flag = providerService.deleteProviderById(id);
if(flag == 0){//删除成功
resultMap.put("delResult", "true");
}else if(flag == -1){//删除失败
resultMap.put("delResult", "false");
}else if(flag > 0){//该供应商下有订单,不能删除,返回订单数
resultMap.put("delResult", String.valueOf(flag));
}
}else{
resultMap.put("delResult", "notexit");
}
//把resultMap转换成json对象输出
// response.setContentType("application/json");
request.setCharacterEncoding("utf-8");
response.setContentType("text/html;charset=utf-8");
PrintWriter outPrintWriter = response.getWriter();
outPrintWriter.write(JSONArray.toJSONString(resultMap));
outPrintWriter.flush();
outPrintWriter.close();
}
private String modify(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String proContact = request.getParameter("proContact");
String proPhone = request.getParameter("proPhone");
String proAddress = request.getParameter("proAddress");
String proFax = request.getParameter("proFax");
String proDesc = request.getParameter("proDesc");
String id = request.getParameter("id");
Provider provider = new Provider();
provider.setId(Integer.valueOf(id));
provider.setProContact(proContact);
provider.setProPhone(proPhone);
provider.setProFax(proFax);
provider.setProAddress(proAddress);
provider.setProDesc(proDesc);
provider.setModifyBy(((User)request.getSession().getAttribute(Constants.USER_SESSION)).getId());
provider.setModifyDate(new Date());
boolean flag = false;
flag = providerService.modify(provider);
System.out.println("--------flag="+flag);
if(flag){
return "redirect:/provider.do?method=query";
// response.sendRedirect(request.getContextPath()+"/jsp/provider.do?method=query");
}else{
return "providermodify";
// request.getRequestDispatcher("providermodify.jsp").forward(request, response);
}
}
private String getProviderById(HttpServletRequest request, HttpServletResponse response, String url)
throws ServletException, IOException {
String id = request.getParameter("proid");
System.out.println("-------------"+id);
if(!StringUtils.isNullOrEmpty(id)){
Provider provider = null;
provider = providerService.getProviderById(id);
request.setAttribute("provider", provider);
System.out.println("-------------"+id);
return url;
// request.getRequestDispatcher(url).forward(request, response);
}
return "error";
}
private String add(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String proCode = request.getParameter("proCode");
String proName = request.getParameter("proName");
String proContact = request.getParameter("proContact");
String proPhone = request.getParameter("proPhone");
String proAddress = request.getParameter("proAddress");
String proFax = request.getParameter("proFax");
String proDesc = request.getParameter("proDesc");
Provider provider = new Provider();
provider.setProCode(proCode);
provider.setProName(proName);
provider.setProContact(proContact);
provider.setProPhone(proPhone);
provider.setProFax(proFax);
provider.setProAddress(proAddress);
provider.setProDesc(proDesc);
provider.setCreatedBy(((User)request.getSession().getAttribute(Constants.USER_SESSION)).getId());
provider.setCreationDate(new Date());
boolean flag = false;
flag = providerService.add(provider);
if(flag){
return "redirect:/provider.do?method=query";
// response.sendRedirect(request.getContextPath()+"/jsp/provider.do?method=query");
}else{
return "provideradd";
// request.getRequestDispatcher("provideradd.jsp").forward(request, response);
}
}
private String query(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String queryProName = request.getParameter("queryProName");
String queryProCode = request.getParameter("queryProCode");
if(StringUtils.isNullOrEmpty(queryProName)){
queryProName = "";
}
if(StringUtils.isNullOrEmpty(queryProCode)){
queryProCode = "";
}
List providerList = new ArrayList();
providerList = providerService.getProviderList(queryProName,queryProCode);
request.setAttribute("providerList", providerList);
request.setAttribute("queryProName", queryProName);
request.setAttribute("queryProCode", queryProCode);
return "providerlist";
// request.getRequestDispatcher("providerlist.jsp").forward(request, response);
}
}
源码获取:博客首页 "资源" 里下载!