SSM框架整合步骤
1:搭建一个web工程,将所需的jar包放进去
2,创建数据库,创建表
插入2条初始数据进去
INSERT INTO product VALUE(NULL,"手机",899,2);
INSERT INTO product VALUE(NULL,"电视",1299,5);
有了数据之后,将实体类创建出来
package com.jgl.model;
public class Product {
private int id; //商品编号
private String pName; //商品名称
private double pPrice; //商品价格
private int pNumber; //商品数量
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getpName() {
return pName;
}
public void setpName(String pName) {
this.pName = pName;
}
public double getpPrice() {
return pPrice;
}
public void setpPrice(double pPrice) {
this.pPrice = pPrice;
}
public int getpNumber() {
return pNumber;
}
public void setpNumber(int pNumber) {
this.pNumber = pNumber;
}
public Product() {
super();
// TODO Auto-generated constructor stub
}
public Product(int id, String pName, double pPrice, int pNumber) {
super();
this.id = id;
this.pName = pName;
this.pPrice = pPrice;
this.pNumber = pNumber;
}
@Override
public String toString() {
return "User [id=" + id + ", pName=" + pName + ", pPrice=" + pPrice + ", pNumber=" + pNumber + "]";
}
}
我习惯从前端往后端的顺序开发:
3,前端部分
3.1 先画首页index.jsp,放在WebContend下面
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
首页
进入商品展示页面
3.2 在WEB-INF下面创建一个文件夹jsp,并在里面画3个页面
allProduct.jsp代码如下所示:
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
商品列表
添加商品
商品管理
商品名称
商品价格
商品数量
操作
${product.pName }
¥${product.pPrice}
${product.pNumber}
编辑
删除
addProduct.jsp代码如下所示:
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
添加商品
添加商品
editProduct.jsp代码如下所示:
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
编辑商品
编辑商品
4 后端部分
4.1,表现层开发:创建一个包package com.jgl.controller,创建一个ProductController
package com.jgl.controller;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
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 com.jgl.model.Product;
import com.jgl.service.ProductService;
@Controller
@RequestMapping("/product")
public class ProductController {
@Autowired
private ProductService productService;
/**
* 获取所有商品列表
* @param request
* @return
*/
@RequestMapping("/getAllProduct")
public String getAllProduct(HttpServletRequest request,Model model){
List product = productService.findAll();
model.addAttribute("productList", product);
request.setAttribute("productList", product);
return "/allProduct";
}
/**
* 跳转到添加商品界面
* @param request
* @return
*/
@RequestMapping("/toAddProduct")
public String toAddProduct(){
return "/addProduct";
}
/**
* 添加商品并重定向到展示所有商品页面
* @param product
* @param request
* @return
*/
@RequestMapping("/addProduct")
public String addProduct(Product product,Model model){
productService.save(product);
return "redirect:/product/getAllProduct";
}
/**
*编辑商品
* @param product
* @param request
* @return
*/
@RequestMapping("/updateProduct")
public String updateProduct(Product product,HttpServletRequest request,Model model){
if(productService.update(product)){
product = productService.findById(product.getId());
request.setAttribute("product", product);
model.addAttribute("product", product);
return "redirect:/product/getAllProduct";
}else{
return "/error";
}
}
/**
* 根据id查询单个商品
* @param id
* @param request
* @return
*/
@RequestMapping("/getProduct")
public String getProduct(int id,HttpServletRequest request,Model model){
request.setAttribute("product", productService.findById(id));
model.addAttribute("product", productService.findById(id));
return "/editProduct";
}
/**
* 删除商品
* @param id
* @param request
* @param response
*/
@RequestMapping("/delProduct")
public String deleteProduct(int id,HttpServletRequest request,HttpServletResponse response){
productService.delete(id);
return "redirect:/product/getAllProduct";
}
}
4.2 业务层开发 :先来2个包com.jgl.service; com.jgl.service.impl;
创建一个ProductService接口,ProductServiceImpl实现类
ProductService.java代码如下
package com.jgl.service;
import java.util.List;
import com.jgl.model.Product;
public interface ProductService {
void save(Product product);
boolean update(Product product);
boolean delete(int id);
Product findById(int id);
List findAll();
}
ProductServiceImpl.java代码如下
package com.jgl.service.impl;
import java.util.List;
import javax.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.jgl.mapper.ProductMapper;
import com.jgl.model.Product;
import com.jgl.service.ProductService;
@Service
@Transactional
public class ProductServiceImpl implements ProductService{
@Resource
private ProductMapper mapper;
/**
* 根据 id 删除 数据
*/
public boolean delete(int id) {
return mapper.delete(id);
}
/**
* 查询User的全部数据
*/
public List findAll() {
List findAllList = mapper.findAll();
return findAllList;
}
/**
* 根据 id 查询 对应数据
*/
public Product findById(int id) {
Product user = mapper.findById(id);
return user;
}
/**
* 新增数据
*/
public void save(Product user) {
mapper.save(user);
}
/**
* 根据 id 修改对应数据
*/
public boolean update(Product user) {
return mapper.update(user);
}
}
4.3 数据访问层开发 先来个包com.jgl.mapper;
创建ProductMapper.java,并在同一个包中创建ProductMapper.xml
ProductMapper.java代码如下:
package com.jgl.mapper;
import java.util.List;
import com.jgl.model.Product;
public interface ProductMapper {
void save(Product product);
boolean update(Product product);
boolean delete(int id);
Product findById(int id);
List findAll();
}
ProductMapper.xml代码如下:
insert into product(p_name,p_price,p_number) values(#{pName},#{pPrice},#{pNumber})
update product set p_name=#{pName},p_price=#{pPrice},p_number=#{pNumber} where p_id=#{id}
delete from product where p_id=#{id}
5.所需的配置文件
log4j.properties代码如下:
### set log levels ###
log4j.rootLogger = info , Console , D
#Console
log4j.appender.Console=org.apache.log4j.ConsoleAppender
log4j.appender.Console.layout=org.apache.log4j.PatternLayout
log4j.appender.Console.layout.ConversionPattern=%d [%t] %-5p [%c] - %m%n
log4j.logger.java.sql.ResultSet=INFO
log4j.logger.org.apache=INFO
log4j.logger.java.sql.Connection=INFO
log4j.logger.java.sql.Statement=INFO
log4j.logger.java.sql.PreparedStatement=INFO
#output2file
log4j.appender.D = org.apache.log4j.DailyRollingFileAppender
log4j.appender.D.File = D\:/logs/log.log
log4j.appender.D.Append = true
log4j.appender.D.Threshold = INFO \#\# \u00E8\u00BE\u0093\u00E5\u0087\u00BAinfo\u00E7\u00BA\u00A7\u00E5\u0088\u00AB\u00E4\u00BB\u00A5\u00E4\u00B8\u008A\u00E7\u009A\u0084\u00E6\u0097\u00A5\u00E5\u00BF\u0097
log4j.appender.D.layout = org.apache.log4j.PatternLayout
log4j.appender.D.layout.ConversionPattern = %-d{yyyy-MM-dd HH\:mm\:ss} [ %t\:%r ] - [ %p ] %m%n
mybatis-config.xml中代码如下:
spring-common.xml中代码如下:
spring-mvc.xml中代码如下:
效果如下: