暂时告一段落
This commit is contained in:
@@ -2,6 +2,8 @@ package com.itheima.springbootweb01.controller;
|
|||||||
|
|
||||||
import cn.hutool.core.io.IoUtil;
|
import cn.hutool.core.io.IoUtil;
|
||||||
import com.itheima.springbootweb01.pojo.User;
|
import com.itheima.springbootweb01.pojo.User;
|
||||||
|
import com.itheima.springbootweb01.service.UserService;
|
||||||
|
import com.itheima.springbootweb01.service.lmpl.UserServiceImpl;
|
||||||
import org.springframework.web.bind.annotation.RequestMapping;
|
import org.springframework.web.bind.annotation.RequestMapping;
|
||||||
import org.springframework.web.bind.annotation.RestController;
|
import org.springframework.web.bind.annotation.RestController;
|
||||||
|
|
||||||
@@ -22,28 +24,39 @@ import static java.lang.System.in;
|
|||||||
//用户信息控制器
|
//用户信息控制器
|
||||||
@RestController //作用:将controller返回值直接作为响应数据返回给浏览器@ResponseBody
|
@RestController //作用:将controller返回值直接作为响应数据返回给浏览器@ResponseBody
|
||||||
public class UserController {
|
public class UserController {
|
||||||
@RequestMapping("/hello");
|
/**
|
||||||
|
* 接受请求 相应数据
|
||||||
|
*/
|
||||||
|
|
||||||
|
private UserService userService = new UserServiceImpl();
|
||||||
|
|
||||||
|
// @RequestMapping("/list")
|
||||||
|
// public List<User> list() throws Exception{
|
||||||
|
// //1.加载并读取user.txt文件,获取用户数据
|
||||||
|
// // 绝对路径
|
||||||
|
// //InputStream in = new FileInputStream(new File("C:\\Users\\syx\\Desktop\\JavaWeb\\04. 后端Web基础(基础知识)\\springboot-web-01\\src\\main\\resources\\user.txt"));
|
||||||
|
// //相对路径
|
||||||
|
// InputStream in = this.getClass().getClassLoader().getResourceAsStream("user.txt");
|
||||||
|
// ArrayList<String> lines = IoUtil.readLines(in, StandardCharsets.UTF_8, new ArrayList<>());
|
||||||
|
// //2.将用户数据封装成User对象 ->list集合
|
||||||
|
// List<User> UserList= lines.stream().map(line -> {
|
||||||
|
// String[] split = line.split(",");
|
||||||
|
// Integer id = Integer.parseInt(split[0]);
|
||||||
|
// String username = split[1];
|
||||||
|
// String password = split[2];
|
||||||
|
// String name = split[3];
|
||||||
|
// Integer age = Integer.parseInt(split[4]);
|
||||||
|
// LocalDateTime updateTime = LocalDateTime.parse(split[5], DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
|
||||||
|
// return new User(id, username, password, name, age, updateTime);
|
||||||
|
// }).toList();
|
||||||
|
// //3.返回数据(json)
|
||||||
|
// return UserList;
|
||||||
|
// }
|
||||||
@RequestMapping("/list")
|
@RequestMapping("/list")
|
||||||
public List<User> list() throws Exception{
|
public List<User> list() throws Exception{
|
||||||
//1.加载并读取user.txt文件,获取用户数据
|
//1.调用service层数据
|
||||||
// 绝对路径
|
List<User> UserList= userService.findAll();
|
||||||
//InputStream in = new FileInputStream(new File("C:\\Users\\syx\\Desktop\\JavaWeb\\04. 后端Web基础(基础知识)\\springboot-web-01\\src\\main\\resources\\user.txt"));
|
|
||||||
//相对路径
|
|
||||||
InputStream in = this.getClass().getClassLoader().getResourceAsStream("user.txt");
|
|
||||||
ArrayList<String> lines = IoUtil.readLines(in, StandardCharsets.UTF_8, new ArrayList<>());
|
|
||||||
//2.将用户数据封装成User对象 ->list集合
|
|
||||||
List<User> UserList= lines.stream().map(line -> {
|
|
||||||
String[] split = line.split(",");
|
|
||||||
Integer id = Integer.parseInt(split[0]);
|
|
||||||
String username = split[1];
|
|
||||||
String password = split[2];
|
|
||||||
String name = split[3];
|
|
||||||
Integer age = Integer.parseInt(split[4]);
|
|
||||||
LocalDateTime updateTime = LocalDateTime.parse(split[5], DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
|
|
||||||
return new User(id, username, password, name, age, updateTime);
|
|
||||||
}).toList();
|
|
||||||
//3.返回数据(json)
|
//3.返回数据(json)
|
||||||
return UserList;
|
return UserList;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,8 @@
|
|||||||
|
package com.itheima.springbootweb01.dao;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
public interface UserDao {
|
||||||
|
// 加载用户数据
|
||||||
|
public List<String> findAll();
|
||||||
|
}
|
||||||
@@ -0,0 +1,26 @@
|
|||||||
|
package com.itheima.springbootweb01.dao.impl;
|
||||||
|
|
||||||
|
import cn.hutool.core.io.IoUtil;
|
||||||
|
import com.itheima.springbootweb01.dao.UserDao;
|
||||||
|
|
||||||
|
import java.io.InputStream;
|
||||||
|
import java.nio.charset.StandardCharsets;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
public class UserDaoImpl implements UserDao {
|
||||||
|
/**
|
||||||
|
* 数据层
|
||||||
|
* @return 数据访问层数据
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public List<String> findAll() {
|
||||||
|
//1.加载并读取user.txt文件,获取用户数据
|
||||||
|
// 绝对路径
|
||||||
|
//InputStream in = new FileInputStream(new File("C:\\Users\\syx\\Desktop\\JavaWeb\\04. 后端Web基础(基础知识)\\springboot-web-01\\src\\main\\resources\\user.txt"));
|
||||||
|
//相对路径
|
||||||
|
InputStream in = this.getClass().getClassLoader().getResourceAsStream("user.txt");
|
||||||
|
ArrayList<String> lines = IoUtil.readLines(in, StandardCharsets.UTF_8, new ArrayList<>());
|
||||||
|
return lines;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,9 @@
|
|||||||
|
package com.itheima.springbootweb01.service;
|
||||||
|
|
||||||
|
import com.itheima.springbootweb01.pojo.User;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
public interface UserService {
|
||||||
|
public List<User> findAll();
|
||||||
|
}
|
||||||
@@ -0,0 +1,35 @@
|
|||||||
|
package com.itheima.springbootweb01.service.lmpl;
|
||||||
|
|
||||||
|
import com.itheima.springbootweb01.dao.UserDao;
|
||||||
|
import com.itheima.springbootweb01.dao.impl.UserDaoImpl;
|
||||||
|
import com.itheima.springbootweb01.pojo.User;
|
||||||
|
import com.itheima.springbootweb01.service.UserService;
|
||||||
|
|
||||||
|
import java.time.LocalDateTime;
|
||||||
|
import java.time.format.DateTimeFormatter;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
public class UserServiceImpl implements UserService {
|
||||||
|
/**
|
||||||
|
* 业务逻辑层
|
||||||
|
*/
|
||||||
|
private UserDao userDao = new UserDaoImpl();
|
||||||
|
@Override
|
||||||
|
public List<User> findAll() {
|
||||||
|
//1.加载Dao层数据
|
||||||
|
List<String> lines = userDao.findAll();
|
||||||
|
|
||||||
|
//2.将用户数据封装成User对象 ->list集合
|
||||||
|
List<User> userList= lines.stream().map(line -> {
|
||||||
|
String[] split = line.split(",");
|
||||||
|
Integer id = Integer.parseInt(split[0]);
|
||||||
|
String username = split[1];
|
||||||
|
String password = split[2];
|
||||||
|
String name = split[3];
|
||||||
|
Integer age = Integer.parseInt(split[4]);
|
||||||
|
LocalDateTime updateTime = LocalDateTime.parse(split[5], DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
|
||||||
|
return new User(id, username, password, name, age, updateTime);
|
||||||
|
}).toList();
|
||||||
|
return userList;
|
||||||
|
}
|
||||||
|
}
|
||||||
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Reference in New Issue
Block a user