项目描述
本项目为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
这个作业做得时间挺久了,许多细节都记不清楚了,就先复盘这些~