创建一个SpringBoot项目,添加如下依赖:
数据库使用之前的即可.
DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.example.demo.model.UserMapper">
<insert id="insert">
insert into user(username,password) values(#{username},#{password});
insert>
<select id="selectById" resultType="com.example.demo.model.User">
select * from user where userId = #{userId}
select>
<select id="selectByName" resultType="com.example.demo.model.User">
select * from user where username = #{username}
select>
mapper>
DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.example.demo.model.BlogMapper">
<insert id="insert">
insert into blog values(null,#{title},#{blog_content},#{userId},now())
insert>
<select id="selectByBlogId" resultType="com.example.demo.model.Blog">
select * from blog where blogId = #{blogId}
select>
<select id="selectByUserId" resultType="com.example.demo.model.Blog">
select * from blog where userId = #{userId}
select>
<select id="selectAll" resultType="com.example.demo.model.Blog">
select * from blog order by postTime desc
select>
<delete id="deleteByBlogId">
delete from blog where blogId = #{blogId}
delete>
mapper>
@Data
public class Blog {
private int blogId;
private String title;
private String blog_content;
private int userId;
private Timestamp postTime;
public String getPostTime() {
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
return simpleDateFormat.format(postTime);
}
}
@Data
public class User {
private int userId = 0;
private String username = "";
private String password = "";
}
@Mapper
public interface BlogMapper {
int deleteByBlogId(Integer blogId);
Blog selectByBlogId(Integer blogId);
List<Blog> selectByUserId(Integer userId);
int insert(Blog blog);
List<Blog> selectAll();
}
@Mapper
public interface BlogMapper {
int deleteByBlogId(Integer blogId);
Blog selectByBlogId(Integer blogId);
List<Blog> selectByUserId(Integer userId);
int insert(Blog blog);
List<Blog> selectAll();
}
@Service
public class BlogService {
@Resource
private BlogMapper blogMapper;
public int insert(Blog blog){
return blogMapper.insert(blog);
}
public List<Blog> selectByUserId(Integer userId){
return blogMapper.selectByUserId(userId);
}
public Blog selectByBlogId(Integer BlogId){
return blogMapper.selectByBlogId(BlogId);
}
public void deleteByBlogId(Integer BlogId){
blogMapper.deleteByBlogId(BlogId);
}
public List<Blog> selectAll(){
return blogMapper.selectAll();
}
}
@Service
public class UserService {
@Resource
private UserMapper userMapper;
public User selectById(Integer id){
return userMapper.selectById(id);
}
public int insert(User user){
return userMapper.insert(user);
}
public User selectByName(String username){
return userMapper.selectByName(username);
}
}
1
2
3
4
5
6
7
8
前端代码基本不用修改,直接引入即可
和原来一样,我们依旧可以通过判断是否有拥有参数来实现跳转到博客主页还是详情页
具体的可参考之前的博客: 博客系统
public class LoginIntercept implements HandlerInterceptor {
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
HttpSession session = request.getSession(false);
if(session != null && session.getAttribute("user") != null){
return true;
}
response.setStatus(401);
response.sendRedirect("/login.html");
return false;
}
}
@Configuration
public class APPConfig implements WebMvcConfigurer {
@Override
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(new LoginIntercept()).
//所有路由都拦截
addPathPatterns("/**").
//不拦截这个后缀的所有文件
excludePathPatterns("/**/*.css").
excludePathPatterns("/**/*.js").
excludePathPatterns("/**/*.png").
excludePathPatterns("/**/*.jpg").
//登录页和注册页也不用拦截
excludePathPatterns("/**/login.html").
excludePathPatterns("/**/register.html").
excludePathPatterns("/**/login").
excludePathPatterns("/**/register");
}
}
java - jar 加包名即可运行