在【01】章中介绍了mybatis与Spring整合之后的开发流程,Spring与mybatis整合,本质上是Spring将各层进行整合,(Spring管理持久层的mapper、Spring管理业务层service、Spring管理表现层的handler)。本章将介绍Spring与service及controller进行整合的操作流程思路简单如下:
1.使用配置方式(后期会使用注解方式,显示配置方式也要会)将service接口配置在Spring配置文件中
2.实现事务控制
3.整合Springmvc
4.配置前端控制器
5.编写controller
6.编写jsp
7.加载Spring容器
8.测试
具体操作流程如下:
【1】定义service接口(ItemsService.java),定义接口实现类(ItemsServiceImpl.java)。新建service文件夹,路径:src\main\java\ssm\service,在文件夹下创建两个文件:
public interface ItemsService {
//商品查询列表
List<ItemsCustom> findItemsList(ItemsQueryVo itemsQueryVo) throws Exception;
}
实现类代码:
public class ItemsServiceImpl implements ItemsService {
@Autowired
private ItemsCustomMapper itemsCustomMapper;
public List<ItemsCustom> findItemsList(ItemsQueryVo itemsQueryVo) throws Exception {
//通过itemsMapperCustom查询数据库
return itemsCustomMapper.findItemsList(itemsQueryVo);
}
}
【2】在Spring容器配置service(applicationContext-service.xml)
创建applicationContext-service.xml文件,路径(src\main\resources\spring\applicationContext-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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<!--商品管理的service-->
<bean id="itemsService" class="ssm.service.itemsServiceImpl"/>
</beans>
【3】事务控制(applicationContext-transaction.xml)路径:src\main\resources\spring\applicationContext-transaction.xml,代码如下:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/tx