以图书管理为例,学习SSM整合,从数据库到前端
学习原文:
项目过程:
分析需求——>设计数据库求——>业务实现求——>前端界面
一.创建数据库:
数据库版本:mysql5.7:
id为主键
分类为int类型,便于到时候,用分类id数字代表
价格为浮点类型,保留两位数字
CREATE TABLE `books` (
`id` int(11) NOT NULL COMMENT '图书id',
`book_name` varchar(255) NOT NULL COMMENT '书名',
`boo_kclass` int(11) DEFAULT NULL COMMENT '图书分类',
`book_price` float(100,2) DEFAULT NULL COMMENT '图书价格',
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
添加部分数据:

二.创建java项目:
参考狂神的配置文件:https://mp.weixin.qq.com/s/gXFMNU83_7PqTkNZUgvigA
JDK11
Maven:3.4
1.创建Maven-web项目:

添加java目录

2.添加依赖项目
依赖项目:
- junit:测试
- mysql:数据库驱动
- c3p0:数据库连接池
- mybteis:数据库操作
- mybatisSpring:用Spring连接mybatis
- Spring:框架
- Servlet:网络服务
- jsp:界面
- aspectJ AOP:切面程序
- lombok:简化开发pojo类
-
fastjson:阿帕奇处理json
代码如下
<dependencies>
<!--junit:测试包-->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<!-- 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>
<!-- mybatis:数据库操作-->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.2</version>
</dependency>
<!-- mybatisSpring:用Spring连接mybatis-->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>2.0.2</version>
</dependency>
<!-- Spring:框架-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.1.10.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>5.1.10.RELEASE</version>
</dependency>
<!-- Servlet:网络服务 jsp jstl 三个固定一起-->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.2</version>
<scope>provided</scope>
</dependency>
<!-- jsp:界面-->
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>jsp-api</artifactId>
<version>2.1</version>
<scope>provided</scope>
</dependency>
<!-- jstl-->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<!-- aspectJ AOP:切面程序-->
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.9.4</version>
</dependency>
<!-- lombok简化pojo开发-->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.16.10</version>
</dependency>
<!-- fastjson处理json-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.4</version>
</dependency>
</dependencies>
3.静态资源导出插件
<build>
<!--解决静态资源无法导出-->
<resources>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.properties</include>
<include>**/*.xml</include>
</includes>
<filtering>true</filtering>
</resource>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>**/*.properties</include>
<include>**/*.xml</include>
</includes>
<filtering>true</filtering>
</resource>
</resources>
</build>
三.IDAE连接数据
便于操作数据库,和验效sql语法

连接成功:

四.创建程序目录
1.创建基础文件和包
java根包下:
- dao:对接数据库,基本的增删改查方法
- servlet:业务层
- pojo:实体类曾
- Controller:控制器
- utils:工具类
- exception:自定义异常类
resources资源目录下:
- mybatis-config.xml :mybatis配置
- applicatioContext..xml :spring核心配置文件
- detabase.properties :数据库配置文件

2.写入文件头
mybatis文件头
<?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>
spring文件头
<?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
http://www.springframework.org/schema/beans/spring-beans.xsd">
</beans>
顶部黄色提示,创建新的applicationContext,直接确定,一会需要加入到里面

3.编写配置文件
编辑数据库连接数据:
mysql jdbc url具体参数全解_jdbcurl配置参数
jdbc.driver = com.mysql.jdbc.Driver
jdbc.url = jdbc:mysql://localhost:3306/tu_shu_guan?=useSSL=true&useUnicode=true&characterEncoding=utf-8
jdbc.username = root
jdbc.password = root
原先使用mybatis需要 配置数据源
但是我们用mybatis则交给spring去做
我们只需要配置别名就可以了,在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>
<!--自动给每个类起个别名-->
<typeAliases>
<package name="zhe.xin.pojo"/>
</typeAliases>
</configuration>
五.整合mybatis,dao层
1.创建pojo,使用lombok注解,进行封装
/**
* 注解:
* get,set方法
* 有参构造
* 无参构造
*/
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Books {
private int id;
private String book_name;
private String boo_kclass;
private float book_price;
}
2.创建数据库对应的映射器Mapper接口
1.编写增删改查接口:
public interface BooksMapper {
//增加一本书
int addBook(Books books);
//删除一本书
int deleteBook(int bookID);
//更新一本书
int updataBook(Books books);
//查询一本书
Books queryBook(int bookID);
//查询全部的书
List<Books> queryAllBook();
}
2.编写对应Mybatis的xml
1.创建包名和类
由于一个接口对应Mapper, 防止搞错, 在同目录下创建xml目录, 编写对应Mapper的mybatis, 的xml文件

2.写入头文件:
只需复制源mybatis的头文件, 改掉所有configuration和url中的config ,修改为mapper
3.绑定同名的接口
<?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 namespace="zhe.xin.dao.BooksMapper">
</mapper>
4.编写操作数据库接口的实现sql语句
<?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 namespace="zhe.xin.dao.BooksMapper">
<!--对应的接口实现sql-->
<!-- tongg标签寻找,在mybatis中设置了自动生成标签 -->
<!-- 增加一本书-->
<insert id="addBook" parameterType="Books">
insert into tu_shu_guan.books (book_name, boo_kclass, book_price)
values (#{book_name}, #{boo_kclass}, #{book_price});
</insert>
<!-- 删除一本书-->
<delete id="deleteBook" parameterType="int">
delete
from tu_shu_guan.books
where id=#{bookID}
</delete>
<!-- 更新一本书-->
<update id="updataBook" parameterType="Books">
update tu_shu_guan.books
set book_name=#{book_name}, boo_kclass=#{boo_kclass}, book_price=#{book_price}
</update>
<!-- 查询一本书-->
<select id="queryBook" resultType="Books">
select *
from tu_shu_guan.books
where id = #{bookID}
</select>
<!-- 查询全部的书-->
<select id="queryAllBook" resultType="Books">
SELECT *
from tu_shu_guan.books
</select>
</mapper>
5.编写完成后立刻将mapper注册到配置文件中
在mybatis-config.xml文件中,扫描这个包下的mapper
<mappers>
<!--注册到总配置-->
<mapper resource="zhe/xin/dao/xml/BooksMapper.xml"/>
</mappers>
六.整合dao层
1.创建整合dao层的xml

需要,保证都在Application下面:

2.需要做的事情:
- 关联数据库文件
- 连接池
- SQLSessionFactory sql会话工厂
1.关联数据库文件
<context:property-placeholder/>数据库配置文件专用标签<?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
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
https://www.springframework.org/schema/context/spring-context.xsd">
<!-- 关联数据库文件-->
<!-- 固定写法 专用配置,classpath:导航到资源目录-->
<context:property-placeholder location="classpath:database.properties"/>
<!-- 连接池-->
<!-- SQLSessionFactory-->
</beans>
2.连接池:连接池介绍
在外部库中都找到数据库连接池:

无论哪一个数据库连接池,固定不变的都是这四个,直接打单词就能够看到IDEA提示:
<!-- 连接池-->
<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}"/>
</bean>
只是每个数据库连接池都有自己的独有属性,例如c3p0:
<!-- 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"/>
3.SQLSessionFactory
<!-- 配置SqlSessionFactory对象 -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<!-- 注入数据库连接池 -->
<property name="dataSource" ref="dataSource"/>
<!-- 绑定mybatis配置文件-->
<property name="configLocation" value="classpath:mybatis-config.xml"/>
</bean>
3.配置dao接口扫描包
动态实现了Dao接口可以注入到Spring容器中
SqlSession是你和数据库之间的一次会话,可以理解为一次连接。它提供了执行SQL语句的方法,如select, insert, update, delete等。你可以通过SqlSession获取mapper接口的实例,然后使用这个实例执行SQL语句。
原先手动实现方式一:
public class BooksSessionImpl extends SqlSessionDaoSupport implements BooksMapper{
public SqlSession getSqlSessions() {
return getSqlSession();
}
}
继承这个SqlSessionDaoSupport就可以直接返回SqlSession了
原先手动实现方式二:
手动添加属性:
public class BooksSessionImpl extends SqlSessionDaoSupport implements BooksMapper{
SqlSessionTemplate sqlSessionTemplate;
现在使用全自动操作,动态实现
利用反射的原理实现:mapper
<!-- 固定写法 配置dao动态扫描-->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<!-- 注入sqlSessionFactory-->
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
<!-- 要扫描的Dao包-->
<property name="basePackage" value="zhe.xin.dao"/>
</bean>
整合代码:
<?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
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
https://www.springframework.org/schema/context/spring-context.xsd">
<!-- 关联数据库文件-->
<!-- 固定写法 专用配置,classpath:导航到资源目录-->
<context:property-placeholder location="classpath:database.properties"/>
<!-- 连接池-->
<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>
<!-- 配置SqlSessionFactory对象 -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<!-- 注入数据库连接池 -->
<property name="dataSource" ref="dataSource"/>
<!-- 绑定mybatis配置文件-->
<property name="configLocation" value="classpath:mybatis-config.xml"/>
</bean>
<!-- 固定写法 配置dao动态扫描-->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<!-- 注入sqlSessionFactory-->
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
<!-- 要扫描的Dao包-->
<property name="basePackage" value="zhe.xin.dao"/>
</bean>
</beans>
七.整合Service层
一.创建Service层包
1.创建包:

2.编写接口
由于servlet必须调用bao层,所以直接复制dao层的接口代码进行更改即可
public interface BooksServlet {
//增加一本书
int addBook(Books books);
//删除一本书
int deleteBook(int bookID);
//更新一本书
int updataBook(Books books);
//查询一本书
Books queryBook(int id);
//查询全部的书
List<Books> queryAllBook();
}
2.编写实现类:
package zhe.xin.service.Impl;
import zhe.xin.dao.BooksMapper;
import zhe.xin.pojo.Books;
import zhe.xin.service.BooksService;
import java.util.List;
public class BooksServiceImpl implements BooksService {
//调用dao层的操作,设置一个set接口,方便Spring管理
private BooksMapper booksMapper;
//这里的setBooksMapper名称会影响配置文件中property标签的name属性 <property name="booksMapper" ref="booksMapper"/>
public void setBookMapper(BooksMapper bookMapper) {
this.booksMapper = bookMapper;
}
@Override
public int addBook(Books books) {
return booksMapper.addBook(books);
}
@Override
public int deleteBook(int bookID) {
return booksMapper.deleteBook(bookID);
}
@Override
public int updataBook(Books books) {
return booksMapper.updataBook(books);
}
@Override
public Books queryBook(int id) {
return booksMapper.queryBook(id);
}
@Override
public List<Books> queryAllBook() {
return booksMapper.queryAllBook();
}
}
二.创建Service层配置xml
1.创建文件: (头文件和dao层的一样)

2.要做的事情:
- 自动扫描service层的包
- 将我们的所有类注入到Spring
-
声明事务 -关联连接池
-
aop注入事务
3.自动扫描service层的包
<!-- 自动扫描service层的包-->
<context:component-scan base-package="zhe.xin.servlet"/>
4.将我们的所有类注入到Spring
可以通过注解,可以是配置 ,这里我们使用配置进行学习
1.配置版: property为注入的类,对应zhe.xin.service.Impl.BooksServiceImpl中的setBooksMapper
<!-- 将这个类作为bean注入到Spring,并初始化传参数 -->
<bean id="BooksServiceImpl" class="zhe.xin.service.Impl.BooksServiceImpl">
<property name="bookMapper" ref="booksMapper"/>
</bean>
这里前提是必须保证都在Application里

2.注解版:
参考之前文章:Spring -1,2,3,4 章节的文章
@Service
public class BooksServiceImpl implements BooksService {
@Autowired
BooksMapper booksMapper;
5.声明事务 -关联连接池
<!-- 声明事务 -关联连接池-->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"/>
</bean>
这里的ref对应的是dao层的配置:跳转
6. aop注入事务
这里按自己的需求编写
参考之前的文章:Spring - AOP切入:5,6,7,8 章节的文章
7.总体配置: service就是整合前面dao和spring层
<?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
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
https://www.springframework.org/schema/context/spring-context.xsd">
<!-- 自动扫描service层的包-->
<context:component-scan base-package="zhe.xin.service"/>
<!-- 将这个类作为bean注入到Spring,并初始化传参数 -->
<bean id="BooksServiceImpl" class="zhe.xin.service.Impl.BooksServiceImpl">
<property name="bookMapper" ref="booksMapper"/>
</bean>
<!-- 声明事务 -关联连接池-->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"/>
</bean>
<!-- aop注入事务-->
</beans>
八.配置WEB
配置完后WEB.xml就不用在编写了, 因为这样就被Spring核心分发器给接管了DispatcherServlet
我们最开始就是创建的web项目,所有不要再手动添加框架
编辑web头文件:

<?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">
</web-app>
1.需要做的事情
-
DispatchServlet : 发送服务,用于启动springmvc
-
乱码过滤
- 配置Session的存活时长
1.DispatchServlet : 发送服务(用于启动springmvc)
这里在后面发生了错误:查看解决
<!-- DispatchServlet : 发送服务 用于启动springmvc-->
<servlet>
<!-- 引入mvc框架-->
<servlet-name>springmvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<!-- 配置mvc的位置-->
<param-name>contextConfigLocation</param-name>
<param-value>classpath:Spring-mvc.xml</param-value>
</init-param>
</servlet>
<!-- 启动目录设置为/ 让Springmvc和Tomcat同时启动-->
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
由于contextConfigLocation需要一个Spring-mvc.xml就去创建一个

同时将之前写的各个分层都添加到配置文件applicatioContext..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
http://www.springframework.org/schema/beans/spring-beans.xsd">
<import resource="Spring-dao.xml"/>
<import resource="Spring-service.xml"/>
<import resource="Spring-mvc.xml"/>
</beans>
2.乱码过滤
创建过滤器实现
首先在web.xml中编写
<!-- 乱码过滤-->
<filter>
<filter-name>encodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<!-- 编写CharacterEncodingFilter类的配置-->
<init-param>
<!-- 配置的名称和值-->
<param-name>encoding</param-name>
<param-value>utf-8</param-value>
</init-param>
</filter>
<!-- 映射到url /*为过滤所有-->
<filter-mapping>
<filter-name>encodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
查看org.springframework.web.filter.CharacterEncodingFilter这个类的源码:
发现encoding参数为@Nullable,可以为空, 所以IDEA不进行提示, 但是可以设置

3.配置Session的存活时长
<!-- Session 配置Session的存活时长为15分钟-->
<session-config>
<session-timeout>15</session-timeout>
</session-config>
2.编写mvc配置文件
<?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
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/mvc
https://www.springframework.org/schema/mvc/spring-mvc.xsd">
<!-- 配置SpringMVC -->
<!-- 1.开启SpringMVC注解驱动 -->
<mvc:annotation-driven />
<!-- 2.静态资源默认servlet配置-->
<mvc:default-servlet-handler/>
<!-- 3.配置jsp 显示ViewResolver视图解析器 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass" value="org.springframework.web.servlet.view.JstlView" />
<!--默认头,和尾巴,,,这样之后只要在/WEB-INF/jsp/目录下的界面,只需要写名称,自动填充路径-->
<property name="prefix" value="/WEB-INF/jsp/" />
<property name="suffix" value=".jsp" />
</bean>
<!-- 4.扫描web相关的bean -->
<context:component-scan base-package="zhe.xin.controller" />
</beans>
并且创建对应文件夹:

至此整个框架就搭建完后成了 , 在进行业务逻辑实现,只需要关注增删改查就可以了
九.测试所有代码,解决错误
1.Controller层:
@Controller //注册到bean
@RequestMapping("/book") //注册url地址
public class BookController {
//Controller层调用service层
//注入service
@Autowired
@Qualifier("BooksServiceImpl")
private BooksService booksService;
//查询全部书籍并返回一个页面:
@RequestMapping("/allBook")
public String list(Model model){ //传入控制器
//调用service层, 查询全部书籍
List<Books> allBook = booksService.queryAllBook();
//存入到控制器
model.addAttribute("booksList" , allBook);
//返回一个界面,,只需要写名称,因为在Spring-MVC.xml中已经注册了前缀和后缀
return "allBook";
}
}
自动注入@Qualifier("BooksServiceImpl")对应Spring-service.xml的

2.web层
1.创建jsp:

写一个简单的界面
<h1>书籍展示页面</h1>
然后在index中添加入口:
<h3>
<a href="${pageContext.request.contextPath}/book/allBook" >进入书籍展示页面</a>
</h3>
错误解决:
运行Tomcat:


排错:
问题: bean不符合
查找问题:
1.bookService,发现可以被Spring解析 ,,说明没有问题

2.Junit单元测试, 测试代码是否能查询到结果
@Test
public void queryallBook(){
//获取配置Spring的配置文件
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
//用Spring获取实例
BooksServiceImpl books = (BooksServiceImpl) context.getBean("BooksServiceImpl");
//调用实例中的方法
for (Books books1 : books.queryAllBook()) {
System.out.println(books1);
}
}
报错一:

问题: 路径找不到: org.xml.sax.SAXParseException错误
文件头的导包缺少:
<?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
http://www.springframework.org/schema/beans/spring-beans.xsd">
所有使用classpath:的改成:
<?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
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
https://www.springframework.org/schema/context/spring-context.xsd">
发现web.xml中只加载了mvc,我们把它改成 <param-value>classpath:applicationContext.xml</param-value>

运行成功:

报错三: 首页jsp乱码

在第一行,(必须第一行)添加:
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
解决:

十. 编写前端界面,和业务实现
前面的都是框架,只要框架写好业务和界面就很简单了,所以根基一定要牢固
1.编写界面:
<%@ 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>书籍列表 —— 显示所有书籍</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="${requestScope.get('booksList')}">
<tr>
<td>${book.getId()}</td>
<td>${book.getBook_name()}</td>
<td>${book.getBoo_kclass()}</td>
<td>${book.getBook_price()}</td>
</tr>
</c:forEach>
</tbody>
</table>
</div>
</div>
</div>
其他操作,增删改查都大同小异, 就不继续了,,看下成果:

项目地址:qq群:884095735

十一. 总结
还没想好...........

浙公网安备 33010602011771号