一篇文章带你轻松搞定 SpringMVC ,Spring 和 Mybatis 的整合
发布日期:2021-05-07 19:44:13 浏览次数:15 分类:精选文章

本文共 2956 字,大约阅读时间需要 9 分钟。

文章目录

一、搭建整合环境

整合说明:SSM整合可以使用多种方式,咱们会选择XML + 注解的方式

自己写的类可以尽量使用注解,选用的框架的类尽量使用配置问价

整合的思路:

  • 先搭建整合的环境
  • 先把Spring的配置搭建完成
  • 再使用Spring整合SpringMVC框架
  • 最后使用Spring整合MyBatis框架

每一个环境搭建好之后,需要单独测试下,通过后,再进行下一步操作。

在这里插入图片描述
所以整个过程重要的是靠 Spring 整合两者,将三者关联起来

二、搭建Spring框架

保证 Spring 框架在 web 工程中独立运行

(1)编写 spring 配置文件 applicationContext.xml ,编写具体的配置信息
在这里插入图片描述
(2)编写测试方法,进行测试

@Test    public void run1(){           //1.加载配置文件        ApplicationContext ac = new ClassPathXmlApplicationContext("classpath:applicationContext.xml");        //2.获取对象        AccountService as = (AccountService) ac.getBean("accountService");        //3.调用方法        as.findAll();    }

三、搭建SpringMVC框架

(1)在web.xml中配置DispatcherServlet前端控制器

(2)在web.xml中配置DispatcherServlet过滤器解决中文乱码
(3)创建springmvc.xml的配置文件,编写配置文件
(4)测试SpringMVC的框架搭建是否成功

编写index.jsp和list.jsp编写,超链接	创建AccountController类,编写方法,进行测试

四、Spring整合SpringMVC框架

在这里插入图片描述

(1)目的:在 controller 中能成功的调用 service 对象中的方法
(2)在项目启动的时候,就去加载applicationContext.xml的配置文件,在web.xml中配置
ContextLoaderListener监听器(该监听器只能加载 WEB-INF 目录下的applicationContext.xml的配置文件)

为了保证配置文件都在 resources下方便管理,这里还需另外配置文件的路径

org.springframework.web.context.ContextLoaderListener
contextConfigLocation
classpath:applicationContext.xml

(3)在controller中注入service对象,调用service对象的方法进行测试

在这里插入图片描述

五、搭建 MyBatis 的环境并整合到 spring 中

(1)在 web 项目中编写SqlMapConfig.xml的配置文件,编写核心配置文件

(2)在 AccountDao 接口的方法上添加注解,编写SQL语句

在这里插入图片描述
(3)把SqlMapConfig.xml配置文件中的内容配置到applicationContext.xml配置文件中
在这里插入图片描述
(4)配置 Spring 的声明式事务管理

在这里插入图片描述

六、代码文件

在这里插入图片描述

(1)AccountServiceImpl.java

@Service("accountService")public class AccountServiceImpl implements AccountService {       @Autowired    private AccountDao accountDao;    @Override    public List
findAll() { System.out.println("业务层:查询所有账户..."); return accountDao.findAll(); } @Override public void saveAccount(Account account) { System.out.println("业务层:保存帐户..."); accountDao.saveAccount(account); }}

(2)AccountDao.java

/** * 帐户dao接口 */@Repositorypublic interface AccountDao {       /**     * 查询所有账户     * @return     */    @Select("select * from account")    public List
findAll(); /** * 保存账户信息 * @param account */ @Insert("insert into account(id,name,money)values(#{id},#{name},#{money})") public void saveAccount(Account account);}

(3)AccountController.java

/** * 账户控制层 */@Controller@RequestMapping("/account")public class AccountController {       @Autowired    private AccountService accountService;    @RequestMapping("/findAll")    public String findAll(Model model){           System.out.println("表现层:查询所有账户");        List
list = accountService.findAll(); model.addAttribute("list",list); return "list"; } @RequestMapping("/save") public String save(Account account){ System.out.println("表现层:保存账户"); accountService.saveAccount(account); //保存之后重定向到查询方法 return "redirect:/account/findAll"; }}
上一篇:(SpringMVC)springMVC.xml 和 web.xml
下一篇:Spring-SpringMVC-Mybatis 整合相关 xml 配置文件

发表评论

最新留言

逛到本站,mark一下
[***.202.152.39]2025年04月06日 05时29分20秒