SpringMVC+Spring+mybatis 项目实践

2021-05-06 10:28

阅读:479

标签:帮助   return   loading   import   aop   equal   ret   resultmap   mode   

一、配置文件

1.1 jdbc配置文件

技术图片

 

 1.2 mybatis配置文件

技术图片

 

 1.3 spring-mybatis配置文件

技术图片

 

 1.4 springmvc配置文件

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">


    
    mvc:annotation-driven>
    mvc:annotation-driven>
    
    
    context:component-scan base-package="cn/edu360/ssm">context:component-scan>
    
    bean
        class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        
        property name="prefix" value="/WEB-INF/pages/" />
        
        property name="suffix" value=".jsp" />
    bean>
    
     bean id="multipartResolver" 
        class="org.springframework.web.multipart.commons.CommonsMultipartResolver"> 
         
        property name="maxUploadSize"> 
        value>1048576value> 
        property> 
    bean>

   mvc:interceptors>
     
    mvc:interceptor>

        mvc:mapping path="/*"/>
        bean class="cn.edu360.ssm.Interceptor.SessionInterceptor">bean>
    mvc:interceptor>
    
     
   

mvc:interceptors>
   
beans>

二、Controller层

package cn.edu360.ssm.controller;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.text.SimpleDateFormat;
import java.util.Collections;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import cn.edu360.ssm.model.News;
import cn.edu360.ssm.service.NewsService;

@Controller
public class NewsController {
    
    @Resource
    private NewsService newsService;
    
    //跳转到发布新闻界面
    @RequestMapping("/publishNews.shtml")
    public String publishNews() {
        return "publishNews";
    }
    
    //插入新闻
    @RequestMapping("/insertNews.shtml")
    public String InsertNews(News news, HttpServletRequest request, HttpServletResponse response) {
        if(news.getTitle().trim().equals("") || news.getTitle().trim().equals("")) {
            return "redirect:newsHref.shtml";
        }
        Date date = new Date();
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd:HH:mm");
        String date2String = format.format(date);
        news.setDate(date2String);
        newsService.insertNews(news);
        Cookie[] cookies = request.getCookies();
        if(cookies != null) {
            for(Cookie cookie : cookies) {
                if(cookie.getName().equals("newsTitle") 
                        || cookie.getName().equals("newsBody")) {
                    cookie.setMaxAge(0);
                    response.addCookie(cookie);
                    
                }
            }
        }
        return "redirect:newsHref.shtml";
    }
    
    //查找新闻将url显示在界面
    @RequestMapping("/newsHref.shtml")
    public String searchNewsHref(Model model) {
        List newList = newsService.findAllNewsHref();
        Collections.reverse(newList);
        model.addAttribute("newsList", newList);
        return "news";
    }
    
    //按照url后的id查找新闻
    @RequestMapping("/newsView.shtml")
    public String findNewsById(int newsId, HttpServletRequest request) {
        News news = newsService.findNewsById(newsId);
        request.getSession().setAttribute("news", news);
        return "newsView";
    }
    
    //保存新闻cookie
    @RequestMapping("/save.shtml")
    public String saveNewsCookie(News news, HttpServletRequest request, HttpServletResponse response) {
        Cookie newsTitleCookie = null;
        Cookie newsBodyCookie = null;
        try {
            newsTitleCookie = new Cookie("newsTitle", URLEncoder
                    .encode(news.getTitle(), "utf-8"));
            newsBodyCookie = new Cookie("newsBody", URLEncoder
                    .encode(news.getBody(), "utf-8"));
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } // 对中文编码保存
        
        newsTitleCookie.setMaxAge(60 * 60 * 24);
        newsTitleCookie.setPath("/ssm");
        response.addCookie(newsTitleCookie);
        
        newsBodyCookie.setMaxAge(60 * 60 * 24);
        newsBodyCookie.setPath("/ssm");
        response.addCookie(newsBodyCookie);
        return "redirect:index.shtml";
    }
    
}

三、Service层

3.1 接口

技术图片

 

 3.2 实现类

技术图片

 

 四、DAO层

4.1 接口

技术图片

 

 4.2 mybatis配置实现

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.edu360.ssm.dao.NewsMapper" >

  resultMap id="BaseResultMap" type="cn.edu360.ssm.model.News" >
    result column="news_id" property="newsId" jdbcType="INTEGER" />
    result column="title" property="title" jdbcType="VARCHAR" />
    result column="body" property="body" jdbcType="VARCHAR" />
    result column="news_date" property="date" jdbcType="VARCHAR" />
  resultMap>
  
  select id="findNewsById" parameterType="java.lang.Integer" resultMap="BaseResultMap">
    select * from news where news_id = #{newsId}
  select>
  
  select id="findAllNewsHref" resultMap="BaseResultMap">
    select * from news
  select>
  
  insert id="insertNews" parameterType="news">
      insert into news (title, body, news_date) values(#{title}, #{body}, #{date})
  insert>
  
mapper>

五、效果

5.1 新闻发布

技术图片

 

 技术图片

 

 5.2 查看:

技术图片

 5.3 删除

技术图片

 

 技术图片

码云地址:https://gitee.com/chaserff/ssm.git

SpringMVC+Spring+mybatis 项目实践

标签:帮助   return   loading   import   aop   equal   ret   resultmap   mode   

原文地址:https://www.cnblogs.com/chaserFF/p/13168854.html


评论


亲,登录后才可以留言!