基于Spring-SpringMVC-Mybatis框架的Bootstrap3样式表格的数据增删改查
00 ssmPaging整体项目结构
该项目基于Spring-SpringMVC-Mybatis框架和Bootstrap3样式表格,实现了数据增删改查和分页,作为基础构件可以用于学习以及构建更为复杂的web项目。
01 数据集
需要导入数据集tb_item.sql,建议使用Navicat导入。
02 POJO对象
Item.java文件
package com.xuyafei.pojo;
import java.util.Date;
public class Item {
private Long id;
private String title;
private String sellPoint;
private Long price;
private Integer num;
private String barcode;
private String image;
private Long cid;
private Byte status;
private Date created;
private Date updated;
@Override
public String toString() {
return "Item [id=" + id + ", title=" + title + ", sellPoint=" + sellPoint + ", price=" + price + ", num=" + num
+ ", barcode=" + barcode + ", image=" + image + ", cid=" + cid + ", status=" + status + ", created="
+ created + ", updated=" + updated + "]";
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title == null ? null : title.trim();
}
public String getSellPoint() {
return sellPoint;
}
public void setSellPoint(String sellPoint) {
this.sellPoint = sellPoint == null ? null : sellPoint.trim();
}
public Long getPrice() {
return price;
}
public void setPrice(Long price) {
this.price = price;
}
public Integer getNum() {
return num;
}
public void setNum(Integer num) {
this.num = num;
}
public String getBarcode() {
return barcode;
}
public void setBarcode(String barcode) {
this.barcode = barcode == null ? null : barcode.trim();
}
public String getImage() {
return image;
}
public void setImage(String image) {
this.image = image == null ? null : image.trim();
}
public Long getCid() {
return cid;
}
public void setCid(Long cid) {
this.cid = cid;
}
public Byte getStatus() {
return status;
}
public void setStatus(Byte status) {
this.status = status;
}
public Date getCreated() {
return created;
}
public void setCreated(Date created) {
this.created = created;
}
public Date getUpdated() {
return updated;
}
public void setUpdated(Date updated) {
this.updated = updated;
}
}
Page.java文件
package com.xuyafei.pojo;
public class Page {
int start=0;
int count = 5;
int last = 0;
public int getStart() {
return start;
}
public void setStart(int start) {
this.start = start;
}
public int getCount() {
return count;
}
public void setCount(int count) {
this.count = count;
}
public int getLast() {
return last;
}
public void setLast(int last) {
this.last = last;
}
public void caculateLast(int total) {
// 假设总数是50,是能够被5整除的,那么最后一页的开始就是45
if (0 == total % count)
last = total - count;
// 假设总数是51,不能够被5整除的,那么最后一页的开始就是50
else
last = total - total % count;
}
@Override
public String toString() {
return "Page [start=" + start + ", count=" + count + ", last=" + last + "]";
}
}
03 Mybatis映射文件
ItemMapper.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="com.xuyafei.dao.ItemMapper" >
<resultMap id="BaseResultMap" type="com.xuyafei.pojo.Item" >
<id column="id" property="id" jdbcType="BIGINT" />
<result column="title" property="title" jdbcType="VARCHAR" />
<result column="sell_point" property="sellPoint" jdbcType="VARCHAR" />
<result column="price" property="price" jdbcType="BIGINT" />
<result column="num" property="num" jdbcType="INTEGER" />
<result column="barcode" property="barcode" jdbcType="VARCHAR" />
<result column="image" property="image" jdbcType="VARCHAR" />
<result column="cid" property="cid" jdbcType="BIGINT" />
<result column="status" property="status" jdbcType="TINYINT" />
<result column="created" property="created" jdbcType="TIMESTAMP" />
<result column="updated" property="updated" jdbcType="TIMESTAMP" />
</resultMap>
<sql id="Base_Column_List" >
id, title, sell_point, price, num, barcode, image, cid, status, created, updated
</sql>
<select id="total" resultType="int">
select count(*) from tb_item
</select>
<select id="selectAll" resultMap="BaseResultMap">
select * from tb_item
<if test="start!=null and count!=null">
limit #{start},#{count}
</if>
</select>
<select id="selectByPrimaryKey" resultMap="BaseResultMap" parameterType="java.lang.Long" >
select
<include refid="Base_Column_List" />
from tb_item
where id = #{id,jdbcType=BIGINT}
</select>
<delete id="deleteByPrimaryKey" parameterType="java.lang.Long" >
delete from tb_item
where id = #{id,jdbcType=BIGINT}
</delete>
<insert id="insert" parameterType="com.xuyafei.pojo.Item" >
insert into tb_item (id, title, sell_point,
price, num, barcode,
image, cid, status,
created, updated)
values (#{id,jdbcType=BIGINT}, #{title,jdbcType=VARCHAR}, #{sellPoint,jdbcType=VARCHAR},
#{price,jdbcType=BIGINT}, #{num,jdbcType=INTEGER}, #{barcode,jdbcType=VARCHAR},
#{image,jdbcType=VARCHAR}, #{cid,jdbcType=BIGINT}, #{status,jdbcType=TINYINT},
#{created,jdbcType=TIMESTAMP}, #{updated,jdbcType=TIMESTAMP})
</insert>
<insert id="insertSelective" parameterType="com.xuyafei.pojo.Item" >
insert into tb_item
<trim prefix="(" suffix=")" suffixOverrides="," >
<if test="id != null" >
id,
</if>
<if test="title != null" >
title,
</if>
<if test="sellPoint != null" >
sell_point,
</if>
<if test="price != null" >
price,
</if>
<if test="num != null" >
num,
</if>
<if test="barcode != null" >
barcode,
</if>
<if test="image != null" >
image,
</if>
<if test="cid != null" >
cid,
</if>
<if test="status != null" >
status,
</if>
<if test="created != null" >
created,
</if>
<if test="updated != null" >
updated,
</if>
</trim>
<trim prefix="values (" suffix=")" suffixOverrides="," >
<if test="id != null" >
#{id,jdbcType=BIGINT},
</if>
<if test="title != null" >
#{title,jdbcType=VARCHAR},
</if>
<if test="sellPoint != null" >
#{sellPoint,jdbcType=VARCHAR},
</if>
<if test="price != null" >
#{price,jdbcType=BIGINT},
</if>
<if test="num != null" >
#{num,jdbcType=INTEGER},
</if>
<if test="barcode != null" >
#{barcode,jdbcType=VARCHAR},
</if>
<if test="image != null" >
#{image,jdbcType=VARCHAR},
</if>
<if test="cid != null" >
#{cid,jdbcType=BIGINT},
</if>
<if test="status != null" >
#{status,jdbcType=TINYINT},
</if>
<if test="created != null" >
#{created,jdbcType=TIMESTAMP},
</if>
<if test="updated != null" >
#{updated,jdbcType=TIMESTAMP},
</if>
</trim>
</insert>
<update id="updateByPrimaryKeySelective" parameterType="com.xuyafei.pojo.Item" >
update tb_item
<set >
<if test="title != null" >
title = #{title,jdbcType=VARCHAR},
</if>
<if test="sellPoint != null" >
sell_point = #{sellPoint,jdbcType=VARCHAR},
</if>
<if test="price != null" >
price = #{price,jdbcType=BIGINT},
</if>
<if test="num != null" >
num = #{num,jdbcType=INTEGER},
</if>
<if test="barcode != null" >
barcode = #{barcode,jdbcType=VARCHAR},
</if>
<if test="image != null" >
image = #{image,jdbcType=VARCHAR},
</if>
<if test="cid != null" >
cid = #{cid,jdbcType=BIGINT},
</if>
<if test="status != null" >
status = #{status,jdbcType=TINYINT},
</if>
<if test="created != null" >
created = #{created,jdbcType=TIMESTAMP},
</if>
<if test="updated != null" >
updated = #{updated,jdbcType=TIMESTAMP},
</if>
</set>
where id = #{id,jdbcType=BIGINT}
</update>
<update id="updateByPrimaryKey" parameterType="com.xuyafei.pojo.Item" >
update tb_item
set title = #{title,jdbcType=VARCHAR},
sell_point = #{sellPoint,jdbcType=VARCHAR},
price = #{price,jdbcType=BIGINT},
num = #{num,jdbcType=INTEGER},
barcode = #{barcode,jdbcType=VARCHAR},
image = #{image,jdbcType=VARCHAR},
cid = #{cid,jdbcType=BIGINT},
status = #{status,jdbcType=TINYINT},
created = #{created,jdbcType=TIMESTAMP},
updated = #{updated,jdbcType=TIMESTAMP}
where id = #{id,jdbcType=BIGINT}
</update>
</mapper>
04 Dao层代码
ItemMapper.java
package com.xuyafei.dao;
import java.util.List;
import com.xuyafei.pojo.Item;
import com.xuyafei.pojo.Page;
public interface ItemMapper {
int deleteByPrimaryKey(Long id);
int insert(Item record);
int insertSelective(Item record);
Item selectByPrimaryKey(Long id);
int updateByPrimaryKeySelective(Item record);
int updateByPrimaryKey(Item record);
List<Item> selectAll();
List<Item> selectAll(Page page);
public int total();
}
05 Service层代码
接口类ItemCRUD.java
package com.xuyafei.service;
import java.util.List;
import com.xuyafei.pojo.Item;
import com.xuyafei.pojo.Page;
public interface ItemCRUD {
public List<Item> checkAllMethod();
List<Item> checkAllMethod(Page page);
int total();
public Item checkOneMethod(Long itemId);
public int deleteOne(Long itemId);
public int addOne(Item item);
public int updataOne(Item item);
}
接口实现类ItemCRUDImpl.java
package com.xuyafei.service;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.xuyafei.dao.ItemMapper;
import com.xuyafei.pojo.Item;
import com.xuyafei.pojo.Page;
/**
*
* @author xuyafei
*
*/
@Service
public class ItemCRUDImpl implements ItemCRUD {
@Autowired
private ItemMapper im;
/**
* 查询所有记录
*/
@Override
public List<Item> checkAllMethod() {
List<Item> selectAll = im.selectAll();
return selectAll;
}
/**
* 删除一条记录
*/
@Override
public int deleteOne(Long itemId) {
return im.deleteByPrimaryKey(itemId);
}
/**
* 新增一条记录
*/
@Override
public int addOne(Item item) {
int insertSelective = im.insertSelective(item);
return insertSelective;
}
/**
* 更改一条记录
*/
@Override
public int updataOne(Item item) {
return im.updateByPrimaryKeySelective(item);
}
/**
* 查询一条记录
*/
@Override
public Item checkOneMethod(Long itemId) {
return im.selectByPrimaryKey(itemId);
}
// 分页查询
@Override
public List<Item> checkAllMethod(Page page) {
return im.selectAll(page);
}
// 查出总记录数
@Override
public int total() {
return im.total();
}
}
06 控制层代码
yfxTableCrudJspTest.java
package com.xuyafei.controller;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
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.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import com.xuyafei.pojo.Item;
import com.xuyafei.pojo.Page;
import com.xuyafei.service.ItemCRUD;
/**
*
* @author xuyafei
*
*/
@Controller
public class yfxTableCrudJspTest {
@Autowired
private ItemCRUD itemCrud;
/**
* 分页
*/
@RequestMapping("/paging2")
public ModelAndView listCategory(Page page){
if(page.getStart()<0){
page.setStart(0);
}
ModelAndView mav = new ModelAndView();
List<Item> checkAllMethod2= itemCrud.checkAllMethod(page);
int total = itemCrud.total();
page.caculateLast(total);
mav.addObject("checkAllMethod2",checkAllMethod2);
// 放入jsp路径
mav.setViewName("yfxTableCrudJspTestAdd");
return mav;
}
/**
* 增加记录
*/
@RequestMapping("/add12071936")
@ResponseBody
public String doAdd(Item item) {
Item item2 =new Item();
item2.setBarcode("666");
item2.setCid(666L);
item2.setCreated(new Date());
item2.setId(item.getId());
item2.setImage("666");
item2.setNum(item.getNum());
item2.setPrice(item.getPrice());
item2.setSellPoint(item.getSellPoint());
item2.setTitle(item.getTitle());
item2.setUpdated(new Date());
item2.setStatus(item.getStatus());
int rows = itemCrud.addOne(item2);
if(rows > 0){
return "OK";
}else{
return "FAIL";
}
}
/**
* 删除记录
*/
@RequestMapping(value="/delete12071936")
@ResponseBody
public String doDelete(Integer id) {
Long id2 = new Long(id);
int rows = itemCrud.deleteOne(id2);
if(rows > 0){
return "OK";
}else{
return "FAIL";
}
}
/**
* 修改记录
*/
@RequestMapping("/update12071936")
@ResponseBody
public String doUpdate(Item item) {
Item item2 =new Item();
item2.setBarcode("666");
item2.setCid(666L);
item2.setCreated(new Date());
item2.setId(item.getId());
item2.setImage("666");
item2.setNum(item.getNum());
item2.setPrice(item.getPrice());
item2.setSellPoint(item.getSellPoint());
item2.setTitle(item.getTitle());
item2.setUpdated(new Date());
item2.setStatus(item.getStatus());
System.out.println(item2);
int rows = itemCrud.updataOne(item2);
if(rows > 0){
return "OK";
}else{
return "FAIL";
}
}
/**
* 查询一条记录
* @return
*/
@RequestMapping(value="/checkOne12071936")
@ResponseBody
public Item doCheckOne(String id){
System.out.println("从前台拿到的数据ID=="+id);
Long id2 = Long.parseLong(id);
//ModelAndView mav = new ModelAndView();
//mav.setViewName("yfxTableCrudJspTestAdd"); //返回的文件名
// pojo
Item checkOne = itemCrud.checkOneMethod(id2);
//mav.addObject("checkOne",checkOne);
System.out.println("Item数据=="+checkOne);
return checkOne;
}
}
07 最后前端JS+Bootstrap代码较多在此处省略
整个项目所有代码下载链接,
https://download.csdn.net/download/up2mike4/10851611
如果有问题可以发邮件103109371@qq.com