2016-09-16 3 views
0

메모리 내장 데이터베이스를 사용하여 독립 실행 형 Java 응용 프로그램을 개발 중입니다. 나는 거의 문서를 심문하지 않았고 다음 코드를 작성했습니다. 나는 봄 부팅을 사용하고 있습니다. 이것들은 내가 지금까지 한 단계들입니다 : 있음, pom 파일들 이러한 의존성을 추가했습니다.봄에 메모리 데이터베이스에 액세스하는 방법

<dependency> 
      <groupId>org.springframework.boot</groupId> 
      <artifactId>spring-boot-starter-jdbc</artifactId> 
     </dependency> 
     <dependency> 
      <groupId>com.h2database</groupId> 
      <artifactId>h2</artifactId> 
     </dependency> 
     <dependency> 
      <groupId>mysql</groupId> 
      <artifactId>mysql-connector-java</artifactId> 
     <version>5.1.36</version> 
    </dependency> 
application.properties이

spring.datasource.driver-class-name=com.mysql.jdbc.Driver 
spring.datasource.url=jdbc:mysql://localhost:3306/test 
spring.datasource.username=root 
spring.datasource.password=admin 

내가이 스크립트는 schema.sql 생성 된 파일에서

및 data.sql schema.sql

CREATE TABLE users 
(
    id int(11) NOT NULL AUTO_INCREMENT, 
    name varchar(100) NOT NULL, 
    email varchar(100) DEFAULT NULL, 
    PRIMARY KEY (id) 
); 

data.sql

insert into users(id, name, email) values(1,'demouser','[email protected]'); 

여기에 내 r입니다. 저장소 클래스. 내 JUnit을 나는 다음과 같은 오류를 얻고을 실행할 때

@Repository 
public class UserRepository { 

    @Autowired 
    private JdbcTemplate jdbcTemplate; 

    @Transactional(readOnly = true) 
    public List<User> findAll() { 
     return jdbcTemplate.query("select * from users", new UserRowMapper()); 
    } 

    class UserRowMapper implements RowMapper<User> { 
     @Override 
     public User mapRow(ResultSet rs, int rowNum) throws SQLException { 
      User user = new User(); 
      user.setId(rs.getInt("id")); 
      user.setName(rs.getString("name")); 
      user.setEmail(rs.getString("email")); 
      return user; 
     } 

여기 내 JUnit을 클래스

@Autowired 
    private UserRepository userRepository; 
    private EmbeddedDatabase db; 

    @Before 
    public void setUp() { 
     // db = new EmbeddedDatabaseBuilder().addDefaultScripts().build(); 
     Object db = new EmbeddedDatabaseBuilder().setType(EmbeddedDatabaseType.H2).addScript("schema.sql") 
       .addScript("data.sql").build(); 
    } 

    @Test 
    public void findAllUsers() { 
     List<User> users = userRepository.findAll(); 
     System.out.println(users.get(0).getName()); 
     assertNotNull(users); 
     assertTrue(!users.isEmpty()); 
    } 

입니다.

org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'userRepository': Unsatisfied dependency expressed through field 'jdbcTemplate': Error creating bean with name 'org.springframework.boot.autoconfigure.jdbc.JdbcTemplateAutoConfiguration': Unsatisfied dependency expressed through constructor parameter 0: Error creating bean with name 'dataSource' defined in class path resource [org/springframework/boot/autoconfigure/jdbc/DataSourceConfiguration$Tomcat.class]: Initialization of bean failed; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'dataSourceInitializer': Invocation of init method failed; nested exception is org.springframework.jdbc.datasource.init.UncategorizedScriptException: Failed to execute database script; nested exception is org.springframework.jdbc.CannotGetJdbcConnectionException: Could not get JDBC Connection; nested exception is com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure 

다른 종속성을 추가하거나 데이터베이스를 다운로드해야합니까?

답변

1

당신은 H2 인 인 메모리 데이터베이스를 사용하기 때문에 pom.xml과 mysql 관련 속성이 mysql에 종속되지 않아야합니다 (application.properties).

pom.xml

<!--Delete This--> 
    <dependency> 
     <groupId>mysql</groupId> 
     <artifactId>mysql-connector-java</artifactId> 
    <version>5.1.36</version> 

에서 MySQL의 커넥터 의존성을 제거하고 application.properties 파일의 모든 속성을 삭제합니다. 비워두면 스프링 부트가 H2 데이터베이스의 속성을 자동으로 구성합니다.

GitHub 저장소의 전체 Spring Boot JDBC Demo 프로젝트를 확인하십시오.

+0

@madhu 귀하의 질문에 대한 답변이 있으면 받아주십시오. –

관련 문제