码迷,mamicode.com
首页 > 其他好文 > 详细

【SSM】(一)SSM整合-增删改查书籍

时间:2021-02-24 12:59:25      阅读:0      评论:0      收藏:0      [点我收藏+]

标签:点击   lse   oar   select   factor   编号   显示   localhost   eps   

一、Mybatis层

  • 数据库

    CREATE DATABASE `ssmbuild`;
    
    USE `ssmbuild`;
    
    DROP TABLE IF EXISTS `books`;
    CREATE TABLE `books` (
    `bookID` INT(10) NOT NULL AUTO_INCREMENT COMMENT ‘书id‘,
    `bookName` VARCHAR(100) NOT NULL COMMENT ‘书名‘,
    `bookCounts` INT(11) NOT NULL COMMENT ‘数量‘,
    `detail` VARCHAR(200) NOT NULL COMMENT ‘描述‘,
    KEY `bookID` (`bookID`)
    ) ENGINE=INNODB DEFAULT CHARSET=utf8
    
    
    INSERT  INTO `books`(`bookID`,`bookName`,`bookCounts`,`detail`)VALUES
    (1,‘Java‘,1,‘从入门到放弃‘),
    (2,‘MySQL‘,10,‘从删库到跑路‘),
    (3,‘Linux‘,5,‘从进门到进牢‘);
    
  • pom.xml

    <dependencies>
            <dependency>
                <groupId>junit</groupId>
                <artifactId>junit</artifactId>
                <version>4.11</version>
            </dependency>
            <dependency>
                <groupId>mysql</groupId>
                <artifactId>mysql-connector-java</artifactId>
                <version>5.1.47</version>
            </dependency>
            <!-- 数据库连接池:c3p0 -->
            <dependency>
                <groupId>com.mchange</groupId>
                <artifactId>c3p0</artifactId>
                <version>0.9.5.2</version>
            </dependency>
            <!-- Servlet Jsp Jstl-->
            <dependency>
                <groupId>javax.servlet</groupId>
                <artifactId>javax.servlet-api</artifactId>
                <version>3.1.0</version>
                <scope>provided</scope>
            </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.2</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.2.12.RELEASE</version>
            </dependency>
            <!-- Spring操作数据库,需要spring-jdbc -->
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-jdbc</artifactId>
                <version>5.2.12.RELEASE</version>
            </dependency>
            <!-- AspectJ Weaver -->
            <dependency>
                <groupId>org.aspectj</groupId>
                <artifactId>aspectjweaver</artifactId>
                <version>1.9.6</version>
            </dependency>
    
            <!--lombok -->
            <dependency>
                <groupId>org.projectlombok</groupId>
                <artifactId>lombok</artifactId>
                <version>1.18.12</version>
                <scope>provided</scope>
            </dependency>
        </dependencies>
    
    
        <!--在build中配置resources,防止资源导出失败的问题-->
        <build>
            <resources>
                <resource>
                    <directory>src/main/resources</directory>
                    <includes>
                        <include>**/*.properties</include>
                        <include>**/*.xml</include>
                    </includes>
                    <filtering>false</filtering>
                </resource>
                <resource>
                    <directory>src/main/java</directory>
                    <includes>
                        <include>**/*.properties</include>
                        <include>**/*.xml</include>
                    </includes>
                    <filtering>false</filtering>
                </resource>
            </resources>
        </build>
    
  • database.properties

    jdbc.driver=com.mysql.jdbc.Driver
    # 如果使用的是MySQL 8.0+,需要增加时区的配置;&serverTimezone=Asia/Shanghai
    jdbc.url=jdbc:mysql://localhost:3306/ssmbuild?useSSL=true&useUnicode=true&characterEncoding=utf8
    jdbc.username=root
    jdbc.password=root
    
  • mybatis-config.xml:别名、日志、配置xxxMapper(数据源在spring-dao.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>
    
        <settings>
            <setting name="logImpl" value="STDOUT_LOGGING"/>
        </settings>
    
        <typeAliases>
            <package name="com.musecho.pojo"/>
        </typeAliases>
    
        <mappers>
            <mapper class="com.musecho.dao.BookMapper"/>
        </mappers>
    </configuration>
    

  • Books

    @Data
    @AllArgsConstructor
    @NoArgsConstructor
    public class Books {
    
        private int bookID;
        private String bookName;
        private int bookCounts;
        private String detail;
    }
    
  • BookMapper

    public interface BookMapper {
    
        int addBook(Books books);
    
        int deleteBookById(@Param("bookId") int id);
    
        int updateBook(Books books);
    
        Books queryBookById(@Param("bookId")int id);
    
        List<Books> queryAllBooks();
    
    }
    
  • BookMapper.xml

    <?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>
    
        <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" parameterType="int" resultType="books">
            select * from ssmbuild.books
            where bookID=#{bookId};
        </select>
    
        <select id="queryAllBooks" resultType="books">
            select * from ssmbuild.books;
        </select>
    
    </mapper>
    
  • BookService

    public interface BookService {
    
        int addBook(Books books);
    
        int deleteBookById(int id);
    
        int updateBook(Books books);
    
        Books queryBookById(int id);
    
        List<Books> queryAllBooks();
    
    }
    
  • BookServiceImpl

    public class BookServiceImpl implements BookService {
    
        private BookMapper bookMapper;
    
        public void setBookMapper(BookMapper bookMapper) {
            this.bookMapper = bookMapper;
        }
    
        public int addBook(Books books) {
            return bookMapper.addBook(books);
        }
    
        public int deleteBookById(int id) {
            return bookMapper.deleteBookById(id);
        }
    
        public int updateBook(Books books) {
            return bookMapper.updateBook(books);
        }
    
        public Books queryBookById(int id) {
            return bookMapper.queryBookById(id);
        }
    
        public List<Books> queryAllBooks() {
            return bookMapper.queryAllBooks();
        }
    
    }
    


二、Spring层

  • spring-dao.xml

    配置dao接口动态扫描包:将所有dao类注入到Spring容器,并注入sqlSessionFactory。

    就不用写xxxMapper接口的实现类(通过sqlSession获取mapper,返回操作结果)了。

    <?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
            https://www.springframework.org/schema/beans/spring-beans.xsd
            http://www.springframework.org/schema/context
            https://www.springframework.org/schema/context/spring-context.xsd">
    
        <!--1. 关联数据库配置文件-->
        <context:property-placeholder location="classpath:database.properties"/>
    
        <!--2.连接池
            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>
    
        <!-- 3.sqlSessionFactory-->
        <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
            <property name="dataSource" ref="dataSource"/>
            <!-- 绑定Mybatis的配置文件-->
            <property name="configLocation" value="classpath:mybatis-config.xml"/>
        </bean>
    
        <!-- 4.配置dao接口扫描包,动态的实现了Dao接口注入到Spring容器中-->
        <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
            <!-- 注入 sqlSessionFactory-->
            <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
            <!-- 要扫描的dao包-->
            <property name="basePackage" value="com.musecho.dao"/>
        </bean>
    
    </beans>
    
  • spring-service.xml

    需要配置 aop事务支持:由于增删改操作都需要提交事务,所以要开启自动提交事务。并且SSM框架整合后,不需要我们手动调用mybatis代码得到SqlSession从而得到mapper对象了(都在spring配置文件配置了),所以无法执行sqlSessionFactory.openSession(true);。因此我们进行aop织入操作。

    <?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:tx="http://www.springframework.org/schema/tx"
           xmlns:aop="http://www.springframework.org/schema/aop"
           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
            http://www.springframework.org/schema/tx/spring-tx.xsd
            http://www.springframework.org/schema/aop
            https://www.springframework.org/schema/aop/spring-aop.xsd">
    
    
        <!-- 1.扫描service下的包-->
        <context:component-scan base-package="com.musecho.service"/>
    
        <!-- 2.将我们的所有业务类,注入到Spring,可以通过配置或注解实现-->
        <bean id="BookServiceImpl" class="com.musecho.service.BookServiceImpl">
            <property name="bookMapper" ref="bookMapper"/>
        </bean>
    
    
        <!-- 3.声明式事务配置-->
        <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
            <!--注入到数据源-->
    <!--        <property name="dataSource" ref="dataSource"/>-->
            <constructor-arg index="0" ref="dataSource" />
        </bean>
    
        <!-- 4.aop事务支持-->
        <!--配置事务通知-->
        <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.musecho.dao.*.*(..))"/>
            <aop:advisor advice-ref="txAdvice" pointcut-ref="txPointCut"/>
        </aop:config>
    
    </beans>
    

三、SpringMVC层

  • web.xml:DispatcherServlet,乱码过滤

    <?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:spring-mvc.xml</param-value>
            </init-param>
        </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-->
        <session-config>
            <session-timeout>15</session-timeout>
        </session-config>
    
    </web-app>
    
  • spring-mvc.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:mvc="http://www.springframework.org/schema/mvc"
           xmlns:context="http://www.springframework.org/schema/context"
           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">
    
        <!--1.注解驱动-->
        <mvc:annotation-driven/>
        <!--2.静态资源过滤-->
        <mvc:default-servlet-handler/>
        <!--3.扫描包:controller-->
        <context:component-scan base-package="com.musecho.controller" />
    
        <!--4.视图解析器-->
        <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
            <property name="prefix" value="/WEB-INF/jsp/" />
            <property name="suffix" value=".jsp" />
        </bean>
    
    </beans>
    
  • 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"
           xsi:schemaLocation="http://www.springframework.org/schema/beans
            https://www.springframework.org/schema/beans/spring-beans.xsd">
    
       <import resource="spring-dao.xml"/>
       <import resource="spring-service.xml"/>
       <import resource="spring-mvc.xml"/>
    
    </beans>
    

  • 报错404:有可能是IDEA的Project Structure里lib没配置

  • 报错500:xxxService的bean不存在

    org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name ‘bookController‘: Unsatisfied dependency expressed through field ‘bookService‘; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type ‘com.musecho.service.BookService‘ available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true), @org.springframework.beans.factory.annotation.Qualifier(value=BookServiceImpl)}
    

    排错步骤:

    • 1.查看bean注入是否成功【ok】

    • 2.Junit单元测试,查看是否能够查询出结果【ok】

    • 3.上述两步没问题的话,问题就不在底层,在spring

    • 4.SpringMVC,整合时没调用到service层的bean

      • 1.applicationContext.xml没有注入bean【ok】

      • 2.web.xml中,绑定上下文配置的文件写错了。【问题在这】

        应该写applicationContext.xml

    技术图片



三、编写功能

1 查询所有书籍列表

  • 控制器BookController

    @Controller
    @RequestMapping("/book")
    public class BookController {
    
        @Autowired
        @Qualifier("BookServiceImpl")
        private BookService bookService;
    
        //查询全部的书籍,并且返回一个书籍展示页面
        @RequestMapping("/allBooks2")
        public String queryAllBooks(Model model) {
            List<Books> list = bookService.queryAllBooks();
            model.addAttribute("list", list);
    
            return "allBooks";
        }
        
    }
    
  • 首页index.jsp

    <%@ page contentType="text/html;charset=UTF-8" language="java" %>
    <html>
    
    <head>
        <title>首页</title>
        <style type="text/css">
            a {
                text-decoration: none;
                color: black;
                font-size: 30px;
            }
    
            h2 {
                width: 400px;
                height: 55px;
                margin: 250px auto;
                text-align: center;
                line-height: 49px;
                background: deepskyblue;
                border-radius: 10px;
            }
        </style>
    
    </head>
    <body>
    <h2>
        <a href="${pageContext.request.contextPath}/book/allBooks2">点击进入书籍列表页面</a>
    </h2>
    
    </body>
    
    </html>
    
  • allBooks.jsp

    <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
    <%@ page contentType="text/html;charset=UTF-8" language="java" %>
    <html>
    
    <head>
        <title>书籍列表展示页面</title>
    
        <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@3.3.7/dist/css/bootstrap.min.css"
              integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
    
    </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>
    
        <div class="row">
            <div class="col-md-4 column">
                <a class="btn btn-primary" href="${pageContext.request.contextPath}/book/toAddBook">新增书籍</a>
            </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>
    
                    <tbody>
                    <c:forEach var="book" items="${requestScope.get(‘list‘)}">
                        <tr>
                            <td>${book.getBookID()}</td>
                            <td>${book.getBookName()}</td>
                            <td>${book.getBookCounts()}</td>
                            <td>${book.getDetail()}</td>
                            <td>
                                <a href="${pageContext.request.contextPath}/book/toUpdateBook/${book.getBookID()}">修改</a>
                                <a href="${pageContext.request.contextPath}/book/deleteBook/${book.getBookID()}">删除</a>
                            </td>
                        </tr>
                    </c:forEach>
                    </tbody>
                </table>
            </div>
        </div>
    </div>
    </body>
    
    </html>
    

2 新增书籍

  • BookController新增方法

    	//跳转到增加书籍的页面
        @RequestMapping("/toAddBook")
        public String toAddBook() {
            return "addBook";
        }
    
        //增加书籍
        @RequestMapping("/addBook2")
        public String addBook(Books book) {
            System.out.println(book);
            bookService.addBook(book);
            return "redirect:/book/allBooks2";
        }
    
  • 新增页面addBook.jsp

    <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
    <%@ page contentType="text/html;charset=UTF-8" language="java" %>
    <html>
    
    <head>
        <title>新增书籍页面</title>
    
        <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@3.3.7/dist/css/bootstrap.min.css"
              integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
    
    </head>
    
    <style>
        form-group {
            margin-bottom: 50px;
        }
    </style>
    <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/addBook2" method="post" class="form-horizontal">
            <div class="form-group" style="margin-bottom: 30px;">
                <label for="bookName" class="control-label col-md-1">书籍名字</label>
                <div class="col-md-4">
                    <input name="bookName" class="form-control" id="bookName" required>
                </div>
            </div>
            <div class="form-group" style="margin-bottom: 30px;">
                <label for="bookCount" class="control-label col-md-1">书籍数量</label>
                <div class="col-md-4">
                    <input name="bookCounts" class="form-control" id="bookCount" required>
                </div>
            </div>
            <div class="form-group" style="margin-bottom: 30px;">
                <label for="bookDetail" class="control-label col-md-1">书籍详情</label>
                <div class="col-md-4">
                    <input name="detail" class="form-control" id="bookDetail" required>
                </div>
            </div>
            <div class="form-group" style="margin-bottom: 30px;">
                <div class="col-md-offset-1 col-md-2">
                    <button type="submit" class="btn btn-primary">提交</button>
                </div>
            </div>
        </form>
    
    
    </div>
    </body>
    
    </html>
    

3 修改书籍

  • BookController修改方法

    	//跳转到增加书籍的页面
        @RequestMapping("/toAddBook")
        public String toAddBook() {
            return "addBook";
        }
    
        //增加书籍
        @RequestMapping("/addBook2")
        public String addBook(Books book) {
            System.out.println(book);
            bookService.addBook(book);
            return "redirect:/book/allBooks2";
        }
    
  • addBook.jsp

    <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
    <%@ page contentType="text/html;charset=UTF-8" language="java" %>
    <html>
    
    <head>
        <title>新增书籍页面</title>
    
        <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@3.3.7/dist/css/bootstrap.min.css"
              integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
    
    </head>
    
    <style>
        form-group {
            margin-bottom: 50px;
        }
    </style>
    <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/addBook2" method="post" class="form-horizontal">
            <div class="form-group" style="margin-bottom: 30px;">
                <label for="bookName" class="control-label col-md-1">书籍名字</label>
                <div class="col-md-4">
                    <input name="bookName" class="form-control" id="bookName" required>
                </div>
            </div>
            <div class="form-group" style="margin-bottom: 30px;">
                <label for="bookCount" class="control-label col-md-1">书籍数量</label>
                <div class="col-md-4">
                    <input name="bookCounts" class="form-control" id="bookCount" required>
                </div>
            </div>
            <div class="form-group" style="margin-bottom: 30px;">
                <label for="bookDetail" class="control-label col-md-1">书籍详情</label>
                <div class="col-md-4">
                    <input name="detail" class="form-control" id="bookDetail" required>
                </div>
            </div>
            <div class="form-group" style="margin-bottom: 30px;">
                <div class="col-md-offset-1 col-md-2">
                    <button type="submit" class="btn btn-primary">提交</button>
                </div>
            </div>
        </form>
    
    
    </div>
    </body>
    
    </html>
    

4 删除书籍

  • BookController删除方法

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

5 模糊搜索书籍

  • BookMapper

    List<Books> queryBooksLikeName(@Param("name") String bookName);
    
  • BookService

  • BookServiceImpl

     public List<Books> queryBooksLikeName(String bookName) {
            String str ="%"+bookName+"%";
    //        String str = bookName;
            System.err.println("bookName -->" + bookName);
            return bookMapper.queryBooksLikeName(str);
     }
    
  • BookController搜索方法

    	//根据书名,模糊搜索书籍
        @RequestMapping("/queryBooksLikeName")
        public String queryBooksLikeName(String bookName, Model model) {
            System.err.println("controller层bookName"+bookName);
            List<Books> list = bookService.queryBooksLikeName(bookName);
            model.addAttribute("list", list);
    
            return "allBooks";
        }
    
  • 修改allBooks.jsp

    	<div class="row">
            <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/allBooks2">显示全部书籍</a>
            </div>
            <div class="col-md-offset-4 col-md-4 column">
                <f	orm action="${pageContext.request.contextPath}/book/queryBooksLikeName"
                      method="post" class="form-inline">
                    <div class="form-group">
                        <input name="bookName" class="form-control">
                        <button class="btn btn-primary" type="submit">搜索</button>
                    </div>
                </form>
            </div>
        </div>
    

* 报错:

  • aop报错

    Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name ‘txPointCut‘: Lookup method resolution failed; nested exception is java.lang.IllegalStateException: Failed to introspect Class [org.springframework.aop.aspectj.AspectJExpressionPointcut] from ClassLoader [ParallelWebappClassLoader
    

    解决:

    不一定是aop相关的东西写错。中途加入的依赖,在project structure中要更新lib!

【SSM】(一)SSM整合-增删改查书籍

标签:点击   lse   oar   select   factor   编号   显示   localhost   eps   

原文地址:https://www.cnblogs.com/musecho/p/14436391.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!