这一篇我们将把spring和SpringMVC框架加入到我们的项目中,首先进行spring-mybaits的整合,再mybatis篇中我们时将我们的db.properties再我们mybatis-config.xml中进行引用,而我们spring进来后可以再spring中进行我们数据库的连接配置,创建我们的spring-dao.xml配置文件。
再spring-dao.xml配置文件中,我们主要做一下几件事:
1.配置我们的数据库连接参数,关联我们的数据库文件,交给spring托管。
2.配置我们的数据库连接池,再mybatis中说到我们这次使用的时c3p0连接池,支持自动化操作,实现自动注入。
3.创建我们的sqlSessionFactory的bean实例,就不需要在后续使用时手动创建。
4.实现我们mapper包下的自动扫描,之后可自动注入我们写下的*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:p="http://www.springframework.org/schema/p"
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
http://www.springframework.org/schema/context/spring-context-4.2.xsd">
<!--1.关联数据库文件-->
<context:property-placeholder location="classpath:db.properties"/>
<!--2.连接池
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="${user}"/>
<property name="password" value="${password}"/>
<!--设置最大连接池数量和最小连接池数量-->
<property name="maxPoolSize" value="30"/>
<property name="minPoolSize" value="10"/>
<!--设置关闭不自动commit-->
<property name="autoCommitOnClose" value="false"/>
<!--获取连接超时时间-->
<property name="checkoutTimeout" value="1000"/>
<!--获取连接失败重试次数-->
<property name="acquireRetryAttempts" value="2"/>
</bean>
<!--3.sqlSessionFactory-->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="configLocation" value="classpath:mybatis-config.xml"/>
</bean>
<!--配置mapper接口扫描包,实现自动注入-->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<!--要扫描的包-->
<property name="basePackage" value="com.chenxi.mapper"/>
<!--注入sqlSessionFactory-->
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
</bean>
</beans>
我们的mapper(dao)层与spring结合了,接下来便是我们的业务层与spring的整合。
创建spring-service.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:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
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/mvc
https://www.springframework.org/schema/mvc/spring-mvc.xsd">
<context:component-scan base-package="com.chenxi.service"/>
<!--将业务类注入进spring-->
<bean id="BookServiceImpl" class="com.chenxi.service.BookServiceImpl">
<property name="bookMapper" ref="bookMapper"/>
</bean>
<!--声明式事务配置-->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<!--注入数据源-->
<property name="dataSource" ref="dataSource"/>
</bean>
</beans>
spring的整合中我们新加了两个xml配置文件,建议将这两个配置文件以及我们后续整合springmvc的配置文件都导入在我们spring的applicationContext.xml中,这样在整合springmvc时也便于在web.xml中一步引入三个配置文件。(否则会报空指针异常)
接下来将springmvc整合进来:
我们首先让我们的项目添加web支持,
配置我们的web.xml:
为防止我们的数据传递出现乱码,我们使用spingmvc自带的过滤器,注意过滤器的url一定要加*,否则不过过滤我们的jsp文件,导致乱码依旧存在。(也不要忘了设置servlet的启动级别哦!)
<?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: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默认过期时间-->
<session-config>
<session-timeout>15</session-timeout>
</session-config>
</web-app>
还有我们的springmvc-servlet.xml配置文件,在其中添加注解驱动和配置视图解析器(不要忘记import到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"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
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/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd">
<!--1.注解驱动-->
<mvc:annotation-driven/>
<!--2.静态资源过滤-->
<mvc:default-servlet-handler/>
<!--3.扫描包controller-->
<context:component-scan base-package="com.chenxi.controller"/>
<!--4.视图解析器-->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="suffix" value=".jsp"/>
<property name="prefix" value="/WEB-INF/JSP/"/>
</bean>
</beans>
然后我们先简单写一个查询所有书籍的controller接口:
package com.chenxi.controller;
import com.chenxi.pojo.Books;
import com.chenxi.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.RequestMapping;
import java.util.List;
/**
* @author chenxi
* @Date 2022/9/24
* @Description
*/
@Controller
public class BookController {
//controller调用service层
@Autowired
@Qualifier("BookServiceImpl")
private BookService bookService;
@RequestMapping("/book/allBook")
private String queryAllBook(Model model){
List<Books> books = bookService.queryAllBook();
model.addAttribute("bookmsg",books);
return "allBook";
}
}
接下来我们便开始进行前端页面的简单编写:
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>首页</title>
<style>
a {
text-decoration: none;
color: black;
font-size: 18px;
}
h2 {
width: 180px;
height: 38px;
margin: 100px auto;
text-align: center;
line-height: 38px;
background: deepskyblue;
border-radius: 5px;
}
</style>
</head>
<body>
<h2>
<a href="${pageContext.request.contextPath}/book/allBook">进入书籍页面</a>
</h2>
</body>
</html>
然后就是我们的书籍展示 :(用到了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>
<link rel="stylesheet" href="https://cdn.staticfile.org/twitter-bootstrap/3.3.7/css/bootstrap.min.css">
</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 clearfix">
<div class="col-md-12 column">
<table class="table table-hover table-striped">
<thead>
<tr>
<th>书籍编号</th>
<th>书籍名称</th>
<th>书籍数量</th>
<th>书籍详情</th>
</tr>
</thead>
<tbody>
<c:forEach var="book" items="${bookmsg}">
<tr>
<td>${book.bookID}</td>
<td>${book.bookName}</td>
<td>${book.bookCounts}</td>
<td>${book.detail}</td>
</tr>
</c:forEach>
</tbody>
</table>
</div>
</div>
</div>
</body>
</html>
做到这我们的项目基础已经搭建的差不多啦!接下来要做的便是完善功能和优化前端页面!