美文网首页
springmvc和mybatis Demo

springmvc和mybatis Demo

作者: Stringer | 来源:发表于2016-12-05 20:30 被阅读31次

    配置文件
    web.xml
    添加spring容器监听器,加载spring容器

    <?xml version="1.0" encoding="UTF-8"?>
    <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
      
      <display-name>springmvc_mybatis01</display-name>
      
      <!-- 加载spring的容器 -->
        <context-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>/WEB-INF/classes/spring/applicationContext-*.xml</param-value>
        </context-param>
        <listener>
            <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
        </listener>
      
      <!-- springmvc前端控制器 -->
      <servlet>
        <servlet-name>springmvc</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <!-- contextConfigLocation配置springmvc加载的配置文件(处理器的映射器、适配器等) -->
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:spring/springmvc.xml</param-value>
        </init-param>
      </servlet>
      <servlet-mapping>
        <servlet-name>springmvc</servlet-name>
        <!-- 
            1.  .action  访问以.action结尾的由DispatcherServlet进行解析
            2.  /       搜有访问地址都有DispatcherServlet解析,对于静态文件的解析需要配置不让DispatcherServlet解析,
                        使用此方法可以实现RESTful风格的url
         -->
        <url-pattern>*.action</url-pattern>
      </servlet-mapping>
      
      <!-- post乱码过虑器 -->
        <filter>
            <filter-name>CharacterEncodingFilter</filter-name>
            <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
            <init-param>
                <param-name>encoding</param-name>
                <param-value>UTF-8</param-value>
            </init-param>
        </filter>
        <filter-mapping>
            <filter-name>CharacterEncodingFilter</filter-name>
            <url-pattern>/*</url-pattern>
        </filter-mapping>
      
      <welcome-file-list>
        <welcome-file>index.html</welcome-file>
        <welcome-file>index.htm</welcome-file>
        <welcome-file>index.jsp</welcome-file>
        <welcome-file>default.html</welcome-file>
        <welcome-file>default.htm</welcome-file>
        <welcome-file>default.jsp</welcome-file>
      </welcome-file-list>
    </web-app>```
    
    db.properties
    
    log4j.properties
    
    sqlMapConfig.xml
    配置:别名
    

    <?xml version="1.0" encoding="UTF-8" ?>
    <!DOCTYPE configuration
    PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
    "http://mybatis.org/dtd/mybatis-3-config.dtd">
    <configuration>

    <!-- 配置别名 -->
    <typeAliases>
        <!-- 批量扫描 -->
        <package name="cn.ztc.ssm.po"/>
    </typeAliases>
    

    </configuration>```

    applicationContext-dao.xml
    配置:数据源、SqlSessionFactory、mapper扫描器

    <beans xmlns="http://www.springframework.org/schema/beans"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc"
        xmlns:context="http://www.springframework.org/schema/context"
        xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
        xsi:schemaLocation="http://www.springframework.org/schema/beans 
            http://www.springframework.org/schema/beans/spring-beans-3.2.xsd 
            http://www.springframework.org/schema/mvc 
            http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd 
            http://www.springframework.org/schema/context 
            http://www.springframework.org/schema/context/spring-context-3.2.xsd 
            http://www.springframework.org/schema/aop 
            http://www.springframework.org/schema/aop/spring-aop-3.2.xsd 
            http://www.springframework.org/schema/tx 
            http://www.springframework.org/schema/tx/spring-tx-3.2.xsd ">
    
        <!-- 加载配置文件 -->
        <context:property-placeholder location="classpath:db.properties" />
    
        <!-- 配置数据源,dbcp --> 
        <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
            <property name="driverClassName" value="${jdbc.driver}"/>
            <property name="url" value="${jdbc.url}"/>
            <property name="username" value="${jdbc.username}"/>
            <property name="password" value="${jdbc.password}"/>
            <property name="maxActive" value="10"/>
            <property name="maxIdle" value="5"/>
        </bean> 
        
        <!-- sqlSessionFactory 
            在mybatis和spring的整合包中
        -->
        <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
            <!-- 加载mybatis的配置文件 -->
            <property name="configLocation" value="classpath:mybatis/sqlMapConfig.xml"/>
            <!-- 数据源 -->
            <property name="dataSource" ref="dataSource"/>
        </bean>
        
        <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
            <!-- 指定扫描包名
                如果扫描多个包,每个包中间使用半角逗号分隔 
                可删除sqlmapconfig.xml文件中的批量扫描配置
            -->
            
            <property name="basePackage" value="cn.ztc.ssm.mapper"/>
            <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
        </bean>
        
    </beans>```
    
    applicationContext-service.xml
    让spring管理service接口
    

    <beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
    http://www.springframework.org/schema/mvc
    http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context-3.2.xsd
    http://www.springframework.org/schema/aop
    http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
    http://www.springframework.org/schema/tx
    http://www.springframework.org/schema/tx/spring-tx-3.2.xsd ">

    <!-- 定义 商品管理的service--> 
    <bean id="itemsService" class="cn.ztc.ssm.service.impl.ItemsServiceImpl"/>
    

    </beans>```

    applicationContext-transaction.xml

    <beans xmlns="http://www.springframework.org/schema/beans"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc"
        xmlns:context="http://www.springframework.org/schema/context"
        xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
        xsi:schemaLocation="http://www.springframework.org/schema/beans 
            http://www.springframework.org/schema/beans/spring-beans-3.2.xsd 
            http://www.springframework.org/schema/mvc 
            http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd 
            http://www.springframework.org/schema/context 
            http://www.springframework.org/schema/context/spring-context-3.2.xsd 
            http://www.springframework.org/schema/aop 
            http://www.springframework.org/schema/aop/spring-aop-3.2.xsd 
            http://www.springframework.org/schema/tx 
            http://www.springframework.org/schema/tx/spring-tx-3.2.xsd ">
            
        <!-- 事务管理器
             对mybatis操作数据库事务控制,spring使用jdbc的事务控制类
         -->
         <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
            <!-- 数据源 -->
            <property name="dataSource" ref="dataSource"></property>
         </bean>
         
         <!-- 通知 -->
         <tx:advice id="txAdvice" transaction-manager="transactionManager">
            <tx:attributes>
                <!-- 传播行为 -->
                <tx:method name="save*" propagation="REQUIRED"/>
                <tx:method name="delete*" propagation="REQUIRED"/>
                <tx:method name="insert*" propagation="REQUIRED"/>
                <tx:method name="update*" propagation="REQUIRED"/>
                <tx:method name="find*" propagation="SUPPORTS" read-only="true"/>
                <tx:method name="get*" propagation="SUPPORTS" read-only="true"/>
                <tx:method name="select*" propagation="SUPPORTS" read-only="true"/>
            </tx:attributes>
         </tx:advice>
         
         <!-- aop -->
         <aop:config>
            <aop:advisor advice-ref="txAdvice" pointcut="execution(* cn.ztc.ssm.service.impl.*.*(..))"/>
         </aop:config>
            
    </beans>```
    
    springmvc.xml
    配置处理器映射器、适配器、视图解析器
    

    <beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
    http://www.springframework.org/schema/mvc
    http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context-3.2.xsd
    http://www.springframework.org/schema/aop
    http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
    http://www.springframework.org/schema/tx
    http://www.springframework.org/schema/tx/spring-tx-3.2.xsd ">

    <!-- 注解的handller
        实际开发中建议使用组件扫描
     -->
    <!-- <bean class="cn.ztc.ssm.controller.ItemsController3"/> -->
    <!-- 可以扫描controller service -->
    <context:component-scan base-package="cn.ztc.ssm.controller"></context:component-scan>
    
    <!-- 使用 mvc:annotation-driven可以代替注解映射器、注解适配器的配置
        默认加载了很多参数绑定方法,例如json转换解析器
        实际开发中用这个配置
    -->
    <mvc:annotation-driven conversion-service="conversionService"></mvc:annotation-driven>
    
    
    <!-- 视图解析器 
        解析jsp,默认使用jstl标签
    -->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/jsp/"/>
        <property name="suffix" value=".jsp"/>
    </bean>
    
    <!-- 自定义参数绑定 -->
    <bean id="conversionService" class="org.springframework.format.support.FormattingConversionServiceFactoryBean">
        <property name="converters">
            <list>
                <!-- 日期类型转换 -->
                <bean class="cn.ztc.ssm.controller.converter.CustomDateConverter"/>
            </list>
        </property>
    </bean>
    

    </beans>```

    Mapper接口和配置文件
    po类由逆向工程生成

    ItemsCustomMapper.xml

    <?xml version="1.0" encoding="UTF-8" ?>
    <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
    <mapper namespace="cn.ztc.ssm.mapper.ItemsCustomMapper" >
    
        <!-- 定义商品查询的sql片段,就是商品的查询条件
            商品查询条件通过ItemsQueryVo包装对象中itemsCustom属性传递
         -->
        <sql id="query_items_where">
            <!-- 使用动态sql,通过if判断,满足条件进行sql拼接 -->
            <if test="itemsCustom!=null">
                <if test="itemsCustom.name!=null and itemsCustom.name!=''">
                    items.name like '%${itemsCustom.name}%'
                </if>
            </if>
        </sql>
        
        <!-- 商品的列表查询 
            parameterType建议传入包装对象
            resultType建议使用扩展对象
        -->
        <select id="findItemsList" parameterType="cn.ztc.ssm.po.ItemsQueryVo" resultType="cn.ztc.ssm.po.ItemsCustom">
            SELECT * FROM items
            <where>
                <include refid="query_items_where"></include>
            </where>
        </select>
    </mapper>```
    
    ItemsCustomMapper.java
    

    package cn.ztc.ssm.mapper;

    import java.util.List;

    import cn.ztc.ssm.po.*;

    public interface ItemsCustomMapper {

    public List<ItemsCustom> findItemsList(ItemsQueryVo itemsQueryVo)throws Exception;
    

    }

    
    ItemsService.java
    

    package cn.ztc.ssm.service;

    import java.util.List;

    import cn.ztc.ssm.po.ItemsCustom;
    import cn.ztc.ssm.po.ItemsQueryVo;

    public interface ItemsService {
    //商品查询列表
    public List<ItemsCustom> findItemsList(ItemsQueryVo itemsQueryVo)throws Exception;

    //根据id查询商品信息
    public ItemsCustom findItemsById(Integer id)throws Exception;
    
    //修改商品信息
    public void updateItems(Integer id, ItemsCustom itemsCustom)throws Exception;
    

    }

    
    ItemsServiceImpl.java
    

    package cn.ztc.ssm.service.impl;

    import java.util.List;

    import org.springframework.beans.BeanUtils;
    import org.springframework.beans.factory.annotation.Autowired;

    import cn.ztc.ssm.mapper.;
    import cn.ztc.ssm.po.
    ;
    import cn.ztc.ssm.service.ItemsService;

    public class ItemsServiceImpl implements ItemsService{

    @Autowired
    private ItemsCustomMapper itemsCustomMapper;
    
    @Autowired
    private ItemsMapper itemsMapper;
    
    @Override
    public List<ItemsCustom> findItemsList(ItemsQueryVo itemsQueryVo) throws Exception {
        // TODO Auto-generated method stub
        return itemsCustomMapper.findItemsList(itemsQueryVo);
    }
    
    
    @Override
    public ItemsCustom findItemsById(Integer id) throws Exception {
        Items items = itemsMapper.selectByPrimaryKey(id);
        ItemsCustom itemsCustom = new ItemsCustom();
        BeanUtils.copyProperties(items, itemsCustom);
        return itemsCustom;
    }
    
    
    @Override
    public void updateItems(Integer id, ItemsCustom itemsCustom) throws Exception {
        //添加校验
        
        //更新商品信息,可以更新表中的所有字段,包括大文本类型
        itemsCustom.setId(id);
        itemsMapper.updateByPrimaryKeyWithBLOBs(itemsCustom);
        
    }
    

    }

    
    ItemsController.java
    

    package cn.ztc.ssm.controller;

    import java.util.ArrayList;
    import java.util.List;

    import javax.servlet.http.HttpServletRequest;

    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Controller;
    import org.springframework.ui.Model;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RequestMethod;
    import org.springframework.web.bind.annotation.RequestParam;
    import org.springframework.web.servlet.ModelAndView;

    import cn.ztc.ssm.po.*;
    import cn.ztc.ssm.service.ItemsService;

    @Controller
    @RequestMapping("/items")
    public class ItemsController {

    @Autowired
    private ItemsService itemsService;
    
    @RequestMapping("/queryItems")
    public ModelAndView queryItems(HttpServletRequest request) throws Exception{
        //调用service查找数据库,查询商品列表,这里使用静态数据模拟
        List<ItemsCustom> itemsList = itemsService.findItemsList(null);
        //测试forward后request是否能共享
        System.out.println(request.getParameter("id"));
        
        ModelAndView modelAndView = new ModelAndView();
        //在jsp页面中通过itemsList取数据
        modelAndView.addObject("itemsList",itemsList);
        //指定视图
        modelAndView.setViewName("items/itemsList");
    
        return modelAndView;
    }
    
    
    
    //商品信息修改页面
    

    // @RequestMapping(value="/editItems",method={RequestMethod.POST,RequestMethod.GET})
    // public ModelAndView editItems() throws Exception{
    // ItemsCustom itemsCustom = itemsService.findItemsById(1);
    //
    // ModelAndView modelAndView = new ModelAndView();
    //
    // modelAndView.addObject("itemsCustom", itemsCustom);
    //
    // modelAndView.setViewName("items/editItems");
    //
    // return modelAndView;
    // }

    //返回逻辑视图名
    //jsp全路径:前缀+逻辑视图名+后缀    
    @RequestMapping(value="/editItems",method={RequestMethod.POST,RequestMethod.GET})
    //@RequestParam里面指定request传入参数名称和形参绑定
    public String editItems(Model model,@RequestParam(value="id",required=true,defaultValue="1")Integer items_id) throws Exception{
        ItemsCustom itemsCustom = itemsService.findItemsById(items_id);
        
        model.addAttribute("itemsCustom",itemsCustom);
        return "items/editItems";
    }
    
    //商品信息修改提交
    @RequestMapping("/editItemsSubmit")
    public String editItemsSubmit(HttpServletRequest request,Integer id,ItemsCustom itemsCustom) throws Exception{
        itemsService.updateItems(id, itemsCustom);
    
        return "success";
        //重定向到商品的查询列表,request不能共享
        //return "redirect:queryItems.action";
        //页面转发,request能共享
    

    // return "forward:queryItems.action";
    }
    }

    
    CustomDateConverter.java
    

    package cn.ztc.ssm.controller.converter;

    import java.text.ParseException;
    import java.text.SimpleDateFormat;
    import java.util.Date;

    import org.springframework.core.convert.converter.Converter;

    public class CustomDateConverter implements Converter<String, Date>{

    @Override
    public Date convert(String s) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        try {
            return simpleDateFormat.parse(s);
        } catch (ParseException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return null;
    }
    

    }

    
    页面
    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"%>
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>查询商品列表</title>
    </head>
    <body>
    <form action="${pageContext.request.contextPath }/item/queryItem.action" method="post">
    查询条件:
    <table width="100%" border=1>
    <tr>
    <td><input type="submit" value="查询"/></td>
    </tr>
    </table>
    商品列表:
    <table width="100%" border=1>
    <tr>
    <td>商品名称</td>
    <td>商品价格</td>
    <td>生产日期</td>
    <td>商品描述</td>
    <td>操作</td>
    </tr>
    <c:forEach items="${itemsList }" var="item">
    <tr>
    <td>${item.name }</td>
    <td>${item.price }</td>
    <td><fmt:formatDate value="${item.createtime}" pattern="yyyy-MM-dd HH:mm:ss"/></td>
    <td>${item.detail }</td>

    <td><a href="${pageContext.request.contextPath }/items/editItems.action?id=${item.id}">修改</a></td>
    

    </tr>
    </c:forEach>

    </table>
    </form>
    </body>

    </html>```
    editItems.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"%>
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>修改商品信息</title>
    
    </head>
    <body> 
    
    <form id="itemForm" action="${pageContext.request.contextPath }/items/editItemsSubmit.action" method="post" >
    <input type="hidden" name="id" value="${itemsCustom.id }"/>
    修改商品信息:
    <table width="100%" border=1>
    <tr>
        <td>商品名称</td>
        <td><input type="text" name="name" value="${itemsCustom.name }"/></td>
    </tr>
    <tr>
        <td>商品价格</td>
        <td><input type="text" name="price" value="${itemsCustom.price }"/></td>
    </tr>
    <tr>
        <td>商品生产日期</td>
        <td><input type="text" name="createtime" value="<fmt:formatDate value="${itemsCustom.createtime}" pattern="yyyy-MM-dd HH:mm:ss"/>"/></td>
    </tr>
    <%-- <tr>
        <td>商品图片</td>
        <td>
            <c:if test="${item.pic !=null}">
                <img src="/pic/${item.pic}" width=100 height=100/>
                <br/>
            </c:if>
            <input type="file"  name="pictureFile"/> 
        </td>
    </tr> --%>
    <tr>
        <td>商品简介</td>
        <td>
        <textarea rows="3" cols="30" name="detail">${itemsCustom.detail }</textarea>
        </td>
    </tr>
    <tr>
    <td colspan="2" align="center"><input type="submit" value="提交"/>
    </td>
    </tr>
    </table>
    
    </form>
    </body>
    
    </html>```
    
    spring参数绑定过程
    
    从客户端请求key/value数据,经过参数绑定,将key/value数据绑定到controller方法的形参上。springmvc中,接收页面提交的数据是通过方法形参来接收。
    
    1.默认支持的类型:直接在controller方法形参上定义下边类型的对象,就可以使用这些对象。在参数绑定过程中,如果遇到下边类型直接进行绑定。
    
    HttpServletRequest
    
    通过request对象获取请求信息
    
    HttpServletResponse
    
    通过response处理响应信息
    
    HttpSession
    
    通过session对象得到session中存放的对象
    
    Model/ModelMap
    
    model是一个接口,modelMap是一个接口实现 。
    作用:将model数据填充到request域。
    
    2.pojo绑定
    页面中input的name和controller的pojo形参中的属性名称一致,将页面中数据绑定到pojo。
    
    3.自定义参数绑定实现日期类型绑定
    对于controller形参中pojo对象,如果属性中有日期类型,需要自定义参数绑定。
    将请求日期数据串传成 日期类型,要转换的日期类型和pojo中日期属性的类型保持一致。

    相关文章

      网友评论

          本文标题:springmvc和mybatis Demo

          本文链接:https://www.haomeiwen.com/subject/wvkxmttx.html