来自狂神
目录
- ssm简易整合
- java代码
- controller
- dao
- pojo
- service
- resource文件
- applicationContext.xml
- database.properties
- mybatis-config.xml
- spring-mvc.xml
- springservice.xml
- srping-dao.xml
- jsp文件
- addBook.jsp
- allBook.jsp
- updateBook.jsp
- web.xml
- pom.xml
- 依赖:
- 静态资源过滤
BookController类
package com.yc.controller; import com.yc.pojo.Books; import com.yc.service.BookService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import java.util.ArrayList; import java.util.List; /** * @author yc_ on 2021/11/11. * @version 1.0 */ @Controller@RequestMapping("/book") public class BookController { //controller层调用service层 @Autowired @Qualifier("bookServiceImpl") private BookService bookService; //查询全部书籍,并且返回到一个书籍展示页面 @RequestMapping("/allBook") public String list(Model model){ List<Books> list = bookService.queryAllBook(); model.addAttribute("list",list); return "allBook"; } //跳转到增加书籍页面 @RequestMapping("/toAddBook") public String toAddPaper() { return "addBook"; } //添加书籍请求 @RequestMapping("/addBook") public String addPaper(Books books) { bookService.addBook(books); return "redirect:/book/allBook"; //重定向 } //跳转到修改书籍页面 @RequestMapping("/toUpdateBook") public String toUpdateBook(Model model, int id) { Books books = bookService.queryBookByID(id); model.addAttribute("book",books ); return "updateBook"; } //修改书籍请求 @RequestMapping("/updateBook") public String updateBook(Model model, Books book) { bookService.updateBook(book); Books books = bookService.queryBookByID(book.getBookID()); model.addAttribute("books", books); return "redirect:/book/allBook"; } //删除书籍 @RequestMapping("/del/{bookID}") public String deleteBook(@PathVariable("bookID") int id) { bookService.deleteBookByID(id); return "redirect:/book/allBook"; } //查询书籍 @RequestMapping("/queryBook") public String queryBook(String queryBookName, Model model){ if ("".equals(queryBookName)){ return "redirect:/book/allBook"; } Books books = bookService.queryBookByName(queryBookName); List<Books> list = new ArrayList<>(); list.add(books); model.addAttribute("list",list); return "allBook"; } }
BookMapper类
package com.yc.dao; import com.yc.pojo.Books; import org.apache.ibatis.annotations.Param; import java.util.List; /** * @author yc_ on 2021/11/11. * @version 1.0 */ public interface BookMapper { /** * 增加一本书 * @param book * @return */ int addBook(Books book); /** * 删除一本书 * @param id * @return */ int deleteBookByID(@Param("bookID") int id); /** * 更新一本书 * @param book * @return */ int updateBook(Books book); /** * 查询一本书 * @param id * @return */ Books queryBookByID(@Param("bookID") int id); /** * 查询全部书 * @return */ List<Books> queryAllBook(); /** * 通过名字查询书籍 * @param bookName * @return */ Books queryBookByName(@Param("bookName") String bookName); }
BookMapper.xml bookMapper.对应文件
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <mapper namespace="com.yc.dao.BookMapper"> <insert id="addBook" parameterType="Books"> insert into ssmbuild.books (bookName, bookCounts, detail) values (#{bookName},#{bookCounts},#{detail}); </insert> <delete id="deleteBookByID" parameterType="int"> delete from ssmbuild.books where bookID = #{bookID} </delete> <update id="updateBook" parameterType="Books"> update ssmbuild.books set bookName = #{bookName}, bookCounts = #{bookCounts}, detail = #{detail} where bookID = #{bookID}; </update> <select id="queryBookByID" resultType="Books"> select * from ssmbuild.books where bookID = #{bookID}; </select> <select id="queryAllBook" resultType="Books"> select * from ssmbuild.books; </select> <select id="queryBookByName" resultType="Books"> select * from ssmbuild.books where bookName = #{bookName}; </select> </mapper>
Books类
package com.yc.pojo; import lombok.AllArgsConstructor; import lombok.Data; import lombok.NoArgsConstructor; /** * @author yc_ on 2021/11/10. * @version 1.0 */ @Data @AllArgsConstructor @NoArgsConstructor public class Books { private int bookID; private String bookName; private int bookCounts; private String detail; }
BookService类
package com.yc.service; import com.yc.pojo.Books; import org.apache.ibatis.annotations.Param; import java.util.List; /** * @author yc_ on 2021/11/11. * @version 1.0 */ public interface BookService { /** * 增加一本书 * @param book * @return */ int addBook(Books book); /** * 删除一本书 * @param id * @return */ int deleteBookByID(int id); /** * 更新一本书 * @param book * @return */ int updateBook(Books book); /** * 查询一本书 * @param id * @return */ Books queryBookByID(int id); /** * 查询全部书 * @return */ List<Books> queryAllBook(); /** * 通过名字查询书籍 * @param bookName * @return */ Books queryBookByName(String bookName); }
BookServiceImpl实现类
package com.yc.service; import com.yc.dao.BookMapper; import com.yc.pojo.Books; import java.util.List; /** * @author yc_ on 2021/11/11. * @version 1.0 */ public class BookServiceImpl implements BookService{ private BookMapper bookMapper; public void setBookMapper(BookMapper bookMapper) { this.bookMapper = bookMapper; } @Override public int addBook(Books book) { return bookMapper.addBook(book); } @Override public int deleteBookByID(int id) { return bookMapper.deleteBookByID(id); } @Override public int updateBook(Books book) { return bookMapper.updateBook(book); } @Override public Books queryBookByID(int id) { return bookMapper.queryBookByID(id); } @Override public List<Books> queryAllBook() { return bookMapper.queryAllBook(); } @Override public Books queryBookByName(String bookName) { return bookMapper.queryBookByName(bookName); } }
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" 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"> <import resource="srping-dao.xml"/> <import resource="springservice.xml"/> <import resource="spring-mvc.xml"/> </beans>
jdbc.driver=com.mysql.jdbc.Driver jdbc.url=jdbc:mysql://localhost:3306/ssmbuild?useSSL=true&useUnicode=true&characterEncoding=utf8 jdbc.username=root jdbc.password=123456
<?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> <!-- 配置数据源 交给spring去做--> <!-- <settings>--> <!-- <setting name="logImpl" value="STDOUT_LOGGING"/>--> <!-- </settings>--> <typeAliases> <package name="com.yc.pojo"/> </typeAliases> <mappers> <mapper class="com.yc.dao.BookMapper"/> </mappers> </configuration>
<?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.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc https://www.springframework.org/schema/mvc/spring-mvc.xsd"> <!-- 注解驱动--> <mvc:annotation-driven/> <!-- 静态资源过滤--> <mvc:default-servlet-handler/> <!-- 扫描包:com.yc.service--> <context:component-scan base-package="com.yc.controller"/> <!-- 视图解析器--> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" id="internalResourceViewResolver"> <property name="prefix" value="/WEB-INF/jsp/"/> <property name="suffix" value=".jsp"/> </bean> </beans>
<?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 https://www.springframework.org/schema/context/spring-context.xsd"> <!-- 扫描service下的包--> <context:component-scan base-package="com.yc.service"/> <!-- 将所有业务类注入到spring,配置或注解--> <bean id="bookServiceImpl" class="com.yc.service.BookServiceImpl"> <property name="bookMapper" ref="bookMapper"/> </bean> <!-- 声明式事务配置--> <bean class="org.springframework.jdbc.datasource.DataSourceTransactionManager" id="transactionManager"> <!-- 注入数据源--> <property name="dataSource" ref="dataSource"/> </bean> <!-- aop事务支持--> </beans>
<?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 https://www.springframework.org/schema/context/spring-context.xsd"> <!-- 关联数据库文件--> <context:property-placeholder location="classpath:database.properties"/> <!-- 连接池 dbcp:半自动化操作,不能自动连接 c3p0:自动化操作,自动化的加载配置文件,并且自动设置到对象中 druid hikari --> <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"> <property name="driverClass" value="${jdbc.driver}"/> <property name="jdbcUrl" value="${jdbc.url}"/> <property name="user" value="${jdbc.username}"/> <property name="password" value="${jdbc.password}"/> <!-- c3p0连接池的私有属性 --> <property name="maxPoolSize" value="30"/> <property name="minPoolSize" value="10"/> <!-- 关闭连接后不自动commit --> <property name="autoCommitOnClose" value="false"/> <!-- 获取连接超时时间 --> <property name="checkoutTimeout" value="10000"/> <!-- 当获取连接失败重试次数 --> <property name="acquireRetryAttempts" value="2"/> </bean> <!-- sqlSessionFactory--> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="dataSource"/> <!-- 绑定Mybatis配置文件--> <property name="configLocation" value="classpath:mybatis-config.xml"/> </bean> <!-- 配置dao接口扫描包,动态实现dao接口注入到spring容器--> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <!-- 注入SQLSessionFactory--> <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/> <!-- 要扫描的dao包--> <property name="basePackage" value="com.yc.dao"/> </bean> </beans>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> <%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>新增书籍</title> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <!-- 引入 Bootstrap --> <link href="https://cdn.bootcss.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet"> </head> <body> <div class="container"> <div class="row clearfix"> <div class="col-md-12 column"> <div class="page-header"> <h1> <small>新增书籍</small> </h1> </div> </div> </div> <form action="${pageContext.request.contextPath}/book/addBook" method="post"> 书籍名称:<input type="text" name="bookName" required><br><br><br> 书籍数量:<input type="text" name="bookCounts" required><br><br><br> 书籍详情:<input type="text" name="detail" required><br><br><br> <input type="submit" value="添加"> </form> </div> </body> </html>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> <%@ page contentType="text/html;charset=UTF-8" language="java" isELIgnored="false" %> <html> <head> <title>书籍展示</title> <link href="https://cdn.bootcss.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet"> <%-- bootstrap美化界面--%> <style> a{ text-decoration: none; color: black; font-size: 18px; } </style> </head> <body> <div class="container"> <div class="row clearfix"> <div class="col-md-12 column"> <div class="page-header"> <h1> <small>书籍列表 - 显示所有数据</small> </h1> </div> </div> <div class="row"> <%-- toAddBook--%> <div class="col-md-4 column"> <a class="btn btn-primary" href="${pageContext.request.contextPath}/book/toAddBook">新增书籍</a> <a class="btn btn-primary" href="${pageContext.request.contextPath}/book/allBook">显示全部书籍</a> </div><div class="col-md-4 column"></div> <div class="col-md-4 column"> <%--查询书籍--%> <form action="${pageContext.request.contextPath}/book/queryBook" method="post" style="right: auto" class="form-inline"> <input type="text" name="queryBookName" class="form-control" placeholder="请输入要查询书籍的名称" style="width: 200px"> <input type="submit" value="查询" class="btn btn-primary"> </form> </div> </div> </div> <div class="row clearfix"> <div class="col-md-12 column"> <table class="table table-hover table-striped"> <thead> <tr> <th>书籍编号</th> <th>书籍名称</th> <th>书籍数量</th> <th>书籍详情</th> <th>操作</th> </tr> </thead> <%-- 书籍从数据库中查询出来,从这个list中遍历出来 foreach--%> <tbody> <c:if test="${not empty list}" > <c:forEach var="book" items="${list}"> <tr> <td>${book.bookID}</td> <td>${book.bookName}</td> <td>${book.bookCounts}</td> <td>${book.detail}</td> <td> <a href="${pageContext.request.contextPath}/book/toUpdateBook?id=${book.bookID}">修改</a> | <a href="${pageContext.request.contextPath}/book/del/${book.bookID}">删除</a> </td> </tr> </c:forEach> </c:if> </tbody> </table> </div> </div> </div> </body> </html>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> <%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>修改信息</title> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <!-- 引入 Bootstrap --> <link href="https://cdn.bootcss.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet"> </head> <body> <div class="container"> <div class="row clearfix"> <div class="col-md-12 column"> <div class="page-header"> <h1> <small>修改信息</small> </h1> </div> </div> </div> <form action="${pageContext.request.contextPath}/book/updateBook" method="post"> <input type="hidden" name="bookID" value="${book.bookID}"/> 书籍名称:<input type="text" name="bookName" value="${book.bookName}"/> 书籍数量:<input type="text" name="bookCounts" value="${book.bookCounts}"/> 书籍详情:<input type="text" name="detail" value="${book.detail}"/> <input type="submit" value="提交"/> </form> </div> </body> </html>
<?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"> <!--DispatcherServlet--> <servlet> <servlet-name>springMVC</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:applicationContext.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>springMVC</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> <!-- 乱码过滤--> <filter> <filter-name>encodingFilter</filter-name> <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class> <init-param> <param-name>encoding</param-name> <param-value>utf-8</param-value> </init-param> </filter> <filter-mapping> <filter-name>encodingFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <session-config> <session-timeout>15</session-timeout> </session-config> </web-app>
<dependencies> <!--Junit--> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> </dependency> <!--数据库驱动--> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>8.0.25</version> </dependency> <!-- 数据库连接池 --> <dependency> <groupId>com.mchange</groupId> <artifactId>c3p0</artifactId> <version>0.9.5.2</version> </dependency> <!--Servlet - JSP --> <dependency> <groupId>javax.servlet</groupId> <artifactId>servlet-api</artifactId> <version>2.5</version> </dependency> <dependency> <groupId>javax.servlet.jsp</groupId> <artifactId>jsp-api</artifactId> <version>2.2</version> </dependency> <dependency> <groupId>javax.servlet</groupId> <artifactId>jstl</artifactId> <version>1.2</version> </dependency> <!--Mybatis--> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>3.5.7</version> </dependency> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis-spring</artifactId> <version>2.0.6</version> </dependency> <!--Spring--> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>5.3.12</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-jdbc</artifactId> <version>5.3.12</version> </dependency> <!-- lombok--> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <version>1.18.12</version> </dependency> </dependencies>
<build> <resources> <resource> <directory>src/main/java</directory> <includes> <include>**/*.properties</include> <include>**/*.xml</include> </includes> <filtering>false</filtering> </resource> <resource> <directory>src/main/resources</directory> <includes> <include>**/*.properties</include> <include>**/*.xml</include> </includes> <filtering>false</filtering> </resource> </resources> </build>