使用SpringMVC+Spring+MyBatis实现图书管理系统——试题讲解

题目:使用SpringMVC+Spring+MyBatis实现图书管理系统——试题讲解

语言和环境

A、实现语言
Java
B、环境要求
JDK1.8、Eclipse、Tomcat7、SpringMVC、Spring、Mybatis、Mysql、Maven

功能要求

使用SSM(SpringMVC+Spring+MyBatis)实现图书管理系统,MySql作为后台数据库,该系统包括用户登录,图书信息列表、图书的新增、修改、删除,并实现分页功能,具体要求如下:

  1. 用户登录功能。打开图书管理系统首页,会出现用户登录界面,输入用户名和密码后,点击登录,如果用户名和密码不正确,给出相应的提示信息,如下图所示。在这里插入图片描述
  2. 图书信息列表显示功能。用户登录成功后,进入图书信息列表显示首页。默认按图书ID降序列出所有的图书。并能够根据用户类型显示登录用户的用户信息。提供新增图书按钮,提供修改链接,提供删除链接。如下图所示:
    在这里插入图片描述
  3. 图书新增功能。点击“新增图书”按钮,跳转到新增的页面,其中书名,作者、出版社、出版日期为必填项,如下图所示。
    在这里插入图片描述
    点击“提交”时,需要执行非空验证,如果书名为空,提示“图书名称不能为空!”,如果作者为空,则提示“作者不能为空!”,出版社和出版日期一样,同样不能为空。日期的输入格式为”年-月-日”,页面效果如下图所示。
    在这里插入图片描述
    提交并保存图书信息后返回到图书信息列表。点击“返回”按钮,直接返回原来的图书信息列表。
  4. 图书修改功能。点击“修改”超链接,显示要修改的图书信息,如下图所示。
    在这里插入图片描述
    提交并更新图书信息后,返回到图书信息列表。点击“返回”按钮,直接返回原来的图书信息列表。
  5. 分页功能。点击第一页、上一页、下一页、最后一页、能够实现分页功能。能够显示总记录数,当前页数/总页数。设置每页条数和转到第几页可以不实现。效果如下图所示。
    在这里插入图片描述
  6. 图书删除功能。点击“删除”超链接,执行删除图书功能,并返回图书列表页。

数据库设计

用户表如下,其他设置参见下表:
在这里插入图片描述

图书信息表如下:
在这里插入图片描述

主要源码

测试数据
-- ----------------------------
-- Table structure for book
-- ----------------------------
DROP TABLE IF EXISTS `book`;
CREATE TABLE `book` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `name` varchar(100) NOT NULL,
  `author` varchar(100) NOT NULL,
  `publish` varchar(100) NOT NULL,
  `publishdate` date NOT NULL,
  `page` int(11) DEFAULT NULL,
  `price` decimal(8,2) DEFAULT NULL,
  `content` varchar(500) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=8 DEFAULT CHARSET=utf8;

-- ----------------------------
-- Records of book
-- ----------------------------
INSERT INTO `book` VALUES ('1', 'JSP编程', '李四', '电力出版社', '2011-12-02', '300', '40.00', 'JSP编程开发');
INSERT INTO `book` VALUES ('2', 'JSP编程2', '张三', '电力出版社', '2011-12-02', '150', '16.80', 'JSP编程开发');
INSERT INTO `book` VALUES ('3', 'JSP编程3', '张三', '电力出版社', '2011-12-02', '150', '16.80', 'JSP编程开发');
INSERT INTO `book` VALUES ('7', 'java基础', 'tom', '电力', '2010-10-10', '200', '30.00', 'test');
实体类
package com.neu.bean;

import java.math.BigDecimal;
import java.util.Date;

import org.springframework.format.annotation.DateTimeFormat;

public class Book {    
    private Integer id;
    private String name;
    private String author;
    private String publish;
    @DateTimeFormat(pattern="yyyy-MM-dd")
    private Date publishdate;
    private Integer page;
    private BigDecimal price;
    private String content;
    public Integer getId() {
        return id;
    }
    public void setId(Integer id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getAuthor() {
        return author;
    }
    public void setAuthor(String author) {
        this.author = author;
    }
    public String getPublish() {
        return publish;
    }
    public void setPublish(String publish) {
        this.publish = publish;
    }
    public Date getPublishdate() {
        return publishdate;
    }
    public void setPublishdate(Date publishdate) {
        this.publishdate = publishdate;
    }
    public Integer getPage() {
        return page;
    }
    public void setPage(Integer page) {
        this.page = page;
    }
    public BigDecimal getPrice() {
        return price;
    }
    public void setPrice(BigDecimal price) {
        this.price = price;
    }
    public String getContent() {
        return content;
    }
    public void setContent(String content) {
        this.content = content;
    }
}
getpaged.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
<script type="text/javascript">
function goPage(){
	var page = document.getElementById("page").value;
	location = "${pageContext.request.contextPath }/book/getPaged.action?pageNum="+page;
}

function del(id){
	if(confirm("是否删除?")){
		location = "${pageContext.request.contextPath }/book/delete.action?id="+id;
	}
}

function add(){
	location = "${pageContext.request.contextPath }/book/getadd.action";
}
</script>
</head>
<body>
	<table border="1" width="600">
		<c:forEach items="${list }" var="book">
			<tr>
				<td>${ book.name }</td>
				<td>${ book.author }</td>
				<td>${ book.publish }</td>
				<td><fmt:formatDate value="${ book.publishdate }" pattern="yyyy-MM-dd"/> </td>
				<td>${ book.page }</td>
				<td>${ book.price }</td>
				<td>${ book.content }</td>
				<td>
					<a href="${pageContext.request.contextPath }/book/edit.action?id=${book.id}">编辑</a> 
					<a href="#" onclick="del(${book.id})">删除</a>
				</td>
			</tr>
		</c:forEach>
		<tr>
			<td colspan="8">
				<input type="button" value="新增图书" onclick="add()">
				共 ${ count } 条记录
				每页 <input type="text" size="1" value="${ pageSize }">条
				第 ${ pageNum } 页/共${ pageCount } 页
				<c:if test="${ pageNum ==1 }">
					<a>第一页</a>
				</c:if>
				<c:if test="${ pageNum  > 1 }">
					<a href="${pageContext.request.contextPath }/book/getPaged.action?pageNum=1">第一页</a>
				</c:if>				
				<c:if test="${ pageNum ==1 }">
					<a>上一页</a>
				</c:if>
				<c:if test="${ pageNum  > 1 }">
					<a href="${pageContext.request.contextPath }/book/getPaged.action?pageNum=${pageNum -1}">上一页</a>
				</c:if>				
				<c:if test="${ pageNum ==pageCount }">
					<a>下一页</a>
				</c:if>
				<c:if test="${ pageNum < pageCount }">
					<a href="${pageContext.request.contextPath }/book/getPaged.action?pageNum=${pageNum +1}">下一页</a>
				</c:if>
				<c:if test="${ pageNum ==pageCount }">
					<a>最后一页</a>
				</c:if>
				<c:if test="${ pageNum < pageCount }">
					<a href="${pageContext.request.contextPath }/book/getPaged.action?pageNum=${ pageCount }">最后一页</a>
				</c:if>
				转到第<input type="text" size="1" name="page" id="page"> <input type="button" value="go" onclick="goPage()">
			</td>
		</tr>
	</table>
</body>
</html>
edit.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
	<form action="${ pageContext.request.contextPath }/book/update.action?id=${ book.id }" method="post">
	<table>
		<tr>
			<td>书名:</td>
			<td><input type="text" name="name"  value="${ book.name }"></td>
		</tr>
		<tr>
			<td>作者:</td>
			<td><input type="text" name="author"  value="${ book.author }"></td>
		</tr>
		<tr>
			<td>出版社:</td>
			<td><input type="text"  name="publish" value="${ book.publish }"></td>
		</tr>
		<tr>
			<td>出版日期:</td>
			<td><input type="text" name="publishdate"  value='<fmt:formatDate value="${ book.publishdate }" pattern="yyyy-MM-dd" />'> </td>
		</tr>
		<tr>
			<td>页数:</td>
			<td><input type="text" name="page"  value="${ book.page }"></td>
		</tr>
		<tr>
			<td>价格:</td>
			<td><input type="text" name="price"  value="${ book.price }"></td>
		</tr>
		<tr>
			<td>内容摘要:</td>
			<td>
				<textarea  name="content" rows="3" cols="10">${ book.content }</textarea>
			</td>
		</tr>
		<tr>
			<td colspan="2">
				<input type="submit" value="提交">
			</td>
		</tr>
	</table>
	</form>
</body>
</html>
add.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
	<form action="${ pageContext.request.contextPath }/book/add.action" method="post">
	<table>
		<tr>
			<td>书名:</td>
			<td><input type="text" name="name"></td>
		</tr>
		<tr>
			<td>作者:</td>
			<td><input type="text" name="author"></td>
		</tr>
		<tr>
			<td>出版社:</td>
			<td><input type="text"  name="publish"></td>
		</tr>
		<tr>
			<td>出版日期:</td>
			<td><input type="text" name="publishdate"'> </td>
		</tr>
		<tr>
			<td>页数:</td>
			<td><input type="text" name="page"></td>
		</tr>
		<tr>
			<td>价格:</td>
			<td><input type="text" name="price"></td>
		</tr>
		<tr>
			<td>内容摘要:</td>
			<td>
				<textarea  name="content" rows="3" cols="10"></textarea>
			</td>
		</tr>
		<tr>
			<td colspan="2">
				<input type="submit" value="提交">
			</td>
		</tr>
	</table>
	</form>
</body>
</html>
控制器
package com.neu.controller;
import java.util.List;
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 com.github.pagehelper.Page;
import com.neu.bean.Book;
import com.neu.service.BookService;

@Controller
@RequestMapping("book")
public class BookController {
	@Autowired
	private BookService bookSerivce;
	
	@RequestMapping("getPaged")
	public String getPaged(@RequestParam(value="pageNum",defaultValue="1") int pageNum,Model model) {
		int pageSize = 5;
		
		List<Book> list = bookSerivce.getPaged(pageSize, pageNum);
		model.addAttribute("list", list);
		
		Page<Book> page = (Page<Book>)list;
		
		int pageCount = page.getPages();//一共有多少页
		int count = (int)page.getTotal();//一共有多少行
		
		model.addAttribute("pageCount", pageCount);
		model.addAttribute("count", count);
		model.addAttribute("pageSize", pageSize);
		model.addAttribute("pageNum", pageNum);
		
		return "book/getpaged";
		
	}
	@RequestMapping("delete")
	public String delete(int id) {
		bookSerivce.delete(id);
		
		return "forward:/book/getPaged.action";
	}
	
	@RequestMapping("edit")
	public String edit(int id,Model model) {
		Book book = bookSerivce.getById(id);
		
		model.addAttribute("book", book);
		
		return "book/edit";
	}
	
	
	@RequestMapping("update")
	public String update(Book book) {
		bookSerivce.update(book);
		
		return "forward:/book/getPaged.action";
	}
	
	@RequestMapping("getadd")
	public String getadd() {
		
		return "book/add";
	}
	
	@RequestMapping("add")
	public String add(Book book) {
		bookSerivce.insert(book);
		
		return "forward:/book/getPaged.action";
	}
}

业务逻辑接口
package com.neu.service;
import java.util.List;
import com.neu.bean.Book;

public interface BookService {
	public List<Book> getPaged(int pageSize,int pageNum);	
	public int insert(Book book);
	public int update(Book book);	
	public int delete(int id);	
	public Book getById(int id);
}

业务逻辑实现类
package com.neu.service;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.neu.bean.Book;
import com.neu.mapper.BookMapper;

@Service
public class BookServiceImpl implements BookService {
	@Autowired
	private BookMapper bookMapper;

	@Override
	public List<Book> getPaged(int pageSize, int pageNum) {
		List<Book> list = bookMapper.getPaged(pageSize, pageNum);
		return list;
	}

	@Override
	public int insert(Book book) {
		int n = bookMapper.insert(book);
		return n;
	}

	@Override
	public int update(Book book) {
		int n = bookMapper.updateByPrimaryKey(book);
		return n;
	}

	@Override
	public int delete(int id) {
		int n = bookMapper.deleteByPrimaryKey(id);
		return n;
	}

	@Override
	public Book getById(int id) {
		Book book = bookMapper.selectByPrimaryKey(id);
		return book;
	}	
}
mapper接口(使用MBG代码生成器自动生成)
package com.neu.mapper;

import com.neu.bean.Book;
import com.neu.bean.BookExample;
import java.util.List;
import org.apache.ibatis.annotations.Param;

public interface BookMapper {
    long countByExample(BookExample example);
    int deleteByExample(BookExample example);
    int deleteByPrimaryKey(Integer id);
    int insert(Book record);
    int insertSelective(Book record);
    List<Book> selectByExample(BookExample example);
    Book selectByPrimaryKey(Integer id);
    int updateByExampleSelective(@Param("record") Book record, @Param("example") BookExample example);
    int updateByExample(@Param("record") Book record, @Param("example") BookExample example);
    int updateByPrimaryKeySelective(Book record);
    int updateByPrimaryKey(Book record);
	//分页方法为自己添加
    public List<Book> getPaged(@Param("pageSize") int pageSize,@Param("pageNum")int pageNum);
}
mapper(使用MBG代码生成器自动生成)
<?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.neu.mapper.BookMapper">
  <resultMap id="BaseResultMap" type="com.neu.bean.Book">
    <!--
      WARNING - @mbg.generated
      This element is automatically generated by MyBatis Generator, do not modify.
      This element was generated on Fri Oct 26 09:27:47 CST 2018.
    -->
    <id column="id" jdbcType="INTEGER" property="id" />
    <result column="name" jdbcType="VARCHAR" property="name" />
    <result column="author" jdbcType="VARCHAR" property="author" />
    <result column="publish" jdbcType="VARCHAR" property="publish" />
    <result column="publishdate" jdbcType="DATE" property="publishdate" />
    <result column="page" jdbcType="INTEGER" property="page" />
    <result column="price" jdbcType="DECIMAL" property="price" />
    <result column="content" jdbcType="VARCHAR" property="content" />
  </resultMap>
  <sql id="Example_Where_Clause">
    <!--
      WARNING - @mbg.generated
      This element is automatically generated by MyBatis Generator, do not modify.
      This element was generated on Fri Oct 26 09:27:47 CST 2018.
    -->
    <where>
      <foreach collection="oredCriteria" item="criteria" separator="or">
        <if test="criteria.valid">
          <trim prefix="(" prefixOverrides="and" suffix=")">
            <foreach collection="criteria.criteria" item="criterion">
              <choose>
                <when test="criterion.noValue">
                  and ${criterion.condition}
                </when>
                <when test="criterion.singleValue">
                  and ${criterion.condition} #{criterion.value}
                </when>
                <when test="criterion.betweenValue">
                  and ${criterion.condition} #{criterion.value} and #{criterion.secondValue}
                </when>
                <when test="criterion.listValue">
                  and ${criterion.condition}
                  <foreach close=")" collection="criterion.value" item="listItem" open="(" separator=",">
                    #{listItem}
                  </foreach>
                </when>
              </choose>
            </foreach>
          </trim>
        </if>
      </foreach>
    </where>
  </sql>
  <sql id="Update_By_Example_Where_Clause">
    <!--
      WARNING - @mbg.generated
      This element is automatically generated by MyBatis Generator, do not modify.
      This element was generated on Fri Oct 26 09:27:47 CST 2018.
    -->
    <where>
      <foreach collection="example.oredCriteria" item="criteria" separator="or">
        <if test="criteria.valid">
          <trim prefix="(" prefixOverrides="and" suffix=")">
            <foreach collection="criteria.criteria" item="criterion">
              <choose>
                <when test="criterion.noValue">
                  and ${criterion.condition}
                </when>
                <when test="criterion.singleValue">
                  and ${criterion.condition} #{criterion.value}
                </when>
                <when test="criterion.betweenValue">
                  and ${criterion.condition} #{criterion.value} and #{criterion.secondValue}
                </when>
                <when test="criterion.listValue">
                  and ${criterion.condition}
                  <foreach close=")" collection="criterion.value" item="listItem" open="(" separator=",">
                    #{listItem}
                  </foreach>
                </when>
              </choose>
            </foreach>
          </trim>
        </if>
      </foreach>
    </where>
  </sql>
  <sql id="Base_Column_List">
    <!--
      WARNING - @mbg.generated
      This element is automatically generated by MyBatis Generator, do not modify.
      This element was generated on Fri Oct 26 09:27:47 CST 2018.
    -->
    id, name, author, publish, publishdate, page, price, content
  </sql>
  <select id="selectByExample" parameterType="com.neu.bean.BookExample" resultMap="BaseResultMap">
    <!--
      WARNING - @mbg.generated
      This element is automatically generated by MyBatis Generator, do not modify.
      This element was generated on Fri Oct 26 09:27:47 CST 2018.
    -->
    select
    <if test="distinct">
      distinct
    </if>
    <include refid="Base_Column_List" />
    from book
    <if test="_parameter != null">
      <include refid="Example_Where_Clause" />
    </if>
    <if test="orderByClause != null">
      order by ${orderByClause}
    </if>
  </select>
  <select id="selectByPrimaryKey" parameterType="java.lang.Integer" resultMap="BaseResultMap">
    <!--
      WARNING - @mbg.generated
      This element is automatically generated by MyBatis Generator, do not modify.
      This element was generated on Fri Oct 26 09:27:47 CST 2018.
    -->
    select 
    <include refid="Base_Column_List" />
    from book
    where id = #{id,jdbcType=INTEGER}
  </select>
  <delete id="deleteByPrimaryKey" parameterType="java.lang.Integer">
    <!--
      WARNING - @mbg.generated
      This element is automatically generated by MyBatis Generator, do not modify.
      This element was generated on Fri Oct 26 09:27:47 CST 2018.
    -->
    delete from book
    where id = #{id,jdbcType=INTEGER}
  </delete>
  <delete id="deleteByExample" parameterType="com.neu.bean.BookExample">
    <!--
      WARNING - @mbg.generated
      This element is automatically generated by MyBatis Generator, do not modify.
      This element was generated on Fri Oct 26 09:27:47 CST 2018.
    -->
    delete from book
    <if test="_parameter != null">
      <include refid="Example_Where_Clause" />
    </if>
  </delete>
  <insert id="insert" parameterType="com.neu.bean.Book">
    <!--
      WARNING - @mbg.generated
      This element is automatically generated by MyBatis Generator, do not modify.
      This element was generated on Fri Oct 26 09:27:47 CST 2018.
    -->
    insert into book (id, name, author, 
      publish, publishdate, page, 
      price, content)
    values (#{id,jdbcType=INTEGER}, #{name,jdbcType=VARCHAR}, #{author,jdbcType=VARCHAR}, 
      #{publish,jdbcType=VARCHAR}, #{publishdate,jdbcType=DATE}, #{page,jdbcType=INTEGER}, 
      #{price,jdbcType=DECIMAL}, #{content,jdbcType=VARCHAR})
  </insert>
  <insert id="insertSelective" parameterType="com.neu.bean.Book">
    <!--
      WARNING - @mbg.generated
      This element is automatically generated by MyBatis Generator, do not modify.
      This element was generated on Fri Oct 26 09:27:47 CST 2018.
    -->
    insert into book
    <trim prefix="(" suffix=")" suffixOverrides=",">
      <if test="id != null">
        id,
      </if>
      <if test="name != null">
        name,
      </if>
      <if test="author != null">
        author,
      </if>
      <if test="publish != null">
        publish,
      </if>
      <if test="publishdate != null">
        publishdate,
      </if>
      <if test="page != null">
        page,
      </if>
      <if test="price != null">
        price,
      </if>
      <if test="content != null">
        content,
      </if>
    </trim>
    <trim prefix="values (" suffix=")" suffixOverrides=",">
      <if test="id != null">
        #{id,jdbcType=INTEGER},
      </if>
      <if test="name != null">
        #{name,jdbcType=VARCHAR},
      </if>
      <if test="author != null">
        #{author,jdbcType=VARCHAR},
      </if>
      <if test="publish != null">
        #{publish,jdbcType=VARCHAR},
      </if>
      <if test="publishdate != null">
        #{publishdate,jdbcType=DATE},
      </if>
      <if test="page != null">
        #{page,jdbcType=INTEGER},
      </if>
      <if test="price != null">
        #{price,jdbcType=DECIMAL},
      </if>
      <if test="content != null">
        #{content,jdbcType=VARCHAR},
      </if>
    </trim>
  </insert>
  <select id="countByExample" parameterType="com.neu.bean.BookExample" resultType="java.lang.Long">
    <!--
      WARNING - @mbg.generated
      This element is automatically generated by MyBatis Generator, do not modify.
      This element was generated on Fri Oct 26 09:27:47 CST 2018.
    -->
    select count(*) from book
    <if test="_parameter != null">
      <include refid="Example_Where_Clause" />
    </if>
  </select>
  <update id="updateByExampleSelective" parameterType="map">
    <!--
      WARNING - @mbg.generated
      This element is automatically generated by MyBatis Generator, do not modify.
      This element was generated on Fri Oct 26 09:27:47 CST 2018.
    -->
    update book
    <set>
      <if test="record.id != null">
        id = #{record.id,jdbcType=INTEGER},
      </if>
      <if test="record.name != null">
        name = #{record.name,jdbcType=VARCHAR},
      </if>
      <if test="record.author != null">
        author = #{record.author,jdbcType=VARCHAR},
      </if>
      <if test="record.publish != null">
        publish = #{record.publish,jdbcType=VARCHAR},
      </if>
      <if test="record.publishdate != null">
        publishdate = #{record.publishdate,jdbcType=DATE},
      </if>
      <if test="record.page != null">
        page = #{record.page,jdbcType=INTEGER},
      </if>
      <if test="record.price != null">
        price = #{record.price,jdbcType=DECIMAL},
      </if>
      <if test="record.content != null">
        content = #{record.content,jdbcType=VARCHAR},
      </if>
    </set>
    <if test="_parameter != null">
      <include refid="Update_By_Example_Where_Clause" />
    </if>
  </update>
  <update id="updateByExample" parameterType="map">
    <!--
      WARNING - @mbg.generated
      This element is automatically generated by MyBatis Generator, do not modify.
      This element was generated on Fri Oct 26 09:27:47 CST 2018.
    -->
    update book
    set id = #{record.id,jdbcType=INTEGER},
      name = #{record.name,jdbcType=VARCHAR},
      author = #{record.author,jdbcType=VARCHAR},
      publish = #{record.publish,jdbcType=VARCHAR},
      publishdate = #{record.publishdate,jdbcType=DATE},
      page = #{record.page,jdbcType=INTEGER},
      price = #{record.price,jdbcType=DECIMAL},
      content = #{record.content,jdbcType=VARCHAR}
    <if test="_parameter != null">
      <include refid="Update_By_Example_Where_Clause" />
    </if>
  </update>
  <update id="updateByPrimaryKeySelective" parameterType="com.neu.bean.Book">
    <!--
      WARNING - @mbg.generated
      This element is automatically generated by MyBatis Generator, do not modify.
      This element was generated on Fri Oct 26 09:27:47 CST 2018.
    -->
    update book
    <set>
      <if test="name != null">
        name = #{name,jdbcType=VARCHAR},
      </if>
      <if test="author != null">
        author = #{author,jdbcType=VARCHAR},
      </if>
      <if test="publish != null">
        publish = #{publish,jdbcType=VARCHAR},
      </if>
      <if test="publishdate != null">
        publishdate = #{publishdate,jdbcType=DATE},
      </if>
      <if test="page != null">
        page = #{page,jdbcType=INTEGER},
      </if>
      <if test="price != null">
        price = #{price,jdbcType=DECIMAL},
      </if>
      <if test="content != null">
        content = #{content,jdbcType=VARCHAR},
      </if>
    </set>
    where id = #{id,jdbcType=INTEGER}
  </update>
  <update id="updateByPrimaryKey" parameterType="com.neu.bean.Book">
    <!--
      WARNING - @mbg.generated
      This element is automatically generated by MyBatis Generator, do not modify.
      This element was generated on Fri Oct 26 09:27:47 CST 2018.
    -->
    update book
    set name = #{name,jdbcType=VARCHAR},
      author = #{author,jdbcType=VARCHAR},
      publish = #{publish,jdbcType=VARCHAR},
      publishdate = #{publishdate,jdbcType=DATE},
      page = #{page,jdbcType=INTEGER},
      price = #{price,jdbcType=DECIMAL},
      content = #{content,jdbcType=VARCHAR}
    where id = #{id,jdbcType=INTEGER}
  </update>
  
  <select id="getPaged" resultMap="BaseResultMap">
  	select * from book order by id
  </select>
</mapper>
applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns:context="http://www.springframework.org/schema/context"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd">
	<!-- 组件扫描 -->
	<context:component-scan base-package="com.neu.service,com.neu.mapper"></context:component-scan>
	
	<!-- 读取属性文件 -->
	<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
		<property name="locations">
			<list>
				<value>classpath:db.properties</value>
			</list>
		</property>
	</bean>
	
	<!-- jdbcTemplate,封装了数据库的常用操作的工具类 -->
	<!-- <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
		<property name="dataSource" ref="dataSource"></property>
	</bean> -->
	
	<!-- 数据源,可以支持连接池 -->
	<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
		<property name="url" value="${jdbc.url}"></property>
		<property name="driverClassName" value="${jdbc.driverClassName}"></property>
		<property name="username" value="${jdbc.username}"></property>
		<property name="password" value="${jdbc.password}"></property>
		
		<!-- 最大激活数 -->
		<property name="maxActive" value="20"></property>
		<!-- 最大闲置数 -->
		<property name="maxIdle" value="5"></property>
		<!-- 最大等待时间,单位为毫秒,超过这个时间,就抛出异常 -->
		<property name="maxWait" value="1000"></property>
	</bean>
	
	<bean id="sqlSessionFactoryBean" class="org.mybatis.spring.SqlSessionFactoryBean">
		<property name="dataSource" ref="dataSource"></property>
		<property name="configLocation" value="classpath:SqlMapConfig.xml"></property>
	</bean>
	
	<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
		<property name="basePackage" value="com.neu.mapper"></property>
		<property name="sqlSessionFactoryBeanName" value="sqlSessionFactoryBean"></property>
	</bean>
</beans>
springmvc.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns:context="http://www.springframework.org/schema/context"
	xmlns:mvc="http://www.springframework.org/schema/mvc"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd
		http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd">
	<!-- 组件扫描 -->
	<context:component-scan base-package="com.neu.controller"></context:component-scan>
	
	<!-- 日期进行转换的格式设置 -->
	<bean id="formattingConversionServiceFactoryBean"
		class="org.springframework.format.support.FormattingConversionServiceFactoryBean">
		<property name="converters">
			<list>
				<!-- <bean class="com.neu.controller.DateConverter"></bean> -->
			</list>
		</property>
	</bean>
	<!-- 使用springmvc的注解 -->
	<mvc:annotation-driven validator="validator" conversion-service="formattingConversionServiceFactoryBean" ></mvc:annotation-driven>
	<!-- 视图解析器,通过前缀和后缀来得到视图的物理路径 -->
	<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
		<property name="prefix" value="/WEB-INF/jsp/"></property>
		<property name="suffix" value=".jsp"></property>
	</bean>
	<!-- 配置校验错误信息文件,主要用于国际化 -->
	<bean id="messageSource" class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
		<property name="basenames">
			<list>
				<value>classpath:CustomValidationMessages</value>
			</list>
		</property>
		<property name="fileEncodings" value="utf-8"></property>
		<property name="cacheSeconds" value="120"></property>
	</bean>
	
	<!-- 配置验证器 -->
	<bean 
		id="validator" 
		class="org.springframework.validation.beanvalidation.LocalValidatorFactoryBean">
		<property name="providerClass" value="org.hibernate.validator.HibernateValidator"></property>
		<property name="validationMessageSource" ref="messageSource"></property>
	</bean>
	
	<!-- <bean class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver">
		默认的异常处理页面视图
		<property name="defaultErrorView" value="error/err"></property>
		默认异常名为:exception,但是可以通过该属性修改异常的名称
		<property name="exceptionAttribute" value="ex"></property>
		
		<property name="exceptionMappings">
			<props>
				<prop key="ArithmeticException">error/ArithmeticException</prop>
				<prop key="IOException">error/IOException</prop>
				<prop key="java.sql.SQLException">error/SQLException</prop>
			</props>
		</property>
	</bean> -->
	
	<!-- 用于文件上传 -->
	<!-- <bean id="multipartResolver"
		class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
		配置上传文件的总大小,单位为:字节
		<property name="maxUploadSize" value="5242880"></property>
		设置文本编码
		<property name="defaultEncoding" value="utf-8"></property>
	</bean> -->
	<!-- 配置静态资源的解析方法 -->
	<mvc:resources location="/js/" mapping="/js/**"></mvc:resources>
	<mvc:resources location="/css/" mapping="/css/**"></mvc:resources>
	<mvc:resources location="/images/" mapping="/images/**"></mvc:resources>
	
	<!-- 登录检查的拦截器 -->
	<!-- <mvc:interceptors>
		<mvc:interceptor>
			<mvc:mapping path="/**"/>
			需要排除的请求
			<mvc:exclude-mapping path="/user/login.action"/>
			<bean class="com.neu.controller.LoginCheckInterceptor"></bean>
		</mvc:interceptor>
	</mvc:interceptors> -->
</beans>
SqlMapConfig.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE configuration
		PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
		"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
	<!-- <environments default="mysql">
		<environment id="mysql">
			事物管理器
			<transactionManager type="jdbc"></transactionManager>
			type="pooled"表示使用连接池
			<dataSource type="pooled">
				<property name="url" value="jdbc:mysql://localhost:3306/mydb?serverTimezone=UTC&amp;allowMultiQueries=true"/>
				<property name="driver" value="com.mysql.jdbc.Driver"/>
				<property name="username" value="root"/>
				<property name="password" value="root"/>
			</dataSource>
		</environment>
	</environments> -->
	
	<!-- <mappers>
		添加映射文件配置
		<mapper resource="com/neu/dao/DeptMapper.xml" />
		<mapper resource="com/neu/dao/EmpMapper.xml" />
	</mappers> -->
</configuration>
db.properties
jdbc.url=jdbc:mysql://localhost:3306/mydb?serverTimezone=UTC
jdbc.driverClassName=com.mysql.jdbc.Driver
jdbc.username=root
jdbc.password=root

#jdbc.url=jdbc:oracle:thin:@localhost:1521:ORCL
#jdbc.driverClassName=oracle.jdbc.driver.OracleDriver
#jdbc.username=scott
#jdbc.password=tiger
log4j.properties
# Global logging configuration
#\u751F\u4EA7\u73AF\u5883\u914D\u7F6Einfo   ERROR
log4j.rootLogger=DEBUG,stdout
# MyBatis logging configuration...
log4j.logger.org.mybatis.example.BlogMapper=TRACE
# 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
源代码下载

https://download.csdn.net/download/pcbhyy/10767989

  • 20
    点赞
  • 128
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值