1. 程式人生 > 實用技巧 >SpringMVC+Spring+mybatis 專案實踐

SpringMVC+Spring+mybatis 專案實踐

一、配置檔案

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> <!-- 附件掃描器 會掃描指定包下面的controller、service、repository等註解 springIoc容器會幫助咱們建立和依賴注入po --> <context:component-scan base-package="cn/edu360/ssm"></context:component-scan>
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <!-- 配置jsp路徑的字首 --> <property name="prefix" value="/WEB-INF/pages/" /> <!-- 配置jsp路徑的字尾 --> <property name="suffix" value=".jsp" /> </bean> <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"> <!-- 設定上傳檔案的最大尺寸為1MB --> <property name="maxUploadSize"> <value>1048576</value> </property> </bean> <mvc:interceptors> <!-- session的攔截器,預設會按照順序進行處理 --> <mvc:interceptor> <!-- 許可權攔截路徑 --> <mvc:mapping path="/*"/> <bean class="cn.edu360.ssm.Interceptor.SessionInterceptor"></bean> </mvc:interceptor> <!-- 許可權的攔截器 --> <!-- <mvc:interceptor> <mvc:mapping path="/**"/> <bean class="cn.edu360.ssm.intercepter.PermIntercepter"></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<News> 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