SpringMVC ,Spring 和 Mybatis 的整合

一、搭建整合环境

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

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

每一个环境搭建好之后,需要单独测试下,通过后,再进行下一步操作。
在这里插入图片描述
所以整个过程重要的是靠 Spring 整合两者,将三者关联起来

SSM整合相关 pom 和 xml 配置文件

二、搭建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下方便管理,这里还需另外配置文件的路径

<!--配置Spring的监听器,默认只加载WEB-INF目录下的applicationContext.xml配置文件-->
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    <!--设置配置文件的路径-->
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:applicationContext.xml</param-value>
    </context-param>

(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<Account> findAll() {
        System.out.println("业务层:查询所有账户...");
        return accountDao.findAll();
    }
    @Override
    public void saveAccount(Account account) {
        System.out.println("业务层:保存帐户...");
        accountDao.saveAccount(account);
    }
}

(2)AccountDao.java

/**
 * 帐户dao接口
 */
@Repository
public interface AccountDao {
    /**
     * 查询所有账户
     * @return
     */
    @Select("select * from account")
    public List<Account> 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<Account> 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";
    }
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章