基于javaweb+mysql的springboot+mybatis新闻管理系统(java+html+springboot+javabean+mysql+tomcat)
运行环境
Java≥8、MySQL≥5.7
开发工具
eclipse/idea/myeclipse/sts等均可配置运行
适用
课程设计,大作业,毕业设计,项目练习,学习演示等
功能说明
用户:登录、注册、新闻增删改
技术框架
HTML CSS JavaScript jQuery Bootstrap thymeleaf SpringBoot SpringMVC MyBatis
基于javaweb+mysql的SpringBoot+MyBatis新闻管理系统(java+html+springboot+javabean+mysql+tomcat)
/**
* 全局页面传入日期字符串,自动转换成日期格式
*/
@Component
public class DateConverterConfig implements Converter<String, Date> {
private static final List<String> formarts = new ArrayList<>(4);
static{
formarts.add("yyyy-MM");
formarts.add("yyyy-MM-dd");
formarts.add("yyyy-MM-dd hh:mm");
formarts.add("yyyy-MM-dd hh:mm:ss");
}
@Override
public Date convert(String source) {
String value = source.trim();
if ("".equals(value)) {
return null;
}
if(source.matches("^\\d{4}-\\d{1,2}$")){
return parseDate(source, formarts.get(0));
}else if(source.matches("^\\d{4}-\\d{1,2}-\\d{1,2}$")){
return parseDate(source, formarts.get(1));
}else if(source.matches("^\\d{4}-\\d{1,2}-\\d{1,2} {1}\\d{1,2}:\\d{1,2}$")){
return parseDate(source, formarts.get(2));
}else if(source.matches("^\\d{4}-\\d{1,2}-\\d{1,2} {1}\\d{1,2}:\\d{1,2}:\\d{1,2}$")){
return parseDate(source, formarts.get(3));
}else {
throw new IllegalArgumentException("Invalid boolean value '" + source + "'");
}
}
/**
* 格式化日期
* @param dateStr String 字符型日期
* @param format String 格式
* @return Date 日期
*/
if (users.size()>0&& !users.isEmpty()){
return false;//有人
}else {
return true;//没人
}
}
public List<User> checkLogin(User user){
UserExample userExample=new UserExample();
userExample.createCriteria().andUsernameEqualTo(user.getUsername());
List<User> users=userMapper.selectByExample(userExample);
return users;
}
}
@Service
public class NewsService {
@Autowired
private NewsMapper newsMapper;
public List<News> findAll() {
List<News> news=newsMapper.selectByExampleWithBLOBs(null);
return news;
}
public void addNews(News news) {
newsMapper.insertSelective(news);
}
public void updateNews(News news) {
newsMapper.updateByPrimaryKeySelective(news);
}
public void deleteUserByID(Integer id) {
newsMapper.deleteByPrimaryKey(id);
}
public News selectNewsById(Integer id) {
News news=newsMapper.selectByPrimaryKey(id);
return news;
</if>
</trim>
</insert>
<update id="updateByPrimaryKeySelective" parameterType="com.demo.pojo.News" >
update news
<set >
<if test="title != null" >
title = #{title,jdbcType=VARCHAR},
</if>
<if test="url != null" >
url = #{url,jdbcType=VARCHAR},
</if>
<if test="time != null" >
time = #{time,jdbcType=TIMESTAMP},
</if>
<if test="info != null" >
info = #{info,jdbcType=LONGVARCHAR},
</if>
</set>
where newsid = #{newsid,jdbcType=INTEGER}
</update>
<update id="updateByPrimaryKeyWithBLOBs" parameterType="com.demo.pojo.News" >
update news
set title = #{title,jdbcType=VARCHAR},
url = #{url,jdbcType=VARCHAR},
time = #{time,jdbcType=TIMESTAMP},
info = #{info,jdbcType=LONGVARCHAR}
where newsid = #{newsid,jdbcType=INTEGER}
</update>
<update id="updateByPrimaryKey" parameterType="com.demo.pojo.News" >
update news
set title = #{title,jdbcType=VARCHAR},
url = #{url,jdbcType=VARCHAR},
time = #{time,jdbcType=TIMESTAMP}
where newsid = #{newsid,jdbcType=INTEGER}
</update>
</mapper>
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>用户注册</title>
<link href="https://cdn.staticfile.org/twitter-bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet">
<link rel="stylesheet" th:href="@{/css/register.css}">
</head>
}
public void setDistinct(boolean distinct) {
this.distinct = distinct;
}
public boolean isDistinct() {
return distinct;
}
public List<Criteria> getOredCriteria() {
return oredCriteria;
}
public void or(Criteria criteria) {
oredCriteria.add(criteria);
}
public Criteria or() {
Criteria criteria = createCriteriaInternal();
oredCriteria.add(criteria);
return criteria;
}
public Criteria createCriteria() {
Criteria criteria = createCriteriaInternal();
if (oredCriteria.size() == 0) {
oredCriteria.add(criteria);
}
return criteria;
}
protected Criteria createCriteriaInternal() {
Criteria criteria = new Criteria();
return criteria;
}
public void clear() {
oredCriteria.clear();
orderByClause = null;
distinct = false;
}
protected abstract static class GeneratedCriteria {
protected List<Criterion> criteria;
protected GeneratedCriteria() {
super();
criteria = new ArrayList<Criterion>();
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex == null ? null : sex.trim();
}
public String getIntroduction() {
return introduction;
}
public void setIntroduction(String introduction) {
this.introduction = introduction == null ? null : introduction.trim();
}
@Override
public String toString() {
return "User{" +
"id=" + id +
", email='" + email + '\'' +
", username='" + username + '\'' +
", password='" + password + '\'' +
", age=" + age +
", sex='" + sex + '\'' +
", introduction='" + introduction + '\'' +
'}';
}
}
addCriterion("password not like", value, "password");
return (Criteria) this;
}
public Criteria andPasswordIn(List<String> values) {
addCriterion("password in", values, "password");
return (Criteria) this;
}
public Criteria andPasswordNotIn(List<String> values) {
addCriterion("password not in", values, "password");
return (Criteria) this;
}
public Criteria andPasswordBetween(String value1, String value2) {
addCriterion("password between", value1, value2, "password");
return (Criteria) this;
}
public Criteria andPasswordNotBetween(String value1, String value2) {
addCriterion("password not between", value1, value2, "password");
return (Criteria) this;
}
public Criteria andAgeIsNull() {
addCriterion("age is null");
return (Criteria) this;
}
public Criteria andAgeIsNotNull() {
addCriterion("age is not null");
return (Criteria) this;
}
public Criteria andAgeEqualTo(Integer value) {
addCriterion("age =", value, "age");
return (Criteria) this;
}
public Criteria andAgeNotEqualTo(Integer value) {
addCriterion("age <>", value, "age");
}
public Criteria andTitleLike(String value) {
addCriterion("title like", value, "title");
return (Criteria) this;
}
public Criteria andTitleNotLike(String value) {
addCriterion("title not like", value, "title");
return (Criteria) this;
}
public Criteria andTitleIn(List<String> values) {
addCriterion("title in", values, "title");
return (Criteria) this;
}
public Criteria andTitleNotIn(List<String> values) {
addCriterion("title not in", values, "title");
return (Criteria) this;
}
public Criteria andTitleBetween(String value1, String value2) {
addCriterion("title between", value1, value2, "title");
return (Criteria) this;
}
public Criteria andTitleNotBetween(String value1, String value2) {
addCriterion("title not between", value1, value2, "title");
return (Criteria) this;
}
public Criteria andUrlIsNull() {
addCriterion("url is null");
return (Criteria) this;
}
public Criteria andUrlIsNotNull() {
addCriterion("url is not null");
return (Criteria) this;
}
public Criteria andUrlEqualTo(String value) {
addCriterion("url =", value, "url");
return (Criteria) this;
}
public Criteria andUrlNotEqualTo(String value) {
addCriterion("url <>", value, "url");
return (Criteria) this;
}
protected List<Criteria> oredCriteria;
public NewsExample() {
oredCriteria = new ArrayList<Criteria>();
}
public void setOrderByClause(String orderByClause) {
this.orderByClause = orderByClause;
}
public String getOrderByClause() {
return orderByClause;
}
public void setDistinct(boolean distinct) {
this.distinct = distinct;
}
public boolean isDistinct() {
return distinct;
}
public List<Criteria> getOredCriteria() {
return oredCriteria;
}
public void or(Criteria criteria) {
oredCriteria.add(criteria);
}
public Criteria or() {
Criteria criteria = createCriteriaInternal();
oredCriteria.add(criteria);
return criteria;
}
public Criteria createCriteria() {
Criteria criteria = createCriteriaInternal();
if (oredCriteria.size() == 0) {
oredCriteria.add(criteria);
}
return criteria;
}
protected Criteria createCriteriaInternal() {
Criteria criteria = new Criteria();
return criteria;
}
public void clear() {
oredCriteria.clear();
orderByClause = null;
update news
set title = #{title,jdbcType=VARCHAR},
url = #{url,jdbcType=VARCHAR},
time = #{time,jdbcType=TIMESTAMP},
info = #{info,jdbcType=LONGVARCHAR}
where newsid = #{newsid,jdbcType=INTEGER}
</update>
<update id="updateByPrimaryKey" parameterType="com.demo.pojo.News" >
update news
set title = #{title,jdbcType=VARCHAR},
url = #{url,jdbcType=VARCHAR},
time = #{time,jdbcType=TIMESTAMP}
where newsid = #{newsid,jdbcType=INTEGER}
</update>
</mapper>
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>用户注册</title>
<link href="https://cdn.staticfile.org/twitter-bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet">
<link rel="stylesheet" th:href="@{/css/register.css}">
</head>
<body>
<div class="container ">
<div class="col-md-5 imgstyle">
<img th:src="@{/imgs/tree.jpg}" alt="树">
</div>
<form class="form-horizontal col-md-7 formstyle" action="/registerUser">
<div class="form-group "><h2 class="col-sm-6 col-sm-offset-2">用户注册</h2></div>
<div class="form-group">
<label for="inputEmail3" class="col-sm-2 control-label">用户邮箱:</label>
<div class="col-sm-10 usually">
<input type="email" class="form-control" name="email" id="inputEmail3" placeholder="邮箱" required>
</div>
</div>
<div class="form-group ">
<label class="col-sm-2 control-label">用户昵称:</label>
<div class="col-sm-10 usually">
<input type="text" class="form-control" name="username" placeholder="请输入用户名" required>
</div>
</div>
<div class="form-group">
<label class="col-sm-2 control-label">用户密码:</label>
<div class="col-sm-10 usually">
<input type="password" class="form-control" name="password" id="inputPassword3" placeholder="Password"
required>
<if test="orderByClause != null" >
order by ${orderByClause}
</if>
</select>
<select id="selectByPrimaryKey" resultMap="ResultMapWithBLOBs" parameterType="java.lang.Integer" >
select
<include refid="Base_Column_List" />
,
<include refid="Blob_Column_List" />
from news
where newsid = #{newsid,jdbcType=INTEGER}
</select>
<delete id="deleteByPrimaryKey" parameterType="java.lang.Integer" >
delete from news
where newsid = #{newsid,jdbcType=INTEGER}
</delete>
<insert id="insert" parameterType="com.demo.pojo.News" >
insert into news (newsid, title, url,
time, info)
values (#{newsid,jdbcType=INTEGER}, #{title,jdbcType=VARCHAR}, #{url,jdbcType=VARCHAR},
#{time,jdbcType=TIMESTAMP}, #{info,jdbcType=LONGVARCHAR})
</insert>
<insert id="insertSelective" parameterType="com.demo.pojo.News" >
insert into news
<trim prefix="(" suffix=")" suffixOverrides="," >
<if test="newsid != null" >
newsid,
</if>
<if test="title != null" >
title,
</if>
<if test="url != null" >
url,
</if>
<if test="time != null" >
time,
</if>
<if test="info != null" >
info,
</if>
public Criteria andUrlIsNotNull() {
addCriterion("url is not null");
return (Criteria) this;
}
public Criteria andUrlEqualTo(String value) {
addCriterion("url =", value, "url");
return (Criteria) this;
}
public Criteria andUrlNotEqualTo(String value) {
addCriterion("url <>", value, "url");
return (Criteria) this;
}
public Criteria andUrlGreaterThan(String value) {
addCriterion("url >", value, "url");
return (Criteria) this;
}
public Criteria andUrlGreaterThanOrEqualTo(String value) {
addCriterion("url >=", value, "url");
return (Criteria) this;
}
public Criteria andUrlLessThan(String value) {
addCriterion("url <", value, "url");
return (Criteria) this;
}
public Criteria andUrlLessThanOrEqualTo(String value) {
addCriterion("url <=", value, "url");
return (Criteria) this;
}
public Criteria andUrlLike(String value) {
addCriterion("url like", value, "url");
return (Criteria) this;
}
public Criteria andUrlNotLike(String value) {
addCriterion("url not like", value, "url");
return (Criteria) this;
}
public void setNewsid(Integer newsid) {
this.newsid = newsid;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title == null ? null : title.trim();
}
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url == null ? null : url.trim();
}
public String getTime() {
return time;
}
public void setTime(String time) {
this.time = time;
}
public String getInfo() {
return info;
}
public void setInfo(String info) {
this.info = info == null ? null : info.trim();
}
@Override
public String toString() {
return "News{" +
"newsid=" + newsid +
/**
* 全局页面传入日期字符串,自动转换成日期格式
*/
@Component
public class DateConverterConfig implements Converter<String, Date> {
private static final List<String> formarts = new ArrayList<>(4);
static{
formarts.add("yyyy-MM");
formarts.add("yyyy-MM-dd");
formarts.add("yyyy-MM-dd hh:mm");
formarts.add("yyyy-MM-dd hh:mm:ss");
}
@Override
public Date convert(String source) {
String value = source.trim();
if ("".equals(value)) {
return null;
}
if(source.matches("^\\d{4}-\\d{1,2}$")){
return parseDate(source, formarts.get(0));
}else if(source.matches("^\\d{4}-\\d{1,2}-\\d{1,2}$")){
return parseDate(source, formarts.get(1));
}else if(source.matches("^\\d{4}-\\d{1,2}-\\d{1,2} {1}\\d{1,2}:\\d{1,2}$")){
return parseDate(source, formarts.get(2));
}else if(source.matches("^\\d{4}-\\d{1,2}-\\d{1,2} {1}\\d{1,2}:\\d{1,2}:\\d{1,2}$")){
return parseDate(source, formarts.get(3));
}else {
throw new IllegalArgumentException("Invalid boolean value '" + source + "'");
}
}
/**
* 格式化日期
* @param dateStr String 字符型日期
* @param format String 格式
/**
* 全局页面传入日期字符串,自动转换成日期格式
*/
@Component
public class DateConverterConfig implements Converter<String, Date> {
private static final List<String> formarts = new ArrayList<>(4);
static{
formarts.add("yyyy-MM");
formarts.add("yyyy-MM-dd");
formarts.add("yyyy-MM-dd hh:mm");
formarts.add("yyyy-MM-dd hh:mm:ss");
}
@Override
public Date convert(String source) {
String value = source.trim();
if ("".equals(value)) {
return null;
}
if(source.matches("^\\d{4}-\\d{1,2}$")){
return parseDate(source, formarts.get(0));
}else if(source.matches("^\\d{4}-\\d{1,2}-\\d{1,2}$")){
return parseDate(source, formarts.get(1));
}else if(source.matches("^\\d{4}-\\d{1,2}-\\d{1,2} {1}\\d{1,2}:\\d{1,2}$")){
return parseDate(source, formarts.get(2));
}else if(source.matches("^\\d{4}-\\d{1,2}-\\d{1,2} {1}\\d{1,2}:\\d{1,2}:\\d{1,2}$")){
return parseDate(source, formarts.get(3));
}else {
throw new IllegalArgumentException("Invalid boolean value '" + source + "'");
}
}
/**
* 格式化日期
* @param dateStr String 字符型日期
* @param format String 格式
* @return Date 日期
*/
public Date parseDate(String dateStr, String format) {
Date date=null;
try {
DateFormat dateFormat = new SimpleDateFormat(format);
date = dateFormat.parse(dateStr);
} catch (Exception e) {
<if test="introduction != null" >
#{introduction,jdbcType=LONGVARCHAR},
</if>
</trim>
</insert>
<update id="updateByPrimaryKeySelective" parameterType="com.demo.pojo.User" >
update users
<set >
<if test="email != null" >
email = #{email,jdbcType=VARCHAR},
</if>
<if test="username != null" >
username = #{username,jdbcType=VARCHAR},
</if>
<if test="password != null" >
password = #{password,jdbcType=VARCHAR},
</if>
<if test="age != null" >
age = #{age,jdbcType=INTEGER},
</if>
<if test="sex != null" >
sex = #{sex,jdbcType=VARCHAR},
</if>
<if test="introduction != null" >
introduction = #{introduction,jdbcType=LONGVARCHAR},
</if>
</set>
where id = #{id,jdbcType=INTEGER}
</update>
<update id="updateByPrimaryKeyWithBLOBs" parameterType="com.demo.pojo.User" >
update users
set email = #{email,jdbcType=VARCHAR},
username = #{username,jdbcType=VARCHAR},
password = #{password,jdbcType=VARCHAR},
age = #{age,jdbcType=INTEGER},
sex = #{sex,jdbcType=VARCHAR},
introduction = #{introduction,jdbcType=LONGVARCHAR}
where id = #{id,jdbcType=INTEGER}
</update>
<update id="updateByPrimaryKey" parameterType="com.demo.pojo.User" >
update users
set email = #{email,jdbcType=VARCHAR},
username = #{username,jdbcType=VARCHAR},
password = #{password,jdbcType=VARCHAR},
age = #{age,jdbcType=INTEGER},
sex = #{sex,jdbcType=VARCHAR}
where id = #{id,jdbcType=INTEGER}
</update>
</mapper>
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="com.demo.mapper.NewsMapper" >
public String findAll(@RequestParam(value = "pn", defaultValue = "1") Integer pn, Model model) {
PageHelper.startPage(pn, 4);
List<News> newsList = newsService.findAll();
PageInfo<News> pageInfo = new PageInfo<>(newsList, 5);
model.addAttribute("newsList", pageInfo);
return "AllNews";
}
@PostMapping("/addNews")
public String addNews(News news, @RequestParam("uploadfile") List<MultipartFile> uploadfile,
HttpServletRequest request) {
// 判断所上传文件是否存在
if (!uploadfile.isEmpty() && uploadfile.size() > 0) {
// 循环输出上传的文件
for (MultipartFile file : uploadfile) {
// 获取上传文件的原始名称
String originalFilename = file.getOriginalFilename();
// 设置上传文件的保存地址目录
String dirPath = System.getProperty("user.dir") + "/src/main/resources/static/upload/";
File filePath = new File(dirPath);
// 如果保存文件的地址不存在,就先创建目录
if (!filePath.exists()) {
filePath.mkdirs();
}
// 使用UUID重新命名上传的文件名称(上传人_uuid_原始文件名称)
String newFilename = UUID.randomUUID() + "_" + originalFilename;
String imgurl = "/upload/" + newFilename;
System.err.println("" + imgurl);
news.setUrl(imgurl);
try {
// 使用MultipartFile接口的方法完成文件上传到指定位置
file.transferTo(new File(dirPath + newFilename));
System.err.println(dirPath + newFilename);
} catch (Exception e) {
e.printStackTrace();
return "Error";
}
}
newsService.addNews(news);
// 跳转到成功页面
return "redirect:/allNews";
} else {
return "Error";
}
}
@PostMapping("/updateNews")
public String updateNews(News news, @RequestParam("uploadfile") List<MultipartFile> uploadfile,
HttpServletRequest request) {
// 判断所上传文件是否存在
if (!uploadfile.isEmpty() && uploadfile.size() > 0) {
// 循环输出上传的文件
for (MultipartFile file : uploadfile) {
private String url;
private String time;
private String info;
public Integer getNewsid() {
return newsid;
}
public void setNewsid(Integer newsid) {
this.newsid = newsid;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title == null ? null : title.trim();
}
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url == null ? null : url.trim();
}
public String getTime() {
return time;
}
public void setTime(String time) {
this.time = time;
}
public String getInfo() {
return info;
}
public void setInfo(String info) {
this.info = info == null ? null : info.trim();
}
@Override
public String toString() {
return "News{" +
"newsid=" + newsid +
", title='" + title + '\'' +