ssm整合——设置修改/删除书籍功能

1、增加修改和删除的功能

allBook.jsp

<c:forEach var="book" items="${list}">
    <tr>
        <td>${book.bookid}</td>
        <td>${book.bookname}</td>
        <td>${book.bookcount}</td>
        <td>${book.dateail}</td>
        <td>
            <a href="${pageContext.request.contextPath}/book/updateBooks?id=${book.bookid}">修改</a>
            &nbsp; | &nbsp;
            <a href="${pageContext.request.contextPath}/book/deleteBook/${book.bookid}">删除</a>
        </td>
    </tr>
</c:forEach>

修改书籍

BookController.java

//修改书籍
@RequestMapping("/updateBook")
public String updatebook(Books books){
    System.out.println("updateboos=>"+books);
    int i=bookService.updatebook(books);
    if (i>0){
        System.out.println("添加成功"+books);

    }
    return "redirect:/book/allBook";
}

updateBooks.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>修改书籍</title>
    <link href="https://cdn.staticfile.org/twitter-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>
                    <samp>修改数据</samp>
                </h1>
            </div>
        </div>
        <form action="${pageContext.request.contextPath}/book/updateBook" method="post">
            <%--出现的问题,提交了sql请求,,但是是修改失败,再次考虑,是事务的问题,配备完毕依旧失败
                看一下sql语句能否执行成功,发现sql执行失败,修改为未完成
                解决方法:
                        前端传递隐藏域--%>
            <input type="hidden" name="bookid" value="${sbook.bookid}">

            <div class="form-group">
                <label >书籍名称:</label>
                <input type="text" name="bookname" class="form-control" value="${sbook.bookname}" required  >
            </div>
            <div class="form-group">
                <label >书籍数量:</label>
                <input type="text" name="bookcount" class="form-control" value="${sbook.bookcount}" required >
            </div>
            <div class="form-group">
                <label >书籍描述:</label>
                <input type="text" name="dateail" class="form-control" value="${sbook.dateail}" required >
            </div>
            <div class="form-group">
                <input type="submit"  class="btn btn-success form-control" value="修改">
            </div>
        </form>

    </div>

</div>
</body>
</html>

spring-service.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:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context-4.2.xsd
        http://www.springframework.org/schema/aop
        https://www.springframework.org/schema/aop/spring-aop.xsd
        http://www.springframework.org/schema/tx
        http://www.springframework.org/schema/tx/spring-tx.xsd">

    <!--1、扫描service下的包-->
    <context:component-scan base-package="com.zy.service"/>

    <!--2、将我们的所有业务类,注入到spring,可以通过配置或者通过注解实现-->
    <bean id="BookServiceImpl" class="com.zy.service.BookServiceImpl">
        <property name="bookMapper" ref="bookMapper"/>
    </bean>

    <!--3、声明式事务配置-->
    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <!--注入数据源-->
        <property name="dataSource" ref="dataSource"/>
    </bean>
    <!--4、aop事务支持-->
    <!--结合aop,实现事务的织入-->
    <!--配置事务通知:-->

    <!-- 配置事务管理器 -->
    <bean id="TransactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource"></property>
    </bean>
    <!-- 配置事务属性 -->
    <tx:advice id="txAdvice" transaction-manager="TransactionManager">
        <tx:attributes>
            <tx:method name="*" propagation="REQUIRED"/>
        </tx:attributes>
    </tx:advice>
    <!--配置事务切入-->
    <aop:config>
        <aop:pointcut id="txPointCut" expression="execution(* com.zy.dao.*.*(..))"/>
        <aop:advisor advice-ref="txAdvice" pointcut-ref="txPointCut"/>
    </aop:config>
</beans>

在设置一个日志,更好的看到后台输出的sql语句

mybatis-config.xml

<!--配置日志-->
<settings>
    <setting name="logImpl" value="STDOUT_LOGGING"/>
</settings>

删除书籍

BookController.java

//删除书籍
@RequestMapping("/deleteBook/{bookid}")
public String deletebook(@PathVariable("bookid") int id){
    bookService.deletebook(id);
    return "redirect:/book/allBook";

}

显示结果:

ssm整合——设置修改/删除书籍功能

 

上一篇:Java顺序结构综合练习一之银行复利计息收益计算


下一篇:python调用机器喇叭发出蜂鸣声(Beep)的代码