无法使用mysql连接spring

时间:2015-04-01 12:51:13

标签: java mysql spring

我目前正在处理来自spring文档的spring hello示例。因为他们正在连接嵌入式数据库,而我正在尝试配置mysql数据库。 来自git https://github.com/spring-guides/gs-batch-processing.git

的样本

请指导我一步。

将jdbc-driver添加到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>

    <groupId>org.springframework</groupId>
    <artifactId>gs-batch-processing</artifactId>
    <version>0.1.0</version>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.2.2.RELEASE</version>
    </parent>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-batch</artifactId>
        </dependency>

        <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-jdbc</artifactId>
        <version>4.1.6.RELEASE</version>
        </dependency>



       <dependency>
                <groupId>mysql</groupId>
                <artifactId>mysql-connector-java</artifactId>
                <version>5.1.34</version>
        </dependency>

        <dependency>
        <groupId>javax.batch</groupId>
        <artifactId>javax.batch-api</artifactId>
        <version>1.0.1-b01</version>
        </dependency>
    </dependencies>



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

</project>

这是数据库配置文件

<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">

    <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName" value="com.mysql.jdbc.Driver" />
        <property name="url" value="jdbc:mysql://localhost:3306/spring" />
        <property name="username" value="root" />
        <property name="password" value="" />
    </bean>

</beans>

我在这里遗漏了什么?但仍然连接到嵌入式hsql db。见下文,

  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::        (v1.2.2.RELEASE)

2015-04-01 18:10:41.746  INFO 10234 --- [           main] hello.Application                        : Starting Application v0.1.0 on alaska with PID 10234 (/home/malarvizhi/a/spring/bs2/target/gs-batch-processing-0.1.0.jar started by malarvizhi in /home/malarvizhi/a/spring/bs2)
2015-04-01 18:10:41.822  INFO 10234 --- [           main] s.c.a.AnnotationConfigApplicationContext : Refreshing org.springframework.context.annotation.AnnotationConfigApplicationContext@9c7bcc0: startup date [Wed Apr 01 18:10:41 IST 2015]; root of context hierarchy
2015-04-01 18:10:42.961  WARN 10234 --- [           main] o.s.c.a.ConfigurationClassEnhancer       : @Bean method ScopeConfiguration.stepScope is non-static and returns an object assignable to Spring's BeanFactoryPostProcessor interface. This will result in a failure to process annotations such as @Autowired, @Resource and @PostConstruct within the method's declaring @Configuration class. Add the 'static' modifier to this method to avoid these container lifecycle issues; see @Bean javadoc for complete details
2015-04-01 18:10:42.975  WARN 10234 --- [           main] o.s.c.a.ConfigurationClassEnhancer       : @Bean method ScopeConfiguration.jobScope is non-static and returns an object assignable to Spring's BeanFactoryPostProcessor interface. This will result in a failure to process annotations such as @Autowired, @Resource and @PostConstruct within the method's declaring @Configuration class. Add the 'static' modifier to this method to avoid these container lifecycle issues; see @Bean javadoc for complete details
2015-04-01 18:10:43.253  INFO 10234 --- [           main] o.s.j.d.e.EmbeddedDatabaseFactory        : Creating embedded database 'testdb'
2015-04-01 18:10:43.764  INFO 10234 --- [           main] o.s.jdbc.datasource.init.ScriptUtils     : Executing SQL script from URL [jar:file:/home/malarvizhi/a/spring/bs2/target/gs-batch-processing-0.1.0.jar!/schema-all.sql]
2015-04-01 18:10:43.769  INFO 10234 --- [           main] o.s.jdbc.datasource.init.ScriptUtils     : Executed SQL script from URL [jar:file:/home/malarvizhi/a/spring/bs2/target/gs-batch-processing-0.1.0.jar!/schema-all.sql] in 5 ms.
2015-04-01 18:10:44.269  INFO 10234 --- [           main] o.s.jdbc.datasource.init.ScriptUtils     : Executing SQL script from class path resource [org/springframework/batch/core/schema-hsqldb.sql]
2015-04-01 18:10:44.279  INFO 10234 --- [           main] o.s.jdbc.datasource.init.ScriptUtils     : Executed SQL script from class path resource [org/springframework/batch/core/schema-hsqldb.sql] in 9 ms.
2015-04-01 18:10:44.460  INFO 10234 --- [           main] o.s.j.e.a.AnnotationMBeanExporter        : Registering beans for JMX exposure on startup
2015-04-01 18:10:44.479  INFO 10234 --- [           main] o.s.b.a.b.JobLauncherCommandLineRunner   : Running default command line with: []
2015-04-01 18:10:44.492  INFO 10234 --- [           main] o.s.b.c.r.s.JobRepositoryFactoryBean     : No database type set, using meta data indicating: HSQL
2015-04-01 18:10:44.628  INFO 10234 --- [           main] o.s.b.c.l.support.SimpleJobLauncher      : No TaskExecutor has been set, defaulting to synchronous executor.
2015-04-01 18:10:44.694  INFO 10234 --- [           main] o.s.b.c.l.support.SimpleJobLauncher      : Job: [FlowJob: [name=importUserJob]] launched with the following parameters: [{run.id=1}]
2015-04-01 18:10:44.720  INFO 10234 --- [           main] o.s.batch.core.job.SimpleStepHandler     : Executing step: [step1]
Converting (firstName: 

1 个答案:

答案 0 :(得分:0)

试试这个。我使用以下属性连接到Mysql。我可以看到你使用的那些属性可能适用于postgresql。

<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClass" value="com.mysql.jdbc.Driver" />
        <property name="jdbcUrl" value="jdbc:mysql://localhost:3306/spring" />
        <property name="username" value="root" />
        <property name="password" value="" />
    </bean>

如果不起作用,请告诉我。