简体   繁体   中英

Spring Boot/Web with MySql database

I have read various SO question, spring tutorials and looked at different example projects, but i still do not get it. Among others were these: unable to connect spring with mysql http://www.codejava.net/frameworks/spring/spring-mvc-with-jdbctemplate-example

My goal is to get a GRADLE spring boot/web application that connects to my MySql database .

My problem is there are many different ways to configure your project.

I would like to use as few XML as possible.

So my question is, whether it is possible to achieve my goal without having a single XML file and if so - how?

Or if i need an XML file(it should then be the pom.xml I think). I would like an explanation why I need it and what the file does in general.

Please NOTE: I have about zero knowledge how to configure spring, mysql, hibernate. So also things that may be straightforward for you, could be unclear to me. So I would really appreciate explanantions.

EDIT2 :

I got an sample application running using Spring Initializer

BUT, this sample application uses Maven - to be exactly it uses this 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.test</groupId>
    <artifactId>demo</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>demo</name>
    <description>Demo project for Spring Boot</description>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.2.3.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <start-class>demo.DemoApplication</start-class>
        <java.version>1.7</java.version>
    </properties>

    <dependencies>
        <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.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

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

</project>

Is there an eaasy way I can replace this code with gradle or some kind of tutorial that tells me how to replace this code with the correct gradle file?

The simplest way may be for you to download a "bootstrapped" project using the Spring Initializr . You can select the dependencies that you require and the build system, and it will generate your project for you.

For Spring Boot to connect to your MySQL instance, it should be sufficient to just have the following properties in your application.properties file:

spring.datasource.url=jdbc:mysql://localhost:3306/db
spring.datasource.username=user
spring.datasource.password=password

See the Spring Boot Reference for Working with SQL databases for more information.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM