Bootstrap4+MySQL前后端综合实训-Day10-AM【实训汇报-下午返校、项目代码(7个包+7个Html页面)】
【Bootstrap4前端框架+MySQL数据库】前后端综合实训【10天课程 博客汇总表 详细笔记】【附:实训所有代码】
目录
实训汇报
数据库——所有SQL语句
工程文件展示
代码
①package com.newcapec.dao;
BaseDao.java
ItemUserDao.java
LogsInfoDao.java
NewItemDao.java
NewsInfoDao.java
UserInfoDao.java
②package com.newcapec.entity;
ItemUserEntity.java
LogsInfoEntity.java
NewItemEntity.java
NewsInfoEntity.java
ResultData.java
UserInfoEntity.java
③package com.newcapec.servlet;
AddUserServlet.java
DeleteUserServlet.java
LoginServlet.java
SelectUserByIdServlet.java
SelectUserByPageServlet.java
UpdateUserServlet.java
④package com.newcapec.servlet.newitem;
AddItemServlet.java
DeleteItemServlet.java
SelectItemByIdServlet.java
SelectItembyUserServlet.java
UpdateItemServlet.java
⑤package com.newcapec.servlet.news;
AddNewsServlet.java
⑥package com.newcapec.test;
TestItemUserDao.java
TestLogsInfoDao.java
TestNewItemDao.java
NewsInfoDao.java
TestUserInfoDao.java
⑦package com.newcapec.utils;
DBUtils.java
MD5Utils.java
pages
item_manager.html
news_manager.html
user_manager.html
index.html
login.html
loginFailure.html
phone.html
实训汇报
从昨天下午就开始实训答辩了。
今天上午,继续答辩。
上午10:00左右,所有的小组答辩完毕。
革命尚未成功,同学仍需努力~
数据库——所有SQL语句
/*
SQLyog Ultimate v12.08 (64 bit)
MySQL - 8.0.20 : Database - zykkk
*********************************************************************
*/
/*!40101 SET NAMES utf8 */;
/*!40101 SET SQL_MODE=''*/;
/*!40014 SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0 */;
/*!40014 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */;
/*!40101 SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO' */;
/*!40111 SET @OLD_SQL_NOTES=@@SQL_NOTES, SQL_NOTES=0 */;
CREATE DATABASE /*!32312 IF NOT EXISTS*/`zykkk` /*!40100 DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_hungarian_ci */ /*!80016 DEFAULT ENCRYPTION='N' */;
USE `zykkk`;
/*Table structure for table `item_user` */
DROP TABLE IF EXISTS `item_user`;
CREATE TABLE `item_user` (
`item_user_id` int NOT NULL AUTO_INCREMENT,
`user_id` int DEFAULT NULL,
`item_id` int DEFAULT NULL,
`create_time` datetime DEFAULT NULL COMMENT '创建时间',
`update_time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '修改时间',
`status` int DEFAULT '1' COMMENT '1:启用 0:禁用',
PRIMARY KEY (`item_user_id`),
KEY `FK_Reference_2` (`user_id`),
KEY `FK_Reference_3` (`item_id`),
CONSTRAINT `FK_Reference_2` FOREIGN KEY (`user_id`) REFERENCES `user_info` (`user_id`),
CONSTRAINT `FK_Reference_3` FOREIGN KEY (`item_id`) REFERENCES `new_item` (`item_id`)
) ENGINE=InnoDB AUTO_INCREMENT=15 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_hungarian_ci;
/*Data for the table `item_user` */
insert into `item_user`(`item_user_id`,`user_id`,`item_id`,`create_time`,`update_time`,`status`) values (1,1,2,'2020-11-23 11:24:16','2020-11-25 10:27:54',1),(2,2,4,NULL,'2020-11-25 09:38:17',1),(3,1,1,'2020-11-24 09:19:58','2020-11-25 09:38:21',1),(5,1,18,NULL,'2020-11-25 09:44:16',1),(6,1,27,'2020-11-25 11:11:35','2020-11-25 11:11:35',1),(7,1,28,'2020-11-25 11:17:59','2020-11-25 11:17:59',1),(8,1,29,'2020-11-25 11:29:14','2020-11-25 11:29:14',1),(9,1,30,'2020-11-25 11:30:54','2020-11-25 11:30:54',1),(10,1,31,'2020-11-25 11:36:51','2020-11-25 11:36:51',1),(11,1,32,'2020-11-25 16:26:23','2020-11-25 16:26:23',1),(12,1,33,'2020-11-25 16:26:37','2020-11-25 16:26:37',1),(13,1,34,'2020-11-26 10:01:29','2020-11-26 10:01:29',1),(14,1,35,'2020-11-26 10:28:53','2020-11-26 10:28:53',1);
/*Table structure for table `logs_info` */
DROP TABLE IF EXISTS `logs_info`;
CREATE TABLE `logs_info` (
`logs_id` int NOT NULL AUTO_INCREMENT,
`user_id` int DEFAULT NULL,
`logs_content` text CHARACTER SET utf8mb4 COLLATE utf8mb4_hungarian_ci,
`create_time` datetime DEFAULT NULL COMMENT '创建时间',
`update_time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '修改时间',
PRIMARY KEY (`logs_id`),
KEY `FK_Reference_1` (`user_id`),
CONSTRAINT `FK_Reference_1` FOREIGN KEY (`user_id`) REFERENCES `user_info` (`user_id`)
) ENGINE=InnoDB AUTO_INCREMENT=17 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_hungarian_ci;
/*Data for the table `logs_info` */
insert into `logs_info`(`logs_id`,`user_id`,`logs_content`,`create_time`,`update_time`) values (1,1,NULL,NULL,'2020-11-24 09:27:05'),(2,2,NULL,NULL,'2020-11-24 09:27:12'),(3,4,NULL,NULL,'2020-11-23 11:29:06'),(14,1,'woshishenren','2020-11-24 09:24:52','2020-11-24 09:24:52'),(15,1,'woshishenren','2020-11-24 09:25:58','2020-11-24 09:25:58');
/*Table structure for table `new_item` */
DROP TABLE IF EXISTS `new_item`;
CREATE TABLE `new_item` (
`item_id` int NOT NULL AUTO_INCREMENT,
`item_name` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_hungarian_ci NOT NULL,
`create_time` datetime DEFAULT NULL COMMENT '创建时间',
`update_time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '修改时间',
`status` int DEFAULT '1' COMMENT '1:启用 0:禁用',
PRIMARY KEY (`item_id`)
) ENGINE=InnoDB AUTO_INCREMENT=36 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_hungarian_ci;
/*Data for the table `new_item` */
insert into `new_item`(`item_id`,`item_name`,`create_time`,`update_time`,`status`) values (1,'呵呵哒','2020-11-24 15:47:00','2020-11-26 10:28:39',1),(2,'党支部','2020-11-24 15:47:03','2020-11-25 14:44:31',0),(3,'分团委','2020-11-24 15:47:05','2020-11-25 14:43:54',1),(4,'院团委','2020-11-24 15:47:08','2020-11-25 14:44:38',1),(5,'111','2020-11-23 15:22:54','2020-11-25 14:45:55',1),(6,'学生会','2020-11-24 09:27:36','2020-11-25 14:46:01',1),(8,'党支部','2020-11-24 13:51:13','2020-11-25 14:46:07',1),(18,'党支部','2020-11-25 09:11:51','2020-11-25 15:49:06',1),(19,'院团委','2020-11-25 10:42:54','2020-11-25 14:46:16',1),(20,'111','2020-11-25 10:54:12','2020-11-25 14:46:19',1),(21,'学生会','2020-11-25 10:56:21','2020-11-25 14:46:35',1),(22,'党支部','2020-11-25 10:57:35','2020-11-25 14:46:43',1),(23,'分团委','2020-11-25 11:00:20','2020-11-25 14:46:48',1),(24,'院团委','2020-11-25 11:00:47','2020-11-25 14:46:55',1),(25,'qweqwe','2020-11-25 11:01:37','2020-11-25 11:01:37',1),(26,'eqweqweqwe','2020-11-25 11:01:53','2020-11-25 11:01:53',1),(27,'分团委','2020-11-25 11:11:35','2020-11-25 15:49:18',1),(28,'sadsads','2020-11-25 11:17:59','2020-11-25 11:18:40',0),(29,'院团委','2020-11-25 11:29:13','2020-11-25 15:49:25',1),(30,'789','2020-11-25 11:30:54','2020-11-25 11:37:19',0),(31,'zyk','2020-11-25 11:36:51','2020-11-25 11:37:19',0),(32,'委员会','2020-11-25 16:26:23','2020-11-26 08:37:48',0),(33,'委员会~~~','2020-11-25 16:26:37','2020-11-26 10:01:40',1),(34,'演示~','2020-11-26 10:01:29','2020-11-26 10:01:33',0),(35,'筑基修士!!!','2020-11-26 10:28:53','2020-11-26 10:29:14',1);
/*Table structure for table `news_info` */
DROP TABLE IF EXISTS `news_info`;
CREATE TABLE `news_info` (
`new_id` int NOT NULL AUTO_INCREMENT,
`item_id` int DEFAULT NULL,
`news_title` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_hungarian_ci NOT NULL,
`news_image` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_hungarian_ci DEFAULT NULL,
`news_content` text CHARACTER SET utf8mb4 COLLATE utf8mb4_hungarian_ci,
`create_time` datetime DEFAULT NULL COMMENT '创建时间',
`update_time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '修改时间',
PRIMARY KEY (`new_id`),
KEY `FK_Reference_4` (`item_id`),
CONSTRAINT `FK_Reference_4` FOREIGN KEY (`item_id`) REFERENCES `new_item` (`item_id`)
) ENGINE=InnoDB AUTO_INCREMENT=19 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_hungarian_ci;
/*Data for the table `news_info` */
insert into `news_info`(`new_id`,`item_id`,`news_title`,`news_image`,`news_content`,`create_time`,`update_time`) values (1,2,'蓝桥杯比赛',NULL,NULL,NULL,'2020-11-23 09:27:39'),(2,3,'新学期学费',NULL,NULL,NULL,'2020-11-23 09:28:10'),(3,1,'拔河比赛',NULL,'拔河比赛要使劲!!!','2020-11-25 14:57:28','2020-11-25 14:57:32'),(4,18,'街舞比赛',NULL,'一起摇摆~','2020-11-25 15:54:09','2020-11-25 15:54:11'),(10,27,'数学建模',NULL,'一起加油!','2020-11-25 16:10:02','2020-11-25 22:17:19'),(11,29,'班班唱',NULL,'《走向复兴》','2020-11-25 16:12:23','2020-11-25 16:12:23'),(12,1,'篮球比赛',NULL,'冲冲冲~','2020-11-25 16:13:04','2020-11-25 16:13:04'),(13,1,'NECCS',NULL,'冲呀~','2020-11-25 16:27:22','2020-11-26 08:38:03'),(14,18,'卓见杯',NULL,'啦啦啦~','2020-11-25 17:41:32','2020-11-25 22:17:56'),(15,33,'动则升阳',NULL,'年轻不养生,年老养医生!','2020-11-26 00:12:42','2020-11-26 00:12:42'),(16,33,'11月26日',NULL,'筑基修士','2020-11-26 10:02:20','2020-11-26 10:02:20'),(17,35,'大家好',NULL,'333','2020-11-26 10:29:35','2020-11-26 10:29:35'),(18,35,'大家好!!!',NULL,'333','2020-11-26 10:29:45','2020-11-26 10:29:45');
/*Table structure for table `user_info` */
DROP TABLE IF EXISTS `user_info`;
CREATE TABLE `user_info` (
`user_id` int NOT NULL AUTO_INCREMENT,
`user_name` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_hungarian_ci NOT NULL,
`user_pwd` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_hungarian_ci NOT NULL,
`create_time` datetime DEFAULT NULL COMMENT '创建时间',
`update_time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '修改时间',
`status` int DEFAULT '1' COMMENT '1:启用 0:禁用',
PRIMARY KEY (`user_id`)
) ENGINE=InnoDB AUTO_INCREMENT=120 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_hungarian_ci;
/*Data for the table `user_info` */
insert into `user_info`(`user_id`,`user_name`,`user_pwd`,`create_time`,`update_time`,`status`) values (1,'宋书航','1','2020-11-23 09:30:16','2020-11-25 22:16:25',1),(2,'雨柔子','1','2020-11-23 11:25:41','2020-11-25 22:16:25',1),(4,'王五','1','2020-11-23 11:25:58','2020-11-25 22:16:26',0),(5,'赵柳','1','2020-11-23 11:26:12','2020-11-25 22:16:26',0),(8,'田七','1','2020-11-23 11:26:29','2020-11-25 22:16:27',0),(9,'田七','1','2020-11-23 15:03:23','2020-11-25 22:16:28',0),(10,'田七','1','2020-11-23 15:03:43','2020-11-25 22:16:28',0),(11,'戴沐白','1','2020-11-24 10:45:06','2020-11-25 22:16:29',1),(12,'张小凡','1','2020-11-24 10:45:29','2020-11-25 22:16:29',1),(13,'userName2','1','2020-11-24 10:45:29','2020-11-25 22:16:30',0),(15,'碧瑶','1','2020-11-24 10:45:29','2020-11-25 22:16:31',1),(16,'赵恋凡','1','2020-11-24 10:45:29','2020-11-25 22:16:31',1),(17,'李长寿','1','2020-11-24 10:45:29','2020-11-25 22:16:32',1),(18,'蓝梦娥','1','2020-11-24 10:45:29','2020-11-25 22:16:33',1),(22,'路明非','123456','2020-11-24 10:45:29','2020-11-25 17:44:31',1),(23,'楚子航','123456','2020-11-24 10:45:29','2020-11-25 22:14:26',1),(33,'乔微尼','123456','2020-11-24 10:45:29','2020-11-25 23:05:48',1),(97,'userName86','123456','2020-11-24 10:45:32','2020-11-24 10:45:32',1),(98,'userName87','123456','2020-11-24 10:45:32','2020-11-24 10:45:32',1),(99,'userName88','123456','2020-11-24 10:45:32','2020-11-24 10:45:32',1),(100,'userName89','123456','2020-11-24 10:45:32','2020-11-24 10:45:32',1),(101,'2020年好运来~','123456','2020-11-24 10:45:32','2020-11-26 10:01:06',1),(102,'333','123456','2020-11-24 10:45:32','2020-11-26 10:28:14',1),(103,'666','888','2020-11-24 10:45:32','2020-11-26 10:28:26',1),(104,'userName93','123456','2020-11-24 10:45:32','2020-11-26 10:00:54',0),(105,'userName94','123456','2020-11-24 10:45:32','2020-11-26 10:00:47',0),(106,'userName95','123456','2020-11-24 10:45:32','2020-11-26 10:00:47',0),(107,'userName96','123456','2020-11-24 10:45:32','2020-11-26 10:00:47',0),(108,'userName97','123456','2020-11-24 10:45:32','2020-11-26 10:00:47',0),(109,'userName98','123456','2020-11-24 10:45:32','2020-11-25 10:12:58',0),(110,'userName99','123456','2020-11-24 10:45:32','2020-11-25 10:12:58',0),(111,'userName100','123456','2020-11-24 10:45:33','2020-11-25 10:12:58',0),(115,'萧潜','1','2020-11-25 23:00:16','2020-11-25 23:00:16',1),(116,'演示视频','haha','2020-11-26 10:00:33','2020-11-26 10:27:37',0),(117,'啦啦啦','1','2020-11-26 10:27:14','2020-11-26 10:27:37',0),(118,'演示视频','222','2020-11-26 10:27:23','2020-11-26 10:27:37',0),(119,'实训小组hyy','111','2020-11-26 14:37:14','2020-11-26 14:37:14',1);
/*!40101 SET SQL_MODE=@OLD_SQL_MODE */;
/*!40014 SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS */;
/*!40014 SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS */;
/*!40111 SET SQL_NOTES=@OLD_SQL_NOTES */;
工程文件展示
代码
①package com.newcapec.dao;
BaseDao.java
package com.newcapec.dao;
import java.sql.SQLException;
import java.util.List;
/**
* JDBC在后期需要使用hibernate/mybatis优化
*
* @author user
*
* @param <T>
*/
public interface BaseDao<T> {
// public int insert(T t) throws ClassNotFoundException, SQLException;
public boolean deleteById(int id) throws ClassNotFoundException, SQLException;
public boolean batchDeleteById(int[] ids) throws ClassNotFoundException, SQLException;
public boolean update(T t) throws ClassNotFoundException, SQLException;
public T selectById(int id) throws ClassNotFoundException, SQLException;
public List<T> selectAll() throws ClassNotFoundException, SQLException;
}
ItemUserDao.java
package com.newcapec.dao;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import com.newcapec.entity.ItemUserEntity;
import com.newcapec.utils.DBUtils;
public class ItemUserDao implements BaseDao<ItemUserEntity> {
public boolean insert(ItemUserEntity t) throws ClassNotFoundException, SQLException {
boolean flag = false;
// 获取与数据库的连接
Connection connection = DBUtils.openConn();
// ?表示占位符 可以解决sql注入的问题
String sql = "insert into item_user (user_id,item_id,create_time) values (?,?,now())";
PreparedStatement statement = connection.prepareStatement(sql);
statement.setInt(1, t.getUserId());
statement.setInt(2, t.getItemId());
// 执行sql语句
int count = statement.executeUpdate();
if (count > 0) {
flag = true;
}
statement.close();
connection.close();
return flag;
}
@Override
public boolean deleteById(int id) throws ClassNotFoundException, SQLException {
boolean flag = false;
Connection connection = DBUtils.openConn();
String sql = "delete from item_user where item_user_id = ?";
PreparedStatement statement = connection.prepareStatement(sql);
statement.setInt(1, id);
int count = statement.executeUpdate();
if (count > 0) {
flag = true;
}
statement.close();
connection.close();
return flag;
}
@Override
public boolean update(ItemUserEntity t) throws ClassNotFoundException, SQLException {
boolean flag = false;
// 获取连接
Connection connection = DBUtils.openConn();
// sql语句
String sql = "update item_user set user_id = ?,item_id = ? where item_user_id = ?";
// 预处理对象
PreparedStatement statement = connection.prepareStatement(sql);
// 添加参数
statement.setInt(1, t.getUserId());
statement.setInt(2, t.getItemId());
statement.setInt(3, t.getItemUserId());
// 执行预处理对象
int count = statement.executeUpdate();
if (count > 0) {
flag = true;
}
statement.close();
connection.close();
return flag;
}
@Override
public ItemUserEntity selectById(int id) throws ClassNotFoundException, SQLException {
ItemUserEntity ItemUserEntity = null;
Connection connection = DBUtils.openConn();
String sql = "select * from item_user where item_user_id = ?";
PreparedStatement statement = connection.prepareStatement(sql);
statement.setInt(1, id);
ResultSet resultSet = statement.executeQuery();
while (resultSet.next()) {
int itemUserId = resultSet.getInt("item_user_id");
int userId = resultSet.getInt("user_id");
int itemId = resultSet.getInt("item_id");
Date createTime = resultSet.getTimestamp("create_time");
Date updateTime = resultSet.getTimestamp("update_time");
ItemUserEntity = new ItemUserEntity(itemUserId, userId, itemId, createTime, updateTime);
}
resultSet.close();
statement.close();
connection.close();
return ItemUserEntity;
}
@Override
public List<ItemUserEntity> selectAll() throws ClassNotFoundException, SQLException {
List<ItemUserEntity> list = new ArrayList<>();
Connection connection = DBUtils.openConn();
String sql = "select * from item_user and status = 1";
PreparedStatement statement = connection.prepareStatement(sql);
ResultSet resultSet = statement.executeQuery();
while (resultSet.next()) {
int itemUserId = resultSet.getInt("item_user_id");
int userId = resultSet.getInt("user_id");
int itemId = resultSet.getInt("item_id");
Date createTime = resultSet.getTimestamp("create_time");
Date updateTime = resultSet.getTimestamp("update_time");
ItemUserEntity itemUserEntity = new ItemUserEntity(itemUserId, userId, itemId, createTime, updateTime);
list.add(itemUserEntity);
}
resultSet.close();
statement.close();
connection.close();
return list;
}
@Override
public boolean batchDeleteById(int[] ids) throws ClassNotFoundException, SQLException {
// TODO 自动生成的方法存根
return false;
}
}
LogsInfoDao.java
package com.newcapec.dao;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import com.newcapec.entity.LogsInfoEntity;
import com.newcapec.utils.DBUtils;
public class LogsInfoDao implements BaseDao<LogsInfoEntity> {
public boolean insert(LogsInfoEntity t) throws ClassNotFoundException, SQLException {
boolean flag = false;
Connection connection = DBUtils.openConn();
String sql = "insert into logs_info (user_id,logs_content,create_time) values (?,?,now())";
PreparedStatement statement = connection.prepareStatement(sql);
statement.setInt(1, t.getUserId());
statement.setString(2, t.getLogsContent());
int count = statement.executeUpdate();
if (count > 0) {
flag = true;
}
statement.close();
connection.close();
return flag;
}
@Override
public boolean deleteById(int id) throws ClassNotFoundException, SQLException {
boolean flag = false;
Connection connection = DBUtils.openConn();
String sql = "delete from logs_info where logs_id = ?";
PreparedStatement statement = connection.prepareStatement(sql);
statement.setInt(1, id);
int count = statement.executeUpdate();
if (count > 0) {
flag = true;
}
statement.close();
connection.close();
return flag;
}
@Override
public boolean update(LogsInfoEntity t) throws ClassNotFoundException, SQLException {
boolean flag = false;
Connection connection = DBUtils.openConn();
String sql = "update logs_info set user_id = ?, logs_content = ? where logs_id = ?";
PreparedStatement statement = connection.prepareStatement(sql);
statement.setInt(1, t.getUserId());
statement.setString(2, t.getLogsContent());
statement.setInt(3, t.getLogsId());
int count = statement.executeUpdate();
if (count > 0) {
flag = true;
}
statement.close();
connection.close();
return flag;
}
@Override
public LogsInfoEntity selectById(int id) throws ClassNotFoundException, SQLException {
LogsInfoEntity logsInfoEntity = null;
Connection connection = DBUtils.openConn();
String sql = "select * from logs_info where logs_id = ?";
PreparedStatement statement = connection.prepareStatement(sql);
statement.setInt(1, id);
ResultSet resultSet = statement.executeQuery();
while (resultSet.next()) {
int newsId = resultSet.getInt("logs_id");
int itemId = resultSet.getInt("user_id");
String logsContent = resultSet.getString("logs_content");
Date createTime = resultSet.getDate("create_time");
Date updateTime = resultSet.getDate("update_time");
logsInfoEntity = new LogsInfoEntity(newsId, itemId, logsContent, createTime, updateTime);
}
resultSet.close();
statement.close();
connection.close();
return logsInfoEntity;
}
@Override
public List<LogsInfoEntity> selectAll() throws ClassNotFoundException, SQLException {
List<LogsInfoEntity> list = new ArrayList<LogsInfoEntity>();
Connection connection = DBUtils.openConn();
String sql = "select * from logs_info";
PreparedStatement statement = connection.prepareStatement(sql);
ResultSet resultSet = statement.executeQuery();
while (resultSet.next()) {
int newsId = resultSet.getInt("logs_id");
int itemId = resultSet.getInt("user_id");
String logsContent = resultSet.getString("logs_content");
Date createTime = resultSet.getDate("create_time");
Date updateTime = resultSet.getDate("update_time");
LogsInfoEntity logsInfoEntity = new LogsInfoEntity(newsId, itemId, logsContent, createTime, updateTime);
list.add(logsInfoEntity);
}
resultSet.close();
statement.close();
connection.close();
return list;
}
@Override
public boolean batchDeleteById(int[] ids) throws ClassNotFoundException, SQLException {
// TODO 自动生成的方法存根
return false;
}
}
NewItemDao.java
package com.newcapec.dao;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import com.newcapec.entity.NewItemEntity;
import com.newcapec.utils.DBUtils;
public class NewItemDao implements BaseDao<NewItemEntity> {
public int insert(NewItemEntity t) throws ClassNotFoundException, SQLException {
Connection connection = DBUtils.openConn();
String sql = "insert into new_item (item_name,create_time) values (?,now())";
PreparedStatement statement = connection.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS);
statement.setString(1, t.getItemName());
statement.executeUpdate();
ResultSet resultSet = statement.getGeneratedKeys();
resultSet.next();
int id = resultSet.getInt(1);
statement.close();
connection.close();
return id;
}
@Override
public boolean deleteById(int id) throws ClassNotFoundException, SQLException {
boolean flag = false;
// 获取连接
Connection connection = DBUtils.openConn();
// sql语句
String sql = "delete from new_item where item_id = ?";
// 预处理对象
PreparedStatement statement = connection.prepareStatement(sql);
// 添加参数
statement.setInt(1, id);
// 执行预处理对象
int count = statement.executeUpdate();
if (count > 0) {
flag = true;
}
statement.close();
connection.close();
return flag;
}
@Override
public boolean update(NewItemEntity t) throws ClassNotFoundException, SQLException {
boolean flag = false;
// 获取连接
Connection connection = DBUtils.openConn();
// sql语句
String sql = "update new_item set item_name = ? where item_id = ?";
// 预处理对象
PreparedStatement statement = connection.prepareStatement(sql);
// 添加参数
statement.setString(1, t.getItemName());
statement.setInt(2, t.getItemId());
// 执行预处理对象
int count = statement.executeUpdate();
if (count > 0) {
flag = true;
}
statement.close();
connection.close();
return flag;
}
@Override
public NewItemEntity selectById(int id) throws ClassNotFoundException, SQLException {
NewItemEntity newItemEntity = null;
Connection connection = DBUtils.openConn();
String sql = "select * from new_item where item_id = ?";
PreparedStatement statement = connection.prepareStatement(sql);
statement.setInt(1, id);
ResultSet resultSet = statement.executeQuery();
while (resultSet.next()) {
int itemId = resultSet.getInt("item_id");
String itemName = resultSet.getString("item_name");
Date createTime = resultSet.getTimestamp("create_time");
Date updateTime = resultSet.getTimestamp("update_time");
newItemEntity = new NewItemEntity(itemId, itemName, createTime, updateTime);
}
resultSet.close();
statement.close();
connection.close();
return newItemEntity;
}
@Override
public List<NewItemEntity> selectAll() throws ClassNotFoundException, SQLException {
List<NewItemEntity> list = new ArrayList<>();
Connection connection = DBUtils.openConn();
String sql = "select * from new_item ";
PreparedStatement statement = connection.prepareStatement(sql);
ResultSet resultSet = statement.executeQuery();
while (resultSet.next()) {
int itemId = resultSet.getInt("item_id");
String itemName = resultSet.getString("item_name");
Date createTime = resultSet.getTimestamp("create_time");
Date updateTime = resultSet.getTimestamp("update_time");
NewItemEntity newItemEntity = new NewItemEntity(itemId, itemName, createTime, updateTime);
list.add(newItemEntity);
}
resultSet.close();
statement.close();
connection.close();
return list;
}
public List<NewItemEntity> selectAllByUserId(int userId) throws ClassNotFoundException, SQLException {
List<NewItemEntity> list = new ArrayList<>();
Connection connection = DBUtils.openConn();
String sql = "SELECT new_item.`item_id`,new_item.`item_name`,new_item.`create_time`,new_item.`update_time` FROM new_item \r\n"
+ " INNER JOIN item_user ON new_item.`item_id` = item_user.`item_id`\r\n"
+ " INNER JOIN user_info ON user_info.`user_id` = item_user.`user_id`\r\n"
+ " WHERE user_info.user_id = ? and new_item.status = 1";
PreparedStatement statement = connection.prepareStatement(sql);
statement.setInt(1, userId);
ResultSet resultSet = statement.executeQuery();
while (resultSet.next()) {
int itemId = resultSet.getInt("item_id");
String itemName = resultSet.getString("item_name");
Date createTime = resultSet.getTimestamp("create_time");
Date updateTime = resultSet.getTimestamp("update_time");
NewItemEntity newItemEntity = new NewItemEntity(itemId, itemName, createTime, updateTime);
list.add(newItemEntity);
}
resultSet.close();
statement.close();
connection.close();
return list;
}
public int selectAllByUserIdCount(int userId) throws ClassNotFoundException, SQLException {
int count = 0;
Connection connection = DBUtils.openConn();
String sql = "SELECT count(*) as allCount FROM new_item \r\n"
+ " INNER JOIN item_user ON new_item.`item_id` = item_user.`item_id`\r\n"
+ " INNER JOIN user_info ON user_info.`user_id` = item_user.`user_id`\r\n where new_item.status = 1 AND user_info.`user_id` = ?";
PreparedStatement statement = connection.prepareStatement(sql);
statement.setInt(1, userId);
ResultSet resultSet = statement.executeQuery();
while (resultSet.next()) {
count = resultSet.getInt("allCount");
}
resultSet.close();
statement.close();
connection.close();
return count;
}
@Override
public boolean batchDeleteById(int[] ids) throws ClassNotFoundException, SQLException {
boolean flag = false;
// 获取连接
Connection connection = DBUtils.openConn();
// sql语句
// String sql = "delete from new_item where item_id = ?";
String sql = "update new_item set status = 0 where item_id = ?";
// 预处理对象
PreparedStatement statement = connection.prepareStatement(sql);
// 添加参数
for (int id : ids) {
statement.setInt(1, id);
statement.addBatch();
}
int[] result = statement.executeBatch();
// 执行预处理对象
// int count = statement.executeUpdate();
if (result.length > 0) {
flag = true;
}
statement.close();
connection.close();
return flag;
}
}
NewsInfoDao.java
package com.newcapec.dao;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import com.newcapec.entity.NewsInfoEntity;
import com.newcapec.utils.DBUtils;
public class NewsInfoDao implements BaseDao<NewsInfoEntity> {
public boolean insert(NewsInfoEntity t) throws ClassNotFoundException, SQLException {
boolean flag = false;
Connection connection = DBUtils.openConn();
String sql = "insert into news_info (item_id,news_title,news_content,create_time) values (?,?,?,now())";
PreparedStatement statement = connection.prepareStatement(sql);
statement.setInt(1, t.getItemId());
statement.setString(2, t.getNewsTitle());
statement.setString(3, t.getNewsContent());
int count = statement.executeUpdate();
if (count > 0) {
flag = true;
}
statement.close();
connection.close();
return flag;
}
@Override
public boolean deleteById(int id) throws ClassNotFoundException, SQLException {
boolean flag = false;
Connection connection = DBUtils.openConn();
String sql = "delete from news_info where news_id = ?";
PreparedStatement statement = connection.prepareStatement(sql);
statement.setInt(1, id);
int count = statement.executeUpdate();
if (count > 0) {
flag = true;
}
statement.close();
connection.close();
return flag;
}
@Override
public boolean update(NewsInfoEntity t) throws ClassNotFoundException, SQLException {
boolean flag = false;
Connection connection = DBUtils.openConn();
String sql = "update news_info set item_id = ?, news_title = ?, news_image = ?, news_content=? where news_id = ?";
PreparedStatement statement = connection.prepareStatement(sql);
statement.setInt(1, t.getItemId());
statement.setString(2, t.getNewsTitle());
statement.setString(3, t.getNewsImage());
statement.setString(4, t.getNewsContent());
statement.setInt(5, t.getNewsId());
int count = statement.executeUpdate();
if (count > 0) {
flag = true;
}
statement.close();
connection.close();
return flag;
}
@Override
public NewsInfoEntity selectById(int id) throws ClassNotFoundException, SQLException {
NewsInfoEntity newsInfoEntity = null;
Connection connection = DBUtils.openConn();
String sql = "select * from news_info where news_id = ?";
PreparedStatement statement = connection.prepareStatement(sql);
statement.setInt(1, id);
ResultSet resultSet = statement.executeQuery();
while (resultSet.next()) {
int newsId = resultSet.getInt("news_id");
int itemId = resultSet.getInt("item_id");
String newsTitle = resultSet.getString("news_title");
String newsContent = resultSet.getString("news_content");
String newsImage = resultSet.getString("news_image");
Date createTime = resultSet.getDate("create_time");
Date updateTime = resultSet.getDate("update_time");
newsInfoEntity = new NewsInfoEntity(newsId, itemId, newsTitle, newsImage, newsContent, createTime,
updateTime);
}
resultSet.close();
statement.close();
connection.close();
return newsInfoEntity;
}
@Override
public List<NewsInfoEntity> selectAll() throws ClassNotFoundException, SQLException {
List<NewsInfoEntity> list = new ArrayList<NewsInfoEntity>();
Connection connection = DBUtils.openConn();
String sql = "select * from news_info";
PreparedStatement statement = connection.prepareStatement(sql);
ResultSet resultSet = statement.executeQuery();
while (resultSet.next()) {
int newsId = resultSet.getInt("news_id");
int itemId = resultSet.getInt("item_id");
String newsTitle = resultSet.getString("news_title");
String newsContent = resultSet.getString("news_content");
String newsImage = resultSet.getString("news_image");
Date createTime = resultSet.getDate("create_time");
Date updateTime = resultSet.getDate("update_time");
NewsInfoEntity newsInfoEntity = new NewsInfoEntity(newsId, itemId, newsTitle, newsImage, newsContent,
createTime, updateTime);
list.add(newsInfoEntity);
}
resultSet.close();
statement.close();
connection.close();
return list;
}
@Override
public boolean batchDeleteById(int[] ids) throws ClassNotFoundException, SQLException {
// TODO 自动生成的方法存根
return false;
}
}
UserInfoDao.java
package com.newcapec.dao;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import com.newcapec.entity.UserInfoEntity;
import com.newcapec.utils.DBUtils;
public class UserInfoDao implements BaseDao<UserInfoEntity> {
public UserInfoEntity login(String userName, String userPwd) throws ClassNotFoundException, SQLException {
UserInfoEntity userInfoEntity = null;
Connection connection = DBUtils.openConn();
String sql = "select * from user_info where user_name = ? and user_pwd = ?";
PreparedStatement statement = connection.prepareStatement(sql);
statement.setString(1, userName);
statement.setString(2, userPwd);
ResultSet resultSet = statement.executeQuery();
while (resultSet.next()) {
int userId = resultSet.getInt("user_id");
Date createTime = resultSet.getTimestamp("create_time");
Date updateTime = resultSet.getTimestamp("update_time");
userInfoEntity = new UserInfoEntity(userId, userName, userPwd, createTime, updateTime);
}
resultSet.close();
statement.close();
connection.close();
return userInfoEntity;
}
public boolean insert(UserInfoEntity t) throws ClassNotFoundException, SQLException {
boolean flag = false;
// 获取与数据库的连接
Connection connection = DBUtils.openConn();
// ?表示占位符 可以解决sql注入的问题
String sql = "insert into user_info (user_name,user_pwd,create_time) values (?,?,now())";
PreparedStatement statement = connection.prepareStatement(sql);
statement.setString(1, t.getUserName());
statement.setString(2, t.getUserPwd());
// 执行sql语句
int count = statement.executeUpdate();
if (count > 0) {
flag = true;
}
statement.close();
connection.close();
return flag;
}
@Override
public boolean deleteById(int id) throws ClassNotFoundException, SQLException {
boolean flag = false;
// 获取连接
Connection connection = DBUtils.openConn();
// sql语句
// String sql = "delete from user_info where user_id = ?";
String sql = "update user_info set status = 0 where user_id = ?";
// 预处理对象
PreparedStatement statement = connection.prepareStatement(sql);
// 添加参数
statement.setInt(1, id);
// 执行预处理对象
int count = statement.executeUpdate();
if (count > 0) {
flag = true;
}
statement.close();
connection.close();
return flag;
}
@Override
public boolean update(UserInfoEntity t) throws ClassNotFoundException, SQLException {
boolean flag = false;
// 获取连接
Connection connection = DBUtils.openConn();
// sql语句
String sql = "update user_info set user_name = ?,user_pwd = ? where user_id = ?";
// 预处理对象
PreparedStatement statement = connection.prepareStatement(sql);
// 添加参数
statement.setString(1, t.getUserName());
statement.setString(2, t.getUserPwd());
statement.setInt(3, t.getUserId());
// 执行预处理对象
int count = statement.executeUpdate();
if (count > 0) {
flag = true;
}
statement.close();
connection.close();
return flag;
}
@Override
public UserInfoEntity selectById(int id) throws ClassNotFoundException, SQLException {
UserInfoEntity userInfoEntity = null;
Connection connection = DBUtils.openConn();
String sql = "select * from user_info where user_id = ? and status = 1";
PreparedStatement statement = connection.prepareStatement(sql);
statement.setInt(1, id);
ResultSet resultSet = statement.executeQuery();
while (resultSet.next()) {
int userId = resultSet.getInt("user_id");
String userName = resultSet.getString("user_name");
String userPwd = resultSet.getString("user_pwd");
Date createTime = resultSet.getTimestamp("create_time");
Date updateTime = resultSet.getTimestamp("update_time");
userInfoEntity = new UserInfoEntity(userId, userName, userPwd, createTime, updateTime);
}
resultSet.close();
statement.close();
connection.close();
return userInfoEntity;
}
@Override
public List<UserInfoEntity> selectAll() throws ClassNotFoundException, SQLException {
List<UserInfoEntity> list = new ArrayList<>();
Connection connection = DBUtils.openConn();
String sql = "select * from user_info";
PreparedStatement statement = connection.prepareStatement(sql);
ResultSet resultSet = statement.executeQuery();
while (resultSet.next()) {
int userId = resultSet.getInt("user_id");
String userName = resultSet.getString("user_name");
String userPwd = resultSet.getString("user_pwd");
Date createTime = resultSet.getTimestamp("create_time");
Date updateTime = resultSet.getTimestamp("update_time");
UserInfoEntity userInfoEntity = new UserInfoEntity(userId, userName, userPwd, createTime, updateTime);
list.add(userInfoEntity);
}
resultSet.close();
statement.close();
connection.close();
return list;
}
public List<UserInfoEntity> selectByPage(int offset, int limit) throws ClassNotFoundException, SQLException {
List<UserInfoEntity> list = new ArrayList<>();
Connection connection = DBUtils.openConn();
String sql = "select * from user_info where status = 1 limit ?,?";
PreparedStatement statement = connection.prepareStatement(sql);
statement.setInt(1, offset);
statement.setInt(2, limit);
ResultSet resultSet = statement.executeQuery();
while (resultSet.next()) {
int userId = resultSet.getInt("user_id");
String userName = resultSet.getString("user_name");
String userPwd = resultSet.getString("user_pwd");
Date createTime = resultSet.getTimestamp("create_time");
Date updateTime = resultSet.getTimestamp("update_time");
UserInfoEntity userInfoEntity = new UserInfoEntity(userId, userName, userPwd, createTime, updateTime);
list.add(userInfoEntity);
}
resultSet.close();
statement.close();
connection.close();
return list;
}
public int selectCount() throws ClassNotFoundException, SQLException {
int count = 0;
Connection connection = DBUtils.openConn();
String sql = "SELECT COUNT(*) AS userCount FROM user_info where status = 1";
PreparedStatement statement = connection.prepareStatement(sql);
ResultSet resultSet = statement.executeQuery();
while (resultSet.next()) {
count = resultSet.getInt("userCount");
}
resultSet.close();
statement.close();
connection.close();
return count;
}
@Override
public boolean batchDeleteById(int[] ids) throws ClassNotFoundException, SQLException {
boolean flag = false;
// 获取连接
Connection connection = DBUtils.openConn();
// sql语句
// String sql = "delete from user_info where user_id = ?";
String sql = "update user_info set status = 0 where user_id = ?";
// 预处理对象
PreparedStatement statement = connection.prepareStatement(sql);
// 添加参数
for (int id : ids) {
statement.setInt(1, id);
statement.addBatch();
}
int[] result = statement.executeBatch();
// 执行预处理对象
// int count = statement.executeUpdate();
if (result.length > 0) {
flag = true;
}
statement.close();
connection.close();
return flag;
}
}
②package com.newcapec.entity;
ItemUserEntity.java
package com.newcapec.entity;
import java.util.Date;
public class ItemUserEntity extends Object {
private int itemUserId;
private int userId;
private int itemId;
private Date createTime;
private Date updateTime;
public ItemUserEntity() {
}
public ItemUserEntity(int itemUserId, int userId, int itemId, Date createTime, Date updateTime) {
super();
this.itemUserId = itemUserId;
this.userId = userId;
this.itemId = itemId;
this.createTime = createTime;
this.updateTime = updateTime;
}
public int getItemUserId() {
return itemUserId;
}
public void setItemUserId(int itemUserId) {
this.itemUserId = itemUserId;
}
public int getUserId() {
return userId;
}
public void setUserId(int userId) {
this.userId = userId;
}
public int getItemId() {
return itemId;
}
public void setItemId(int itemId) {
this.itemId = itemId;
}
public Date getCreateTime() {
return createTime;
}
public void setCreateTime(Date createTime) {
this.createTime = createTime;
}
public Date getUpdateTime() {
return updateTime;
}
public void setUpdateTime(Date updateTime) {
this.updateTime = updateTime;
}
@Override
public String toString() {
return "ItemUserEntity [itemUserId=" + itemUserId + ", userId=" + userId + ", itemId=" + itemId
+ ", createTime=" + createTime + ", updateTime=" + updateTime + "]";
}
}
LogsInfoEntity.java
package com.newcapec.entity;
import java.util.Date;
public class LogsInfoEntity extends Object {
private int logsId;
private int userId;
private String logsContent;
private Date createTime;
private Date updateTime;
public LogsInfoEntity() {
}
public LogsInfoEntity(int logsId, int userId, String logsContent, Date createTime, Date updateTime) {
super();
this.logsId = logsId;
this.userId = userId;
this.logsContent = logsContent;
this.createTime = createTime;
this.updateTime = updateTime;
}
public int getLogsId() {
return logsId;
}
public void setLogsId(int logsId) {
this.logsId = logsId;
}
public int getUserId() {
return userId;
}
public void setUserId(int userId) {
this.userId = userId;
}
public String getLogsContent() {
return logsContent;
}
public void setLogsContent(String logsContent) {
this.logsContent = logsContent;
}
public Date getCreateTime() {
return createTime;
}
public void setCreateTime(Date createTime) {
this.createTime = createTime;
}
public Date getUpdateTime() {
return updateTime;
}
public void setUpdateTime(Date updateTime) {
this.updateTime = updateTime;
}
@Override
public String toString() {
return "LogsInfoEntity [logsId=" + logsId + ", userId=" + userId + ", logsContent=" + logsContent
+ ", createTime=" + createTime + ", updateTime=" + updateTime + "]";
}
}
NewItemEntity.java
package com.newcapec.entity;
import java.util.Date;
import com.alibaba.fastjson.annotation.JSONField;
public class NewItemEntity extends Object {
private int itemId;
private String itemName;
@JSONField(format = "yyyy-MM-dd HH:mm:ss")
private Date createTime;
@JSONField(format = "yyyy-MM-dd HH:mm:ss")
private Date updateTime;
public NewItemEntity() {
}
public NewItemEntity(int itemId, String itemName) {
super();
this.itemId = itemId;
this.itemName = itemName;
}
public NewItemEntity(int itemId, String itemName, Date createTime, Date updateTime) {
super();
this.itemId = itemId;
this.itemName = itemName;
this.createTime = createTime;
this.updateTime = updateTime;
}
public int getItemId() {
return itemId;
}
public void setItemId(int itemId) {
this.itemId = itemId;
}
public String getItemName() {
return itemName;
}
public void setItemName(String itemName) {
this.itemName = itemName;
}
public Date getCreateTime() {
return createTime;
}
public void setCreateTime(Date createTime) {
this.createTime = createTime;
}
public Date getUpdateTime() {
return updateTime;
}
public void setUpdateTime(Date updateTime) {
this.updateTime = updateTime;
}
@Override
public String toString() {
return "NewItemEntity [itemId=" + itemId + ", itemName=" + itemName + ", createTime=" + createTime
+ ", updateTime=" + updateTime + "]";
}
}
NewsInfoEntity.java
package com.newcapec.entity;
import java.util.Date;
public class NewsInfoEntity extends Object {
private int newsId;
private int itemId;
private String newsTitle;
private String newsImage;
private String newsContent;
private Date createTime;
private Date updateTime;
public NewsInfoEntity() {
}
public NewsInfoEntity(int newsId, int itemId, String newsTitle, String newsImage, String newsContent,
Date createTime, Date updateTime) {
super();
this.newsId = newsId;
this.itemId = itemId;
this.newsTitle = newsTitle;
this.newsImage = newsImage;
this.newsContent = newsContent;
this.createTime = createTime;
this.updateTime = updateTime;
}
public int getNewsId() {
return newsId;
}
public void setNewsId(int newsId) {
this.newsId = newsId;
}
public int getItemId() {
return itemId;
}
public void setItemId(int itemId) {
this.itemId = itemId;
}
public String getNewsTitle() {
return newsTitle;
}
public void setNewsTitle(String newsTitle) {
this.newsTitle = newsTitle;
}
public String getNewsImage() {
return newsImage;
}
public void setNewsImage(String newsImage) {
this.newsImage = newsImage;
}
public String getNewsContent() {
return newsContent;
}
public void setNewsContent(String newsContent) {
this.newsContent = newsContent;
}
public Date getCreateTime() {
return createTime;
}
public void setCreateTime(Date createTime) {
this.createTime = createTime;
}
public Date getUpdateTime() {
return updateTime;
}
public void setUpdateTime(Date updateTime) {
this.updateTime = updateTime;
}
@Override
public String toString() {
return "NewsInfoEntity [newsId=" + newsId + ", itemId=" + itemId + ", newsTitle=" + newsTitle + ", newsImage="
+ newsImage + ", newsContent=" + newsContent + ", createTime=" + createTime + ", updateTime="
+ updateTime + "]";
}
}
ResultData.java
package com.newcapec.entity;
public class ResultData<T> {
private int total;
private T rows;
private String code;
public ResultData() {
super();
}
public ResultData(int total, T rows, String code) {
super();
this.total = total;
this.rows = rows;
this.code = code;
}
public int getTotal() {
return total;
}
public void setTotal(int total) {
this.total = total;
}
public T getRows() {
return rows;
}
public void setRows(T rows) {
this.rows = rows;
}
public String getCode() {
return code;
}
public void setCode(String code) {
this.code = code;
}
@Override
public String toString() {
return "ResultData [total=" + total + ", rows=" + rows + ", code=" + code + "]";
}
}
UserInfoEntity.java
package com.newcapec.entity;
import java.util.Date;
import com.alibaba.fastjson.annotation.JSONField;
public class UserInfoEntity {
private int userId;
private String userName;
private String userPwd;
@JSONField(format = "yyyy-MM-dd HH:mm:ss")
private Date createTime;
@JSONField(format = "yyyy-MM-dd HH:mm:ss")
private Date updateTime;
public UserInfoEntity() {
}
public UserInfoEntity(int userId, String userName, String userPwd) {
super();
this.userId = userId;
this.userName = userName;
this.userPwd = userPwd;
}
public UserInfoEntity(int userId, String userName, String userPwd, Date createTime, Date updateTime) {
super();
this.userId = userId;
this.userName = userName;
this.userPwd = userPwd;
this.createTime = createTime;
this.updateTime = updateTime;
}
public int getUserId() {
return userId;
}
public void setUserId(int userId) {
this.userId = userId;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getUserPwd() {
return userPwd;
}
public void setUserPwd(String userPwd) {
this.userPwd = userPwd;
}
public Date getCreateTime() {
return createTime;
}
public void setCreateTime(Date createTime) {
this.createTime = createTime;
}
public Date getUpdateTime() {
return updateTime;
}
public void setUpdateTime(Date updateTime) {
this.updateTime = updateTime;
}
@Override
public String toString() {
return "UserInfoEntity [userId=" + userId + ", userName=" + userName + ", userPwd=" + userPwd + ", createTime="
+ createTime + ", updateTime=" + updateTime + "]";
}
}
③package com.newcapec.servlet;
AddUserServlet.java
package com.newcapec.servlet;
import java.io.IOException;
import java.sql.SQLException;
import java.util.HashMap;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.alibaba.fastjson.JSON;
import com.newcapec.dao.UserInfoDao;
import com.newcapec.entity.UserInfoEntity;
/**
* Servlet implementation class AddUserServlet
*/
@WebServlet(name = "/AddUserServlet", urlPatterns = "/AddUserServlet")
public class AddUserServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
private UserInfoDao userInfoDao = new UserInfoDao();
/**
* @see HttpServlet#HttpServlet()
*/
public AddUserServlet() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
* response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
// TODO Auto-generated method stub
response.getWriter().append("Served at: ").append(request.getContextPath());
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
* response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
request.setCharacterEncoding("utf-8");
response.setContentType("text/json;charset=utf-8");
String userName = request.getParameter("userName");
String userPwd = request.getParameter("userPwd");
UserInfoEntity userInfoEntity = new UserInfoEntity();
userInfoEntity.setUserName(userName);
userInfoEntity.setUserPwd(userPwd);
try {
boolean flag = userInfoDao.insert(userInfoEntity);
HashMap<String, Boolean> result = new HashMap<>();
result.put("flag", flag);
response.getWriter().write(JSON.toJSONString(result));
} catch (ClassNotFoundException | SQLException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}
}
}
DeleteUserServlet.java
package com.newcapec.servlet;
import java.io.IOException;
import java.sql.SQLException;
import java.util.Arrays;
import java.util.HashMap;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.alibaba.fastjson.JSON;
import com.newcapec.dao.UserInfoDao;
/**
* Servlet implementation class DeleteUserServlet
*/
@WebServlet(name = "/DeleteUserServlet", urlPatterns = "/DeleteUserServlet")
public class DeleteUserServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
private UserInfoDao userInfoDao = new UserInfoDao();
/**
* @see HttpServlet#HttpServlet()
*/
public DeleteUserServlet() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
* response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
// TODO Auto-generated method stub
response.getWriter().append("Served at: ").append(request.getContextPath());
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
* response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
// 此处可以使用过滤器批量处理
request.setCharacterEncoding("utf-8");
response.setContentType("text/json;charset=utf-8");
String[] userIds = request.getParameterValues("userIds[]");
// 使用userInfoDao执行批量删除
int[] ids = new int[userIds.length];
for (int i = 0; i < userIds.length; i++) {
ids[i] = Integer.valueOf(userIds[i]);
}
try {
boolean flag = userInfoDao.batchDeleteById(ids);
HashMap<String, Boolean> result = new HashMap<>();
result.put("flag", flag);
response.getWriter().write(JSON.toJSONString(result));
} catch (ClassNotFoundException | SQLException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}
}
}
LoginServlet.java
package com.newcapec.servlet;
import java.io.IOException;
import java.sql.SQLException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.newcapec.dao.UserInfoDao;
import com.newcapec.entity.UserInfoEntity;
/**
* Servlet implementation class LoginServlet
*/
@WebServlet(name = "/LoginServlet", urlPatterns = "/LoginServlet")
public class LoginServlet extends HttpServlet {
private UserInfoDao userInfoDao = new UserInfoDao();
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public LoginServlet() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
* response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
// TODO Auto-generated method stub
response.getWriter().append("Served at: ").append(request.getContextPath());
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
* response)
*/
// 注意:在正规开发中 登录密码需要加密
// 需要有验证码
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
request.setCharacterEncoding("UTF-8");
String userName = request.getParameter("userName");
String userPwd = request.getParameter("userPwd");
try {
UserInfoEntity userInfoEntity = userInfoDao.login(userName, userPwd);
if (userInfoEntity != null) {
// 登录成功
request.getSession().setAttribute("userInfo", userInfoEntity);
response.sendRedirect("index.html");
} else {
// 登录失败 重定向
response.sendRedirect("loginFailure.html");
}
} catch (ClassNotFoundException | SQLException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}
}
}
SelectUserByIdServlet.java
package com.newcapec.servlet;
import java.io.IOException;
import java.sql.SQLException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.alibaba.fastjson.JSON;
import com.newcapec.dao.UserInfoDao;
import com.newcapec.entity.UserInfoEntity;
/**
* Servlet implementation class SelectUserByIdServlet
*/
@WebServlet(name = "/SelectUserByIdServlet", urlPatterns = "/SelectUserByIdServlet")
public class SelectUserByIdServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
private UserInfoDao userInfoDao = new UserInfoDao();
/**
* @see HttpServlet#HttpServlet()
*/
public SelectUserByIdServlet() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
* response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
// 此处可以使用过滤器批量处理
request.setCharacterEncoding("utf-8");
response.setContentType("text/json;charset=utf-8");
int userId = Integer.parseInt(request.getParameter("userId"));
try {
UserInfoEntity userInfoEntity = userInfoDao.selectById(userId);
response.getWriter().write(JSON.toJSONString(userInfoEntity));
} catch (ClassNotFoundException | SQLException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
* response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
// TODO Auto-generated method stub
doGet(request, response);
}
}
SelectUserByPageServlet.java
package com.newcapec.servlet;
import java.io.IOException;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.alibaba.fastjson.JSON;
import com.newcapec.dao.UserInfoDao;
import com.newcapec.entity.ResultData;
import com.newcapec.entity.UserInfoEntity;
/**
* Servlet implementation class SelectAllServlet
*/
@WebServlet(name = "/SelectAllServlet", urlPatterns = "/SelectAllServlet")
public class SelectUserByPageServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
private UserInfoDao userInfoDao = new UserInfoDao();
/**
* @see HttpServlet#HttpServlet()
*/
public SelectUserByPageServlet() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
* response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setCharacterEncoding("utf-8");
response.setContentType("text/json;charset=utf-8");
// 当前页码
// 页大小
int offset = Integer.parseInt(request.getParameter("offset"));
int limit = Integer.parseInt(request.getParameter("limit"));
// bootstrap-table插件需要后台传回total/rows
ResultData<List<UserInfoEntity>> data = new ResultData<>();
try {
data.setRows(userInfoDao.selectByPage(offset, limit));
data.setTotal(userInfoDao.selectCount());
response.getWriter().write(JSON.toJSONString(data));
} catch (ClassNotFoundException | SQLException e1) {
// TODO 自动生成的 catch 块
e1.printStackTrace();
}
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
* response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
// TODO Auto-generated method stub
doGet(request, response);
}
}
UpdateUserServlet.java
package com.newcapec.servlet;
import java.io.IOException;
import java.sql.SQLException;
import java.util.HashMap;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.alibaba.fastjson.JSON;
import com.newcapec.dao.UserInfoDao;
import com.newcapec.entity.UserInfoEntity;
/**
* Servlet implementation class UpdateUserServlet
*/
@WebServlet(name = "/UpdateUserServlet", urlPatterns = "/UpdateUserServlet")
public class UpdateUserServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
private UserInfoDao userInfoDao = new UserInfoDao();
/**
* @see HttpServlet#HttpServlet()
*/
public UpdateUserServlet() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
* response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
// TODO Auto-generated method stub
response.getWriter().append("Served at: ").append(request.getContextPath());
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
* response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
// 此处可以使用过滤器批量处理
request.setCharacterEncoding("utf-8");
response.setContentType("text/json;charset=utf-8");
int userId = Integer.parseInt(request.getParameter("userId"));
String userName = request.getParameter("userName");
String userPwd = request.getParameter("userPwd");
UserInfoEntity entity = new UserInfoEntity(userId, userName, userPwd);
try {
boolean flag = userInfoDao.update(entity);
HashMap<String, Boolean> result = new HashMap<>();
result.put("flag", flag);
response.getWriter().write(JSON.toJSONString(result));
} catch (ClassNotFoundException | SQLException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}
}
}
④package com.newcapec.servlet.newitem;
AddItemServlet.java
package com.newcapec.servlet.newitem;
import java.io.IOException;
import java.sql.SQLException;
import java.util.HashMap;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.alibaba.fastjson.JSON;
import com.newcapec.dao.ItemUserDao;
import com.newcapec.dao.NewItemDao;
import com.newcapec.entity.ItemUserEntity;
import com.newcapec.entity.NewItemEntity;
import com.newcapec.entity.UserInfoEntity;
/**
* Servlet implementation class AddUserServlet
*/
@WebServlet(name = "/AddItemServlet", urlPatterns = "/AddItemServlet")
public class AddItemServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
private NewItemDao newItemDao = new NewItemDao();
private ItemUserDao itemUserDao = new ItemUserDao();
/**
* @see HttpServlet#HttpServlet()
*/
public AddItemServlet() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
* response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
// TODO Auto-generated method stub
response.getWriter().append("Served at: ").append(request.getContextPath());
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
* response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
request.setCharacterEncoding("utf-8");
response.setContentType("text/json;charset=utf-8");
String ItemName = request.getParameter("itemName");
NewItemEntity newItemEntity = new NewItemEntity();
newItemEntity.setItemName(ItemName);
ItemUserEntity itemUserEntity = new ItemUserEntity();
// itemUserEntity.setItemId(itemId);
try {
int itemId = newItemDao.insert(newItemEntity);
itemUserEntity.setItemId(itemId);
UserInfoEntity userInfoEntity = (UserInfoEntity) request.getSession().getAttribute("userInfo");
itemUserEntity.setUserId(userInfoEntity.getUserId());
boolean flag = itemUserDao.insert(itemUserEntity);
HashMap<String, Boolean> result = new HashMap<>();
result.put("flag", flag);
response.getWriter().write(JSON.toJSONString(result));
} catch (ClassNotFoundException | SQLException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}
}
}
DeleteItemServlet.java
package com.newcapec.servlet.newitem;
import java.io.IOException;
import java.sql.SQLException;
import java.util.HashMap;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.alibaba.fastjson.JSON;
import com.newcapec.dao.NewItemDao;
/**
* Servlet implementation class DeleteItemServlet
*/
@WebServlet(name = "/DeleteItemServlet", urlPatterns = "/DeleteItemServlet")
public class DeleteItemServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
private NewItemDao newItemDao = new NewItemDao();
/**
* @see HttpServlet#HttpServlet()
*/
public DeleteItemServlet() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
* response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
// TODO Auto-generated method stub
response.getWriter().append("Served at: ").append(request.getContextPath());
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
* response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
// 此处可以使用过滤器批量处理
request.setCharacterEncoding("utf-8");
response.setContentType("text/json;charset=utf-8");
String[] itemIds = request.getParameterValues("itemIds[]");
// 使用newItemDao执行批量删除
int[] ids = new int[itemIds.length];
for (int i = 0; i < itemIds.length; i++) {
ids[i] = Integer.valueOf(itemIds[i]);
}
try {
boolean flag = newItemDao.batchDeleteById(ids);
HashMap<String, Boolean> result = new HashMap<>();
result.put("flag", flag);
response.getWriter().write(JSON.toJSONString(result));
} catch (ClassNotFoundException | SQLException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}
}
}
SelectItemByIdServlet.java
package com.newcapec.servlet.newitem;
import java.io.IOException;
import java.sql.SQLException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.alibaba.fastjson.JSON;
import com.newcapec.dao.NewItemDao;
import com.newcapec.entity.NewItemEntity;
/**
* Servlet implementation class SelectUserByIdServlet
*/
@WebServlet(name = "/SelectItemByIdServlet", urlPatterns = "/SelectItemByIdServlet")
public class SelectItemByIdServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
private NewItemDao newItemDao = new NewItemDao();
/**
* @see HttpServlet#HttpServlet()
*/
public SelectItemByIdServlet() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
* response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
// 此处可以使用过滤器批量处理
request.setCharacterEncoding("utf-8");
response.setContentType("text/json;charset=utf-8");
int itemId = Integer.parseInt(request.getParameter("itemId"));
try {
NewItemEntity newItemEntity = newItemDao.selectById(itemId);
response.getWriter().write(JSON.toJSONString(newItemEntity));
} catch (ClassNotFoundException | SQLException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
* response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
// TODO Auto-generated method stub
doGet(request, response);
}
}
SelectItembyUserServlet.java
package com.newcapec.servlet.newitem;
import java.io.IOException;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.alibaba.fastjson.JSON;
import com.newcapec.dao.NewItemDao;
import com.newcapec.entity.NewItemEntity;
import com.newcapec.entity.UserInfoEntity;
/**
* Servlet implementation class SelectItembyUserServlet
*/
@WebServlet(name = "/SelectItembyUserServlet", urlPatterns = "/SelectItembyUserServlet")
public class SelectItembyUserServlet extends HttpServlet { // 获取item数据表中的数据
private static final long serialVersionUID = 1L;
private NewItemDao newItemDao = new NewItemDao();
/**
* @see HttpServlet#HttpServlet()
*/
public SelectItembyUserServlet() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
* response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
request.setCharacterEncoding("utf-8");
response.setContentType("text/json;charset=utf-8");
// 通过userId获取
UserInfoEntity userInfoEntity = (UserInfoEntity) request.getSession().getAttribute("userInfo");
try {
List<NewItemEntity> rows = newItemDao.selectAllByUserId(userInfoEntity.getUserId());
int total = newItemDao.selectAllByUserIdCount(userInfoEntity.getUserId());
HashMap<String, Object> result = new HashMap<>();
result.put("rows", rows);
result.put("total", total);
response.getWriter().write(JSON.toJSONString(result));
} catch (ClassNotFoundException | SQLException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
* response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
// TODO Auto-generated method stub
doGet(request, response);
}
}
UpdateItemServlet.java
package com.newcapec.servlet.newitem;
import java.io.IOException;
import java.sql.SQLException;
import java.util.HashMap;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.alibaba.fastjson.JSON;
import com.newcapec.dao.NewItemDao;
import com.newcapec.entity.NewItemEntity;
/**
* Servlet implementation class UpdateUserServlet
*/
@WebServlet(name = "/UpdateItemServlet", urlPatterns = "/UpdateItemServlet")
public class UpdateItemServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
private NewItemDao newItemDao = new NewItemDao();
/**
* @see HttpServlet#HttpServlet()
*/
public UpdateItemServlet() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
* response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
// TODO Auto-generated method stub
response.getWriter().append("Served at: ").append(request.getContextPath());
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
* response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
// 此处可以使用过滤器批量处理
request.setCharacterEncoding("utf-8");
response.setContentType("text/json;charset=utf-8");
int itemId = Integer.parseInt(request.getParameter("itemId"));
String itemName = request.getParameter("itemName");
NewItemEntity entity = new NewItemEntity(itemId, itemName);
try {
boolean flag = newItemDao.update(entity);
HashMap<String, Boolean> result = new HashMap<>();
result.put("flag", flag);
response.getWriter().write(JSON.toJSONString(result));
} catch (ClassNotFoundException | SQLException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}
}
}
⑤package com.newcapec.servlet.news;
AddNewsServlet.java
package com.newcapec.servlet.news;
import java.io.IOException;
import java.sql.SQLException;
import java.util.HashMap;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.alibaba.fastjson.JSON;
import com.newcapec.dao.NewsInfoDao;
import com.newcapec.entity.NewsInfoEntity;
/**
* Servlet implementation class AddNewsServlet
*/
@WebServlet(name = "/AddNewsServlet", urlPatterns = "/AddNewsServlet")
public class AddNewsServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
private NewsInfoDao newsInfoDao = new NewsInfoDao();
/**
* @see HttpServlet#HttpServlet()
*/
public AddNewsServlet() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
* response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
// TODO Auto-generated method stub
response.getWriter().append("Served at: ").append(request.getContextPath());
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
* response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
// TODO Auto-generated method stub
// doGet(request, response);
request.setCharacterEncoding("utf-8");
response.setContentType("text/json;charset=utf-8");
String addNewsTitle = request.getParameter("addNewsTitle");
String addNewsItem = request.getParameter("addNewsItem"); // 栏目id
String addNewsContent = request.getParameter("addNewsContent");
NewsInfoEntity newsInfoEntity = new NewsInfoEntity();
newsInfoEntity.setNewsTitle(addNewsTitle);
newsInfoEntity.setItemId(Integer.parseInt(addNewsItem));
newsInfoEntity.setNewsContent(addNewsContent);
try {
boolean flag = newsInfoDao.insert(newsInfoEntity);
HashMap<String, Boolean> result = new HashMap<>();
result.put("flag", flag);
response.getWriter().write(JSON.toJSONString(result));
} catch (ClassNotFoundException | SQLException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}
}
}
⑥package com.newcapec.test;
TestItemUserDao.java
package com.newcapec.test;
import java.sql.SQLException;
import java.util.List;
import org.junit.Assert;
import org.junit.Test;
import com.newcapec.dao.ItemUserDao;
import com.newcapec.entity.ItemUserEntity;
public class TestItemUserDao {
private ItemUserDao ItemUserDao = new ItemUserDao();
@Test
public void testInsert() throws ClassNotFoundException, SQLException {
ItemUserEntity itemUserEntity = new ItemUserEntity();
itemUserEntity.setUserId(1);
itemUserEntity.setItemId(1);
Assert.assertTrue(ItemUserDao.insert(itemUserEntity));
}
@Test
public void testDeleteById() throws ClassNotFoundException, SQLException {
Assert.assertTrue(ItemUserDao.deleteById(1));
}
@Test
public void testUpdate() throws ClassNotFoundException, SQLException {
ItemUserEntity entity = ItemUserDao.selectById(2);
entity.setItemId(2);
Assert.assertTrue(ItemUserDao.update(entity));
}
@Test
public void testSelectAll() throws ClassNotFoundException, SQLException {
System.out.println(ItemUserDao.selectAll());
}
}
TestLogsInfoDao.java
package com.newcapec.test;
import java.sql.SQLException;
import java.util.List;
import org.junit.Assert;
import org.junit.Test;
import com.newcapec.dao.LogsInfoDao;
import com.newcapec.entity.LogsInfoEntity;
public class TestLogsInfoDao {
private LogsInfoDao logsInfoDao = new LogsInfoDao();
@Test
public void testInsert() throws ClassNotFoundException, SQLException {
LogsInfoEntity logsInfoEntity = new LogsInfoEntity();
logsInfoEntity.setUserId(1);
logsInfoEntity.setLogsContent("woshishenren");
// userInfoDao.insert(userInfoEntity);
Assert.assertTrue(logsInfoDao.insert(logsInfoEntity));
}
@Test
public void testSelect() throws ClassNotFoundException, SQLException {
List<LogsInfoEntity> list = logsInfoDao.selectAll();
System.out.println(list);
}
@Test
public void testDeleteById() throws ClassNotFoundException, SQLException {
Assert.assertTrue(logsInfoDao.deleteById(2));
}
@Test
public void testUpdate() throws ClassNotFoundException, SQLException {
LogsInfoEntity logsInfoEntity = logsInfoDao.selectById(2);
logsInfoEntity.setUserId(1);
logsInfoDao.update(logsInfoEntity);
}
@Test
public void testselectById() throws ClassNotFoundException, SQLException {
System.out.println(logsInfoDao.selectById(1));
}
}
TestNewItemDao.java
package com.newcapec.test;
import java.sql.SQLException;
import java.util.List;
import org.junit.Assert;
import org.junit.Test;
import com.newcapec.dao.NewItemDao;
import com.newcapec.entity.NewItemEntity;
public class TestNewItemDao {
private NewItemDao newItemDao = new NewItemDao();
@Test
public void testInsert() throws ClassNotFoundException, SQLException {
NewItemEntity newItemEntity = new NewItemEntity();
newItemEntity.setItemId(1);
newItemEntity.setItemName("插入测试");
// Assert.assertTrue(newItemDao.insert(newItemEntity));
}
@Test
public void testDeleteById() throws ClassNotFoundException, SQLException {
Assert.assertTrue(newItemDao.deleteById(14));
}
@Test
public void testUpdate() throws ClassNotFoundException, SQLException {
NewItemEntity entity = newItemDao.selectById(1);
entity.setItemName("更新item测试");
Assert.assertTrue(newItemDao.update(entity));
}
@Test
public void testSelectAll() throws ClassNotFoundException, SQLException {
System.out.println(newItemDao.selectAll());
}
}
NewsInfoDao.java
package com.newcapec.test;
import java.sql.SQLException;
import org.junit.Assert;
import org.junit.Test;
import com.newcapec.dao.NewsInfoDao;
import com.newcapec.entity.NewsInfoEntity;
public class TestNewsInfoDao {
private NewsInfoDao newsInfoDao = new NewsInfoDao();
@Test
public void testInsert() throws ClassNotFoundException, SQLException {
NewsInfoEntity newsInfoEntity = new NewsInfoEntity();
newsInfoEntity.setItemId(1);
newsInfoEntity.setNewsTitle("新能源召回大户特斯拉的“美式”傲慢|观潮");
newsInfoEntity.setNewsImage(
"http://pics1.baidu.com/feed/71cf3bc79f3df8dcecfa18268a75ee8c46102829.jpeg?token=32875797d2ded1ed2a9334df610bccf7");
newsInfoEntity.setNewsContent("新浪科技 花子健\r\n" + "从“硬件升级”到“召回”,理想汽车只用了5天。\r\n"
+ "而不久前的威马汽车召回事件,前后耗时23天,威马汽车将之定义为“史上最快召回”。如果仅仅是简单对比,理想汽车无疑刷新了历史记录。\r\n"
+ "对于新造车势力来说,受限于工艺和新技术,召回是大概率事件。目前除了小鹏未有实际的召回经历之外,蔚来汽车、威马汽车以及理想汽车,包括外来者特斯拉,都遇到过召回。只不过,中国新造车势力和特斯拉表现出来的态度截然不同。\r\n"
+ "“惶恐”的造车新势力\r\n" + "11月1日,理想汽车在媒体沟通会上宣布了针对前悬架下摆臂球销和底盘的“免费升级”,其中前悬架下摆臂球销受力太低与理想ONE多次出现断轴事件有关系。\r\n"
+ "但理想汽车的措辞遭到质疑。一方面是理想汽车CEO李想公开承认“升级肯定是因为产品有缺陷,我们是在行驶当中发生碰撞断掉的几率高于平均值。”而另外一方面,则是在一开始否认“召回”,以“免费升级”代替。\r\n"
+ "理想汽车销售服务副总裁刘杰强调,升级和那种由于车辆本身在行驶过程中出现故障和质量问题的召回,是不一样的。11月6日,理想汽车最终宣布召回10469辆理想ONE,占已交付车辆的将近50%。\r\n"
+ "官方披露的信息显示,截至2020年10月31日,理想ONE累计发生了97起前悬架碰撞事故,其中前悬架下摆臂球头从球销脱出的情况超过10起,造成了理想汽车在碰撞之后断轴的严重事故。");
Assert.assertTrue(newsInfoDao.insert(newsInfoEntity));
}
@Test
public void testDeleteById() throws ClassNotFoundException, SQLException {
Assert.assertTrue(newsInfoDao.deleteById(1));
}
@Test
public void testUpdate() throws ClassNotFoundException, SQLException {
NewsInfoEntity entity = newsInfoDao.selectById(2);
entity.setNewsTitle("图书馆维修,暂停开放一天");
Assert.assertTrue(newsInfoDao.update(entity));
}
@Test
public void testSelectAll() throws ClassNotFoundException, SQLException {
System.out.println(newsInfoDao.selectAll());
}
}
TestUserInfoDao.java
package com.newcapec.test;
import java.sql.SQLException;
import java.util.List;
import org.junit.Assert;
import org.junit.Test;
import com.newcapec.dao.UserInfoDao;
import com.newcapec.entity.UserInfoEntity;
public class TestUserInfoDao {
private UserInfoDao userInfoDao = new UserInfoDao();
@Test
public void testInsert() throws ClassNotFoundException, SQLException {
/*
* UserInfoEntity userInfoEntity = new UserInfoEntity();
* userInfoEntity.setUserName("田七"); userInfoEntity.setUserPwd("123456");
* //userInfoDao.insert(userInfoEntity);
* Assert.assertTrue(userInfoDao.insert(userInfoEntity));
*/
for (int i = 1; i <= 100; i++) {
UserInfoEntity userInfoEntity = new UserInfoEntity();
userInfoEntity.setUserName("userName" + i);
userInfoEntity.setUserPwd("123456");
// userInfoDao.insert(userInfoEntity);
Assert.assertTrue(userInfoDao.insert(userInfoEntity));
}
}
@Test
public void testSelect() throws ClassNotFoundException, SQLException {
List<UserInfoEntity> list = userInfoDao.selectAll();
System.out.println(list);
}
@Test
public void testDeleteById() throws ClassNotFoundException, SQLException {
Assert.assertTrue(userInfoDao.deleteById(7));
}
@Test
public void testUpdate() throws ClassNotFoundException, SQLException {
UserInfoEntity userInfoEntity = userInfoDao.selectById(1);
userInfoEntity.setUserName("zhangsan");
userInfoDao.update(userInfoEntity);
}
@Test
public void testselectById() throws ClassNotFoundException, SQLException {
System.out.println(userInfoDao.selectById(1));
}
}
⑦package com.newcapec.utils;
DBUtils.java
package com.newcapec.utils;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import com.mysql.jdbc.Driver;
/**
* 获取与数据库的连接 此处后期需要使用阿里巴巴的druid数据库连接池优化
*
* @author user
*
*/
public class DBUtils {
public static Connection openConn() throws ClassNotFoundException, SQLException {
// 连接数据库四要素
// url username password driver
String user = "root";
String password = "root";
// String driver = "com.mysql.jdbc.Driver";
// String url = "jdbc:mysql://127.0.0.1:3306/zykkk?characterEncoding=UTF-8";
String driver = "com.mysql.cj.jdbc.Driver";
String url = "jdbc:mysql://localhost:3306/zykkk?characterEncoding=utf-8&serverTimezone=UTC";// (UTC表示标准时区)
// 注册数据库驱动
Class.forName(driver);
return DriverManager.getConnection(url, user, password);
}
}
MD5Utils.java
package com.newcapec.utils;
import java.security.MessageDigest;
public class MD5Utils {
/***
* MD5加码 生成32位md5码
*/
public static String stringMD5(String inStr) {
MessageDigest md5 = null;
try {
md5 = MessageDigest.getInstance("MD5");
} catch (Exception e) {
System.out.println(e.toString());
e.printStackTrace();
return "";
}
char[] charArray = inStr.toCharArray();
byte[] byteArray = new byte[charArray.length];
for (int i = 0; i < charArray.length; i++)
byteArray[i] = (byte) charArray[i];
byte[] md5Bytes = md5.digest(byteArray);
StringBuffer hexValue = new StringBuffer();
for (int i = 0; i < md5Bytes.length; i++) {
int val = ((int) md5Bytes[i]) & 0xff;
if (val < 16)
hexValue.append("0");
hexValue.append(Integer.toHexString(val));
}
return hexValue.toString();
}
/**
* 加密解密算法 执行一次加密,两次解密
*/
public static String convertMD5(String inStr) {
char[] a = inStr.toCharArray();
for (int i = 0; i < a.length; i++) {
a[i] = (char) (a[i] ^ 't');
}
String s = new String(a);
return s;
}
// 测试主函数
public static void main(String args[]) {
String s = new String("1233");
System.out.println("原始:" + s);
System.out.println("MD5后:" + stringMD5(s));
System.out.println("加密的:" + convertMD5(s));
System.out.println("解密的:" + convertMD5(convertMD5(s)));
}
}
pages
item_manager.html
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<!-- 新 Bootstrap4 核心 CSS 文件 -->
<link rel="stylesheet" href="../bootstrap4/css/bootstrap.min.css">
<link rel="stylesheet" type="text/css" href="../font-awesome-4.7.0/css/font-awesome.min.css" />
<link href="../bootstrap-table/bootstrap-table.min.css" rel="stylesheet" />
<!-- jQuery文件。务必在bootstrap.min.js 之前引入 -->
<script src="../jquery/jquery.min.js"></script>
<!-- bootstrap.bundle.min.js 用于弹窗、提示、下拉菜单,包含了 popper.min.js -->
<script src="../js/popper.min.js"></script>
<!-- 最新的 Bootstrap4 核心 JavaScript 文件 -->
<script src="../bootstrap4/js/bootstrap.min.js"></script>
<script src="../bootstrap-table/bootstrap-table.js"></script>
<script src="../bootstrap-table/locale/bootstrap-table-zh-CN.js"></script>
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<title>栏目管理</title>
<script type="text/javascript">
function batchDelete() {
var rows = $('#newItemTab').bootstrapTable('getSelections');
console.log(rows);
var itemIds = new Array();
for (var i = 0; i < rows.length; i++) {
itemIds.push(rows[i].itemId);
}
console.log(itemIds);
$.ajax({
type: "POST",
url: "../DeleteItemServlet",
data: { "itemIds": itemIds },
success: function (msg) {
if (msg.flag) {
alert("batchDelete()成功");
//刷新数据
$('#newItemTab').bootstrapTable("refresh");
} else {
alert("失败");
}
}
});
}
function addNewItem() {
var itemName = $("#addItemName").val();
var itemdata = {
"itemName": itemName
};
$.ajax({
type: "POST",
url: "../AddItemServlet",
data: itemdata,
success: function (msg) {
if (msg.flag) {
alert("addNewItem()成功");
//刷新数据
$('#newItemTab').bootstrapTable("refresh");
} else {
alert("失败");
}
$("#addItemName").val("");
}
});
}
function edit(itemId) { // 编辑用户信息
console.log(itemId);
//通过该ID获取数据
//通过ajax发起请求获取用户信息
$.ajax({
type: "get",
url: "../SelectItemByIdServlet",
data: { "itemId": itemId },
success: function (msg) {
console.log(msg);
//获取成功后填入参数展示模态框
$("input[name='itemId']").val(msg.itemId);
$("input[name='itemName']").val(msg.itemName);
$("#updateNewItem").modal('show');
}
});
//执行更新
}
function updateNewItem() { // 刷新页面信息
var newItem = {
"itemId": $("input[name='itemId']").val(),
"itemName": $("input[name='itemName']").val()
};
$.ajax({
type: "post",
url: "../UpdateItemServlet",
data: newItem,
success: function (msg) {
if (msg.flag) {
alert("updateNewItem()成功");
//刷新数据
$('#newItemTab').bootstrapTable("refresh");
} else {
alert("失败");
}
$("input[name='itemId']").val("");
$("input[name='itemName']").val("");
}
});
}
</script>
</head>
<body>
<div style="padding: 10px">
<div id="toolbar" style="display: flex;">
<button type="button" class="btn btn-info" data-toggle="modal" data-target="#addNewItem"><i
class="fa fa-plus"></i> 添加</button>
<button type="button" class="btn btn-danger" onclick="batchDelete()"><i class="fa fa-minus"></i>
批量删除</button>
<!-- <input type="text" placeholder="请输入栏目名" id="itemName" value="新闻处"/><button class="btn btn-info" onclick='search()'><i class="fa fa-search"></i></button> -->
</div>
<table id="newItemTab" class="table table-hover table-bordered table-striped">
</table>
<div class="modal fade" id="updateNewItem">
<div class="modal-dialog">
<div class="modal-content">
<!-- 模态框头部 -->
<div class="modal-header bg-info">
<h4 class="modal-title">更新栏目信息</h4>
<button type="button" class="close" data-dismiss="modal">×</button>
</div>
<!-- 模态框主体 -->
<div class="modal-body">
<form>
<input type="hidden" name="itemId" />
<div class="input-group mb-3">
<div class="input-group-prepend">
<span class="input-group-text"><i class="fa fa-user"></i></span>
</div>
<input type="text" name="itemName" class="form-control" placeholder="Itemname">
</div>
</form>
</div>
<!-- 模态框底部 -->
<div class="modal-footer">
<button type="button" class="btn btn-success" data-dismiss="modal"
onclick=updateNewItem()>更新</button>
<button type="button" class="btn btn-secondary" data-dismiss="modal">关闭</button>
</div>
</div>
</div>
</div>
<!-- 模态框 -->
<div class="modal fade" id="addNewItem">
<div class="modal-dialog">
<div class="modal-content">
<!-- 模态框头部 -->
<div class="modal-header bg-info">
<h4 class="modal-title">添加栏目信息</h4>
<button type="button" class="close" data-dismiss="modal">×</button>
</div>
<!-- 模态框主体 -->
<div class="modal-body">
<form>
<div class="input-group mb-3">
<div class="input-group-prepend">
<span class="input-group-text"><i class="fa fa-user"></i></span>
</div>
<input type="text" id="addItemName" class="form-control" placeholder="Itemname">
</div>
</form>
</div>
<!-- 模态框底部 -->
<div class="modal-footer">
<button type="button" class="btn btn-success" data-dismiss="modal"
onclick=addNewItem()>添加!</button>
<button type="button" class="btn btn-secondary" data-dismiss="modal">关闭</button>
</div>
</div>
</div>
</div>
</div>
<script>
var icons = {
paginationSwitchDown: 'fa-caret-square-down',
paginationSwitchUp: 'fa-caret-square-up',
refresh: 'fa-refresh',
toggleOff: 'fa-toggle-off',
toggleOn: 'fa-toggle-on',
columns: 'fa-th-list',
fullscreen: 'fa-arrows-alt',
detailOpen: 'fa-plus',
detailClose: 'fa-minus'
};
//异步加载表格数据
$('#newItemTab').bootstrapTable({
url: '../SelectItembyUserServlet', //请求后台的URL(*)
method: 'get', //请求方式(*)
toolbar: '#toolbar', //工具按钮用哪个容器
striped: true, //是否显示行间隔色
cache: false, //是否使用缓存,默认为true,所以一般情况下需要设置一下这个属性(*)
pagination: true, //是否显示分页(*)
icons: icons, //重新定义图标 修复图标加载失败问题
queryParams: function (params) {
var temp = { //这里的键的名字和控制器的变量名必须一直,这边改动,控制器也需要改成一样的
limit: params.limit, //页面大小
offset: params.offset //页码
//itemName: $("#itemName").val()
//statu: $("#txt_search_statu").val()
};
return temp;
},
sidePagination: "server", //分页方式:client客户端分页,server服务端分页(*)
pageNumber: 1, //初始化加载第一页,默认第一页
pageSize: 10, //每页的记录行数(*)
pageList: [10, 15, 20, 25], //可供选择的每页的行数(*)
//search: true, //是否显示表格搜索,此搜索是客户端搜索,不会进服务端,所以,个人感觉意义不大
strictSearch: true,
showRefresh: true, //是否显示刷新按钮
clickToSelect: true, //是否启用点击选中行
uniqueId: "itemId", //每一行的唯一标识,一般为主键列
//showToggle:true, //是否显示详细视图和列表视图的切换按钮
//cardView: false, //是否显示详细视图
columns: [{
checkbox: true
}, {
field: 'itemId',
title: '栏目Id'
}, {
field: 'itemName',
title: '栏目名称'
}, {
field: 'createTime',
title: '创建时间'
}, {
field: 'updateTime',
title: '更新时间'
}, {
field: 'itemId',
title: '编辑',
formatter: function (value, row, index) {
var e = '<a class="btn btn-waring" href="#" mce_href="#" title="编辑" onclick="edit(\''
+ row.itemId
+ '\')"><i class="fa fa-edit"></i>编辑</a> ';
return e;
}
}]
});
</script>
</body>
</html>
news_manager.html
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>新闻管理</title>
<!-- 新 Bootstrap4 核心 CSS 文件 -->
<link rel="stylesheet" href="../bootstrap4/css/bootstrap.min.css">
<link rel="stylesheet" type="text/css" href="../font-awesome-4.7.0/css/font-awesome.min.css" />
<link href="../bootstrap-table/bootstrap-table.min.css" rel="stylesheet" />
<!-- jQuery文件。务必在bootstrap.min.js 之前引入 -->
<script src="../jquery/jquery.min.js"></script>
<!-- bootstrap.bundle.min.js 用于弹窗、提示、下拉菜单,包含了 popper.min.js -->
<script src="../js/popper.min.js"></script>
<!-- 最新的 Bootstrap4 核心 JavaScript 文件 -->
<script src="../bootstrap4/js/bootstrap.min.js"></script>
<script src="../bootstrap-table/bootstrap-table.js"></script>
<script src="../bootstrap-table/locale/bootstrap-table-zh-CN.js"></script>
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<style type="text/css">
body {
display: flex;
flex-direction: column;
min-height: 100vh;
}
#header {
display: flex;
align-items: center;
justify-content: space-between;
font-size: 20px;
margin: 15px;
}
#header>a {
color: #007FB0;
}
#bodychild1>a {
color: #007FB0;
}
#header>#header1 {
display: flex;
flex-direction: row;
}
#header1>#header2 {
display: flex;
flex-direction: row;
}
#body {
display: flex;
flex-direction: column;
border: 1px solid darkgrey;
margin: 15px;
}
#body1 {
height: 50px;
display: flex;
flex-grow: 1;
align-items: center;
padding: 20px;
background-color: #cecece;
border-bottom: 1px solid darkgrey;
}
#body2 {
height: 500px;
display: flex;
flex-direction: column;
margin: 20px;
}
#body3 {
height: 50px;
display: flex;
flex-direction: row;
margin: 10px;
}
#body4 {
height: 40px;
width: 80px;
display: flex;
justify-content: center;
align-items: center;
border: 1px solid darkgrey;
}
</style>
<script type="text/javascript">
$(function () {
$.ajax({
url: "../SelectItembyUserServlet",
method: "get",
success: function (data) {
for (var i = 0; i < data.rows.length; i++) {
//var $option = $("<option value="+data.rows[i].itetmId+">"+data.rows[i].itemName+"</option");
$("#addNewsItem").append("<option value=" + data.rows[i].itemId + ">" + data.rows[i].itemName + "</option");
}
}
});
});
function addNewItem() {
var addNewsTitle = $("#addNewsTitle").val();
var addNewsItem = $("#addNewsItem").val();
var addNewsContent = $("#addNewsContent").val();
console.log(addNewsTitle);
console.log(addNewsItem);
console.log(addNewsContent);
var newsdata = {
"addNewsTitle": addNewsTitle,
"addNewsItem": addNewsItem,
"addNewsContent": addNewsContent
};
console.log(newsdata);
//return;
$.ajax({
type: "POST",
url: "../AddNewsServlet",
data: newsdata,
success: function (msg) {
if (msg.flag) {
alert("成功插入新闻,芜湖~");
//刷新数据 $('#newItemTab').bootstrapTable("refresh");
} else {
alert("插入新闻失败~");
}
//$("#addItemName").val("");
}
});
}
</script>
</head>
<body>
<div id="header">
<a>新闻管理</a>
<div id="header1">
<div class="dropdown">
<button type="button" class="btn btn-secondary dropdown-toggle" data-toggle="dropdown"
style="background-color: white; color: #cbcbcb;">
教务处
</button>
<div class="dropdown-menu">
<a class="dropdown-item" href="#">学生会</a>
<a class="dropdown-item" href="#">党支部</a>
<a class="dropdown-item" href="#">分团委</a>
</div>
<!-- <select>
<option value="volvo">Volvo</option>
<option value="saab">Saab</option>
<option value="opel">Opel</option>
<option value="audi">Audi</option>
</select> -->
</div>
<div id="header2"><input type="text" class="form-control" placeholder="用户名">
<span class="input-group-text"><a href="#"><i class="fa-circle-thin"></i></a></span>
</div>
</div>
</div>
<!-- header yes -->
<div id="body">
<div id="body1">
<a>发布新闻</a>
</div>
<div id="body2">
<div id="body3">
<div id="body4"><a>标题</a> </div>
<div><input id="addNewsTitle" type="text" placeholder="新闻标题" style="height: 40px;width: 300px;"></div>
</div>
<div id="body3">
<div id="body4"><a>栏目</a> </div>
<!-- <div class="dropdown" style="width: 300px">
<button type="button" class="btn btn-secondary dropdown-toggle" data-toggle="dropdown" style="background-color: white; color: #b8b8b8;width: 150px;height: 40px">
教务处
</button>
<div class="dropdown-menu">
<a class="dropdown-item" href="#">学生会</a>
<a class="dropdown-item" href="#">党支部</a>
<a class="dropdown-item" href="#">分团委</a>
<a class="dropdown-item" href="#">院团委</a>
</div>
</div> -->
<select id="addNewsItem" class="form-control" style="height: 40px;width: 300px;">
</select>
</div>
<div style="margin: 10px;">
<textarea id="addNewsContent" rows="10" cols="100" placeholder="新闻内容"></textarea>
</div>
<div style="margin: 10px;"><button type="button" class="btn btn-primary" style="width: 80px;"
onClick="addNewItem()">发布</button></div>
</div>
</body>
</html>
user_manager.html
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<!-- 新 Bootstrap4 核心 CSS 文件 -->
<link rel="stylesheet" href="../bootstrap4/css/bootstrap.min.css">
<link rel="stylesheet" type="text/css" href="../font-awesome-4.7.0/css/font-awesome.min.css" />
<link href="../bootstrap-table/bootstrap-table.min.css" rel="stylesheet" />
<!-- jQuery文件。务必在bootstrap.min.js 之前引入 -->
<script src="../jquery/jquery.min.js"></script>
<!-- bootstrap.bundle.min.js 用于弹窗、提示、下拉菜单,包含了 popper.min.js -->
<script src="../js/popper.min.js"></script>
<!-- 最新的 Bootstrap4 核心 JavaScript 文件 -->
<script src="../bootstrap4/js/bootstrap.min.js"></script>
<script src="../bootstrap-table/bootstrap-table.js"></script>
<script src="../bootstrap-table/locale/bootstrap-table-zh-CN.js"></script>
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<title>用户管理</title>
<script type="text/javascript">
function batchDelete() {
var rows = $('#userInfoTab').bootstrapTable('getSelections');
console.log(rows);
var userIds = new Array();
for (var i = 0; i < rows.length; i++) {
userIds.push(rows[i].userId);
}
console.log(userIds);
$.ajax({
type: "POST",
url: "../DeleteUserServlet",
data: { "userIds": userIds },
success: function (msg) {
if (msg.flag) {
alert("成功");
//刷新数据
$('#userInfoTab').bootstrapTable("refresh");
} else {
alert("失败");
}
}
});
}
function addUserInfo() {
var userName = $("#addUserName").val();
var userPwd = $("#addUserPwd").val();
var userdata = {
"userName": userName,
"userPwd": userPwd
};
$.ajax({
type: "POST",
url: "../AddUserServlet",
data: userdata,
success: function (msg) {
if (msg.flag) {
alert("成功");
//刷新数据
$('#userInfoTab').bootstrapTable("refresh");
} else {
alert("失败");
}
$("#addUserName").val("");
$("#addUserPwd").val("");
}
});
}
function edit(userId) {
console.log(userId);
//通过该ID获取数据
//通过ajax发起请求获取用户信息
$.ajax({
type: "get",
url: "../SelectUserByIdServlet",
data: { "userId": userId },
success: function (msg) {
console.log(msg);
//获取成功后填入参数展示模态框
$("input[name='userId']").val(msg.userId);
$("input[name='userName']").val(msg.userName);
$("input[name='userPwd']").val(msg.userPwd);
$("#updateUserInfo").modal('show');
}
});
//执行更新
}
function updateUserInfo() {
var userInfo = {
"userId": $("input[name='userId']").val(),
"userName": $("input[name='userName']").val(),
"userPwd": $("input[name='userPwd']").val()
};
$.ajax({
type: "post",
url: "../UpdateUserServlet",
data: userInfo,
success: function (msg) {
if (msg.flag) {
alert("成功");
//刷新数据
$('#userInfoTab').bootstrapTable("refresh");
} else {
alert("失败");
}
$("input[name='userId']").val("");
$("input[name='userName']").val("");
$("input[name='userPwd']").val("");
}
});
}
</script>
</head>
<body>
<div style="padding: 10px">
<div id="toolbar" style="display: flex;">
<button type="button" class="btn btn-info" data-toggle="modal" data-target="#addUserInfo"><i
class="fa fa-plus"></i> 添加</button>
<button type="button" class="btn btn-danger" onclick="batchDelete()"><i class="fa fa-minus"></i>
批量删除</button>
<!-- <input type="text" placeholder="请输入用户名" id="userName" value="zhangsan"/><button class="btn btn-info" onclick='search()'><i class="fa fa-search"></i></button> -->
</div>
<table id="userInfoTab" class="table table-hover table-bordered table-striped">
</table>
<div class="modal fade" id="updateUserInfo">
<div class="modal-dialog">
<div class="modal-content">
<!-- 模态框头部 -->
<div class="modal-header bg-info">
<h4 class="modal-title">更新用户信息</h4>
<button type="button" class="close" data-dismiss="modal">×</button>
</div>
<!-- 模态框主体 -->
<div class="modal-body">
<form>
<input type="hidden" name="userId" />
<div class="input-group mb-3">
<div class="input-group-prepend">
<span class="input-group-text"><i class="fa fa-user"></i></span>
</div>
<input type="text" name="userName" class="form-control" placeholder="Username">
</div>
<div class="input-group mb-3">
<div class="input-group-prepend">
<span class="input-group-text"><i class="fa fa-lock"></i></span>
</div>
<input type="password" name="userPwd" class="form-control" placeholder="userpwd">
</div>
</form>
</div>
<!-- 模态框底部 -->
<div class="modal-footer">
<button type="button" class="btn btn-success" data-dismiss="modal"
onclick=updateUserInfo()>更新</button>
<button type="button" class="btn btn-secondary" data-dismiss="modal">关闭</button>
</div>
</div>
</div>
</div>
<!-- 模态框 -->
<div class="modal fade" id="addUserInfo">
<div class="modal-dialog">
<div class="modal-content">
<!-- 模态框头部 -->
<div class="modal-header bg-info">
<h4 class="modal-title">添加用户信息</h4>
<button type="button" class="close" data-dismiss="modal">×</button>
</div>
<!-- 模态框主体 -->
<div class="modal-body">
<form>
<div class="input-group mb-3">
<div class="input-group-prepend">
<span class="input-group-text"><i class="fa fa-user"></i></span>
</div>
<input type="text" id="addUserName" class="form-control" placeholder="Username">
</div>
<div class="input-group mb-3">
<div class="input-group-prepend">
<span class="input-group-text"><i class="fa fa-lock"></i></span>
</div>
<input type="password" id="addUserPwd" class="form-control" placeholder="userpwd">
</div>
</form>
</div>
<!-- 模态框底部 -->
<div class="modal-footer">
<button type="button" class="btn btn-success" data-dismiss="modal"
onclick=addUserInfo()>添加</button>
<button type="button" class="btn btn-secondary" data-dismiss="modal">关闭</button>
</div>
</div>
</div>
</div>
</div>
<script>
var icons = {
paginationSwitchDown: 'fa-caret-square-down',
paginationSwitchUp: 'fa-caret-square-up',
refresh: 'fa-refresh',
toggleOff: 'fa-toggle-off',
toggleOn: 'fa-toggle-on',
columns: 'fa-th-list',
fullscreen: 'fa-arrows-alt',
detailOpen: 'fa-plus',
detailClose: 'fa-minus'
};
//异步加载表格数据
$('#userInfoTab').bootstrapTable({
url: '../SelectAllServlet', //请求后台的URL(*)
method: 'get', //请求方式(*)
toolbar: '#toolbar', //工具按钮用哪个容器
striped: true, //是否显示行间隔色
cache: false, //是否使用缓存,默认为true,所以一般情况下需要设置一下这个属性(*)
pagination: true, //是否显示分页(*)
icons: icons, //重新定义图标 修复图标加载失败问题
queryParams: function (params) {
var temp = { //这里的键的名字和控制器的变量名必须一直,这边改动,控制器也需要改成一样的
limit: params.limit, //页面大小
offset: params.offset //页码
//userName: $("#userName").val()
//statu: $("#txt_search_statu").val()
};
return temp;
},
sidePagination: "server", //分页方式:client客户端分页,server服务端分页(*)
pageNumber: 1, //初始化加载第一页,默认第一页
pageSize: 10, //每页的记录行数(*)
pageList: [10, 15, 20, 25], //可供选择的每页的行数(*)
//search: true, //是否显示表格搜索,此搜索是客户端搜索,不会进服务端,所以,个人感觉意义不大
strictSearch: true,
showRefresh: true, //是否显示刷新按钮
clickToSelect: true, //是否启用点击选中行
uniqueId: "userId", //每一行的唯一标识,一般为主键列
//showToggle:true, //是否显示详细视图和列表视图的切换按钮
//cardView: false, //是否显示详细视图
columns: [{
checkbox: true
}, {
field: 'userId',
title: '用户Id'
}, {
field: 'userName',
title: '用户名'
}, {
field: 'createTime',
title: '创建时间'
}, {
field: 'updateTime',
title: '更新时间'
}, {
field: 'userId',
title: '编辑',
formatter: function (value, row, index) {
var e = '<a class="btn btn-waring" href="#" mce_href="#" title="编辑" onclick="edit(\''
+ row.userId
+ '\')"><i class="fa fa-edit"></i>编辑</a> ';
return e;
}
}]
});
</script>
</body>
</html>
index.html
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
<!-- 新 Bootstrap4 核心 CSS 文件 -->
<link rel="stylesheet" href="bootstrap4/css/bootstrap.min.css">
<link rel="stylesheet" type="text/css" href="font-awesome-4.7.0/css/font-awesome.min.css" />
<!-- jQuery文件。务必在bootstrap.min.js 之前引入 -->
<script src="jquery/jquery.min.js"></script>
<!-- bootstrap.bundle.min.js 用于弹窗、提示、下拉菜单,包含了 popper.min.js -->
<script src="js/popper.min.js"></script>
<!-- 最新的 Bootstrap4 核心 JavaScript 文件 -->
<script src="bootstrap4/js/bootstrap.min.js"></script>
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<title>主页</title>
<style type="text/css">
body {
display: flex;
flex-direction: column;
min-height: 100vh;
}
header {
height: 80px;
background-color: #007FB0;
display: flex;
align-items: center;
padding-left: 40px;
}
#main {
flex-grow: 1;
display: flex;
}
#left {
background-color: #EEEEEE;
/* border: 1px #004085 solid; */
}
#left {
width: 200px;
flex-shrink: 0;
display: flex;
flex-direction: column;
}
#left>p {
height: 40px;
padding: 0px;
margin: 0px;
display: flex;
justify-content: center;
align-items: center;
background-color: #F5F5F5;
border-bottom: 1px solid darkgrey;
}
#left>a {
height: 50px;
background-color: #EEEEEE;
border-bottom: 1px darkgrey solid;
border-right: 1px darkgrey solid;
display: flex;
justify-content: center;
align-items: center;
text-decoration: none;
color: #007FB0;
}
#left>a:hover {
background-color: white;
}
#left>a:nth-child(2) {
background-image: url(img/icon_01.png);
background-repeat: no-repeat;
background-position: 40px 18px;
}
#left>a:nth-child(3) {
background-image: url(img/icon_02.png);
background-repeat: no-repeat;
background-position: 40px 18px;
}
#left>a:nth-child(4) {
background-image: url(img/icon_03.png);
background-repeat: no-repeat;
background-position: 40px 18px;
}
#content {
flex-grow: 1;
}
</style>
</head>
<body>
<header>
<img src="img/tit.png" />
</header>
<div id="main">
<div id="left">
<p>当前用户:管理员“lwx”</p>
<a href="pages/user_manager.html" target="myFrame">账户管理</a>
<a href="pages/item_manager.html" target="myFrame">栏目管理</a>
<a href="pages/news_manager.html" target="myFrame">新闻管理</a>
</div>
<iframe id="content" frameborder="0" name="myFrame" src="pages/user_manager.html"></iframe>
</div>
</body>
</html>
login.html
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
<!-- 新 Bootstrap4 核心 CSS 文件 -->
<link rel="stylesheet" href="bootstrap4/css/bootstrap.min.css">
<link rel="stylesheet" type="text/css" href="font-awesome-4.7.0/css/font-awesome.min.css" />
<!-- jQuery文件。务必在bootstrap.min.js 之前引入 -->
<script src="jquery/jquery.min.js"></script>
<!-- bootstrap.bundle.min.js 用于弹窗、提示、下拉菜单,包含了 popper.min.js -->
<script src="js/popper.min.js"></script>
<!-- 最新的 Bootstrap4 核心 JavaScript 文件 -->
<script src="bootstrap4/js/bootstrap.min.js"></script>
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<style type="text/css">
header,
footer {
height: 100px;
/* background-color: #005CBF; */
}
body {
display: flex;
flex-direction: column;
min-height: 100vh;
}
#main {
flex-grow: 1;
background-image: url(img/banner.jpg);
background-repeat: no-repeat;
background-size: cover;
display: flex;
align-items: center;
justify-content: flex-end;
}
#login {
height: 400px;
width: 400px;
background-color: white;
margin-right: 100px;
border-radius: 10px;
}
</style>
<title>login</title>
</head>
<body>
<header></header>
<div id="main">
<div id="login">
<form style="padding: 20px;" action="LoginServlet" method="post">
<div class="input-group mb-3">
<div class="input-group-prepend">
<span class="input-group-text"><i class="fa fa-user"></i></span>
</div>
<input type="text" name="userName" class="form-control" placeholder="请输入用户名">
</div>
<div class="input-group mb-3">
<div class="input-group-prepend">
<span class="input-group-text"><i class="fa fa-lock"></i></span>
</div>
<input type="text" name="userPwd" class="form-control" placeholder="Username">
</div>
<button type="submit" class="btn btn-info form-control">登录</button>
</form>
</div>
</div>
<footer></footer>
</body>
</html>
loginFailure.html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
登录失败,请重新<a href="login.html">登录</a>
</body>
</html>
phone.html
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
<!-- 新 Bootstrap4 核心 CSS 文件 -->
<link rel="stylesheet" href="bootstrap4/css/bootstrap.min.css">
<link rel="stylesheet" type="text/css" href="font-awesome-4.7.0/css/font-awesome.min.css" />
<!-- jQuery文件。务必在bootstrap.min.js 之前引入 -->
<script src="jquery/jquery.min.js"></script>
<!-- bootstrap.bundle.min.js 用于弹窗、提示、下拉菜单,包含了 popper.min.js -->
<script src="js/popper.min.js"></script>
<!-- 最新的 Bootstrap4 核心 JavaScript 文件 -->
<script src="bootstrap4/js/bootstrap.min.js"></script>
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<title>主页</title>
<style type="text/css">
body {
margin: 0px;
}
header {
height: 54px;
background-color: #F8F8F8;
display: flex;
justify-content: space-between;
padding: 0px 10px;
align-items: center; /*垂直居中*/
}
#back, #close-page {
color: #FFC74F;
}
nav {
height: 45px;
border-bottom: 1px solid #FFC74F;
display: flex;
}
nav>a {
text-decoration: none;
flex-grow: 1; /*flex-grow属性定义项目的放大比例,默认为0,即如果存在剩余空间,也不放大。*/
border-right: 1px #FFC74F solid;
background-color: antiquewhite;
display: flex;
justify-content: center;
align-items: center;
color: brown;
}
.col-7 {
display: flex;
flex-direction: column;
justify-content: space-between;
}
.row {
margin-bottom: 5px;
background-color: rgb(250, 235, 215, 0.5);
}
</style>
</head>
<body>
<header>
<span id="back"><i class="fa fa-chevron-left"></i> 返回</span>
<span>校内新闻</span>
<span id="close-page"><a href="login.html">登录</a></span>
</header>
<nav>
<a href="#">教务处</a>
<a href="#">委员会</a>
<a href="#">管理会</a>
</nav>
<div class="container-fluid">
<div class="row">
<div class="col-5">
<img src="img/1.jpg" class="img-fluid" />
</div>
<div class="col-7">
<h5>我是一条莫得感情的信息。</h5>
<span>2020-11-19 09:15</span>
</div>
</div>
<div class="row">
<div class="col-5">
<img src="img/2.jpg" class="img-fluid" />
</div>
<div class="col-7">
<h5>新闻手机登录端展示。</h5>
<span>2020-11-19 09:15</span>
</div>
</div>
<div class="row">
<div class="col-5">
<img src="img/3.jpg" class="img-fluid" />
</div>
<div class="col-7">
<h5>冲呀~</h5>
<span>2020-11-19 09:15</span>
</div>
</div>
<div class="row">
<div class="col-5">
<img src="img/dragon.gif" class="img-fluid" />
</div>
<div class="col-7">
<h5>奥里给~</h5>
<span>2020-11-19 09:15</span>
</div>
</div>
<div class="row">
<div class="col-5">
<img src="img/1.jpg" class="img-fluid" />
</div>
<div class="col-7">
<h5>我是一条莫得感情的信息。</h5>
<span>2020-11-19 09:15</span>
</div>
</div>
<div class="row">
<div class="col-5">
<img src="img/2.jpg" class="img-fluid" />
</div>
<div class="col-7">
<h5>新闻手机登录端展示。</h5>
<span>2020-11-19 09:15</span>
</div>
</div>
<div class="row">
<div class="col-5">
<img src="img/3.jpg" class="img-fluid" />
</div>
<div class="col-7">
<h5>冲呀~</h5>
<span>2020-11-19 09:15</span>
</div>
</div>
<div class="row">
<div class="col-5">
<img src="img/dragon.gif" class="img-fluid" />
</div>
<div class="col-7">
<h5>奥里给~</h5>
<span>2020-11-19 09:15</span>
</div>
</div>
<div class="row">
<div class="col-5">
<img src="img/dog.jpg" class="img-fluid" />
</div>
<div class="col-7">
<h5>加油~</h5>
<span>2020-11-19 09:15</span>
</div>
</div>
</div>
</body>
</html>
2021年1月30日 下午 附上代码。
还没有评论,来说两句吧...