0
votes

Can't wire layers in Spring Boot | MyBatis application. The problem is probably happening when Service layer uses Mapper.

Controller method sample:

@Controller
@RequestMapping("demo")
public class MessageController {

    @Autowired
    private MessageService messageService;

    @RequestMapping(value = "messages", method = RequestMethod.GET)
    public String getMessages(ModelMap modelMap) {
        modelMap.addAttribute(MESSAGE,  
                      messageService.selectMessages());
        return "messages";
}

Service class:

@Service
public class MessageService {

    @Autowired   // Not sure if I can use Autowired here.
    private MessageMapper messageMapper;

    public MessageService() {
    }

    public Collection<Message> selectMessages() { return 
         messageMapper.selectAll(); }

}

MyBatis Mapper:

@Mapper
public interface MessageMapper {
    @Select("select * from message")
    Collection<Message> selectAll();
} 

UPDATE

It feels like I'm having some fundamental knowledge based mistake. Probably managing external libraries.

Here's maven pom.xml. Looks kind of overloaded, I faced a lot of errors managing different spring-boot packages. Starter for autoconfiguration included. pom.xml

Here's the project structure:

enter image description here

UPDATE #2

I'm sure DB connection is working well, I'm able to track changes in MySQL Workbench while Spring Boot is executing schema.sql and data.sql. But somehow, MyBatis mapper methods throw NullPointerException and page proceeds with exit code 500. Seems like they can't connect.

1
Your are doing new MessageService(); this creates an instance outside the control of Spring and no auto wiring will happen. You need to inject the MessageService into the controller as well. - M. Deinum
@M. Deinum, I was thinking about that. However, when I try to inject it as @Autowired private MessageService messageService it gets underlined by IDEA and doesn't compile. - Zhannur Diyas
You also need to annotate your MessageService with @Service else it will not be detected. Also I doubt that it won't compile I suspect your IDE is telling you that MessageService cannot be auto wired. - M. Deinum
Why doesn't it compile ? Post the error.... - PaulNUK
@M.Deinum, @PaulNUK, I updated how the code looks on this moment. It compiles, but still not working. You were right, IDEA argues the following: Could not autowire: No beans of "MessageMapper" found. - Zhannur Diyas

1 Answers

1
votes

MessageService isn't managed by spring.

You have to annotate the MessageService class with @Service annotation (also, after adding this annotation you can indeed use @Autowired inside the service class)

@Service
public class MessageService {
  @Autowired  
  private MessageMapper messageMapper;

  public Collection<Message> selectMessages() { 
    return messageMapper.selectAll(); 
  }
}

and wire it to the controller with

@Autowired 
private MessageService messageService

and use it in a method like this

@RequestMapping(value = "messages", method = RequestMethod.GET)
public String getMessages(ModelMap modelMap) {
    modelMap.addAttribute(MESSAGE,  messageService.selectMessages());
    return "messages";
}