SpringMVC+Spring+mybatis 项目实践
SpringMVC+Spring+mybatis 项目实践
这次项目采用的是springboot
1.新建springboot项目

2.在application.properies中进行环境的配置

配置mybatis的mapper扫描和过滤器扫描

再来就是项目的开发
1.登录页面
把先前项目做好的登录页面放进resource/static/templates中,然后运行程序看看页面效果

2.登录验证以及数据存储
(1)避免页面乱码先进行过滤器的配置

然后添加过滤器

(2)编写servlet用来接收登录页面发送过来的数据。

(3)使用上个项目的数据库里面的数据

然后配置mybatis

(4)在客户端用cookie保存登录信息并使用seesion记录当前页面登录人数
package com.example.demo.service; import com.example.demo.entity.User; import com.example.demo.mapper.UserMapper; import com.example.demo.result.LoginResult; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.util.DigestUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import javax.servlet.ServletResponse; import javax.servlet.http.Cookie; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; /** * @author false * @date 20/5/29 14:04 */ @Service public class LoginService { Logger logger = LoggerFactory.getLogger(this.getClass()); @Autowired HttpServletRequest httpServletRequest; /** * md5加密字符串 */ public static final String MD_5 ="fploa13./JOKFNpofIODt"; /** * mapper映射 */ @Autowired UserMapper userMapper; public LoginResult loginResult(String name,String pwd,boolean flag,HttpServletResponse response){ logger.info("user:-----------"+name); logger.info("pwd:-----------"+pwd); logger.info("record:-----------"+flag); //查询数据库 User user1 = userMapper.findUser(name,pwd); if(user1==null){ return new LoginResult(false,"","用户名或密码错误"); }else { //写入session if(flag==true){ String token = getMd5(name); httpServletRequest.getSession().setAttribute(token,name); //设置session过期时间30天 httpServletRequest.getSession().setMaxInactiveInterval(30*24*60*60); System.out.println("session过期时间 : "+httpServletRequest.getSession().getMaxInactiveInterval()); System.out.println("session token: "+httpServletRequest.getSession().getAttribute(token)); Cookie cookie = new Cookie("token",token); cookie.setMaxAge(30*24*60*60); response.addCookie(cookie); } return new LoginResult(true,name,""); } } public LoginResult verifyLogin(){ Cookie[] cookies = httpServletRequest.getCookies(); String token=""; for (Cookie cookie:cookies){ if("token".equals(cookie.getName())){ token=cookie.getValue(); break; } } logger.info("token:"+token); if(token==null||token.length()==0){ return new LoginResult(false,"","error"); } String name =(String) httpServletRequest.getSession().getAttribute(token); logger.info("name:"+name); if(name==null||name.length()==0){ return new LoginResult(false,"","error"); } return new LoginResult(true,name,""); } private String getMd5(String name){ return DigestUtils.md5DigestAsHex((name+MD_5).getBytes()); } }


3.新闻列表
(1)整合thymelea和boottstrap,避免页面样式丢失

引入样式的连接也修改一下

(2)把新闻页面像上面登录页面一样放到templates包里

(3)建立新闻类
在entity包里建立新闻类
package com.example.demo.entity; import com.fasterxml.jackson.annotation.JsonFormat; import org.springframework.format.annotation.DateTimeFormat; import java.util.Date; /** * @author false * @date 20/6/10 17:18 */ public class News { private int id; private String title; private String article; private String time; private String source; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getTitle() { return title; } public void setTitle(String title) { this.title = title; } public String getArticle() { return article; } public void setArticle(String article) { this.article = article; } public String getTime() { return time; } public void setTime(String time) { this.time = time; } public String getSource() { return source; } public void setSource(String source) { this.source = source; } @Override public String toString() { return "News{" + "id=" + id + ", title='" + title + '\'' + ", article='" + article + '\'' + ", time=" + time + ", source='" + source + '\'' + '}'; } }
然后建立mapper映射

接着编写新闻服务类用来查询新闻
package com.example.demo.service; import com.example.demo.entity.News; import com.example.demo.mapper.NewsMapper; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import java.util.List; /** * @author false * @date 20/6/10 17:41 */ @Service public class NewsService { Logger logger = LoggerFactory.getLogger(this.getClass()); @Autowired NewsMapper newsMapper; public News findById(int id){ return newsMapper.findById(id); } public List<News> findNews(){ return newsMapper.findNews(); } public boolean delById(int id){ return newsMapper.delById(id)==1; } public boolean insertNews(News news){ return newsMapper.insertNews(news)==1; } public boolean modifyNews(News news){ return newsMapper.modifyNews(news)==1; } }
(4)编写控制器,把新闻数据放到model中去

(5)最后查看结果

4.新闻的增删改查
新闻添加,编辑,展示页面都放到templates包里面

(1)删除新闻
删除新闻的js脚本

控制器,在控制层的新闻管理文件中加入删除代码

然后就是服务层

接着Dao层,写接口


(2)添加新闻
先在控制层写插入代码

然后服务层

接着Dao层写接口


(3)修改新闻
步骤和上面两个一样



(4)查看新闻



(5)效果展示
登录页面

新闻管理主页

新闻列表

添加/编辑新闻

查看新闻

码云地址:https://gitee.com/xiaotiejiang329/SSMNews
浙公网安备 33010602011771号