package com. zr. po;
import javax. persistence. *;
import java. util. ArrayList;
import java. util. Date;
import java. util. List;
@Entity
@Table ( name = "t_blog" )
public class Blog {
@Id
@GeneratedValue
private Long id;
private String title;
@Basic ( fetch = FetchType. LAZY)
@Lob
private String content;
private String firstPicture;
private String flag;
private Integer views;
private boolean appreciation;
private boolean shareStatement;
private boolean commentabled;
private boolean published;
private boolean recommend;
@Temporal ( TemporalType. TIMESTAMP)
private Date createTime;
@Temporal ( TemporalType. TIMESTAMP)
private Date updateTime;
@ManyToOne
private Type type;
@ManyToMany ( cascade = { CascadeType. PERSIST} )
private List< Tag> tags = new ArrayList < > ( ) ;
@ManyToOne
private User user;
@OneToMany ( mappedBy = "blog" )
private List< Comment> comments = new ArrayList < > ( ) ;
@Transient
private String tagIds;
private String description;
public Blog ( ) {
}
public Long getId ( ) {
return id;
}
public void setId ( Long id) {
this . id = id;
}
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 String getFirstPicture ( ) {
return firstPicture;
}
public void setFirstPicture ( String firstPicture) {
this . firstPicture = firstPicture;
}
public String getFlag ( ) {
return flag;
}
public void setFlag ( String flag) {
this . flag = flag;
}
public Integer getViews ( ) {
return views;
}
public void setViews ( Integer views) {
this . views = views;
}
public boolean isAppreciation ( ) {
return appreciation;
}
public void setAppreciation ( boolean appreciation) {
this . appreciation = appreciation;
}
public boolean isShareStatement ( ) {
return shareStatement;
}
public void setShareStatement ( boolean shareStatement) {
this . shareStatement = shareStatement;
}
public boolean isCommentabled ( ) {
return commentabled;
}
public void setCommentabled ( boolean commentabled) {
this . commentabled = commentabled;
}
public boolean isPublished ( ) {
return published;
}
public void setPublished ( boolean published) {
this . published = published;
}
public boolean isRecommend ( ) {
return recommend;
}
public void setRecommend ( boolean recommend) {
this . recommend = recommend;
}
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;
}
public Type getType ( ) {
return type;
}
public void setType ( Type type) {
this . type = type;
}
public List< Tag> getTags ( ) {
return tags;
}
public void setTags ( List< Tag> tags) {
this . tags = tags;
}
public User getUser ( ) {
return user;
}
public void setUser ( User user) {
this . user = user;
}
public List< Comment> getComments ( ) {
return comments;
}
public void setComments ( List< Comment> comments) {
this . comments = comments;
}
public String getTagIds ( ) {
return tagIds;
}
public void setTagIds ( String tagIds) {
this . tagIds = tagIds;
}
public String getDescription ( ) {
return description;
}
public void setDescription ( String description) {
this . description = description;
}
public void init ( ) {
this . tagIds = tagsToIds ( this . getTags ( ) ) ;
}
private String tagsToIds ( List< Tag> tags) {
if ( ! tags. isEmpty ( ) ) {
StringBuffer ids = new StringBuffer ( ) ;
boolean flag = false ;
for ( Tag tag : tags) {
if ( flag) {
ids. append ( "," ) ;
} else {
flag = true ;
}
ids. append ( tag. getId ( ) ) ;
}
return ids. toString ( ) ;
} else {
return tagIds;
}
}
@Override
public String toString ( ) {
return "Blog{" +
"id=" + id +
", title='" + title + '\'' +
", content='" + content + '\'' +
", firstPicture='" + firstPicture + '\'' +
", flag='" + flag + '\'' +
", views=" + views +
", appreciation=" + appreciation +
", shareStatement=" + shareStatement +
", commentabled=" + commentabled +
", published=" + published +
", recommend=" + recommend +
", createTime=" + createTime +
", updateTime=" + updateTime +
", type=" + type +
", tags=" + tags +
", user=" + user +
", comments=" + comments +
", tagIds='" + tagIds + '\'' +
", description='" + description + '\'' +
'}' ;
}
}
package com. zr. po;
import javax. persistence. *;
import java. util. ArrayList;
import java. util. Date;
import java. util. List;
@Entity
@Table ( name = "t_comment" )
public class Comment {
@Id
@GeneratedValue
private Long id;
private String nickname;
private String email;
private String content;
private String avatar;
@Temporal ( TemporalType. TIMESTAMP)
private Date createTime;
@ManyToOne
private Blog blog;
@OneToMany ( mappedBy = "parentComment" )
private List< Comment> replyComments = new ArrayList < > ( ) ;
@ManyToOne
private Comment parentComment;
public Comment ( ) {
}
public Long getId ( ) {
return id;
}
public void setId ( Long id) {
this . id = id;
}
public String getNickname ( ) {
return nickname;
}
public void setNickname ( String nickname) {
this . nickname = nickname;
}
public String getEmail ( ) {
return email;
}
public void setEmail ( String email) {
this . email = email;
}
public String getContent ( ) {
return content;
}
public void setContent ( String content) {
this . content = content;
}
public String getAvatar ( ) {
return avatar;
}
public void setAvatar ( String avatar) {
this . avatar = avatar;
}
public Date getCreateTime ( ) {
return createTime;
}
public void setCreateTime ( Date createTime) {
this . createTime = createTime;
}
public Blog getBlog ( ) {
return blog;
}
public void setBlog ( Blog blog) {
this . blog = blog;
}
public List< Comment> getReplyComments ( ) {
return replyComments;
}
public void setReplyComments ( List< Comment> replyComments) {
this . replyComments = replyComments;
}
public Comment getParentComment ( ) {
return parentComment;
}
public void setParentComment ( Comment parentComment) {
this . parentComment = parentComment;
}
@Override
public String toString ( ) {
return "Comment{" +
"id=" + id +
", nickname='" + nickname + '\'' +
", email='" + email + '\'' +
", content='" + content + '\'' +
", avatar='" + avatar + '\'' +
", createTime=" + createTime +
'}' ;
}
}
package com. zr. po;
import javax. persistence. *;
import java. util. ArrayList;
import java. util. List;
@Entity
@Table ( name = "t_tag" )
public class Tag {
@Id
@GeneratedValue
private Long id;
private String name;
@ManyToMany ( mappedBy = "tags" )
private List< Blog> blogs = new ArrayList < > ( ) ;
public Tag ( ) {
}
public Long getId ( ) {
return id;
}
public void setId ( Long id) {
this . id = id;
}
public String getName ( ) {
return name;
}
public void setName ( String name) {
this . name = name;
}
public List< Blog> getBlogs ( ) {
return blogs;
}
public void setBlogs ( List< Blog> blogs) {
this . blogs = blogs;
}
@Override
public String toString ( ) {
return "Tag{" +
"id=" + id +
", name='" + name + '\'' +
'}' ;
}
}
package com. zr. po;
import javax. persistence. *;
import java. util. ArrayList;
import java. util. List;
@Entity
@Table ( name = "t_type" )
public class Type {
@Id
@GeneratedValue
private Long id;
private String name;
@OneToMany ( mappedBy = "type" )
private List< Blog> blogs = new ArrayList < > ( ) ;
public Type ( ) {
}
public Long getId ( ) {
return id;
}
public void setId ( Long id) {
this . id = id;
}
public String getName ( ) {
return name;
}
public void setName ( String name) {
this . name = name;
}
public List< Blog> getBlogs ( ) {
return blogs;
}
public void setBlogs ( List< Blog> blogs) {
this . blogs = blogs;
}
@Override
public String toString ( ) {
return "Type{" +
"id=" + id +
", name='" + name + '\'' +
'}' ;
}
}
package com. zr. po;
import javax. persistence. *;
import java. util. ArrayList;
import java. util. Date;
import java. util. List;
@Entity
@Table ( name = "t_user" )
public class User {
@Id
@GeneratedValue
private Long id;
private String nickname;
private String username;
private String password;
private String email;
private String avatar;
private Integer type;
@Temporal ( TemporalType. TIMESTAMP)
private Date createTime;
@Temporal ( TemporalType. TIMESTAMP)
private Date updateTime;
@OneToMany ( mappedBy = "user" )
private List< Blog> blogs = new ArrayList < > ( ) ;
public User ( ) {
}
public Long getId ( ) {
return id;
}
public void setId ( Long id) {
this . id = id;
}
public String getNickname ( ) {
return nickname;
}
public void setNickname ( String nickname) {
this . nickname = nickname;
}
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;
}
public String getEmail ( ) {
return email;
}
public void setEmail ( String email) {
this . email = email;
}
public String getAvatar ( ) {
return avatar;
}
public void setAvatar ( String avatar) {
this . avatar = avatar;
}
public Integer getType ( ) {
return type;
}
public void setType ( Integer type) {
this . type = type;
}
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;
}
public List< Blog> getBlogs ( ) {
return blogs;
}
public void setBlogs ( List< Blog> blogs) {
this . blogs = blogs;
}
@Override
public String toString ( ) {
return "User{" +
"id=" + id +
", nickname='" + nickname + '\'' +
", username='" + username + '\'' +
", password='" + password + '\'' +
", email='" + email + '\'' +
", avatar='" + avatar + '\'' +
", type=" + type +
", createTime=" + createTime +
", updateTime=" + updateTime +
'}' ;
}
}
package com. zr. dao;
import com. zr. po. User;
import org. springframework. data. jpa. repository. JpaRepository;
public interface UserDao extends JpaRepository < User, Long> {
User findByUsernameAndPassword ( String username, String password) ;
}
package com. zr. service;
import com. zr. po. User;
public interface IUserService {
User checkUser ( String username, String password) ;
}
package com. zr. service. impl;
import com. zr. dao. UserDao;
import com. zr. po. User;
import com. zr. service. IUserService;
import org. springframework. beans. factory. annotation. Autowired;
import org. springframework. stereotype. Service;
import util. MD5Util;
@Service
public class UserServiceImpl implements IUserService {
@Autowired
private UserDao userDao;
@Override
public User checkUser ( String username, String password) {
return userDao. findByUsernameAndPassword ( username, MD5Util. code ( password) ) ;
}
}
package com. zr. web;
import com. zr. po. User;
import com. zr. service. IUserService;
import com. zr. service. impl. UserServiceImpl;
import org. springframework. beans. factory. annotation. Autowired;
import org. springframework. stereotype. Controller;
import org. springframework. web. bind. annotation. GetMapping;
import org. springframework. web. bind. annotation. PostMapping;
import org. springframework. web. bind. annotation. RequestMapping;
import org. springframework. web. servlet. ModelAndView;
import org. springframework. web. servlet. mvc. support. RedirectAttributes;
import javax. servlet. http. HttpSession;
@Controller
@RequestMapping ( "/admin" )
public class LoginController {
@Autowired
private IUserService userService;
@GetMapping
public String tologin ( ) {
return "admin/login" ;
}
@PostMapping ( "/login" )
public String login ( String username, String password, HttpSession session, RedirectAttributes redirectAttributes) {
User user = userService. checkUser ( username, password) ;
if ( user!= null) {
session. setAttribute ( "user" , user) ;
return "admin/index" ;
} else {
redirectAttributes. addFlashAttribute ( "message" , "用户名或密码错误" ) ;
return "redirect:/admin" ;
}
}
@GetMapping ( "/logout" )
public String logout ( HttpSession session) {
session. removeAttribute ( "user" ) ;
return "admin/login" ;
}
}
package com. zr;
import org. springframework. boot. SpringApplication;
import org. springframework. boot. autoconfigure. SpringBootApplication;
@SpringBootApplication
public class BlogApplication {
public static void main ( String[ ] args) {
SpringApplication. run ( BlogApplication. class , args) ;
}
}
package util;
import java. security. MessageDigest;
import java. security. NoSuchAlgorithmException;
public class MD5Util {
public static String code ( String str) {
try {
MessageDigest md = MessageDigest. getInstance ( "MD5" ) ;
md. update ( str. getBytes ( ) ) ;
byte [ ] byteDigest = md. digest ( ) ;
int i;
StringBuffer buf = new StringBuffer ( "" ) ;
for ( int offset = 0 ; offset < byteDigest. length; offset++ ) {
i = byteDigest[ offset] ;
if ( i < 0 )
i += 256 ;
if ( i < 16 )
buf. append ( "0" ) ;
buf. append ( Integer. toHexString ( i) ) ;
}
return buf. toString ( ) ;
} catch ( NoSuchAlgorithmException e) {
e. printStackTrace ( ) ;
return null;
}
}
public static void main ( String[ ] args) {
System. out. println ( code ( "1" ) ) ;
}
}