springboot-cms(1):初始化項目鏈接mysql數據庫

一、建立springboot項目在pom.xml中添加基本依賴

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

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
    </properties>

   
    <dependencies>
        <!--使用jpa來作 增刪查改 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>

        <!--web 支持-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <!--mysql 驅動包-->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <scope>runtime</scope>
        </dependency>

        <!--springboot 測試-->
        <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>
            <!--指定項目jdk編譯版本和編碼方式-->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.1</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                    <encoding>UTF-8</encoding>
                </configuration>
            </plugin>
        </plugins>
    </build>

    <!--這裏配置本項目的maven倉庫地址爲國內的阿里雲 -->
    <repositories>
        <repository>
            <id>central</id>
            <name>Central Repository</name>
            <url>http://maven.aliyun.com/nexus/content/repositories/central</url>
            <layout>default</layout>
            <snapshots>
                <enabled>false</enabled>
            </snapshots>
        </repository>
    </repositories>

二、配置mysql數據庫鏈接

在application.properties文件加入鏈接mysql的配置java

spring.datasource.url=jdbc:mysql://localhost:3306/springboot_cms
spring.datasource.username=username
spring.datasource.password=password
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.jpa.hibernate.ddl-auto=update

三、建立實體類Article.java

@Entity
@Table(name = "tb_article")
public class Article {

    @Id
    private String id;

    @Column
    private String title;

    @Column
    private String summary;

    //getter setter ....
}

四、 啓動程序SpringBootCmsApplication.java

@SpringBootApplication
public class SpringBootCmsApplication {

	public static void main(String[] args) {
		SpringApplication.run(SpringBootCmsApplication.class, args);
	}
}

springboot內置了Tomcat,而且默認端口是8080,因此能夠直接運行XXXApplication.java文件就能夠啓動程序。mysql

啓動成功後查看mysql數據的表是已經成功生成了git

在瀏覽器中輸入:http://localhost:8080web

顯示此頁面表示程序已經成功啓動了。spring

項目源碼:https://git.oschina.net/wangxianhong/springbootcms.gitsql

相關文章
相關標籤/搜索