目录
首先使用mybatis逆向工程将数据库表中的信息映射出来,自动生成pojo类、mapper 映射文件和 mapper 接口
项目主要功能
本项目建立一个在线个人日记系统,通过注册和登录,在用户个人日记模块,用户可以述说自己的心情,发表自己的言论,该言论有两种发布方式:秘密和公开。公开发布的言论其他用户可以看到,而秘密发布的言论只有用户自身能看到,其他用户是看不到的。在后台管理模块,用户可以对自己发表的帖子进行回复和删除操作.可以对自己的一些信息进行修改操作,查询日记详情,对日记进行评论的相关操作,也可以对自己喜欢的文章进行点赞(只能赞一次),也能为搞笑的评论无限点赞。
项目使用技术
前端页面基于HTML语言,采用渐进式VUE框架编写前端页面,前端UI使用的是element,基于Vue2.0的组件库,完成前端UI的定制,使用VsCode作为开发工具能够满足前端开发的所有需要。
后端代码基于Java语言,采用SSM框架集整合而成,使用IDEA作为开发工具。采用了MyBatis逆向工程完成项目主体、添加了邮箱发送功能、MD5加密算法对密码加密等操作
后台的数据库采用SQL Server 8.0管理整个系统的后台数据。
项目主要结构
项目主要界面展示
项目数据库设计
项目依赖
<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.5.6</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.cdnu</groupId>
<artifactId>diary</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>diary</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-validation</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.15</version>
</dependency>
<!--Druid数据源依赖-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.2.6</version>
</dependency>
<!--Mybatis启动器-->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.2.0</version>
</dependency>
<!--邮箱-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
</dependency>
<!--md5-->
<dependency>
<groupId>org.apache.shiro</groupId>
<artifactId>shiro-core</artifactId>
<version>1.4.2</version>
</dependency>
<!--aop-其他方式已解决-未用-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-aop</artifactId>
</dependency>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.2.0</version>
</dependency>
<!--html标签去除-->
<dependency>
<groupId>org.jsoup</groupId>
<artifactId>jsoup</artifactId>
<version>1.11.2</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<excludes>
<exclude>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</exclude>
</excludes>
</configuration>
</plugin>
<!--配置generator插件-->
<plugin>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-maven-plugin</artifactId>
<version>1.3.5</version>
<dependencies>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.15</version>
</dependency>
</dependencies>
<!--指定配置文件的路径-->
<configuration>
<configurationFile>src/main/resources/generatorConfig.xml</configurationFile>
<verbose>true</verbose>
<overwrite>true</overwrite>
</configuration>
</plugin>
</plugins>
<!--配置资源拷贝插件-->
<resources>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.xml</include>
</includes>
</resource>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>**/*.css</include>
<include>**/*.js</include>
<include>**/*.properties</include>
<include>**/*.html</include>
<include>**/*.jpg</include>
<include>**/*.png</include>
<include>**/*.tif</include>
<include>**/*.gif</include>
</includes>
</resource>
</resources>
</build>
</project>
项目开发步骤
全局异常跳转器
@Configuration
public class GlobalException implements HandlerExceptionResolver {
@Override
public ModelAndView resolveException(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) {
ModelAndView mv = new ModelAndView();
if(ex instanceof Exception){
mv.setViewName("error");
}
System.out.println(ex);
return mv;
}
}
拦截器
@WebFilter(urlPatterns = "*")
public class GlobalFilter implements Filter {
@Override
public void init(FilterConfig filterConfig) throws ServletException {
}
@Override
public void destroy() {
}
@Override
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
HttpServletRequest request=(HttpServletRequest)servletRequest;
String uri=request.getRequestURI();
if(uri.contains("login") || uri.contains("register") || uri.contains("pwd")||uri.contains("error")){
filterChain.doFilter(servletRequest,servletResponse);
}else {
HttpSession session = request.getSession();
TAdmin admin = (TAdmin) session.getAttribute(Constants.Admin_Session_Key);
if(admin!=null){
filterChain.doFilter(servletRequest,servletResponse);
}else {
request.getRequestDispatcher("/page/login").forward(servletRequest,servletResponse);
}
}
}
}
session监听器
public class HttpSessionLifeListener implements HttpSessionListener {
@Override
public void sessionCreated(HttpSessionEvent se) {
}
@Override
public void sessionDestroyed(HttpSessionEvent se) {
HttpSession session = se.getSession();
ServletContext servletContext = session.getServletContext();
TAdmin admin= (TAdmin) servletContext.getAttribute(Constants.Admin_Session_Key);
servletContext.removeAttribute(admin.getPkId()+"");
}
}
常量池
public abstract class Constants {
public static String Admin_Session_Key="admin";
public static String Verification="pwd";
public static String Context_VerifyCode="verifyCode";
public static String Thymeleaf_Email="emailTemplate";
public static String Diary_Image_FilePath="F:/diaryimg";
public static String Admin_Image_FilePath="F:/headimg";
}
静态资源映射器
@Configuration
public class WebConfigUtil implements WebMvcConfigurer {
//解决没有权限访问本地资源文件夹问题。。。。
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/imgx/**").addResourceLocations("file:/F:/diaryimg/");
registry.addResourceHandler("/img/**").addResourceLocations("file:/F:/headimg/");
}
}
配置文件
spring.datasource.url=jdbc:mysql://localhost:3306/game?userSSL=false&useUnicode=true&characterEncoding=UTF8&useTimezone=true&serverTimezone=GMT%2B8
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.username=root
spring.datasource.password=
spring.datasource.type=com.alibaba.druid.pool.DruidDataSource
spring.mail.default-encoding=UTF-8
spring.mail.host=smtp.qq.com
//qq邮箱
spring.mail.username=
//qq邮箱授权码
spring.mail.password=
spring.mail.properties.mail.smtp.auth=true
spring.mail.properties.mail.smtp.starttls.enable=true
spring.mail.properties.mail.smtp.starttls.required=true
spring.web.resources.static-locations= classpath:/
#设置单次上传文件最大值
spring.servlet.multipart.max-file-size = 20MB
spring.servlet.multipart.max-request-size = 200MB
首先使用mybatis逆向工程将数据库表中的信息映射出来,自动生成pojo类、mapper 映射文件和 mapper 接口
生成效果如:
实现登录注册功能(对密码实现了MD5加密算法)
用户服务层
@Service
public class AdminServiceImpl implements AdminService {
@Autowired
private TAdminMapper adminMapper;
@Override
public boolean login(TAdmin inputAdmin) {
TAdminExample example = new TAdminExample();
TAdminExample.Criteria criteria=example.createCriteria();
criteria.andFAdminEqualTo(inputAdmin.getfAdmin());
Md5Hash md5pwd = new Md5Hash(inputAdmin.getfPassword(),inputAdmin.getfAdmin(),2);
criteria.andFPasswordEqualTo(md5pwd.toString());
List<TAdmin> list = this.adminMapper.selectByExample(example);
if (list==null||list.size()<=0){
return false;
}
return true;
}
@Override
public void register(TAdmin admin) {
TAdmin tAdmin = new TAdmin();
tAdmin.setfAdmin(admin.getfAdmin());
tAdmin.setfAge(admin.getfAge());
tAdmin.setfEmail(admin.getfEmail());
//对密码进行MD5加密操作。。
Md5Hash md5pwd = new Md5Hash(admin.getfPassword(),admin.getfAdmin(),2);
tAdmin.setfPassword(md5pwd.toString());
tAdmin.setfName(admin.getfName());
//注册都是默认头像
tAdmin.setfHeadimage("/img/default.jpg");
this.adminMapper.insert(tAdmin);
}
@Override
public Integer findAdmin(String admin) {
TAdminExample example = new TAdminExample();
TAdminExample.Criteria criteria=example.createCriteria();
criteria.andFAdminEqualTo(admin);
List<TAdmin> list = this.adminMapper.selectByExample(example);
return list.get(0).getPkId();
}
@Override
public TAdmin findAdminBypk(Integer pk) {
TAdmin tAdmin = this.adminMapper.selectByPrimaryKey(pk);
return tAdmin;
}
@Override
public String findadminimg(Integer adminid) {
TAdmin tAdmin = this.adminMapper.selectByPrimaryKey(adminid);
String s = tAdmin.getfHeadimage();
return s;
}
@Override
public boolean modifyadmin(TAdmin admin,Integer adminid) {
TAdminExample example = new TAdminExample();
TAdminExample.Criteria criteria=example.createCriteria();
criteria.andPkIdEqualTo(adminid);
this.adminMapper.updateByExample(admin,example);
return true;
}
}
用户控制层
@Controller
@ResponseBody
public class AdminController {
@Autowired
private AdminService adminService;
@Autowired
private LogService logService;
@PostMapping("/register")
public boolean register(@RequestBody String inputpwd,HttpSession session){
TAdmin admin = (TAdmin) session.getAttribute(Constants.Admin_Session_Key);
String pwd = (String) session.getAttribute(Constants.Verification);
//pwd-->验证码
if(pwd.equals(inputpwd.substring(1,5))){
this.adminService.register(admin);
return true;
}
else {
return false;
}
}
@PostMapping("/login")
public boolean login(@RequestBody TAdmin admin, Model model, HttpSession session, HttpServletRequest request){
boolean flag = this.adminService.login(admin);
if (!flag){
return false;
}else {
Integer adminid = this.adminService.findAdmin(admin.getfAdmin());
admin.setPkId(adminid);
TAdmin adminBypk = this.adminService.findAdminBypk(adminid);
session.setAttribute(Constants.Admin_Session_Key,adminBypk);
ServletContext servletContext=session.getServletContext();
HttpSession temp = (HttpSession) servletContext.getAttribute(adminid+"");
if(temp!=null){
temp.invalidate();
}
servletContext.setAttribute(adminid+"",session);
//登录记录设置--成功加入数据库--与之匹配
TLog log = new TLog();
String ip=request.getRemoteAddr();
log.setfIp(ip);
log.setFkAdminid(admin.getPkId());
this.logService.addLog(log);
return true;
}
}
@GetMapping("/exit")
public boolean exit(HttpSession session){
try {
session.invalidate();
}catch (Exception e){
return false;
}
return true;
}
@RequestMapping("/findadminimg")
public String findadminimg( HttpSession session){
TAdmin admin = (TAdmin) session.getAttribute(Constants.Admin_Session_Key);
String findadminimg = this.adminService.findadminimg(admin.getPkId());
return findadminimg;
}
@RequestMapping("/modifyadmin")
public boolean modifyadmin(@RequestBody TAdmin admin){
Integer adminid = this.adminService.findAdmin(admin.getfAdmin());
this.adminService.modifyadmin(admin,adminid);
return true;
}
@RequestMapping("/findadmin")
public TAdmin findadmin(HttpSession session){
TAdmin admin= (TAdmin) session.getAttribute(Constants.Admin_Session_Key);
Integer pkId = admin.getPkId();
TAdmin tAdmin = this.adminService.findAdminBypk(pkId);
return tAdmin;
}
}
就先这样吧,弄这个东西太费时间了,先去玩游戏了,以后再弄。。。。。