【Mybatis升级版-02】mybatis与Spring整合service及controller

本文详细介绍了如何将Mybatis与Spring整合,重点在于service和controller的集成。首先,通过配置方式将service接口加入Spring配置文件,然后实现事务控制,接着整合Springmvc,配置前端控制器,编写controller并设计对应的jsp页面。整个流程涵盖从定义service接口到测试的全部步骤。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

【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
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值