博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
ssm重新开发计科院新闻网站
阅读量:5303 次
发布时间:2019-06-14

本文共 8340 字,大约阅读时间需要 27 分钟。

0、链接

链接:  提取码: cgk9 复制这段内容后打开百度网盘手机App,操作更方便哦

一、运行结果

二、代码

package mySpringMVC;

import javaBean.Employee;

import javaBean.News;
import myConnection.DBConn;
import myConnection.Modify;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;

@Controller

public class MyController {

//登录验证

@RequestMapping("/login")
public ModelAndView checkLogin(HttpServletRequest request){
HttpSession session = request.getSession();
Employee employee = new Employee();
employee.setName(request.getParameter("name"));
employee.setPassword(request.getParameter("password"));
Boolean isTrue = DBConn.check(employee);
session.setAttribute("isTrue",isTrue);
session.setAttribute("userName",request.getParameter("name"));
if (isTrue)
return setIndex();
else
return new ModelAndView("login");
}

//注销

@RequestMapping("/loginOut")
public ModelAndView loginOut(HttpServletRequest request){
HttpSession session = request.getSession();
session.setAttribute("isTrue",false);
return setIndex();
}

//点击单一新闻的请求响应

@RequestMapping("/info")
public ModelAndView dispatcherInfo(@RequestParam("type") String type,
@RequestParam("id") String id){
News news = null;
if ("imagenews".equals(type)){
news = DBConn.getNewsById(type,Integer.parseInt(id));
}
else if ("academiccommunication".equals(type)){
news = DBConn.getNewsById(type,Integer.parseInt(id));
}
else if ("couriernews".equals(type)){
news = DBConn.getNewsById(type,Integer.parseInt(id));
}
else if ("dynamicofparty".equals(type)){
news = DBConn.getNewsById(type,Integer.parseInt(id));
}
else if ("notifydynamic".equals(type)){
news = DBConn.getNewsById(type,Integer.parseInt(id));
}
else if ("projectlist".equals(type)){
news = DBConn.getNewsById(type,Integer.parseInt(id));
}
return getModel(news);
}

//设置网站的访问路径和全部新闻的显示,以及返回按钮访问新闻主页

@RequestMapping(value = {"/index","/back"})
public ModelAndView setIndex(){
ModelAndView modelAndView = new ModelAndView("index");
modelAndView.addObject("imagenewsList",DBConn.getNews("imagenews"));
modelAndView.addObject("academicList",DBConn.getNews("academiccommunication"));
modelAndView.addObject("couriernewsList",DBConn.getNews("couriernews"));
modelAndView.addObject("dynamicofpartyList",DBConn.getNews("dynamicofparty"));
modelAndView.addObject("notifydynamicList",DBConn.getNews("notifydynamic"));
modelAndView.addObject("projectList",DBConn.getNews("projectlist"));
return modelAndView;
}

//显示该类型的所有新闻

@RequestMapping("/newsPublish")
public ModelAndView getTypeNews(@RequestParam("type") String databaseName){
ModelAndView modelAndView = new ModelAndView("newsPublish");
List<News> list = DBConn.getAllNews(databaseName);
modelAndView.addObject("list",list);
modelAndView.addObject("name",databaseName);
return modelAndView;
}

//设置重定向的视图和数据
public ModelAndView getModel(News news){
ModelAndView modelAndView = new ModelAndView("info");
modelAndView.addObject("oneNews",news);
return modelAndView;
}

//新闻的修改

@RequestMapping("/upData")
public ModelAndView upData(@RequestParam("type") String databaseName, @RequestParam("id") int id){
ModelAndView modelAndView = new ModelAndView("addNews");
//数据库获取待修改的新闻信息
News news = DBConn.getNewsById(databaseName,id);
//将数据发送至新闻编辑页面进行填充
modelAndView.addObject("updata",news);
modelAndView.addObject("name",databaseName);
//设置修改操作的标识
Modify.modify = true;
//设置修改的新闻id
Modify.modifyId = id;
return modelAndView;
}

//添加新闻

@RequestMapping("AddNews")
public ModelAndView addNews(@RequestParam("title") String title, @RequestParam("type") String type,
@RequestParam("content") String content){

News news = new News();

news.setTitle(title);
news.setContent(content);
news.setTime(new Date());

if ("图片新闻".equals(type))

type = "imagenews";
else if ("学术交流".equals(type))
type = "academiccommunication";
else if ("新闻速递".equals(type))
type = "couriernews";
else if ("党建动态".equals(type))
type = "dynamicofparty";
else if ("通知公告".equals(type))
type = "notifydynamic";
else if ("专题列表".equals(type))
type = "projectlist";
else
type = "imagenews";

if (!Modify.modify){

//向数据库写入数据
DBConn.insertNews(news,type);
}
else{
news.setId(Modify.modifyId);
DBConn.upDataNewsById(news,type);
//重置Modify为默认状态
Modify.modify = false;
Modify.modifyId = -1;
}
return getTypeNews(type);
}

//添加新闻按钮事件

@RequestMapping("addNews")
public String add(){
return "addNews";
}

//删除新闻

@RequestMapping("/delete")
public ModelAndView deleteNews(@RequestParam("type") String type, @RequestParam("id") int id){
DBConn.deleteNews(type,id);
return getTypeNews(type);
}
}

MyController

 

package myConnection;

import javaBean.Database;

import javaBean.DatabaseName;
import javaBean.Employee;
import javaBean.News;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.IOException;

import java.io.InputStream;
import java.util.*;

public final class DBConn {

//连接数据库

public static SqlSession getSqlSession() {

String resource = "myXML/mybatis-config.xml";
InputStream inputStream = null;
SqlSession session = null;
try {
inputStream = Resources.getResourceAsStream(resource);
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
session = sqlSessionFactory.openSession();
} catch (IOException e) {
e.printStackTrace();
}
return session;
}

//校验用户信息

public static Boolean check(Employee emp){
SqlSession sqlSession = getSqlSession();
try {
//selectOne的第一个参数为Mapper.xml的mapper的namespace+id
//参数二为映射的需要传入的参数
Employee employee = sqlSession.selectOne(
"MyMapper.selectEmployee_name", emp.getName());
if (employee!=null && employee.getPassword().equals(emp.getPassword()))
return true;
else
return false;
} finally {
free(sqlSession);
}
}

//通过新闻表名获取该数据库的最新新闻
public static List<News> getNews(String databaseName){
int num = 1;
SqlSession sqlSession = getSqlSession();
//存在一个问题,直接传入String类型的参数时,纯mybatis无法获取参数,所以封装成一个Javabean
DatabaseName name = new DatabaseName();
name.setDatabaseName(databaseName);
List<News> list = sqlSession.selectList("MyMapper.selectByDatabaseName",name);
//只在首页界面显示最新的8条新闻
Collections.reverse(list);
Iterator<News> iterator = list.iterator();
List<News> temp = new ArrayList<>();
while (iterator.hasNext() && num<9){
temp.add(iterator.next());
num++;
}
free(sqlSession);
return temp;
}

//通过新闻表名获取该数据库的全部新闻

public static List<News> getAllNews(String databaseName){
SqlSession sqlSession = getSqlSession();
//存在一个问题,直接传入String类型的参数时,纯mybatis无法获取参数,所以封装成一个Javabean
DatabaseName name = new DatabaseName();
name.setDatabaseName(databaseName);
List<News> list = sqlSession.selectList("MyMapper.selectByDatabaseName",name);
free(sqlSession);
return list;
}

 

//通过id获取某一类别的某一条新闻

public static News getNewsById(String databaseName, int id){
//设置数据库名和新闻id
Database database = new Database();
database.setDatabaseName(databaseName);
database.setId(id);

SqlSession sqlSession = getSqlSession();

News news = sqlSession.selectOne("MyMapper.selectById",database);
free(sqlSession);
return news;
}

//释放Sqlsession
public static void free(SqlSession sqlSession){
sqlSession.close();
}

//向数据库添加新闻

public static void insertNews(News news, String type) {
SqlSession sqlSession = getSqlSession();
news.setDatabaseName(type);
System.out.println(news);
int result = sqlSession.insert("MyMapper.insertNews",news);
//提交事务
sqlSession.commit();
free(sqlSession);
}
//修改新闻信息
public static void upDataNewsById(News news, String type) {
// SqlSession sqlSession = getSqlSession();
// news.setDatabaseName(type);
// System.out.println(news);
// int res = sqlSession.update("MyMapper.updateNews",news);
// sqlSession.commit();
// free(sqlSession);
deleteNews(type,news.getId());
insertNews(news,type);
}

//删除新闻

public static void deleteNews(String databaseName,int id) {
SqlSession sqlSession = getSqlSession();
Database database = new Database();
database.setDatabaseName(databaseName);
database.setId(id);
int result = sqlSession.delete("MyMapper.deleteNews",database);
sqlSession.commit();
free(sqlSession);
}
}

数据库操作

 

转载于:https://www.cnblogs.com/WLYW/p/10890752.html

你可能感兴趣的文章
Educational Codeforces Round 52 (Rated for Div. 2) D. Three Pieces
查看>>
day33
查看>>
redis学习(二)——String数据类型
查看>>
[转]CSS块级元素和行内元素
查看>>
数据结构之队列
查看>>
垃圾回收器
查看>>
20135317的博客目录
查看>>
认知的概率模型(ESSLLI教程) - 第五部分译文 - 奥卡姆剃刀和信仰守恒定律
查看>>
截取utf-8字符串原理
查看>>
文件输入样式修改
查看>>
Scrum/Sprint敏捷开发方法.
查看>>
使用字符串方式
查看>>
NYTimes Objective-C 编程风格指南(参考)
查看>>
innodb_flush_method理解【转】
查看>>
iOS:UIView的CALayer基本演练
查看>>
C++:虚函数的引入
查看>>
struts2
查看>>
oracle 获取周末 及trunc的用法
查看>>
HDU 1505 City Game【DP】
查看>>
UVa 1152 4 Values whose Sum is 0
查看>>