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 删除