diff --git a/04. 后端Web基础(基础知识)/springboot-web-01/src/main/java/com/itheima/springbootweb01/controller/UserController.java b/04. 后端Web基础(基础知识)/springboot-web-01/src/main/java/com/itheima/springbootweb01/controller/UserController.java index 1dfbad7..fb117be 100644 --- a/04. 后端Web基础(基础知识)/springboot-web-01/src/main/java/com/itheima/springbootweb01/controller/UserController.java +++ b/04. 后端Web基础(基础知识)/springboot-web-01/src/main/java/com/itheima/springbootweb01/controller/UserController.java @@ -2,6 +2,8 @@ package com.itheima.springbootweb01.controller; import cn.hutool.core.io.IoUtil; 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.RestController; @@ -22,28 +24,39 @@ import static java.lang.System.in; //用户信息控制器 @RestController //作用:将controller返回值直接作为响应数据返回给浏览器@ResponseBody public class UserController { - @RequestMapping("/hello"); + /** + * 接受请求 相应数据 + */ + private UserService userService = new UserServiceImpl(); + // @RequestMapping("/list") +// public List 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 lines = IoUtil.readLines(in, StandardCharsets.UTF_8, new ArrayList<>()); +// //2.将用户数据封装成User对象 ->list集合 +// List 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") public List 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 lines = IoUtil.readLines(in, StandardCharsets.UTF_8, new ArrayList<>()); - //2.将用户数据封装成User对象 ->list集合 - List 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(); + //1.调用service层数据 + List UserList= userService.findAll(); + //3.返回数据(json) return UserList; } diff --git a/04. 后端Web基础(基础知识)/springboot-web-01/src/main/java/com/itheima/springbootweb01/dao/UserDao.java b/04. 后端Web基础(基础知识)/springboot-web-01/src/main/java/com/itheima/springbootweb01/dao/UserDao.java new file mode 100644 index 0000000..7b703ba --- /dev/null +++ b/04. 后端Web基础(基础知识)/springboot-web-01/src/main/java/com/itheima/springbootweb01/dao/UserDao.java @@ -0,0 +1,8 @@ +package com.itheima.springbootweb01.dao; + +import java.util.List; + +public interface UserDao { +// 加载用户数据 + public List findAll(); +} diff --git a/04. 后端Web基础(基础知识)/springboot-web-01/src/main/java/com/itheima/springbootweb01/dao/impl/UserDaoImpl.java b/04. 后端Web基础(基础知识)/springboot-web-01/src/main/java/com/itheima/springbootweb01/dao/impl/UserDaoImpl.java new file mode 100644 index 0000000..3cc0992 --- /dev/null +++ b/04. 后端Web基础(基础知识)/springboot-web-01/src/main/java/com/itheima/springbootweb01/dao/impl/UserDaoImpl.java @@ -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 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 lines = IoUtil.readLines(in, StandardCharsets.UTF_8, new ArrayList<>()); + return lines; + } +} diff --git a/04. 后端Web基础(基础知识)/springboot-web-01/src/main/java/com/itheima/springbootweb01/service/UserService.java b/04. 后端Web基础(基础知识)/springboot-web-01/src/main/java/com/itheima/springbootweb01/service/UserService.java new file mode 100644 index 0000000..5e0e867 --- /dev/null +++ b/04. 后端Web基础(基础知识)/springboot-web-01/src/main/java/com/itheima/springbootweb01/service/UserService.java @@ -0,0 +1,9 @@ +package com.itheima.springbootweb01.service; + +import com.itheima.springbootweb01.pojo.User; + +import java.util.List; + +public interface UserService { + public List findAll(); +} diff --git a/04. 后端Web基础(基础知识)/springboot-web-01/src/main/java/com/itheima/springbootweb01/service/lmpl/UserServiceImpl.java b/04. 后端Web基础(基础知识)/springboot-web-01/src/main/java/com/itheima/springbootweb01/service/lmpl/UserServiceImpl.java new file mode 100644 index 0000000..f20fab7 --- /dev/null +++ b/04. 后端Web基础(基础知识)/springboot-web-01/src/main/java/com/itheima/springbootweb01/service/lmpl/UserServiceImpl.java @@ -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 findAll() { + //1.加载Dao层数据 + List lines = userDao.findAll(); + + //2.将用户数据封装成User对象 ->list集合 + List 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; + } +} diff --git a/04. 后端Web基础(基础知识)/springboot-web-01/target/classes/com/itheima/springbootweb01/controller/UserController.class b/04. 后端Web基础(基础知识)/springboot-web-01/target/classes/com/itheima/springbootweb01/controller/UserController.class index 68d5dc0..ce4c529 100644 Binary files a/04. 后端Web基础(基础知识)/springboot-web-01/target/classes/com/itheima/springbootweb01/controller/UserController.class and b/04. 后端Web基础(基础知识)/springboot-web-01/target/classes/com/itheima/springbootweb01/controller/UserController.class differ diff --git a/04. 后端Web基础(基础知识)/springboot-web-01/target/classes/com/itheima/springbootweb01/dao/UserDao.class b/04. 后端Web基础(基础知识)/springboot-web-01/target/classes/com/itheima/springbootweb01/dao/UserDao.class new file mode 100644 index 0000000..10e502e Binary files /dev/null and b/04. 后端Web基础(基础知识)/springboot-web-01/target/classes/com/itheima/springbootweb01/dao/UserDao.class differ diff --git a/04. 后端Web基础(基础知识)/springboot-web-01/target/classes/com/itheima/springbootweb01/dao/impl/UserDaoImpl.class b/04. 后端Web基础(基础知识)/springboot-web-01/target/classes/com/itheima/springbootweb01/dao/impl/UserDaoImpl.class new file mode 100644 index 0000000..4fc920f Binary files /dev/null and b/04. 后端Web基础(基础知识)/springboot-web-01/target/classes/com/itheima/springbootweb01/dao/impl/UserDaoImpl.class differ diff --git a/04. 后端Web基础(基础知识)/springboot-web-01/target/classes/com/itheima/springbootweb01/service/UserService.class b/04. 后端Web基础(基础知识)/springboot-web-01/target/classes/com/itheima/springbootweb01/service/UserService.class new file mode 100644 index 0000000..abc0425 Binary files /dev/null and b/04. 后端Web基础(基础知识)/springboot-web-01/target/classes/com/itheima/springbootweb01/service/UserService.class differ diff --git a/04. 后端Web基础(基础知识)/springboot-web-01/target/classes/com/itheima/springbootweb01/service/lmpl/UserServiceImpl.class b/04. 后端Web基础(基础知识)/springboot-web-01/target/classes/com/itheima/springbootweb01/service/lmpl/UserServiceImpl.class new file mode 100644 index 0000000..b071f38 Binary files /dev/null and b/04. 后端Web基础(基础知识)/springboot-web-01/target/classes/com/itheima/springbootweb01/service/lmpl/UserServiceImpl.class differ