gpt4 book ai didi

java - 这是 MVC DAO 的正确方法吗?我收到类似 Autowiring 依赖项注入(inject)失败的错误

转载 作者:行者123 更新时间:2023-11-30 02:41:54 27 4
gpt4 key购买 nike

我是 spring 的新手,我面临以下错误。请帮帮我。提前致谢。

用户界面

package com.system.dao;

import java.util.List;

import com.system.model.User;

public interface UserDAO {

public String createOrUpdate(User user);
public List<User> getAllUser();
public String delete(User user);
}

用户界面实现

public class UserDAOImpl implements UserDAO {

private NamedParameterJdbcTemplate jdbc;

@Autowired
public void setJdbc(DataSource jdbc) {
this.jdbc = new NamedParameterJdbcTemplate(jdbc);
}

@Transactional
@Override
public String createOrUpdate(User user) {
BeanPropertySqlParameterSource params = new BeanPropertySqlParameterSource(user);
if (user.isUpdate()) {
jdbc.update(
"update login set login_id=:loginId,login_password:loginPassword,user_name=:userName,user_contact=:userContact,user_email=:userEmail where user_id=:userId",
params);
return "User Updated";
} else {
jdbc.update(
"insert into login(login_id,login_password,user_name,user_contact,user_email) values(:loginId,:loginPassword,:userName,:userContact,:userEmail)",
params);
return "User added";
}

}

@Override
public List<User> getAllUser() {
jdbc.query("select * from login", new RowMapper<User>() {
@Override
public User mapRow(ResultSet rs, int num) throws SQLException {
// TODO Auto-generated method stub
User user = new User();
user.setLoginId(rs.getString("login_id"));
user.setLoginPassword(rs.getString("login_password"));
user.setUserContact(rs.getString("user_contact"));
user.setUserEmail(rs.getString("user_email"));
user.setUserId(rs.getInt("user_id"));
user.setUserName(rs.getString("user_name"));

return user;
}
});
return null;
}

@Override
public String delete(User user) {
jdbc.update("delete from login where user_id:userId", new MapSqlParameterSource("userId", user.getUserId()));
return "User deleted";
}

}

用户服务

package com.system.service;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.system.dao.impl.UserDAOImpl;
import com.system.model.User;

@Service
public class UserService {

private UserDAOImpl userDAOImpl;

@Autowired(required=true)
public void setUserDAOImpl(UserDAOImpl userDAOImpl) {
this.userDAOImpl = userDAOImpl;
}
public String createOrUpdate(User user) {
return userDAOImpl.createOrUpdate(user);
}
public List<User> getAllUser() {
return userDAOImpl.getAllUser();
}

public String delete(User user) {
return userDAOImpl.delete(user);
}




}

上下文(dao-context)

<context:annotation-config></context:annotation-config>
<context:component-scan base-package="com.system.dao">
</context:component-scan>

<context:component-scan base-package="com.system.dao.impl">
</context:component-scan>
<context:component-scan base-package="com.system.service">
</context:component-scan>

最佳答案

您在 UserDAOImpl 中缺少 @Repository

@Repository("userDAOImpl")
public class UserDAOImpl implements UserDAO {

要将 java 类转换为 spring 组件,您需要使用以下任意内容注释该类

  • @Controller
  • @Component
  • @Repository
  • @Service

由于UserDAOImpl是属于DAO层的dao,因此@Repository是最合适的。

阅读更多内容 http://howtodoinjava.com/spring/spring-core/how-to-use-spring-component-repository-service-and-controller-annotations/

关于java - 这是 MVC DAO 的正确方法吗?我收到类似 Autowiring 依赖项注入(inject)失败的错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41439720/

27 4 0
文章推荐: c++ - 如何构建 cURL 并在 Code::Blocks 项目中使用它(静态)
文章推荐: List 内的 Java 过滤器映射