当前位置:主页 > 软件编程 > JAVA代码 >

基于Java+SSM实现电影院购票系统

时间:2023-01-14 11:32:55 | 栏目:JAVA代码 | 点击:

项目介绍

基于Spring,SpringMVC,Mybatis开发实现。

数据库用的是MySQL5.5。

开发工具用的Eclipse + Tomcat7,JDK1.7-1.8皆可。

效果图展示

首页

电影详情页

购票

登录注册页面

后台管理页面

电影信息列表

添加电影信息

电影票管理

订单管理

热点新闻管理

实现逻辑代码

抽出一些核心代码,如有BUG和问题,欢迎指出。

MovieController

package com.app.controller;
 
import java.util.HashMap;
import java.util.List;
import java.util.Map;
 
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
 
import com.app.bean.Movies;
import com.app.bean.Orders;
import com.app.bean.Ticket;
import com.app.service.MovieService;
import com.app.util.StringUtilsEx;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import static com.app.util.StringUtilsEx.*;
 
@Controller
@RequestMapping("/movie")
public class MovieController {
 
	@Autowired
	private MovieService ms;
	
	@RequestMapping("/listOrder")
	@ResponseBody
	public Map<String,Object> listOrder(Integer page,Integer rows){
		
		Map<String,Object> result = new HashMap<>();
		List<Orders> list = null;
		try {
			
			if(isEmpty(page) || isEmpty(rows)) {
				page = 1;
				rows = 5;
			}
			
			PageHelper.startPage(page, rows);
			list = ms.listOrder();
			result.put("rows", list);
			PageInfo<Orders> pageInfo = new PageInfo<Orders>(list);
			result.put("total", pageInfo.getTotal());
			
		} catch (Exception e) {
			
			e.printStackTrace();
			return null;
		}
		
		return result;
		
	}
	
	//根据电影票ID生成订单
	@RequestMapping("/addOrder")
	@ResponseBody
	public Map<String,Object> addOrder(String id){
		Map<String,Object> returnVo = new HashMap<String, Object>();
		returnVo.put("code", 0);
		
		try{
			ms.addOrder(id);
		}catch(Exception e){
			returnVo.put("code", -1);
		}
		
		return returnVo;
	}
	
	@RequestMapping("/getById")
	@ResponseBody
	public Movies getById(String id){
		return	ms.getById(id);
	}
	
	@RequestMapping("/createTickets")
	@ResponseBody
	public Map<String,Object> createTickets(String id){
		Map<String,Object> returnVo = new HashMap<String, Object>();
		returnVo.put("code", 0);
		
		//先检查这个id的电影是否已经生成了电影票
		List<Ticket> tickets = ms.getTicketsByMovieId(id);
		
		if(tickets != null && !tickets.isEmpty()){
			returnVo.put("code", 2);
			return returnVo;
		}
		
		try{
			ms.createTickets(id);
		}catch(Exception e){
			returnVo.put("code", -1);
		}
		
		return returnVo;
	}
	
	
	@RequestMapping("/update")
	@ResponseBody
	public Map<String,Object> update(String id,String column){
		Map<String,Object> returnVo = new HashMap<String, Object>();
		returnVo.put("code", 0);
		
		try{
			ms.update(id,column);
		}catch(Exception e){
			returnVo.put("code", -1);
		}
		
		return returnVo;
	}
	
	@RequestMapping("/delete")
	@ResponseBody
	public Map<String,Object> delete(String id){
		Map<String,Object> returnVo = new HashMap<String, Object>();
		returnVo.put("code", 0);
		
		try{
			ms.delete(id);
		}catch(Exception e){
			returnVo.put("code", -1);
		}
		
		return returnVo;
	}
	
	@RequestMapping("/add")
	@ResponseBody
	public Map<String,Object> add(Movies movie){
		Map<String,Object> returnVo = new HashMap<String, Object>();
		returnVo.put("code", 0);
		
		try{
			
			if(StringUtilsEx.isNotEmpty(movie.getId())){
				ms.update(movie);
			}else{
				 ms.add(movie);
			}
			
			
			
			
		}catch(Exception e){
			e.printStackTrace();
			returnVo.put("code", -1);
		}
		
		return returnVo;
	}
	
	@RequestMapping("/list")
	@ResponseBody
	public Map<String,Object> list(String moviename,Integer page,Integer rows){
		
		Map<String,Object> result = new HashMap<>();
		List<Movies> list = null;
		try {
			
			if(isEmpty(page) || isEmpty(rows)) {
				page = 1;
				rows = 5;
			}
			
			PageHelper.startPage(page, rows);
			list = ms.list(moviename);
			result.put("rows", list);
			PageInfo<Movies> pageInfo = new PageInfo<Movies>(list);
			result.put("total", pageInfo.getTotal());
			
		} catch (Exception e) {
			
			e.printStackTrace();
			return null;
		}
		
		return result;
		
	}
	
}

NewsController

package com.app.controller;
 
import java.util.HashMap;
import java.util.List;
import java.util.Map;
 
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
 
import com.app.bean.Movies;
import com.app.bean.News;
import com.app.service.MovieService;
import com.app.service.NewsService;
import com.app.util.StringUtilsEx;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import static com.app.util.StringUtilsEx.*;
 
@Controller
@RequestMapping("/news")
public class NewsController {
 
	@Autowired
	private NewsService service;
	
	
	@RequestMapping("/getById")
	@ResponseBody
	public News getById(String id){
		return	service.getById(id);
	}
	
	@RequestMapping("/delete")
	@ResponseBody
	public Map<String,Object> delete(String id){
		Map<String,Object> returnVo = new HashMap<String, Object>();
		returnVo.put("code", 0);
		
		try{
			service.delete(id);
		}catch(Exception e){
			returnVo.put("code", -1);
		}
		
		return returnVo;
	}
	
	@RequestMapping("/add")
	@ResponseBody
	public Map<String,Object> add(News news){
		Map<String,Object> returnVo = new HashMap<String, Object>();
		returnVo.put("code", 0);
		
		try{
			
			if(StringUtilsEx.isNotEmpty(news.getId())){
				service.update(news);
			}else{
				service.add(news);
			}
			
			
			
			
		}catch(Exception e){
			e.printStackTrace();
			returnVo.put("code", -1);
		}
		
		return returnVo;
	}
	
	@RequestMapping("/list")
	@ResponseBody
	public Map<String,Object> list(String newsname,Integer page,Integer rows){
		
		Map<String,Object> result = new HashMap<>();
		List<News> list = null;
		try {
			
			if(isEmpty(page) || isEmpty(rows)) {
				page = 1;
				rows = 5;
			}
			
			PageHelper.startPage(page, rows);
			list = service.list(newsname);
			result.put("rows", list);
			PageInfo<News> pageInfo = new PageInfo<News>(list);
			result.put("total", pageInfo.getTotal());
			
		} catch (Exception e) {
			
			e.printStackTrace();
			return null;
		}
		
		return result;
		
	}
	
}

IndexController

package com.app.controller;
 
import static com.app.util.StringUtilsEx.isEmpty;
 
import java.util.List;
 
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
 
import com.app.bean.Movies;
import com.app.bean.News;
import com.app.bean.Ticket;
import com.app.mapper.NewsMapper;
import com.app.service.MovieService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
 
@Controller
public class IndexController {
 
	@Autowired
	private MovieService ms;
	
	@Autowired
	private NewsMapper newsMapper;
	
	@RequestMapping("front/index")
	public ModelAndView index(String moviename){
		ModelAndView mav = new ModelAndView();
		
		PageHelper.startPage(1, 10);
		List<Movies> list = ms.list(moviename);
		
		//电影
		mav.addObject("movieList",list);
		
		mav.addObject("jrpf",ms.queryList("jrpf"));
		mav.addObject("zsqd",ms.queryList("zsqd"));
		mav.addObject("hprc",ms.queryList("hprc"));
		mav.addObject("newsList",ms.queryList("news"));
		
		mav.setViewName("front/index.jsp");
		return mav;
	}
	
	
	@RequestMapping("front/detail")
	public ModelAndView detail(String id){
		ModelAndView mav = new ModelAndView();
		
		//根据id获取电影的所有信息
		Movies movie = ms.getById(id);
		
		//根据id获取所有的电影票
		List<Ticket> tickets = ms.getTicketsByMovieId(id);
		
		mav.addObject("movie", movie);
		mav.addObject("tickets", tickets);
		
		mav.addObject("jrpf",ms.queryList("jrpf"));
		mav.addObject("zsqd",ms.queryList("zsqd"));
		mav.addObject("hprc",ms.queryList("hprc"));
		mav.addObject("newsList",ms.queryList("news"));
		
		mav.setViewName("front/detail.jsp");
		return mav;
	}
	
	@RequestMapping("front/detailNews")
	public ModelAndView detailNews(String id){
		ModelAndView mav = new ModelAndView();
		
		//根据id获取电影的所有信息
		
		News news = newsMapper.selectByPrimaryKey(Integer.parseInt(id));
		
		mav.addObject("news", news);
		
		mav.addObject("jrpf",ms.queryList("jrpf"));
		mav.addObject("zsqd",ms.queryList("zsqd"));
		mav.addObject("hprc",ms.queryList("hprc"));
		mav.addObject("newsList",ms.queryList("news"));
		
		mav.setViewName("front/detailNews.jsp");
		return mav;
	}
	
	
	@RequestMapping("front/result")
	public ModelAndView result(String moviename,Integer page,Integer rows){
		ModelAndView mav = new ModelAndView();
		
		if(isEmpty(page) || isEmpty(rows)) {
			page = 1;
			rows = 5;
		}
		
		PageHelper.startPage(page, rows);
		
		//根据id获取所有的电影票
		List list = ms.list(moviename);
		mav.addObject("rows",list);
		PageInfo<Movies> pageInfo = new PageInfo<Movies>(list);
		mav.addObject("total",pageInfo.getTotal());
		mav.addObject("page",page);
		
		mav.addObject("jrpf",ms.queryList("jrpf"));
		mav.addObject("zsqd",ms.queryList("zsqd"));
		mav.addObject("hprc",ms.queryList("hprc"));
		mav.addObject("newsList",ms.queryList("news"));
		mav.setViewName("front/result.jsp");
		return mav;
	}
	
	
}

UserController

package com.app.controller;
 
import static com.app.util.StringUtilsEx.isEmpty;
 
import java.util.HashMap;
import java.util.List;
import java.util.Map;
 
import javax.servlet.http.HttpServletRequest;
 
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
 
import com.app.bean.Movies;
import com.app.bean.Users;
import com.app.bean.UsersExample;
import com.app.bean.UsersExample.Criteria;
import com.app.core.ReturnVoCommon;
import com.app.mapper.UsersMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
 
import net.sf.json.JSONObject;
 
@Controller
@RequestMapping("/user")
public class UserController {
	
	@Autowired
	private UsersMapper userMapper;
	
	@RequestMapping("/list")
	@ResponseBody
	public Map<String,Object> list(Integer page,Integer rows){
		
		Map<String,Object> result = new HashMap<>();
		List<Users> list = null;
		try {
			
			if(isEmpty(page) || isEmpty(rows)) {
				page = 1;
				rows = 5;
			}
			
			PageHelper.startPage(page, rows);
			
			list = userMapper.selectByExample(new UsersExample());
			
			result.put("rows", list);
			PageInfo<Users> pageInfo = new PageInfo<Users>(list);
			result.put("total", pageInfo.getTotal());
			
		} catch (Exception e) {
			
			e.printStackTrace();
			return null;
		}
		
		return result;
		
	}
	
	@RequestMapping("/delete")
	@ResponseBody
	public Map<String,Object> delete(String id){
		Map<String,Object> returnVo = new HashMap<String, Object>();
		returnVo.put("code", 0);
		
		try{
			userMapper.deleteByPrimaryKey(Integer.parseInt(id));
		}catch(Exception e){
			returnVo.put("code", -1);
		}
		
		return returnVo;
	}
	
	@RequestMapping("/reg")
	@ResponseBody
	public ReturnVoCommon reg(Users user,HttpServletRequest request){
		
		ReturnVoCommon vo = new ReturnVoCommon();
		
		UsersExample example = new UsersExample();
		Criteria createCriteria = example.createCriteria();
		createCriteria.andNameEqualTo(user.getName());
		List<Users> list = userMapper.selectByExample(example);
		
		if(list.size() == 1){
			vo.setCode(-1);
			vo.setErrMsg("用户名存在!");
			return vo;
		}
		
		user.setAuth("注册用户");
		userMapper.insert(user);
		
		return vo;
	}
	
	
	@RequestMapping("/login")
	@ResponseBody
	public ReturnVoCommon login(Users user,HttpServletRequest request){
		ReturnVoCommon vo = new ReturnVoCommon();
		
		UsersExample example = new UsersExample();
		Criteria createCriteria = example.createCriteria();
		createCriteria.andNameEqualTo(user.getName());
		createCriteria.andPasswordEqualTo(user.getPassword());
		createCriteria.andAuthEqualTo(user.getAuth());
		
		List<Users> list = userMapper.selectByExample(example);
		
		if(list.size() < 1){
			vo.setCode(-1);
			vo.setErrMsg("用户名或者密码错误!");
			return vo;
		}
		
		
		if(user.getAuth().equals("注册用户")){
			vo.setCode(1);
			request.getSession().setAttribute("currentUser", list.get(0));
		}else{
			vo.setCode(2);
			request.getSession().setAttribute("adminUser", list.get(0));
		}
		
		return vo;
	}
 
}
 

您可能感兴趣的文章:

相关文章