SpringBoot实现多数据源的切换实践
389
2022-08-03
基于Java+SSM实现电影院购票系统(ssm电影售票系统)
目录项目介绍效果图展示实现逻辑代码MovieControllerNewsControllerIndexControllerUserController
项目介绍
基于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
Map
List
try {
if(isEmpty(page) || isEmpty(rows)) {
page = 1;
rows = 5;
}
PageHelper.startPage(page, rows);
list = ms.listOrder();
result.put("rows", list);
PageInfo
result.put("total", pageInfo.getTotal());
} catch (Exception e) {
e.printStackTrace();
return null;
}
return result;
}
//根据电影票ID生成订单
@RequestMapping("/addOrder")
@ResponseBody
public Map
Map
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
Map
returnVo.put("code", 0);
//先检查这个id的电影是否已经生成了电影票
List
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
Map
returnVo.put("code", 0);
try{
ms.update(id,column);
}catch(Exception e){
returnVo.put("code", -1);
}
return returnVo;
}
@RequestMapping("/delete")
@ResponseBody
public Map
Map
returnVo.put("code", 0);
try{
ms.delete(id);
}catch(Exception e){
returnVo.put("code", -1);
}
return returnVo;
}
@RequestMapping("/add")
@ResponseBody
public Map
Map
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
Map
List
try {
if(isEmpty(page) || isEmpty(rows)) {
page = 1;
rows = 5;
}
PageHelper.startPage(page, rows);
list = ms.list(moviename);
result.put("rows", list);
PageInfo
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
Map
returnVo.put("code", 0);
try{
service.delete(id);
}catch(Exception e){
returnVo.put("code", -1);
}
return returnVo;
}
@RequestMapping("/add")
@ResponseBody
public Map
Map
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
Map
List
try {
if(isEmpty(page) || isEmpty(rows)) {
page = 1;
rows = 5;
}
PageHelper.startPage(page, rows);
list = service.list(newsname);
result.put("rows", list);
PageInfo
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.springfrhttp://amework.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
//电影
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
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
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
Map
List
try {
if(isEmpty(page) || isEmpty(rows)) {
page = 1;
rows = 5;
}
PageHelper.startPage(page, rows);
list = userMapper.selectByExample(new UsersExample());
result.put("rows", list);
PageInfo
result.put("total", pageInfo.getTotal());
} catch (Exception e) {
e.printStackTrace();
return null;
}
return result;
}
@RequestMapping("/delete")
@ResponseBody
public Map
Map
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
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
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;
}
}
版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。
发表评论
暂时没有评论,来抢沙发吧~