평촌웹개발
95
2020-02-15 14:46:56
2
84

spring boot mariadb 연동 오류


***************************

APPLICATION FAILED TO START

***************************

Description:

Field mapper in com.codingwrite.service.dashboardService required a bean of type 'com.codingwrite.mapper.dashboardMapper' that could not be found.

The injection point has the following annotations:

- @org.springframework.beans.factory.annotation.Autowired(required=true)

Action:

Consider defining a bean of type 'com.codingwrite.mapper.dashboardMapper' in your configuration.


일단 이러한 오류가 나옵니다. 찾아봤더니 bean이 설정이 안되어있어서 그렇다고 하더라구요. 설정했는데도 안되서 질문남깁니다.




구조는 이렇게 했습니다.


DB.xml


<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.codingwrite.mapper.dashboardMapper">
	<select id="selectPosts" resultType="com.codingwrite.domain.dashboardVO"> 
		SELECT * from posts
	</select>
</mapper>


VO.java


package com.codingwrite.domain;

import java.sql.Timestamp;

import lombok.Data;

@Data
public class dashboardVO {
	private int id;
	private String title;
	private String contents;
	private Timestamp date;
	private Timestamp updateDate;
	private int views;
	private int share;
	private int likes;
	private int useful;
	private int subsequent;
	private int understand;
	private int averageTime;
	private int category;
	private String tag;
}


mapper


package com.codingwrite.mapper;

import java.util.List;

import org.apache.ibatis.annotations.Mapper;
import org.springframework.stereotype.Repository;

import com.codingwrite.domain.dashboardVO;

@Repository
@Mapper
public interface dashboardMapper {
	public List<dashboardVO> selectPosts();
}


service


package com.codingwrite.service;

import java.util.List;

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

import com.codingwrite.domain.dashboardVO;
import com.codingwrite.mapper.dashboardMapper;

@Service
public class dashboardService {
	
	@Autowired
	public dashboardMapper mapper;
	
	public List<dashboardVO> selectPosts(){
		return mapper.selectPosts();
	}
}


controller


package com.codingwrite.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.codingwrite.domain.dashboardVO;
import com.codingwrite.service.dashboardService;

@Controller
public class AdminController {
	
	//test
	@Autowired
	dashboardService mainService;

	@RequestMapping("/test")
	public ModelAndView test(ModelAndView mav) throws Exception{
		
		List<dashboardVO> selectPosts = mainService.selectPosts();
		mav.addObject("list", selectPosts);
		return mav;
	}
}


databaseConfig


package com.codingwrite;

import javax.sql.DataSource;

import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.SqlSessionTemplate;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.transaction.annotation.EnableTransactionManagement;

@Configuration
@MapperScan(basePackages="com.codingwrite.config")
@EnableTransactionManagement
public class databaseConfig {

    @Bean
    public  SqlSessionFactory sqlSessionFactory(DataSource dataSource)throws Exception {
        final SqlSessionFactoryBean sessionFactory =new SqlSessionFactoryBean();
        sessionFactory.setDataSource(dataSource);
        PathMatchingResourcePatternResolver resolver =new PathMatchingResourcePatternResolver();
        sessionFactory.setMapperLocations(resolver.getResources("classpath:com/codingwrite/config/*.xml"));
        return sessionFactory.getObject();
    }
    
    @Bean
    public SqlSessionTemplate sqlSessionTemplate(SqlSessionFactory sqlSessionFactory)throws Exception {
      final SqlSessionTemplate sqlSessionTemplate =new SqlSessionTemplate(sqlSessionFactory);
      return sqlSessionTemplate;
    }
}


왜 오류가 나는건지 모르겠어요 도와주세요ㅠ

0
0
  • 답변 2

  • 즈루시
    13k
    2020-02-15 15:06:52
    mapperscan 어노테이션요 
    패키지를 한단계 위에서 스캔하게 하셔야 할것 같아요
    0
  • 우루부루구루
    824
    2020-02-15 15:07:41

    궁금한 게 mapper interface에 

    왜 @Mapper 애노테이션하고 @Repository 애노테이션을 붙이셨나요?

    0
  • 로그인을 하시면 답변을 등록할 수 있습니다.