博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
SpringMVC+Mybatis框架搭建
阅读量:7002 次
发布时间:2019-06-27

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

hot3.png

1. 准备环境


1. 所需jar包
  • 数据库驱动包
  • mybatis jars
  • mybatis和spring的整合包
  • log4j包
  • 数据库连接池包 dbcp
  • spring包
  • jstl
  • junit
  • aspectjweaver.jar
  • commons-pool2-2.4.2
2. 工程结构

输入图片说明

2. dao层


1. sqlMapConfig.xml
  • mybatis本身的配置文件
2. applicationContext-dao.xml
  • 配置:SqlSessionFactory、数据源、mapper扫描器
3. 逆向工程(generatorSqlmapCustom)生成po类以及mapper(单标增删改查)
  • 将生成的文件拷贝到工程中。
4. 手动定义商品查询mapper
  • 针对综合查询mapper,一般情况会有关联查询,所以建议自定义mapper。

  • ItemsMapperCustom.xml

name like '%${itemsCustom.name}%'
  • ItemsmapperCustom.java
package com.swxc.ssm.mapper;import com.swxc.ssm.po.ItemsCustom;import com.swxc.ssm.po.ItemsQueryVo;import java.util.List;/** * Description * Author shadowolf * Date 2017/7/26. 3:47 * Version 1.0 */public interface ItemsMapperCustom {    public List
findItemsList(ItemsQueryVo itemsQueryVo) throws Exception;}

3. service层


1. 定义service 接口
package com.swxc.ssm.service;import com.swxc.ssm.po.ItemsCustom;import com.swxc.ssm.po.ItemsQueryVo;import java.util.List;/** * Description 商品管理service * Author shadowolf * Date 2017/7/26. 8:32 * Version 1.0 */public interface ItemsService {    // 商品查询列表    public List
findItemsList(ItemsQueryVo itemsQueryVo) throws Exception;}
package com.swxc.ssm.service.impl;import com.swxc.ssm.mapper.ItemsMapperCustom;import com.swxc.ssm.po.ItemsCustom;import com.swxc.ssm.po.ItemsQueryVo;import com.swxc.ssm.service.ItemsService;import org.springframework.beans.factory.annotation.Autowired;import java.util.List;/** * Description 商品管理 * Author shadowolf * Date 2017/7/26. 9:22 * Version 1.0 */public class ItemsServiceImpl implements ItemsService {    @Autowired    private ItemsMapperCustom itemsMapperCustom;    @Override    public List
findItemsList(ItemsQueryVo itemsQueryVo) throws Exception { // 通过ItemsMapperCustom查询数据库 return itemsMapperCustom.findItemsList(itemsQueryVo); }}
2. 在spring容器中配置service
  • 创建applicationContext-service.xml,文件中配置service。
3. 事务控制(applicationContext-transaction.xml)
  • 在applicationContext-transaction.xml中,使用spring声明式事务控制方法。

4. springmvc


1. 配置springmvc.xml
  • 创建springmvc文件,配置处理器映射器、适配器、视图解析器。
2. 配置前端控制器(web.xml)
springmvc
org.springframework.web.servlet.DispatcherServlet
contextConfigLocation
classpath:spring/springmvc.xml
springmvc
*.action
3. 编写Controller(就是Handler)
package com.swxc.ssm.controller;import com.swxc.ssm.po.ItemsCustom;import com.swxc.ssm.service.ItemsService;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.servlet.ModelAndView;import java.util.List;/** * Description 商品的Controller * Author shadowolf * Date 2017/7/26. 11:20 * Version 1.0 */@Controllerpublic class ItemsController {    @Autowired    private ItemsService itemsService;    // 商品的查询    @RequestMapping("/queryItems")    public ModelAndView queryItems() throws Exception {        List
itemsList = itemsService.findItemsList(null); ModelAndView modelAndView = new ModelAndView(); modelAndView.addObject("itemsList", itemsList); modelAndView.setViewName("items/itemsList"); return modelAndView; }}
5. 编写jsp页面
  • WEB-INF/jsp/itemsList.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %><%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %><%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt" %>    
查询商品列表
查询条件:
商品列表:
商品名称 商品价值 生产日期 商品描述 操作
${item.name } ${item.price }
${item.detail } 修改

5. 加载spring容器


  • 将mapper、service、controller加载到spring容器中。(以下文件)
    • mapper使用了扫描器
    • service使用了声明式的配置方式
    • controller使用了组件扫描
      输入图片说明
  • 建议使用通配符来加载上边的配置文件
  • 方法:
    • 在web.xml中,添加spring容器的监听器,加载spring容器。
contextConfigLocation
classpath:spring/applicationContext-*.xml
org.springframework.web.context.ContextLoaderListener

转载于:https://my.oschina.net/shadowolf/blog/1512657

你可能感兴趣的文章