赞
踩
数据库: MySql5 .7
JDK: 1.8
lib:
commons-logging-1.2.jar
spring-webmvc-5.2.8.RELEASE.jar
spring-web-5.2.8.RELEASE.jar
spring-tx-5.2.8.RELEASE.jar
spring-jdbc-5.2.8.RELEASE.jar
spring-expression-5.2.8.RELEASE.jar
spring-core-5.2.8.RELEASE.jar
spring-context-5.2.8.RELEASE.jar
spring-beans-5.2.8.RELEASE.jar
spring-aop-5.2.8.RELEASE.jar
mybatis-spring-2.0.1.jar
mysql-connector-java-8.0.16.jar
mybatis-3.5.2.jar
junit-4.12.jar
jstl-1.2.jar
druid-1.1.20.jar
Book.java
package com.ly.pojo; //实体类 public class Book { private Integer id; //图书id private String name; //图书名称 private String publish; //出版社 private String author; //作者 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 getPublish() { return publish; } public void setPublish(String publish) { this.publish = publish; } public String getAuthor() { return author; } public void setAuthor(String author) { this.author = author; } }
BookMapper.java
package com.ly.mapper; import com.ly.pojo.Book; import java.util.List; public interface BookMapper { public Book findBookById(Integer id); public List<Book> findBookAll(); public int insertBook(Book book); public int updateBook(Book book); public int deleteBook(Integer id); }
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.ly.mapper.BookMapper"> <select id="findBookById" parameterType="int" resultType="book"> select * from book where id = #{id} </select> <select id="findBookAll" parameterType="list" resultType="book"> select * from book </select> <insert id="insertBook" parameterType="book"> insert into book (name, publish, author) values (#{name}, #{publish}, #{author}) </insert> <update id="updateBook" parameterType="book"> update book set name = #{name}, publish = #{publish}, author = #{author} where id = #{id} </update> <delete id="deleteBook" parameterType="book"> delete from book where id = #{id} </delete> </mapper>
BookService .java
package com.ly.service; import com.ly.pojo.Book; import java.util.List; public interface BookService { public Book findBookById(Integer id); public List<Book> findBookAll(); public int insertBook(Book book); public int updateBook(Book book); public int deleteBook(Integer id); }
BookServiceImpl .java
package com.ly.service.impl; import com.ly.mapper.BookMapper; import com.ly.pojo.Book; import com.ly.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 private BookMapper bookMapper; @Override public Book findBookById(Integer id) { return bookMapper.findBookById(id); } @Override public List<Book> findBookAll() { return bookMapper.findBookAll(); } @Override public int insertBook(Book book) { return bookMapper.insertBook(book); } @Override public int updateBook(Book book) { return bookMapper.updateBook(book); } @Override public int deleteBook(Integer id) { return bookMapper.deleteBook(id); } }
BookController .java
package com.ly.controller; import com.ly.pojo.Book; import com.ly.service.BookService; 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 java.util.List; @Controller public class BookController { @Autowired private BookService bookService; @RequestMapping("/findBookById") public String findBookById(Integer id, Model model) { Book book = bookService.findBookById(id); if (book != null) { model.addAttribute("book", book); return "book.jsp"; } else { model.addAttribute("msg", "没有找到该书籍"); return "result.jsp"; } } @RequestMapping("/findBookAll") public String findBookAll(Model model) { List<Book> bookList = bookService.findBookAll(); model.addAttribute("bookList", bookList); return "bookList.jsp"; } @RequestMapping("/insertBook") public String insertBook(Model model, Book book) { int i = bookService.insertBook(book); if (i > 0) { model.addAttribute("msg", "添加成功"); } else { model.addAttribute("msg", "添加失败"); } return "result.jsp"; } @RequestMapping("/updateBookId") public String updateBookId(Book book, Model model) { model.addAttribute("book", book); return "updateBook.jsp"; } @RequestMapping("/updateBook") public String updateBook(Model model, Book book) { int i = bookService.updateBook(book); if (i > 0) { model.addAttribute("msg", "修改成功"); } else { model.addAttribute("msg", "修改失败"); } return "result.jsp"; } @RequestMapping("/deleteBook") public String deleteBook(Model model, Integer id) { int i = bookService.deleteBook(id); if (i > 0) { model.addAttribute("msg", "删除成功"); } else { model.addAttribute("msg", "删除失败"); } return "result.jsp"; } }
application-service.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.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <!--引入属性文件--> <context:property-placeholder location="classpath:jdbc.properties"/> <!--创建数据源--> <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource"> <property name="driverClassName" value="${jdbc.driverClassName}"></property> <property name="url" value="${jdbc.url}"></property> <property name="username" value="${jdbc.username}"></property> <property name="password" value="${jdbc.password}"></property> <!--配置数据库连接池参数--> <property name="initialSize" value="${initialSize}"/> <property name="maxActive" value="${maxActive}"/> <property name="maxIdle" value="${maxIdle}"/> <property name="minIdle" value="${minIdle}"/> <property name="maxWait" value="${maxWait}"/> <property name="removeAbandonedTimeout" value="${removeAbandonedTimeout}"/> <property name="removeAbandoned" value="${removeAbandoned}"/> </bean> <!--创建SqlSessionFactory对象--> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <!--注入数据源对象--> <property name="dataSource" ref="dataSource"></property> <property name="configLocation" value="classpath:mybatis-config.xml"/> </bean> <!--扫描Dao包,创建动态代理对象, 会自动存储到spring IOC容器中--> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <!--指定要扫描的dao的包--> <property name="basePackage" value="com.ly.mapper"></property> </bean> </beans>
application-dao.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.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<!--开启注解扫描, 扫描包-->
<context:component-scan base-package="com.ly.service"/>
</beans>
jdbc.properties
jdbc.driverClassName=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/library?useUnicode=true&characterEncoding=utf-8&serverTimezone=Asia/Shanghai
jdbc.username=root
jdbc.password=123456
minIdle=45
maxIdle=50
initialSize=5
maxActive=100
maxWait=100
removeAbandoned=true
removeAbandonedTimeout=180
mybatis-config.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>
<typeAliases>
<package name="com.ly.pojo"/>
</typeAliases>
</configuration>
spring-mvc.xml
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <!-- 配置要扫描的包 --> <context:component-scan base-package="com.ly.controller"/> <!-- 配置注解驱动 --> <mvc:annotation-driven/> </beans>
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd" version="4.0"> <!--配置文件加载--> <context-param> <param-name>contextConfigLocation</param-name> <!-- 加载所有以application-开头的xml文件 --> <param-value>classpath:application-*.xml</param-value> </context-param> <!--容器加载的监听器,作用就是启动Web容器时,自动装配ApplicationContext的配置信息,配合它一起使用的,经常是context-param--> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <!-- Spring MVC 前端控制器 --> <servlet> <servlet-name>DispatcherServlet</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <!--初始化参数 --> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:spring-mvc.xml</param-value> </init-param> <!--项目启动时候,初始化前端控制器 --> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>DispatcherServlet</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> <welcome-file-list> <welcome-file>home.jsp</welcome-file> </welcome-file-list> </web-app>
home.jsp
<%-- Created by IntelliJ IDEA. User: ther Date: 2022/11/6 Time: 9:17 To change this template use File | Settings | File Templates. --%> <%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>首页</title> </head> <body> <a href="addBook.jsp">添加图书</a><br> <form action="findBookById" method="get"> <input type="number" name="id"> <input type="submit" value="根据ID查询图书"> </form> <a href="findBookAll">查询所有图书</a><br> </body> </html>
result.jsp
<%-- Created by IntelliJ IDEA. User: ther Date: 2022/11/6 Time: 19:42 To change this template use File | Settings | File Templates. --%> <%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>操作结果</title> </head> <body> ${msg} <a href="home.jsp">返回首页</a> </body> </html>
book.jsp
<%-- Created by IntelliJ IDEA. User: ther Date: 2022/11/6 Time: 20:01 To change this template use File | Settings | File Templates. --%> <%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>图书信息列表</title> </head> <body> <a href="home.jsp">返回首页</a> <table border="1"> <tr> <th>图书ID</th> <th>图书名称</th> <th>出版社</th> <th>作者</th> <th>操作</th> </tr> <tr> <td>${book.id}</td> <td>${book.name}</td> <td>${book.publish}</td> <td>${book.author}</td> <td> <a href="deleteBook?id=${book.id}">删除</a> <a href="updateBookId?id=${book.id}&name=${book.name}&publish=${book.publish}&author=${book.author}">修改</a> </td> </tr> </table> </body> </html>
bookList.jsp
<%-- Created by IntelliJ IDEA. User: ther Date: 2022/11/6 Time: 20:01 To change this template use File | Settings | File Templates. --%> <%@ page contentType="text/html;charset=UTF-8" language="java" %> <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> <html> <head> <title>图书信息列表</title> </head> <body> <a href="home.jsp">返回首页</a> <table border="1"> <tr> <th>图书ID</th> <th>图书名称</th> <th>出版社</th> <th>作者</th> <th>操作</th> </tr> <c:forEach items="${bookList}" var="item"> <tr> <td>${item.id}</td> <td>${item.name}</td> <td>${item.publish}</td> <td>${item.author}</td> <td> <a href="deleteBook?id=${item.id}">删除</a> <a href="updateBookId?id=${item.id}&name=${item.name}&publish=${item.publish}&author=${item.author}">修改</a> </td> </tr> </c:forEach> </table> </body> </html>
addBook.jsp
<%-- Created by IntelliJ IDEA. User: ther Date: 2022/11/6 Time: 9:15 To change this template use File | Settings | File Templates. --%> <%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>添加图书</title> </head> <body> <a href="home.jsp">返回首页</a> <form action="insertBook" method="get"> <div> <span>图书名称:</span> <input type="text" name="name" id="name"> </div> <div> <span>图书出版社:</span> <input type="text" name="publish" id="publish"> </div> <div> <span>图书作者:</span> <input type="text" name="author" id="author"> </div> <div> <input type="submit" value="添加"> <input type="reset" value="重置"> </div> </form> </body> </html>
updateBook.jsp
<%-- Created by IntelliJ IDEA. User: ther Date: 2022/11/6 Time: 9:19 To change this template use File | Settings | File Templates. --%> <%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>修改图书</title> </head> <body> <a href="home.jsp">返回首页</a> <form action="updateBook" method="get"> <div> <span>图书编号:</span> <input type="text" readonly name="id" id="id" value="${book.id}"> </div> <div> <span>图书名称:</span> <input type="text" name="name" id="name" value="${book.name}"> </div> <div> <span>图书出版社:</span> <input type="text" name="publish" id="publish" value="${book.publish}"> </div> <div> <span>图书作者:</span> <input type="text" name="author" id="author" value="${book.author}"> </div> <div> <input type="submit" value="修改"> <input type="reset" value="重置"> </div> </form> </body> </html>
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。