SpringBoot+ Mybatis 开发一个读书网站

项目描述

本项目为javaweb课程设计作业,开发了一个类似于豆瓣读书的书友交流网站。

效果图

项目结构

                        

 

mapper和pojo可以用generator自动生成,我们只需编写controller就可以了

贴一个userController的代码,其他的都大同小异

package com.tedu.aireading.controller;

import java.util.List;
import java.util.Random;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.tedu.aireading.mapper.UserMapper;
import com.tedu.aireading.pojo.BookExample.Criteria;
import com.tedu.aireading.pojo.User;
import com.tedu.aireading.pojo.UserExample;


@RestController
public class UserController {

	@Autowired
	UserMapper userMapper;
	
	@RequestMapping("/user/test")
	public String test() {
		return "测试";
	}
	
	//登录/注册函数,输入用户名、密码
	//若用户名已存在则为登录,判断密码;若不存在进行注册
	//注册或登陆成功返回id,否则返回失败
	@RequestMapping("/user/registOrLogin")
	public String registOrLogin(String userName,String password) {
		UserExample userExample=new UserExample();
		UserExample.Criteria criteria = userExample.createCriteria();
		criteria.andUserNameEqualTo(userName);
		List<User> list=userMapper.selectByExample(userExample);
		if(list==null||list.size()==0) {//注册
			String str="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
		     Random random=new Random();
		     StringBuffer sb=new StringBuffer();
		     for(int i=0;i<6;i++){
		       int number=random.nextInt(62);
		       sb.append(str.charAt(number));
		     }
		     
			User user=new User();
			user.setUserId(sb.toString());
			user.setUserName(userName);
			user.setPassword(password);
			if(userMapper.insert(user)>0) 
				return "register";
		}
		else {
			User user=list.get(0);
			if(user.getPassword().equals(password))//登录成功
				return user.getUserId().toString();
		}
		return "failed";
	}
	
	@RequestMapping("/user/exit")
	public String exit(String userId) {
		return "1";
	}
}

application.yml写法

server:
  port: 6000
spring:
    application:
        #typeservice前面必须有一个空格
        name: readservice
    datasource:
        driver-class-name: com.mysql.cj.jdbc.Driver
        url: jdbc:mysql://127.0.0.1:3306/aireading?useUnicode=true&characterEncoding=utf-8&useSSL=true&serverTimezone=UTC
        username: root
        password: root

mybatis:
  mapperLocations: classpath:com.tedu.aireading.mapper/*.xml

logging:
  level: 
    com.tedu.aireading: debug
resources: # 指定静态资源的路径
        static-locations: classpath:/
        cache-period: 0

这个作业做得时间挺久了,许多细节都记不清楚了,就先复盘这些~

 

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值