거북
50
2019-06-11 17:25:20
5
275

스프링부트 maven mybatis mysql 연동 중 어노테이션 에러입니다.



스프링 부트를 사용 하는데 미치겠네요..



일단 maven버전으로 데모프로젝트를 만들었는데



Controller.




package com.rjqnrl.test.controller;


import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.rjqnrl.test.service.TestService;

@Controller
public class TestController {

	private Logger logger = LoggerFactory.getLogger(TestController.class);
	
	@Autowired
	private TestService testService;
	
	@RequestMapping(value="/test",method = {RequestMethod.GET, RequestMethod.POST})
	public String test(Model model) {
		
		try {
			
			logger.info("aaa");
			int cnt = testService.getTestData();
			
			model.addAttribute("cnt",cnt);
			
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		return "index";
	}
}


service


package com.rjqnrl.test.service;



public interface TestService {

	public int getTestData() throws Exception;
	
	
}


serviceimpl


package com.rjqnrl.test.service;

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

import com.rjqnrl.test.mapper.TestMapper;

@Service
public class TestServiceImpl implements TestService{

	@Autowired
	private TestMapper testMapper;

	@Override
	public int getTestData() throws Exception {
		System.out.println("aa");
		return testMapper.getTestData();
	}
}



mapper




package com.rjqnrl.test.mapper;

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

@Mapper
public interface TestMapper {

	public int getTestData() throws Exception;
}



vo



package com.rjqnrl.test.model;


public class TestVO {

	private int cnt;

	public int getCnt() {
		return cnt;
	}

	public void setCnt(int cnt) {
		this.cnt = cnt;
	}
	
	
}


aplicatoin.properties



#DB
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://127.0.0.1:3306/rjqnrl?verifyServerCertificate=false&useSSL=false&characterEncoding=UTF-8&serverTimezone=UTC
spring.datasource.username=root
spring.datasource.password=1234


#view
spring.mvc.view.prefix=/WEB-INF/views/
spring.mvc.view.suffix=.jsp

server.port=8091



mapper.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="test">

	<select id="getTestData" resultType="int">
		SELECT COUNT(*) as cnt
		FROM BOARD
	</select>
	
</mapper>



pom.xml



<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>2.1.5.RELEASE</version>
		<relativePath/> <!-- lookup parent from repository -->
	</parent>
	<groupId>com.test</groupId>
	<artifactId>demo-5</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<name>demo-5</name>
	<description>Demo project for Spring Boot</description>

	<properties>
		<java.version>1.8</java.version>
	</properties>

	<dependencies>
		<dependency>
			<groupId>javax.servlet</groupId>
			<artifactId>javax.servlet-api</artifactId>
			<scope>provided</scope>
		</dependency>
		
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>

		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-test</artifactId>
			<scope>test</scope>
		</dependency>
		
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-tomcat</artifactId>
			<scope>provided</scope>
		</dependency>

		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-data-jpa</artifactId>
		</dependency>
		
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-jdbc</artifactId>
		</dependency>
		
		<dependency>
			<groupId>org.mybatis.spring.boot</groupId>
			<artifactId>mybatis-spring-boot-starter</artifactId>
			<version>2.0.1</version>
		</dependency>

		<dependency>
			<groupId>mysql</groupId>
			<artifactId>mysql-connector-java</artifactId>
			<scope>runtime</scope>
		</dependency>
		
		<!-- JSP -->
		<dependency>
			<groupId>org.apache.tomcat.embed</groupId>
			<artifactId>tomcat-embed-jasper</artifactId>
		</dependency>

		<dependency>
			<groupId>javax.servlet</groupId>
			<artifactId>jstl</artifactId>
		</dependency>
		
	</dependencies>

	<build>
		<plugins>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
			</plugin>
		</plugins>
	</build>

</project>



로햇는데 어노테이션 관련 에러가 뜹니다.

 사실  아예 서비스단으로 못넘어가고있어서 어떻게 해야될지모르겠습니다.


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

APPLICATION FAILED TO START

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


Description:


Field testService in com.rjqnrl.test.controller.TestController required a bean of type 'com.rjqnrl.test.service.TestService' 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.rjqnrl.test.service.TestService' in your configuration.





그리고  이건또 다른건데   스프링 부트 프로젝트를 만든뒤에 바로 pom.xml에서 .


바로 이렇게 에러가 납니다..



ㅠㅠㅠ 고수분들 도와주세요.. ㅜㅜㅜ 머리가터질것같아요.


0
0
  • 답변 5

  • tou
    351
    2019-06-11 17:30:22

    TestService를 읽어오는데 문제가 있는거같네요.

    root-context.xml이나 다른파일에 component-scan통해  com.rjqnrl.test.service 되어있는지 확인해보시겠어요?


    1
  • 거북
    50
    2019-06-11 17:34:18

    component-scan 문제를 생각해봤는데 

    이게 spring boot starter  project로 만든 프로젝트인데  component-scan을 어떻게 줘야될지 잘모르겠습니다.

    제가알기로  spring boot는 xml 파일대신 properties로 대신? 하는 느낌으로 알고있습니다 ㅜㅜ


    0
  • tou
    351
    2019-06-11 17:36:28

    그렇다면 어노테이션 사용하시면 될거같습니다~ 

    @ComponentScan({"패키지명"}) 같은형식으로 사용하시면 됩니다.

    0
  • 고냥e
    30
    2019-06-11 17:37:20 작성 2019-06-11 17:38:37 수정됨
    porm.xml 에러
    <version>2.1.5.RELEASE</version>
    <version>2.1.4.RELEASE</version>로 바꾸시고 메이븐 업데이트 하시면 될꺼에요 
    1
  • 거북
    50
    2019-06-11 18:27:16
    ㅠㅠ ㅠ감사합니다 ㅜㅜ 두분 헝헝 내 이일 ㅜㅜㅜ
    0
  • 로그인을 하시면 답변을 등록할 수 있습니다.