博客系统
前端页面基于html+css+js
后端实现基于servlet +themthefe
1.需求分析
软件开发之前,先需求分析。明白需要实现哪些需求
博客系统要实现的功能:
1.能够展示当前的博客列表
博客列表页,展示了很多篇博客.每一篇博客包含标题,包含发布时间,博客的摘要
点击标题,就跳转到博客正文页
2博客正文页
显示该博客的详细内容.
正文里包含标题,发布时间,完整的正文(不实现展示图片)
3.博客编辑页
显示一个编辑框,用户可以在里面编辑博客内容.并提交给服务器.
提交成功之后,博客后台就会保存这个博客的内容接下来就能在列表页中看到这个博客了.
4.删除博客功能.
在博客详情页这里加上一个"删除"按钮.
5.注册&登陆功能
要求,登陆成功,才能发布博客~
如果没有登陆,只能查看博客,不能发布博客~
2.数据库设计操作
分析需要的表和每个表的字段
需要的是blog表和user表
博客表blog表
编号 blogid 标题title 正文 content 发布时间postTime 作者id userid
用户表 user
编号userid 用户名username 密码 password
create database if not exists RocketBlog;
use RocketBlog;
drop table if exists blog;
create table blog(
blogId int primary key auto_increment,
title varchar (512),
content text,
userId int,
postTime datetime
);
drop table if exists user;
create table user(
userId int primary key auto_increment,
username varchar (50),
password varchar (50)
);
封装数据库的操作
在编码之前先补充目录 引入以来 需要引入servlet mysql thymelef(页面渲染)等依赖
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.example</groupId>
<artifactId>rocket_blog</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<!-- https://mvnrepository.com/artifact/javax.servlet/javax.servlet-api -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
<scope>provided</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.49</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.thymeleaf/thymeleaf -->
<dependency>
<groupId>org.thymeleaf</groupId>
<artifactId>thymeleaf</artifactId>
<version>3.0.12.RELEASE</version>
</dependency>
</dependencies>
<packaging>war</packaging>
<build>
<finalName>rocket_blog</finalName>
</build>
<properties>
<encoding>UTF-8</encoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
</project>
<!DOCTYPE web-app PUBLIC
"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd" >
<web-app>
<display-name>Archetype Created Web Application</display-name>
</web-app>
使用jdbc进行连接 -》 使用单例(代码中只有一个实例)模式 -》
package dao;
import com.mysql.jdbc.jdbc2.optional.MysqlDataSource;
import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
public class DBUtil {
private static final String URL = "jdbc:mysql://127.0.0.1:3306/RocketBlog?characterEncoding=utf8&useSSL=false";
private static final String USERNAME = "root";
private static final String PASSWORD = "wangxp184219";
private static DataSource dataSource = null;
// 在这个代码中还存在一个非常严重的 BUG~(多线程)
public static DataSource getDataSource() {
// 看一下 dataSource 当前是否已经持有一个实例了.
// 如果没有持有, 就创建一个新的.
// 如果持有了, 就不必创建新的, 直接返回之前的.
if (dataSource == null) {
//datasource=new MysqlDataSource();
//((MysqlDataSource)datasource).setUrl(URL); 向下转型
MysqlDataSource mysqlDataSource = new MysqlDataSource();
mysqlDataSource.setUrl(URL);
mysqlDataSource.setUser(USERNAME);
mysqlDataSource.setPassword(PASSWORD);
dataSource = mysqlDataSource;
}
return dataSource;
}
public static Connection getConnection() {
try {
return getDataSource().getConnection();
} catch (SQLException e) {
e.printStackTrace();
}
return null;
}
public static void close(Connection connection,
PreparedStatement statement,
ResultSet resultSet) {
if (resultSet != null) {
try {
resultSet.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (statement != null) {
try {
statement.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (connection != null) {
try {
connection.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
创建实体 类-》
package dao;
public class User {
private int userId;
private String username;
private String password;
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 getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
@Override
public String toString() {
return "dao.User{" +
"userId=" + userId +
", username='" + username + '\'' +
", password='" + password + '\'' +
'}';
}
}
package dao;
import java.sql.Date;
import java.sql.Timestamp;
public class Blog {
private int blogId;
private String title;
private String content;
private int userId;
// java.sql.Date 这个类型只能表示日期, 没有时分秒
private Timestamp postTime;
public int getBlogId() {
return blogId;
}
public void setBlogId(int blogId) {
this.blogId = blogId;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
public int getUserId() {
return userId;
}
public void setUserId(int userId) {
this.userId = userId;
}
public Timestamp getPostTime() {
return postTime;
}
public void setPostTime(Timestamp postTime) {
this.postTime = postTime;
} @Override
public String toString() {
return "dao.Blog{" +
"blogId=" + blogId +
", title='" + title + '\'' +
", content='" + content + '\'' +
", userId=" + userId +
", postTime=" + postTime +
'}';
}
}
针对数据库进行增删改查 dao data access object 数据访问对象
package dao;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
public class BlogDao {
// 往数据库中新增一个 博客
public void insert(Blog blog) {
// 1. 和数据库服务器建立连接.
Connection connection = DBUtil.getConnection();
// 2. 拼装 SQL
String sql = "insert into blog values(null, ?, ?, ?, now())";
PreparedStatement statement = null;
try {
statement = connection.prepareStatement(sql);
statement.setString(1, blog.getTitle());
statement.setString(2, blog.getContent());
statement.setInt(3, blog.getUserId());
// 3. 执行 SQL
statement.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
} finally {
// 4. 收尾工作.
DBUtil.close(connection, statement, null);
}
}
// 从数据库删除博客
public void delete(int blogId) {
// 1. 和数据库服务器建立连接.
Connection connection = DBUtil.getConnection();
// 2. 拼装 SQL
String sql = "delete from blog where blogId = ?";
PreparedStatement statement = null;
try {
statement = connection.prepareStatement(sql);
statement.setInt(1, blogId);
// 3. 执行 SQL
statement.executeUpdate();
} catch (SQLException e) {
e.printStackTrace()