三层架构
1)Controller:控制层。接收前端发送的请求,对请求进行处理,并响应数据。
2)Service:业务逻辑层。处理具体的业务逻辑。
3)Dao:数据访问层(Data Access Object),也称为持久层。负责数据访问操作,包括数据的增、删、改、查。
UserController类
点击查看代码
package com.itheima.controller;import cn.hutool.core.io.IoUtil;
import com.itheima.pojo.User;
//import com.itheima.service.UserService;
//import com.itheima.service.impl.UserServiceImpl;
import com.itheima.service.UserService;
import com.itheima.service.impl.UserServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;@RestController
public class UserController {
@Autowiredprivate UserService userService ;@RequestMapping("/list")public List<User> list(){//1.调用ServiceList<User> userList = userService.findAll();//2.响应数据return userList;}// @RequestMapping("/list")
// public List<User> list() throws Exception{
// //1.加载并读取文件
// InputStream in = this.getClass().getClassLoader().getResourceAsStream("user.txt");
// ArrayList<String> lines = IoUtil.readLines(in, StandardCharsets.UTF_8, new ArrayList<>());
//
// //2.解析数据,封装成对象 --> 集合
// List<User> userList = lines.stream().map(line -> {
// String[] parts = line.split(",");
// Integer id = Integer.parseInt(parts[0]);
// String username = parts[1];
// String password = parts[2];
// String name = parts[3];
// Integer age = Integer.parseInt(parts[4]);
// LocalDateTime updateTime = LocalDateTime.parse(parts[5], DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
//
// return new User(id, username, password, name, age, updateTime);
// }).toList();
//
// //3.响应数据
// //return JSONUtil.toJsonStr(userList, JSONConfig.create().setDateFormat("yyyy-MM-dd HH:mm:ss"));
// return userList;
// }}
UserSerivce接口
点击查看代码
package com.itheima.service;import com.itheima.pojo.User;
import java.util.List;public interface UserService {public List<User> findAll();}
UserSerivceImpl
点击查看代码
package com.itheima.service.impl;import com.itheima.dao.UserDao;
import com.itheima.dao.impl.UserDaoImpl;
import com.itheima.pojo.User;
import com.itheima.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;
import java.util.stream.Collectors;
@Service
public class UserServiceImpl implements UserService {
@Autowiredprivate UserDao userDao ;@Overridepublic List<User> findAll() {List<String> lines = userDao.findAll();List<User> userList = lines.stream().map(line -> {String[] parts = line.split(",");Integer id = Integer.parseInt(parts[0]);String username = parts[1];String password = parts[2];String name = parts[3];Integer age = Integer.parseInt(parts[4]);LocalDateTime updateTime = LocalDateTime.parse(parts[5], DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));return new User(id, username, password, name, age, updateTime);}).collect(Collectors.toList());return userList;}
}
UserDao接口
点击查看代码
package com.itheima.dao;import java.util.List;public interface UserDao {public List<String> findAll();}
UserDaoImpl
点击查看代码
package com.itheima.dao.impl;import cn.hutool.core.io.IoUtil;
import com.itheima.dao.UserDao;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Repository;import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;
@Repository
public class UserDaoImpl implements UserDao {@Overridepublic List<String> findAll() {InputStream in = this.getClass().getClassLoader().getResourceAsStream("user.txt");ArrayList<String> lines = IoUtil.readLines(in, StandardCharsets.UTF_8, new ArrayList<>());return lines;}
}