SSM简易项目(增删改查)

本项目是一个Spring+SpringMVC+Mybatis+MySQL的一个小项目,项目只有简单的增删改查。

前端用了JQuery和Boostrap(其实也没用啥)。

纯粹是为了练习一下框架整合。

版本:Spring(4.3.6)、SpringMVC(4.3.6)、Mybatis(3.4.2)、MySQL(5.7.0)

虽然界面是食堂管理系统,但是,这都不是关键。。毕竟想改成啥就是啥。

开发环境:Eclipse+Tomcat8.5

目录

 

1、目录结构

2、配置文件

applicationContext.xml

db.properties

log4j.properties

mybatis-config.xml

springmvc-config.xml

3、po

Food.java

4、Dao

FoodDao.java

FoodDao.xml

5、Service

FoodService.java

FoodServiceImpl.java

6、Controller

FoodController.java

7、index.jsp

8、editFood.jsp

9、数据库.sql

10、页面效果

11、源码下载

 https://download.csdn.net/download/litongzero/10570604

 

1、目录结构

SSM简易项目(增删改查)_第1张图片

2、配置文件

applicationContext.xml

Spring核心配置文件


 
    
    
    
         
         
         
         
         
         
         
         
         
         
         
         
         
         
	
	
	
		
		
	
	
	
		
			
			
			
			
			
			
			
			
			
			
		
	
	
	
		
	
	
	
		
		
		
		
	
	
	
		
	
	

	
	
	

db.properties

数据库配置

jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/ssmtest
jdbc.username=root
jdbc.password=123456
jdbc.maxTotal=30
jdbc.maxIdle=10
jdbc.initialSize=5

log4j.properties

log日志文件配置

# Global logging configuration
log4j.rootLogger=ERROR, stdout
# MyBatis logging configuration...
log4j.logger.com.lt.core=DEBUG
# Console output...
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%5p [%t] - %m%n

mybatis-config.xml

mybatis配置文件




	
	
		
	

springmvc-config.xml

SpringMVC配置文件


   
    
    
    
    
    
    
    
    
    	
    	
    	
    	
    
    
		
		
    	
    
   


3、po

Food.java

package cn.lt.core.po;

import java.io.Serializable;

public class Food implements Serializable{
	private static final long serialVersionUID = 1L;
//	主键
	private String id;
//	食品名称
	private String name;
//	价钱
	private String price;
//	备注
	private String msg;
//	时间
	private String date;
	public String getId() {
		return id;
	}
	public void setId(String id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getPrice() {
		return price;
	}
	public void setPrice(String price) {
		this.price = price;
	}
	public String getMsg() {
		return msg;
	}
	public void setMsg(String msg) {
		this.msg = msg;
	}
	public String getDate() {
		return date;
	}
	public void setDate(String date) {
		this.date = date;
	}
	public static long getSerialversionuid() {
		return serialVersionUID;
	}
	@Override
	public String toString() {
		return "Food [id=" + id + ", name=" + name + ", price=" + price
				+ ", msg=" + msg + ", date=" + date + "]";
	}
	
}

4、Dao

FoodDao.java

package cn.lt.core.dao;
import java.util.List;

import org.apache.ibatis.annotations.Param;

import cn.lt.core.po.Food;
/**
 * 用户DAO层接口
 */
public interface FoodDao {
	/**
	 * 通过账号和密码查询用户
	 */
	public int addFood(Food food);
	
	public List findAllFood();

	public void delFood(int id);
	
	public Food findFoodById(int id);
	
	public int updateFood(Food food);

	public Food findFoodByName(@Param("name")String name);
}

FoodDao.xml





    
    
	    insert into food(
	                     id,
	                     name,
	                     price,
	                     msg,
	                     date
	             )
	             values(#{id},
	                    #{name},
	                    #{price},
	                    #{msg},
	                    #{date}
	            )
	
     
    
    	delete from food where 
    		id=#{id}
    
    
      
        update food set 
        name=#{name},
        price=#{price},
        msg=#{msg},
        date=#{date} 
        where id=#{id}  
     
    
    
    
    
    
    

5、Service

FoodService.java

package cn.lt.core.service;
import java.util.List;

import cn.lt.core.po.Food;

public interface FoodService {
	
	public List findAllFood();

	public boolean addFood(Food food);

	public void delFood(int id);
	
	public Food findFoodById(int id);
	
	public boolean updateFood(Food food);

	public Food findFoodByName(String name);
}

FoodServiceImpl.java

package cn.lt.core.service.impl;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import cn.lt.core.dao.FoodDao;
import cn.lt.core.po.Food;
import cn.lt.core.service.FoodService;
/**
 * 用户Service接口实现类
 */
@Service("foodService")
@Transactional
public class FoodServiceImpl implements FoodService {
	@Autowired
	private FoodDao foodDao;

	@Override
	public boolean addFood(Food food) {
		return this.foodDao.addFood(food)>0 ? true:false;
	}


	@Override
	public List findAllFood() {
		return this.foodDao.findAllFood();
	}


	@Override
	public void delFood(int id) {
		this.foodDao.delFood(id);
	}


	@Override
	public Food findFoodById(int id) {
		return this.foodDao.findFoodById(id);
	}


	@Override
	public boolean updateFood(Food food) {
		return this.foodDao.updateFood(food)>0 ? true:false;
	}


	@Override
	public Food findFoodByName(String name) {
		return this.foodDao.findFoodByName(name);
	}
	

}

6、Controller

FoodController.java

package cn.lt.core.web.controller;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import cn.lt.core.po.Food;
import cn.lt.core.service.FoodService;

@Controller
public class FoodController {
	
	
	@Autowired
	private FoodService foodService;
	
	@RequestMapping(value="/index", method=RequestMethod.GET)
	public String index(Model model){
		List foods = null;
		foods = foodService.findAllFood();
		model.addAttribute("foods",foods);
		return "index";
	}
	
	@RequestMapping(value = "/addFood" ,method = RequestMethod.POST)
	public String sendMail(Model model,String name ,String price,String msg,
			 HttpServletRequest request, HttpServletResponse response) {
		int success =0;
		
			Food food = new Food();
			food.setMsg(msg);
			food.setName(name);
			food.setPrice(price);
			Date date=new Date(); 
			SimpleDateFormat df=new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss"); 
			food.setDate(df.format(date));
			if (foodService.addFood(food)) {
				success=1;
			}
			
		try {
			response.getWriter().write("{\"success\":"+success+"}");
			
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return "redirect:index";
	}
	
	@RequestMapping(value="/delete", method=RequestMethod.GET)
	public String del(int id){
		foodService.delFood(id);
		return "redirect:index";
	}
	
	
	
	@RequestMapping(value="/findFoodByName.action", method=RequestMethod.GET)
	public String findFoodByid(String name,Model model){
		Food food=null;
		System.out.println(name);
		List foods = new ArrayList();
		food = foodService.findFoodByName(name);
		if (food!=null) {
			foods.add(food);
		}else {
			foods = foodService.findAllFood();
		}
		System.out.println(foods);
		model.addAttribute("foods",foods);
		return "index";
	}
//	
//	@RequestMapping(value="/findFoodByid", method=RequestMethod.GET)
//	public String editFoodByid(int id,Model model){
//		Food food=null;
//		food = foodService.finFoodById(id);
//		model.addAttribute("food",food);
//		return "editFood";
//	}
	
	@RequestMapping(value="/editFood", method=RequestMethod.GET)
	public String editUser(int param ,String name ,String price,String msg,int id,Model model){
		Food food=new Food();
		try {
			if(param == 0){
				food = foodService.findFoodById(id);
				model.addAttribute("food",food);
				return "editFood";
			}else if(param == 1){
				food.setId(id+"");
				food.setMsg(msg);
				food.setName(name);
				food.setPrice(price);
				Date date=new Date(); 
				SimpleDateFormat df=new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss"); 
				food.setDate(df.format(date));
				System.out.println(food);
				Boolean aBoolean =  foodService.updateFood(food);
				System.out.println(aBoolean);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "redirect:index";
	}
}

7、index.jsp

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>









食堂管理系统








	

食堂管理系统

菜品名称:
编号 菜品名称 价格 备注 时间 操作
${food.id} ${food.name} ${food.price} ${food.msg} ${food.date} 编辑 删除



添加菜品

菜品名称: 价格: 备注:

8、editFood.jsp

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>



  
    
    
    修改菜品信息
	
	
	    
	
	

  
  
  
  	编辑菜品信息
    
ID:
菜品名称:
菜品价格:
菜品备注:

 

9、数据库.sql

# Host: localhost  (Version 5.7.9-log)
# Date: 2018-07-29 11:41:35
# Generator: MySQL-Front 6.0  (Build 2.20)


#
# Structure for table "food"
#

DROP TABLE IF EXISTS `food`;
CREATE TABLE `food` (
  `Id` int(11) NOT NULL AUTO_INCREMENT,
  `name` varchar(255) DEFAULT NULL,
  `price` varchar(255) DEFAULT NULL,
  `msg` varchar(255) DEFAULT NULL,
  `date` varchar(255) DEFAULT NULL,
  PRIMARY KEY (`Id`)
) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8;

#
# Data for table "food"
#

INSERT INTO `food` VALUES (1,'鱼香肉','15元','好吃','2018年07月29日 10:05:41'),(2,'宫保鸡丁','15元','食堂阿姨推荐','2018年06月11日 00:59:31'),(3,'地三鲜','10元','好吃不上火','2018年06月11日 01:02:37');

 

10、页面效果

简单的增删改,查询可以模糊查询。

SSM简易项目(增删改查)_第2张图片

11、源码下载

 https://download.csdn.net/download/litongzero/10570604

你可能感兴趣的:(J2EE)