上一篇文章已经讲解了如何创建springboot项目,并生成逆向工程。
1、写前端代码
showbooklist.html页面
<!DOCTYPE html>
<html lang="en">
<script src="jquery-3.3.1.min.js"></script>
<script src="book.js"></script>
<head>
<meta charset="UTF-8">
<title>展示列表</title>
</head>
<body>
<a href="insertBook.html">添加</a>
<table border="1">
<thead>
<tr>
<th>图书编号</th>
<th>图书名称</th>
<th>图书作者</th>
<th colspan="2">操作</th>
</tr>
</thead>
<tbody id="tb">
</tbody>
</table>
<button id="previous" onclick="previous()">上一页</button>~
<button id="next" onclick="nextPage()">下一页</button>
</body>
</html>
insertBook.html页面
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<script src="jquery-3.3.1.min.js"></script>
<title>添加图书</title>
</head>
<body>
<form>
图书名称:<input type="text" name="bookName" id="bookName"><br>
图书作者:<input type="text" name="bookWitter" id="bookWitter"><br>
<button type="button" id="insert">提交</button>
</form>
<a href="showbooklist.html">返回</a>
</body>
<script>
$(function () {
$("#insert").click(function () {
var bookName=$("#bookName").val();
var bookWitter=$("#bookWitter").val();
$.get("book/insertBook",{"bookname":bookName,"bookwitter":bookWitter},function (data) {
alert(data);
});
});
})
</script>
</html>
updateBookById.html页面
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<script src="jquery-3.3.1.min.js"></script>
<script src="updata.js"></script>
<title>编辑图书</title>
</head>
<body>
<form>
图书编号:<input type="text" name="bookId" id="bookId" readonly="readonly"><br>
图书名称:<input type="text" name="bookName" id="bookName"><br>
图书作者:<input type="text" name="bookWitter" id="bookWitter"><br>
<button type="button" id="update">提交</button>
</form>
<a href="showbooklist.html">返回</a>
</body>
</html>
book.js
var pageNo=0;
var pageSize=10;
$(function () {
toShow();
})
function toShow() {
$.get("book/selectAll",{"pageNo":pageNo,"pageSize":pageSize},function (data) {
if (data.length<pageSize){
$("#next").attr("disabled",true);
}else {
$("#next").attr("disabled",false);
}
$("#tb").empty();
toAdd(data);
},"json");
}
function toAdd(data) {
for (var i in data){
$("#tb").append('<tr> '+
' <td>'+data[i].bookid+'</td>\n' +
' <td>'+data[i].bookname+'</td>\n' +
' <td>'+data[i].bookwitter+'</td>' +
' <td><a href="updateBookById.html?bookid='+data[i].bookid+'">编辑</a></td>' +
' <td><a href="javascript:" onclick="deleteBook('+data[i].bookid+')">删除</a></td>' +
' </tr>');
}
}
function deleteBook(bookId) {
$.get("book/deleteBook",{"bookid":bookId},function (data) {
alert(data);
});
}
//下一页
function nextPage(){
pageNo++;
toShow();
}
// $("#next").click(function () {
// pageNo++;
// toShow();
// });
//上一页
function previous(){
if (pageNo==0){
return;
}
pageNo--;
toShow()
}
// $("#previous").click(function () {
// if (pageNo==0){
// return;
// }
// pageNo--;
// toShow()
// });
update.js
$(function () {
var bookId=showWindowHref();
$.get("book/getBook",{"bookid":bookId},function (data) {
$("#bookId").val(bookId);
$("#bookName").val(data.bookname);
$("#bookWitter").val(data.bookwitter);
},"json")
$("#update").click(function () {
var bookName=$("#bookName").val();
var bookWitter=$("#bookWitter").val();
$.get("book/updateBook",{"bookid":bookId,"bookname":bookName,"bookwitter":bookWitter},function (data) {
alert(data);
});
});
})
function showWindowHref() {
var sHref = window.location.href;
var args = sHref.split('?');
if (args[0] == sHref) {
return "";
}
var arr = args[1].split('=');
return arr[1];
}
2、后端代码
BookMapper.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.jie.book.mapper.BookMapper" >
<resultMap id="BaseResultMap" type="com.jie.book.domain.Book" >
<id column="bookId" property="bookid" jdbcType="INTEGER" />
<result column="bookName" property="bookname" jdbcType="VARCHAR" />
<result column="bookWitter" property="bookwitter" jdbcType="VARCHAR" />
</resultMap>
<sql id="Base_Column_List" >
bookId, bookName, bookWitter
</sql>
<select id="selectByPrimaryKey" resultMap="BaseResultMap" parameterType="java.lang.Integer" >
select
<include refid="Base_Column_List" />
from book
where bookId = #{bookid,jdbcType=INTEGER}
</select>
<delete id="deleteByPrimaryKey" parameterType="java.lang.Integer" >
delete from book
where bookId = #{bookid,jdbcType=INTEGER}
</delete>
<insert id="insert" parameterType="com.jie.book.domain.Book" >
insert into book (bookId, bookName, bookWitter
)
values (#{bookid,jdbcType=INTEGER}, #{bookname,jdbcType=VARCHAR}, #{bookwitter,jdbcType=VARCHAR}
)
</insert>
<insert id="insertSelective" parameterType="com.jie.book.domain.Book" >
insert into book
<trim prefix="(" suffix=")" suffixOverrides="," >
<if test="bookid != null" >
bookId,
</if>
<if test="bookname != null" >
bookName,
</if>
<if test="bookwitter != null" >
bookWitter,
</if>
</trim>
<trim prefix="values (" suffix=")" suffixOverrides="," >
<if test="bookid != null" >
#{bookid,jdbcType=INTEGER},
</if>
<if test="bookname != null" >
#{bookname,jdbcType=VARCHAR},
</if>
<if test="bookwitter != null" >
#{bookwitter,jdbcType=VARCHAR},
</if>
</trim>
</insert>
<update id="updateByPrimaryKeySelective" parameterType="com.jie.book.domain.Book" >
update book
<set >
<if test="bookname != null" >
bookName = #{bookname,jdbcType=VARCHAR},
</if>
<if test="bookwitter != null" >
bookWitter = #{bookwitter,jdbcType=VARCHAR},
</if>
</set>
where bookId = #{bookid,jdbcType=INTEGER}
</update>
<update id="updateByPrimaryKey" parameterType="com.jie.book.domain.Book" >
update book
set bookName = #{bookname,jdbcType=VARCHAR},
bookWitter = #{bookwitter,jdbcType=VARCHAR}
where bookId = #{bookid,jdbcType=INTEGER}
</update>
<select id="selectAll" parameterType="map" resultMap="BaseResultMap">
select * from book limit #{start},#{end}
</select>
</mapper>
BookMapper.java
package com.jie.book.mapper;
import com.jie.book.domain.Book;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Component;
import java.util.List;
@Mapper
@Component
public interface BookMapper {
int deleteByPrimaryKey(Integer bookId);
int insert(Book record);
int insertSelective(Book record);
Book selectByPrimaryKey(Integer bookId);
int updateByPrimaryKeySelective(Book record);
int updateByPrimaryKey(Book record);
List<Book>selectAll(@Param("start") int start, @Param("end")int end);
}
BookService.java
package com.jie.book.service;
import com.jie.book.domain.Book;
import java.util.List;
public interface BookService {
int deleteBook(Integer bookid);
int insertBook(Book record);
Book getBook(Integer bookid);
int updateBook(Book record);
List<Book> selectAll(int start, int end);
}
BookServiceImpl.java
package com.jie.book.service.impl;
import com.jie.book.domain.Book;
import com.jie.book.mapper.BookMapper;
import com.jie.book.service.BookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class BookServiceImpl implements BookService {
@Autowired
BookMapper bookMapper;
@Override
public int deleteBook(Integer bookid) {
int n=bookMapper.deleteByPrimaryKey(bookid);
return n;
}
@Override
public int insertBook(Book record) {
int n=bookMapper.insertSelective(record);
return n;
}
@Override
public Book getBook(Integer bookid) {
Book book=bookMapper.selectByPrimaryKey(bookid);
return book;
}
@Override
public int updateBook(Book record) {
int n=bookMapper.updateByPrimaryKeySelective(record);
return n;
}
@Override
public List<Book> selectAll(int start, int end) {
List<Book>bookList=bookMapper.selectAll(start,end);
return bookList;
}
}
BookController.java
package com.jie.book.controller;
import com.alibaba.fastjson.JSON;
import com.jie.book.domain.Book;
import com.jie.book.service.BookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
@RestController
@RequestMapping("book")
public class BookController {
@Autowired
BookService bookService;
@RequestMapping("selectAll")
public String selectAll(int pageNo,int pageSize){
int start=pageNo*pageSize;
int end=(pageNo+1)*pageSize;
List<Book>bookList=bookService.selectAll(start,end);
return JSON.toJSONString(bookList);
}
@RequestMapping("deleteBook")
public String deleteBook(int bookid) {
int n=bookService.deleteBook(bookid);
String msg="";
if (n>0){
msg="删除成功";
}else {
msg="删除失败";
}
return msg;
}
@RequestMapping("getBook")
public String getBook(int bookid) {
Book book=bookService.getBook(bookid);
return JSON.toJSONString(book);
}
@RequestMapping("insertBook")
public String insertBook(Book book){
int n=bookService.insertBook(book);
String msg="";
if (n>0){
msg="添加成功";
}else {
msg="添加失败";
}
return msg;
}
@RequestMapping("updateBook")
public String updateBook(Book book){
int n=bookService.updateBook(book);
String msg="";
if (n>0){
msg="更新成功";
}else {
msg="更新失败";
}
return msg;
}
}