本文针对上一篇文章做如下优化:
1.dao层:将dao层的实现类去掉,使用更高功能的类;
<bean id="mapperScannerConfigurer" class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="sqlSessionFactoryBeanName" value="sqlsessionfactory"/>
<property name="basePackage" value="com.fei.dao"/>
</bean>
MapperScannerConfigurer该类的作用:自动扫描配置的包下的接口,会用动态代理生成该接口的对象,并且默认的该对象的id名是接口名(首字母小写)
所以:
1.不需要手动再写dao层接口的实现类
2.由于第一点,所以就不需要在spring-dao.xml文件中配置SqlsessionTemplate和接口实现类啦
3.注意修改spring-service层实现类的引用
故上文中的如下内容可以删除:
<!-- 3.配置sqlsession -->
<bean id="sqlsession" class="org.mybatis.spring.SqlSessionTemplate">
<constructor-arg index="0" ref="sqlsessionfactory"/>
</bean>
<!--配置BookMapperImpl-->
<bean id="BookMapperImpl" class="com.fei.dao.BookMapperImpl">
<property name="sqlSession" ref="sqlsession"/>
</bean>
切记:要想使用注解给该生的对象赋值id,必须增加注解驱动:<context:component-scan base-package=“com.fei.dao”/>
注意:在上述配置好以后,在启动服务器时出现如下错误:war exploded: Error during artifact deployment.
解决方法:在project structure中artifacts中在WEB-INF的目录下增加lib目录,将导入的jar包添加到该目录下
2.service层的优化:是用注解注册service层的实现类 <context:component-scan base-package=“com.fei.controller”/>
到这一步做了测试,出现如下错误:
(1) Loading class `com.mysql.jdbc.Driver'. This is deprecated. The new driver class is `com.mysql.cj.jdbc.Driver'.
The driver is automatically registered via the SPI and manual loading of the driver class is generally unnecessary.
解决:将db,properties的驱动改为: jdbc.driver=com.mysql.cj.jdbc.Driver
(2)空指针异常
在只使用@Qualifier 时,就回报空指针异常,经过查询资料发现,该注解必须与@Autowired结合使用,使@Autowired由type查找转为name查找
或者直接使用@Resource该注解既可以用name查找,也可以用type查找 @Resource(name = “Book”)
3.控制层的优化:自定义控制类,使得一个控制类中可以实现多个方法
(1)在spring-mvc.xml文件中增加注解驱动:mvc:annotation-driven/
以后就不需要手动导入处理器映射器 和处理器适配器
<bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"/>
<bean class="org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter"/>
(2)静态资源过滤: mvc:default-servlet-handler/
简单理解:它会像一个检查员,对进入DispatcherServlet的URL进行筛查,如果发现是静态资源的请求,就将该请求转由Web应用服务器默认的Servlet处理,
如果不是静态资源的请求,才由DispatcherServlet继续处理。
想更深入的理解,可以看: https://www.cnblogs.com/dflmg/p/6393416.html
静态资源和动态资源:https://blog.youkuaiyun.com/weixin_40122615/article/details/78493744
1.静态资源和动态资源的概念
简单来说: 静态资源:一般客户端发送请求到web服务器,web服务器从内存在取到相应的文件,返回给客户端,客户端解析并渲染显示出来。
动态资源:一般客户端请求的动态资源,先将请求交于web容器,web容器连接数据库,数据库处理数据之后,将内容交给web服务器,web服务器返回给客户端解析渲染处理。
2.静态资源和动态资源的区别
a.静态资源一般都是设计好的html页面,而动态资源依靠设计好的程序来实现按照需求的动态响应;
b.静态资源的交互性差,动态资源可以根据需求自由实现;
c.在服务器的运行状态不同,静态资源不需要与数据库参于程序处理,动态可能需要多个数据库的参与运算。
(3)扫描包:controller: <context:component-scan base-package=“com.fei.controller”/>
有这个自动扫描器,就不需要手动导入控制器中的类了
到这一步,做了测试,都没问题,接下来,实现一个控制器可以有多个方法的功能
1.首先自定义一个类,使用@Controller注解该类
2.在该类中利用注解注入ServiceImpl类
3.此时就可以写多个方法了,返回的就是要跳转的页面
如 :
//查询全部书籍,@RequestMapping表示要调用该方法的url,RequestMapping的详细说明:https://www.cnblogs.com/qq78292959/p/3760560.html
@RequestMapping("/AllBook")
public String list(Model model) {
List<Books> list = bookService.selectAll();
model.addAttribute("msg",list);
return "test";
}
OK,接下来完整的吧修改以后的地方写出:
1.修改db.properties中的驱动
jdbc.driver=com.mysql.cj.jdbc.Driver
2.代码
(1)dao层:(只有一个接口(如下)和一个配置文件(不变),实现类不需要了)
package com.fei.dao;
import com.fei.pojo.Books;
import org.springframework.stereotype.Repository;
import java.util.List;
@Repository("Book")
public interface BookMapper {
//增加一本书
public int addBook(Books books);
//根据id删除一本书
public int delete(int bookId);
//修改一本书
public int update(Books books);
//查询所有书籍
public List<Books> selectAll();
//根据id名查询书籍信息
public Books selectById(int bookId);
}
与之相对应的sprig-dao.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"
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">
<context:component-scan base-package="com.fei.dao"/>
<context:property-placeholder location="classpath:db.properties"/>
<!--1.将原先在mybatis中的数据库转移到mybatis-->
<bean id="datasource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="${jdbc.driver}"/>
<property name="url" value="${jdbc.url}"/>
<property name="username" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
</bean>
<!--2.配置Sqlsessionfactory-->
<bean id="sqlsessionfactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="datasource"/>
<property name="configLocation" value="classpath:mybatis-config.xml"/>
<property name="mapperLocations" value="classpath:com/fei/dao/BookMapper.xml"/>
</bean>
<!--3.配置MapperScannerConfigurer,系统会自动扫描fao包,并创建出一个dao接口的对象,不再需要SqlsessionTemplate和接口的实现类了
此时一定要注意spring-service的引用-->
<bean id="mapperScannerConfigurer" class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="sqlSessionFactoryBeanName" value="sqlsessionfactory"/>
<property name="basePackage" value="com.fei.dao"/>
</bean>
</beans>
(2)service层:接口没有变化,实现类增加了注解驱动
package com.fei.servie;
import com.fei.dao.BookMapper;
import com.fei.pojo.Books;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.List;
@Service("BookServiceImpl")
public class BookServiceImpl implements BookService {
@Resource(name = "Book")
private BookMapper bookMapper;
public void setBookMapper(BookMapper bookMapper) {
this.bookMapper = bookMapper;
}
public int addBook(Books books) {
return bookMapper.addBook(books);
}
public int delete(int bookId) {
return bookMapper.delete(bookId);
}
public int update(Books books) {
return bookMapper.update(books);
}
public List<Books> selectAll() {
return bookMapper.selectAll();
}
public Books selectById(int bookId) {
return bookMapper.selectById(bookId);
}
}
与之对应的spring-dao.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"
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">
<context:component-scan base-package="com.fei.servie"/>
</beans>
(3)controller层:不再实现controller接口了,手动定义
package com.fei.controller;
import com.fei.pojo.Books;
import com.fei.servie.BookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import javax.jws.WebParam;
import java.util.List;
@org.springframework.stereotype.Controller
public class Controller {
@Autowired
@Qualifier("BookServiceImpl")
private BookService bookService;
public void setBookService(BookService bookService) {
this.bookService = bookService;
}
//查询全部书籍
@RequestMapping("/AllBook")
public String list(Model model) {
List<Books> list = bookService.selectAll();
model.addAttribute("msg",list);
return "test";
}
//根据id查询
@RequestMapping("BookById")
public String listId(Model model) {
Books list = bookService.selectById(1);
model.addAttribute("msg",list);
return "test";
}
}
与之对应的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: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
https://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/mvc
https://www.springframework.org/schema/mvc/spring-mvc.xsd">
<!--1.增加注解驱动:就不需要手动处理器映射器和处理器适配器了-->
<mvc:annotation-driven/>
<!--2.增加静态资源过滤:使静态资源不需要经过DispatcherServlete处理-->
<mvc:default-servlet-handler/>
<context:component-scan base-package="com.fei.controller"/>
<!--3.视图解析器-->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" id="InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/"/>
<property name="suffix" value=".jsp"/>
</bean>
</beans>
3其余的配置文件:pom.xml,web.xml,application.xml,mybatis.xml,mapper.xml以及pojo实体类不做改变
ok ,目前为止,就对上一篇文章做了简单的优化,可以去启动tomcat啦。
当然还有spring的事务管理和AOP还没有融进来,之后会做进一步的总结,
由于我也是刚开始学习整理这一块的内容,希望大家可以指出错误,欢迎大家积极评论,让我们一起成长,共同感受学习的美丽!