SSM整合加简单的增删改查
- 创建项目
-
添加web项目文件夹
-
在pom.xml 添加依赖
<dependencies> <!-- junit 测试--> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.13</version> <scope>test</scope> </dependency> <!-- mysql--> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>8.0.26</version> </dependency> <!-- c3p0 连接池--> <dependency> <groupId>com.mchange</groupId> <artifactId>c3p0</artifactId> <version>0.9.5.2</version> </dependency> <!-- lombok --> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <version>1.18.24</version> </dependency> <!-- servlet --> <dependency> <groupId>javax.servlet</groupId> <artifactId>servlet-api</artifactId> <version>2.5</version> <scope>provided</scope> </dependency> <!-- servlet-jsp --> <dependency> <groupId>javax.servlet.jsp</groupId> <artifactId>jsp-api</artifactId> <version>2.2</version> <scope>provided</scope> </dependency> <!-- jstl --> <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.10</version> </dependency> <!-- mybatis-spring --> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis-spring</artifactId> <version>2.1.0</version> </dependency> <!-- springmvc--> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>5.3.21</version> </dependency> <!-- spring jdbc :事务--> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-jdbc</artifactId> <version>5.3.21</version> </dependency> <!-- aop--> <dependency> <groupId>org.aspectj</groupId> <artifactId>aspectjweaver</artifactId> <version>1.9.5</version> </dependency> </dependencies> <!-- 导入依赖 配置 --> <build> <resources> <resource> <directory>src/main/java</directory> <includes> <include>**/*.yml</include> <include>**/*.xml</include> <include>**/*.properties</include> </includes> <filtering>true</filtering> </resource> <resource> <directory>src/main/resources</directory> <includes> <include>**/*.yml</include> <include>**/*.xml</include> <include>**/*.properties</include> </includes> <filtering>true</filtering> </resource> </resources> </build>
-
配置文件
-
database.properties: mysql信息配置文件
jdbc.driver=com.mysql.cj.jdbc.Driver jdbc.url=jdbc:mysql://localhost:3306/text?userUnicode=true&characterEncoding=utf8 jdb.username=root jdb.password=123456
-
applictionContext.xml : spring配置文件
<?xml version="1.0" encoding="UTF8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans https://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/tx https://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop.xsd "> <!-- 关联数据文件--> <context:property-placeholder location="classpath:database.properties"></context:property-placeholder> <!-- 配置数据库连接池--> <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"> <!-- 关联数据库连接池属性 --> <property name="driverClass" value="${jdbc.driver}"></property> <property name="jdbcUrl" value="${jdbc.url}"></property> <property name="user" value="${jdb.username}"></property> <property name="password" value="${jdb.password}"></property> <!-- c3p0 配置--> <!-- 最大连接池 和 最小连接池 --> <property name="maxPoolSize" value="30"></property> <property name="minPoolSize" value="10"></property> </bean> <!-- 配置sqlSessionFactory--> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <!-- 注入数据库连接池 --> <property name="dataSource" ref="dataSource"></property> <!-- 配置mybatis文件--> <property name="configLocation" value="classpath:mybatis-config.xml"></property> <!-- 指定mybatis映射文件--> <property name="mapperLocations" value="classpath*:map/*.xml"></property> </bean> <!-- 扫描mapper--> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <!-- 注入sqlSessionFactory--> <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property> <property name="basePackage" value="com.L.mapper"></property> </bean> <!-- 扫描service 使注解生效--> <context:component-scan base-package="com.L.service"></context:component-scan> <!-- 事务 --> <bean id="txManage" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource"></property> </bean> <!-- 配置事务的通知--> <tx:advice id="txAdvice" transaction-manager="txManage"> <tx:attributes> <tx:method name="*" propagation="REQUIRED"/> </tx:attributes> </tx:advice> <!--配置aop事务--> <aop:config> <aop:pointcut id="txPointcut" expression="execution(* com.L.service.*.*(..))"/> <aop:advisor advice-ref="txAdvice" pointcut-ref="txPointcut"></aop:advisor> </aop:config> </beans>
-
springmvc.xml:springmvc配置文件
<?xml version="1.0" encoding="UTF8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:context="http://www.springframework.org/schema/context" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/mvc https://www.springframework.org/schema/mvc/spring-mvc.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd "> <!-- 开启 注解支持--> <mvc:annotation-driven></mvc:annotation-driven> <!-- 静态资源--> <mvc:default-servlet-handler></mvc:default-servlet-handler> <!-- 扫描controller--> <context:component-scan base-package="com.L.controller"></context:component-scan> <mvc:resources location="/static/" mapping="/static/**" /> <!-- 视图解析器--> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/jsp/"></property> <property name="suffix" value=".jsp"></property> </bean> </beans>
-
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> </configuration>
-
beans.xml: 总的配置文件
<?xml version="1.0" encoding="UTF8"?> <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 https://www.springframework.org/schema/beans/spring-beans.xsd "> <import resource="applicationContext.xml"></import> <import resource="springmvc.xml"></import> </beans>
-
-
配置web.xml 文件
<?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"> <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:beans.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>utf8</param-value> </init-param> </filter> <filter-mapping> <filter-name>encodingFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> </web-app>
-
jsp文件:页面
-
index.jsp
<%-- User: LIJIAN Date: 2023/12/4 Time: 11:32 To change this template use File | Settings | File Templates. --%> <%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>$Title$</title> </head> <body> <h3> <a href="${pageContext.request.contextPath}/book/allBook">点击</a> </h3> </body> </html>
-
allBook.jsp
<%-- User: LIJIAN Date: 2023/12/4 Time: 12:21 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> <link rel="stylesheet" href="${pageContext.request.contextPath}/static/bootstrap/css/bootstrap.min.css"/> <body> <div class="container"> <div class="row clearfix"> <div class="row clearfix"> <div class="col-md-12 column"> <div class="page-item"> <h1> <small>书籍列表----显示所有书籍</small> </h1> </div> </div> <div> <div> <a class="btn btn-primary" href="${pageContext.request.contextPath}/book/toAddBook">增加书籍</a> </div> </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> <taody> <c:forEach var="book" items="${list}"> <tr> <td>${book.bookID}</td> <td>${book.bookName}</td> <td>${book.bookCount}</td> <td>${book.detail}</td> <td> <a class="btn btn-danger" href="${pageContext.request.contextPath}/book/toUpdateBook/${book.bookID}">修改</a> <a class="btn btn-danger" href="${pageContext.request.contextPath}/book/deleteBook/${book.bookID}">删除</a> </td> </tr> </c:forEach> </taody> </table> </div> </div> </div> </body> </html>
-
addBook.jsp
<%-- User: LIJIAN Date: 2023/12/4 Time: 12:50 To change this template use File | Settings | File Templates. --%> <%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>添加书籍</title> </head> <link rel="stylesheet" href="${pageContext.request.contextPath}/static/bootstrap/css/bootstrap.min.css"/> <body> <div class="container"> <div class="row clearfix"> <div class="col-md-12 column"> <div class="page-item"> <h1> <small> 新增书籍 </small> </h1> </div> </div> <form action="${pageContext.request.contextPath}/book/addBook" method="post"> <div class="form-group"> <label>书籍名称:</label> <input type="text" name="bookName" class="form-control" required> </div> <div class="form-group"> <label>书籍数量:</label> <input type="text" name="bookCount" class="form-control" required> </div> <div class="form-group"> <label>书籍描述:</label> <input type="text" name="detail" class="form-control" required> </div> <div class="form-group"> <input type="submit" class="form-control" value="添加"> </div> </form> </div> </div> </body> </html>
-
updateBook.jsp
<%-- User: LIJIAN Date: 2023/12/4 Time: 12:58 To change this template use File | Settings | File Templates. --%> <%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>更新书籍</title> </head> <link rel="stylesheet" href="${pageContext.request.contextPath}/static/bootstrap/css/bootstrap.min.css"/> <body> <div class="container"> <div class="row clearfix"> <div class="col-md-12 column"> <div class="page-item"> <h1> <small> 修改书籍 </small> </h1> </div> </div> <form action="${pageContext.request.contextPath}/book/updateBook" method="post"> <input type="hidden" name="bookID" class="form-control" value="${book.bookID}"> <div class="form-group"> <label>书籍名称:</label> <input type="text" name="bookName" class="form-control" required value="${book.bookName}"> </div> <div class="form-group"> <label>书籍数量:</label> <input type="text" name="bookCount" class="form-control" required value="${book.bookCount}"> </div> <div class="form-group"> <label>书籍描述:</label> <input type="text" name="detail" class="form-control" required value="${book.detail}"> </div> <div class="form-group"> <input type="submit" class="form-control" value="修改"> </div> </form> </div> </div> </body> </html>
-
-
代码部分
-
entity:实体类
package com.L.entity; import lombok.AllArgsConstructor; import lombok.Data; import lombok.NoArgsConstructor; /** * @Author LIJIAN * @Date 2023/12/4 13:01 */ //全参构造 @AllArgsConstructor // 无参构造 @NoArgsConstructor // set get @Data public class Book { private int bookID; private String bookName; private int bookCount; private String detail; }
-
mapper
-
BookMapper
package com.L.mapper; import com.L.entity.Book; import org.springframework.stereotype.Repository; import java.util.List; /** * @Author LIJIAN * @Date 2023/12/4 13:08 */ @Repository public interface BookMapper { List<Book> listBook(); int addBook(Book book); int delete(int id); Book upBookID(int id); int upBook(Book book); }
-
-
resources/map
-
BookMapper.xml
<?xml version="1.0" encoding="UTF8" ?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <mapper namespace="com.L.mapper.BookMapper"> <delete id="delete"> delete from ssm_test where bookID = #{id}; </delete> <select id="listBook" resultType="com.L.entity.Book"> select * from ssm_test </select> <insert id="addBook"> insert into ssm_test(bookName, bookCount, detail) value (#{bookName},#{bookCount},#{detail}) </insert> <select id="upBookID" resultType="com.L.entity.Book"> select * from ssm_test where bookID = #{id} </select> <update id="upBook"> update ssm_test set bookName=#{bookName}, bookCount=#{bookCount}, detail=#{detail} where bookID = #{bookID} </update> </mapper>
-
-
service
- BookService
package com.L.service; import com.L.entity.Book; import java.util.List; /** * @Author LIJIAN * @Date 2023/12/4 13:11 */ public interface BookService { List<Book> list(); int addBook(Book book); int delete(int id); Book upBookID(int id); int upBook(Book book); }
-
BookServiceImpl
package com.L.service.impl; import com.L.entity.Book; import com.L.mapper.BookMapper; import com.L.service.BookService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import java.util.List; /** * @Author LIJIAN * @Date 2023/12/4 13:12 */ @Service public class BookServiceImpl implements BookService { @Autowired private BookMapper bookMapper; @Override public List<Book> list() { List<Book> list = bookMapper.listBook(); return list; } @Override public int addBook(Book book) { int i = bookMapper.addBook(book); return i; } @Override public int delete(int id) { int i= bookMapper.delete(id); return i; } @Override public Book upBookID(int id) { return bookMapper.upBookID(id); } @Override public int upBook(Book book) { return bookMapper.upBook(book); } }
-
controller
-
BookController
package com.L.controller; import com.L.entity.Book; import com.L.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.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import java.util.List; /** * @Author LIJIAN * @Date 2023/12/4 13:15 */ @Controller @RequestMapping("/book") public class BookController { @Autowired private BookService bookService; @RequestMapping("/allBook") public String list(Model model) { List<Book> list = bookService.list(); model.addAttribute("list", list); return "allBook"; } @RequestMapping("/toAddBook") public String preAdd() { return "addBook"; } @RequestMapping("/addBook") public String addBook(Book book) { int i = bookService.addBook(book); return "redirect:/book/allBook"; } @RequestMapping("/deleteBook/{bookID}") public String deleteBook(@PathVariable int bookID) { int i = bookService.delete(bookID); return "redirect:/book/allBook"; } @RequestMapping("/toUpdateBook/{bookID}") public String UpdateBookID(@PathVariable int bookID, Model model) { Book book = bookService.upBookID(bookID); model.addAttribute("book", book); return "updateBook"; } @RequestMapping("/updateBook") public String UpdateBook(Book book) { int i = bookService.upBook(book); return "redirect:/book/allBook"; } }
-
-
-
数据库设计
-效果