其他分享
首页 > 其他分享> > SpringMVC10-整合

SpringMVC10-整合

作者:互联网

简略步骤:

章节1 准备数据库的环境和测试数据

第1步,准备数据库及测试数据

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,'从进门到进牢!'),
(4,'Python',1,'从放弃到忘记!');

第2步 : 创建一个普通的Maven项目,项目名称为ssmbuild,并导入一个ssm项目的最基础依赖

对应的pom.xml文件

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>top.aigoo</groupId>
    <artifactId>ssmbuild</artifactId>
    <version>1.0-SNAPSHOT</version>

    <!--依赖
        junit
        数据库驱动 mysql-connector-java
        连接池 c3p0
        servlet servlet-api
        jsp jsp-api,
        jstl
        mybatis mybatis,
        mybatis-spring,
        spring  spring-webmvc,
        spring-jdbc
    -->
    <dependencies>
        <!--Junit-->
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
            <scope>test</scope>
        </dependency>
        <!--mysql数据库驱动-->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.47</version>
        </dependency>
        <!--数据库连接池-->
        <dependency>
            <groupId>com.mchange</groupId>
            <artifactId>c3p0</artifactId>
            <version>0.9.5.2</version>
        </dependency>
        <!--servlet-->
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>servlet-api</artifactId>
            <version>2.5</version>
        </dependency>
        <!--jsp-->
        <dependency>
            <groupId>javax.servlet.jsp</groupId>
            <artifactId>jsp-api</artifactId>
            <version>2.2</version>
        </dependency>
        <!--jstl-->
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>jstl</artifactId>
            <version>1.2</version>
        </dependency>
        
        <!--standard标签库依赖-->
        <dependency>
            <groupId>org.apache.taglibs</groupId>
            <artifactId>taglibs-standard-impl</artifactId>
            <version>1.2.5</version>
            <scope>runtime</scope>
        </dependency>
        
        <!--mybatis-->
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis</artifactId>
            <version>3.5.2</version>
        </dependency>
        <!--mybatis-spring-->
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis-spring</artifactId>
            <version>2.0.2</version>
        </dependency>
        <!--spring-mvc-->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>5.2.0.RELEASE</version>
        </dependency>
        <!--spring-jdbc-->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-jdbc</artifactId>
            <version>5.2.0.RELEASE</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>
</project>

第3步 创建项目目录结构 controller,dao,pojo,service ,

至此,我们就准备好了一个空的SSM 项目,使用MAVEN构建,这个项目可以作为后续各个项目的模板

章节2 整合mybatis和数据库

第1步 在resources 添加 mybaits-config.xml applicationContext.xml, db.properties,并完善各个配置文件的内容,分别如下

其中 ,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>

db.properties的内容为

driver=com.mysql.jdbc.Driver
# 如果使用的是mysql8.0+,需要增加一个时区的配置
url=jdbc:mysql://localhost:3306/ssmbuild?useSSL=false&characterEncoding=utf8&useUnicode=True
#这里一定要用user应为要用c3p0,要是用username会报错连不上数据库.
user=root
password=hsp

对于db.properties 注意,

1.如果使用mysql8.0以上版本,url里面还需要加上一个时区的配置

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">
    
</beans>

第2步 ,配置我们的mybatis,这里我们仅使用setting 和 alias

<?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去做,也就是前面学习的spring-mybatis整合知识-->
    <typeAliases >
        <package name="com.kuang.pojo"/>
    </typeAliases>
    <mappers>
        <mapper class="com.baifu.dao.BookMapper"/>
    </mappers>
</configuration>

第3步,准备mybatis的工作环境,我们需要分别针对数据库的books表,创建对应的实例,及对应的BookMapper和BookMapper.xml

去com.kuang.pojo创建实体类Books, 记住保证字段和数据库字段一模一样

a) 创建和数据库字段一一对应的实体类 Books

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

b)编写对pojo的接口操作 com.baifu.dao.BookMapper.java

package com.kuang.dao;

import com.kuang.pojo.Books;
import org.apache.ibatis.annotations.Param;

import java.util.List;

public interface BookMapper {
    //增加一本书
    int addBook(Books books);
    //删除一本书
    int deleteBookById(@Param("bookID") int id);
    //更新一本书
    int updateBook(Books books);
    //根据id查询一本书
    Books queryBookById(@Param("bookID") int id);
    //查询全部的书
    List<Books> queryAllBook();
}

c) 编写 对BookMapper的mybatis实现, com.baifu.dao.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.kuang.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>
    <!--根据id查询一本书-->
    <select id="queryBookById" resultType="Books">
        select * from ssmbuild.books where bookID=#{bookID};
    </select>
    <!--查询全部的书-->
    <select id="queryAllBook" resultType="Books">
        select * from ssmbuild.books;
    </select>
</mapper>

d) 将编写好的BooksMapper注册到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>
	......
    <mappers>
        <mapper class="com.kuang.dao.BookMapper"/>
    </mappers>
    ......
</configuration>

e) 接着,我们完善service层, 首先编写 UserService.java接口

package com.kuang.service;

import com.kuang.pojo.Books;
import org.apache.ibatis.annotations.Param;

import java.util.List;
// BookService 调用 dao 层
public interface BookService {
    //增加一本书
    int addBook(Books books);
    //删除一本书
    int deleteBookById(int id);
    //更新一本书
    int updateBook(Books books);
    //根据id查询一本书
    Books queryBookById(int id);
    //查询全部的书
    List<Books> queryAllBook();
}

f) 接下来,我们在实现我们的UserService ,com.baifu.service.BookServiceImpl

public class BookServiceImpl implements BookService{
    // 用一个 BookMapper 对象进行对数据库的操作,这个对象由 Spring 创建和注入,
    //  Spring 针对接口进行操作!
    //service 调用dao层
    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> queryAllBook() {
        return bookMapper.queryAllBook();
    }
}

通过内置一个 Mapper 对象,封装了使用 Mapper 对象的操作,方便 Spring 管理,用户只需调用 Service 层即可进行业务操作,不用在意 Dao 层的实现!

上述工作都是为了完成mybatis层,至此,我们就将mybatis的整合全部完成了(包含 Dao 层和 Service 层), 接下来 我们继续spring层的整合

MyBatis层测试

为了保证后面出问题好找,写完 MyBatis 层先测试一下!

为了测试要在配置文件中先加上数据源配置

    <!--    MyBatis 层先测试一下使用 ,  注意放的位置,有顺序要求-->
    <environments default="development">
        <environment id="development">
            <transactionManager type="JDBC"/>
            <dataSource type="POOLED">
                <property name="driver" value="com.mysql.jdbc.Driver"/>
                <property name="url" value="jdbc:mysql://localhost:3306/ssmbuild?useSSL=false&amp;characterEncoding=utf8&amp;useUnicode=True"/>
                <property name="username" value="root"/>
                <property name="password" value="hsp"/>
            </dataSource>
        </environment>
    </environments>

然后写个方法测试一下查询的功能,Mybatis没问题

public class Mytest {
    @Test
    public void Test() throws IOException {
        // 之前 MyBatis 工具类的工作
        String resource = "mybatis-config.xml";
        InputStream inputStream = Resources.getResourceAsStream(resource);
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);

        SqlSession sqlSession = sqlSessionFactory.openSession();
        BookMapper mapper = sqlSession.getMapper(BookMapper.class);

        // 查询所有
        List<Books> books = mapper.queryAllBook();
        for (Books book : books) {
            System.out.println(book.toString());
        }
    }
}

章节3 spring-dao整合业务层接口及服务

搭建完了 MyBatis 层,就要把它纳入 Spring 的框架中来了!

第1步,我们先建立一个spring-dao.xml的配置文件,作为整合配置文件,这个配置文件至少要定义如下功能

a) 关联数据库配置+数据源

b)sqlSessionBeanFactory

c)整合sqlSession
    <!--1.关联数据库配置-->
    <context:property-placeholder location="classpath:db.properties"/>
    <!--2.datasource
        dbcp 半自动化操作,不能自动连接
        c3p0 自动化操作,自动化加载配置文件,并且可以自动配置到对象中
        druid hikari
    -->
    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
        <!-- 配置连接池属性 -->
        <property name="driverClass" value="${driver}"/>
        <property name="jdbcUrl" value="${url}"/>
        <property name="user" value="${username}"/>
        <property name="password" value="${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,绑定 MyBatis 配置文件-->
    <!--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>



    <!--    重点来了!在之前Spring与Mybatis融合的时候,
    需要将Mybatis的接口虚拟的实现类 Mapper.xml 实例化为一个真实的实现类 MapperImpl,
    才能注入到 Service 层的对象中(总不能注入 Mapper.xml 吧)!
    这里通过使用 MapperScannerConfigurer,把这种工作交给 Spring 去做!-->

    <!--采用MapperScannerConfigurer 配置DAO接口扫描包 动态实现UserMapper可以注入Spring-->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <!--step 1.动态注入sqlSessionFactory-->
        <!-- 注入 sqlSessionFactory,注意是 BeanName!所以不用 ref! -->
        <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
        <!-- 给出需要扫描 Dao 接口的包 -->
        <property name="basePackage" value="com.baifu.dao"/>
    </bean>
</beans>

配置连接池(这里使用 c3p0 连接池),本来在 MyBatis 配置文件中做的事情交给 Spring(数据库配置文件在 上一步已经写好了)!

重点来了! 在之前Spring与Mybatis融合的时候,需要将Mybatis的接口虚拟的实现类 Mapper.xml 实例化为一个真实的实现类 MapperImpl,才能注入到 Service 层的对象中(总不能注入 Mapper.xml 吧)!
这里通过使用 MapperScannerConfigurer,把这种工作交给 Spring 去做.

 <!--采用MapperScannerConfigurer 配置DAO接口扫描包 动态实现UserMapper可以注入Spring-->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <!--step 1.动态注入sqlSessionFactory-->
        <!-- 注入 sqlSessionFactory,注意是 BeanName!所以不用 ref! -->
        <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
        <!-- 给出需要扫描 Dao 接口的包 -->
        <property name="basePackage" value="com.baifu.dao"/>
    </bean>

basePackage 属性是让你为映射器接口文件设置基本的包路径,可以使用分号或逗号作为分隔符设置多于一个的包路径。每个映射器将会在指定的包路径中递归地被搜索到。

注意,没有必要去指定 SqlSessionFactory 或 SqlSessionTemplate,因 为 MapperScannerConfigurer 将会创建 MapperFactoryBean,之后自动装配。

但是,如果你使用了一个以上的 DataSource,那么自动装配可能会失效。这种情况下,可以使用 sqlSessionFactoryBeanName 或 sqlSessionTemplateBeanName 属性来设置正确的 bean 名称来使用。这就是它如何来配置的,注意 bean 的名称是必须的,而不是 bean 的引用。

MapperScannerConfigurer 会扫描指定包下的接口和其对应的 Mapper.xml,通过反射自动生成接口实现类(真),也就是说我们虽然没有写 BooksMapperImpl,但它已经帮我们生成了!

章节3.spring-service整合业务层接口及服务

在spring这一层,我们主要是扫描包,将业务注入到spring,声明事务,并执行aop切入

创建 spring-service.xml 文件

将业务层对象交给 Spring 管理,同时在其中注入 Dao 层对象

  <!--step 2. 将我们的所有业务类注入到spring 可以通过配置或者注解实现 因为刚讲springmvc 这里用配置实现 -->
<!--    将业务层对象交给 Spring 管理,同时在其中注入 Dao 层对象-->
    <bean id="BookServiceImpl" class="com.baifu.service.BookServiceImpl">
        <!-- 这里的 bookMapper 就是 MapperScannerConfigurer 扫描接口和其对应的 Mapper.xml 创建的实现类 -->
        <property name="bookMapper" ref="bookMapper"/>
    </bean>

也可以通过注解的方式把业务层对象交给 Spring,不过要在业务层的类上添加注解和开启注解扫描,这里先不用这种方式了

	<!-- 类上 @Service -->
	<!-- 扫描注解,不过这里其实没用到 -->
    <context:component-scan base-package="com.qiyuan.service"/>

总配置

<?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">

    <!--step 1. 扫描service下的包-->
    <context:component-scan base-package="com.baifu.service"/>
    <!--step 2. 将我们的所有业务类注入到spring 可以通过配置或者注解实现 因为刚讲springmvc 这里用配置实现 -->
<!--    将业务层对象交给 Spring 管理,同时在其中注入 Dao 层对象-->
    <bean id="BookServiceImpl" class="com.baifu.service.BookServiceImpl">
        <!-- 这里的 bookMapper 就是 MapperScannerConfigurer 扫描接口和其对应的 Mapper.xml 创建的实现类 -->
        <property name="bookMapper" ref="bookMapper"/>
    </bean>

    <!--step 3.声明式事务配置-->
<!--    开启事务管理器后,就可以使用 AOP 进行事务的织入了-->
    <!-- 配置事务管理器 -->
    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <!--注入数据源-->
        <property name="dataSource" ref="dataSource"/>
    </bean>

    <!--step 4 aop事务支持-->

</beans>

Spring层测试

和之前一样,写完一层测试一下,不然以后出问题找谁去!

在测试前,先在 applicationContext.xml 这个总的配置文件中导入其他配置文件

    <import resource="classpath:spring-dao.xml"/>
    <import resource="spring-service.xml"/>

这样加载的时候只加载这一个文件就行了。

写个测试方法测试一下

@Test
public void TestSpringMybatis(){
    ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
    BookService bookServiceImpl = context.getBean("BookServiceImpl", BookService.class);
    // 增加
    Books book1 = new Books(5, "Python教程", 6, "起飞级Python教程");
    bookServiceImpl.addBook(book1);
    // 查询所有
    List<Books> books = bookServiceImpl.queryAllBook();
    for (Books book : books) {
        System.out.println(book.toString());
    }
}

执行结果

Books(bookID=1, bookName=Java, bookCounts=1, detail=从入门到放弃!)
Books(bookID=2, bookName=Mysql, bookCounts=10, detail=从删库到跑步!)
Books(bookID=3, bookName=Linux, bookCounts=5, detail=从进门到进牢!)
Books(bookID=4, bookName=Python, bookCounts=1, detail=从放弃到忘记!)
Books(bookID=6, bookName=Python教程, bookCounts=6, detail=起飞级Python教程)

注意bug:运行直接报错

java.sql.SQLException: Access denied for user 'Administrator'@'localhost' (using password: YES)

版本信息:

        <!--mysql数据库驱动-->
        <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>

数据库不让访问,而且我的账户名不是’Administrator’啊, 查看数据库配置文件db.properties和 spring-dao.xml

db.properties为以下,且前边的driver、url、username、password已经高亮,有被调用

driver=com.mysql.jdbc.Driver
# 如果使用的是mysql8.0+,需要增加一个时区的配置
url=jdbc:mysql://localhost:3306/ssmbuild?useSSL=false&characterEncoding=utf8&useUnicode=True
username=root
password=hsp

spring-dao.xml为以下,value的值跟db.properties对应也没问题

<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
    <!-- 配置连接池属性 -->
    <property name="driverClass" value="${driver}"/>
    <property name="jdbcUrl" value="${url}"/>
    
    <!-- 就是这里要给为 value="${user}" 跟name一致,db.properties中也要改 -->
    <property name="user" value="${username}"/>
    
    
    <property name="password" value="${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>

经过测试发现因为<property name="user" value="${username}"/>这句, name是"user",value = “${username}”/>导致的问题,将db.properties和 spring-dao.xml中的username改为user就可以了.

章节4.spring-mvc整合controller层接口及服务

step1, 在idea增加web项目的支持

在项目点击右键—Add FrameWorkSupport ,选择web支持,添加框架后配置tomcat然后运行,测试tomcat.

step2 . 配置我们的web.xml,后续请求有DispatcherServlet接管,并配置过滤器和Session等信息

<?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">
    <!--step 1 .DispathcerServlet-->
    <servlet>
        <servlet-name>springmvc</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <!--整合spring-mvc配置-->
        <init-param>
            <param-name>contextConfigLocation</param-name>
             <!--导入最终的Spring配置文件,否则会报bean找不到的错误-->
            <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>

    <!--Step 2 乱码过滤-->
    <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>
    <!--step 3 配置session过期时间-->
    <session-config>
        <session-timeout>60</session-timeout>
    </session-config>
</web-app>

step 3. 完成web.xml的配置,我们需要去调整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
        http://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.kuang.controller"/>
    <!--4.视图解析器-->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/jsp/"/>
        <property name="suffix" value=".jsp"/>
    </bean>
</beans>

spring-mvc.xml配置好别忘记导入到 applicationContext.xml中,保证三个都导入了.

    <import resource="spring-dao.xml"/>
    <import resource="spring-service.xml"/>
    <import resource="spring-mvc.xml"/>

同时,我们配置视图解析器时,就去web/WEB-INF/目录下面把jsp文件夹创建起来

至此,我们就将mybatis+spring+springMVC整合到了一起! 项目的框架和配置都搭建完成了!接下来就是添加业务了,流程为:前端发起请求——控制器——业务处理——返回视图,先把中转站控制器给写了!

bug处理

刚刚配置web应用时候tomcat还能启动,完成web.xml的配置后就不能启动了,出现以下错误:

 Error during artifact deployment. See server log for details.

org.apache.catalina.core.StandardContext.filterStart 启动过滤器异常
	java.lang.ClassNotFoundException: org.springframework.web.filter.CharacterEncodingFilter

org.apache.catalina.core.StandardContext.startInternal 一个或多个筛选器启动失败。

后来发现是过滤器以来问题

解决方法:

​ IDEA :Project Structure --> Artifacts --> 查看对应项目的 /WEB-INF下边没有lib文件–>创建lib—>将对应项目的Available Elements --> 选中所有的 jar 包 --> 右键 Put into /WEB-INF/lib,完美解决!

章节5.测试 实现前台页面 及优化

step1 . 编写一个Controller功能,实现查询图书列表

通过注解定义使其成为一个控制器,类请求路径为 /book,内置了一个 BookService 对象以执行业务,这个对象通过自动装配的方式注入!

@Controller
@RequestMapping("/book")
public class BooksController {
    /*
     service层持有Dao层
    *Controller层持有Service层
    * */
    // 自动装配 bean
    @Autowired
    @Qualifier("BookServiceImpl")
    private BookService bookService;
    /*查询全部书籍并且返回到一个书籍列表页面*/
    @RequestMapping("/allbook")
    public String list(Model model){
        List<Books> books = bookService.queryAllBook();
        model.addAttribute("list", books);
        return "booksList";
    }

}

此处需要记住:

1 service层持有dao层

2、controller层持有service层

Step 2 .在web/WEB-INF/jsp/创建booksList.jsp

先编写首页 index.jsp,从首页可以进入书籍列表的页面(简单设置了一下样式)

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %>
<!DOCTYPE HTML>
<html>
<head>
  <title>首页</title>
  <style type="text/css">
    a {
      text-decoration: none;
      color: black;
      font-size: 18px;
    }
    h3 {
      width: 180px;
      height: 38px;
      margin: 100px auto;
      text-align: center;
      line-height: 38px;
      background: deepskyblue;
      border-radius: 4px;
    }
  </style>
</head>
<body>
<h3>
  <a href="${pageContext.request.contextPath}/book/booksList">点击进入列表页</a>
</h3>
</body>
</html>

在web/WEB-INF/jsp/创建booksList.jsp,然后是 booksList.jsp 页面,展示所有书籍并提供选项进行新增书籍、删除书籍、修改书籍操作(用到了 jstl 标签,需要导入 jstl 包和 standard 包!),这个页面是可以复用的

<%@ 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>书籍列表 —— baifu</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('booksList')}">
                    <tr>
                        <td>${book.getBookID()}</td>
                        <td>${book.getBookName()}</td>
                        <td>${book.getBookCounts()}</td>
                        <td>${book.getDetail()}</td>
                        <td>
                            <a href="${pageContext.request.contextPath}/book/updateBook?id=${book.getBookID()}">更改</a> |
                            <a href="${pageContext.request.contextPath}/book/deleteBook/${book.getBookID()}">删除</a>
                        </td>
                    </tr>
                </c:forEach>
                </tbody>
            </table>
        </div>
    </div>
</div>

step3 在测试可以跳转,我们引入bootstrap优化效果

<%@ 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.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>
                    <small>书籍列表 —— baifu</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('booksList')}">
                    <tr>
                        <td>${book.getBookID()}</td>
                        <td>${book.getBookName()}</td>
                        <td>${book.getBookCounts()}</td>
                        <td>${book.getDetail()}</td>
                        <td>
                            <a href="${pageContext.request.contextPath}/book/updateBook?id=${book.getBookID()}">更改</a> |
                            <a href="${pageContext.request.contextPath}/book/deleteBook/${book.getBookID()}">删除</a>
                        </td>
                    </tr>
                </c:forEach>
                </tbody>
            </table>
        </div>
    </div>
</div>

标签:xml,bookID,int,spring,SpringMVC10,Books,books,整合
来源: https://blog.csdn.net/weixin_39395819/article/details/122734717