新的一天
This commit is contained in:
@@ -3,7 +3,7 @@ package com.itheima.springbootweb01;
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
|
||||
@SpringBootApplication
|
||||
@SpringBootApplication //
|
||||
public class SpringbootWeb01Application {
|
||||
|
||||
public static void main(String[] args) {
|
||||
|
||||
@@ -4,6 +4,9 @@ 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 jakarta.annotation.Resource;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.beans.factory.annotation.Qualifier;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
@@ -28,7 +31,25 @@ public class UserController {
|
||||
* 接受请求 相应数据
|
||||
*/
|
||||
|
||||
private UserService userService = new UserServiceImpl();
|
||||
// @Qualifier("userServiceImpl")
|
||||
// @Autowired
|
||||
// private UserService userService;
|
||||
|
||||
@Resource(name = "userServiceImpl2")
|
||||
private UserService userService;
|
||||
//方式二:构造器注入
|
||||
// private final UserService userService;
|
||||
// @Autowired
|
||||
// public UserController(UserService userService) {
|
||||
// this.userService = userService;
|
||||
// }
|
||||
|
||||
//方式三:setter注入
|
||||
// private UserService userService;
|
||||
// @Autowired
|
||||
// public void setUserService(UserService userService) {
|
||||
// this.userService = userService;
|
||||
// }
|
||||
|
||||
// @RequestMapping("/list")
|
||||
// public List<User> list() throws Exception{
|
||||
|
||||
@@ -2,12 +2,18 @@ package com.itheima.springbootweb01.dao.impl;
|
||||
|
||||
import cn.hutool.core.io.IoUtil;
|
||||
import com.itheima.springbootweb01.dao.UserDao;
|
||||
import org.springframework.stereotype.Component;
|
||||
import org.springframework.stereotype.Repository;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.io.InputStream;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
//@Component // 将当前组件加入到Spring容器中
|
||||
//@Repository //将当前组件加入到Spring容器中
|
||||
@Service //将当前组件加入到Spring容器中
|
||||
public class UserDaoImpl implements UserDao {
|
||||
/**
|
||||
* 数据层
|
||||
|
||||
@@ -4,16 +4,23 @@ 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 org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.autoconfigure.AutoConfigureOrder;
|
||||
import org.springframework.stereotype.Component;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
import java.time.format.DateTimeFormatter;
|
||||
import java.util.List;
|
||||
@Service
|
||||
|
||||
public class UserServiceImpl implements UserService {
|
||||
/**
|
||||
* 业务逻辑层
|
||||
*/
|
||||
private UserDao userDao = new UserDaoImpl();
|
||||
|
||||
@Autowired // 自动注入
|
||||
private UserDao userDao;
|
||||
@Override
|
||||
public List<User> findAll() {
|
||||
//1.加载Dao层数据
|
||||
|
||||
@@ -0,0 +1,43 @@
|
||||
package com.itheima.springbootweb01.service.lmpl;
|
||||
|
||||
import com.itheima.springbootweb01.dao.UserDao;
|
||||
import com.itheima.springbootweb01.pojo.User;
|
||||
import com.itheima.springbootweb01.service.UserService;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.context.annotation.Primary;
|
||||
import org.springframework.stereotype.Component;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
import java.time.format.DateTimeFormatter;
|
||||
import java.util.List;
|
||||
|
||||
@Service
|
||||
//@Primary
|
||||
|
||||
public class UserServiceImpl2 implements UserService {
|
||||
/**
|
||||
* 业务逻辑层
|
||||
*/
|
||||
|
||||
@Autowired // 自动注入
|
||||
private UserDao userDao;
|
||||
@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+200, username, password, name, age, updateTime);
|
||||
}).toList();
|
||||
return userList;
|
||||
}
|
||||
}
|
||||
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Reference in New Issue
Block a user