基于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

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
基于SpringSpring MVC和MyBatis的博客系统开发教程如下: 首先,要准备好开发所需的环境和工具。你需要安装Java开发工具包(JDK)、Eclipse或者IntelliJ IDEA集成开发环境、Apache Tomcat服务器、Maven构建工具以及MySQL数据库。确保这些工具都正确安装和配置。 接下来,创建一个新的Maven项目,并在pom.xml文件中添加依赖项,包括Spring MVC、MyBatis数据库连接池、日志等。这些依赖项可以通过Maven自动下载和管理。 然后,在src/main/java目录下创建相应的包结构,如controller、service、dao等。在dao包下创建相应的数据访问接口,并使用MyBatis提供的注解或XML配置文件实现数据访问的逻辑。在service包下创建对应的服务接口和实现类,用于处理业务逻辑。在controller包下创建控制器类,处理请求和响应。 配置SpringMyBatis的配置文件。在src/main/resources目录下创建一个名为applicationContext.xml的Spring配置文件,并添加相关的配置信息,包括数据库连接、事务管理、包扫描等。同样,在该目录下创建一个名为mybatis-config.xml的MyBatis配置文件,并配置数据源、映射文件等。 编博客系统的前端页面和样式。可以使用HTML、CSS和JavaScript等技术来构建用户界面,使用JSP或Thymeleaf等模板引擎来动态生成页面内容。可以使用Bootstrap等前端框架来加快开发进度并提供良好的用户体验。 最后,测试和部署博客系统。使用JUnit等单元测试框架来测试各个模块的功能是否正常。将项目打包成war文件,并将其部署到Tomcat服务器上运行。 通过以上步骤,你就可以基于SpringSpring MVC和MyBatis开发一个简单的博客系统了。当然,在实际开发中还会涉及到更多的细节和技术选择,需要持续学习和实践。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值