【java】Springboot工程从零创建及添加数据库支持

一、建立springboot工程

1、建立空工程

 

填好,Finish后就建立了一个maven空的工程。

2、添加SpringBoot必要依赖

SpringBoot要求,项目要继承SpringBoot的起步依赖spring-boot-starter-parent

<parent>

<groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-starter-parent</artifactId>

<version>2.0.1.RELEASE</version>

</parent>

 SpringBoot要集成SpringMVC进行Controller的开发,所以项目要导入web的启动依赖

<dependencies>

    <dependency>

        <groupId>org.springframework.boot</groupId>

        <artifactId>spring-boot-starter-web</artifactId>

    </dependency>

</dependencies>

注意:1、pom文件第一行报错可以不用管,添加的内容要在project标签内。

  1. spring-boot-starter-parent 如果总是下载失败,可以在pom.xml添加:

 <repositories>

        <repository>

            <id>spring-snapshots</id>

            <url>http://repo.spring.io/libs-snapshot</url>

        </repository>

    </repositories>

    <pluginRepositories>

        <pluginRepository>

            <id>spring-snapshots</id>

            <url>http://repo.spring.io/libs-snapshot</url>

        </pluginRepository>

    </pluginRepositories>

3、maven编译

在goals中加入clean和install, 要不直接编译会报错。添加之后编译OK

[INFO] Building jar: D:\workspace\springbootDbDemo\target\springbootDbDemo-0.0.1-SNAPSHOT.jar

[INFO]

[INFO] --- maven-install-plugin:2.4:install (default-install) @ springbootDbDemo ---

[INFO] Installing D:\workspace\springbootDbDemo\target\springbootDbDemo-0.0.1-SNAPSHOT.jar to D:\tools\apache-maven-3.3.9\repository\cn\springbootdemo\springbootDbDemo\0.0.1-SNAPSHOT\springbootDbDemo-0.0.1-SNAPSHOT.jar

[INFO] Installing D:\workspace\springbootDbDemo\pom.xml to D:\tools\apache-maven-3.3.9\repository\cn\springbootdemo\springbootDbDemo\0.0.1-SNAPSHOT\springbootDbDemo-0.0.1-SNAPSHOT.pom

[INFO] ------------------------------------------------------------------------

[INFO] BUILD SUCCESS

[INFO] ------------------------------------------------------------------------

4、添加SpringBoot引导类

package springbootDbDemo;

import org.springframework.boot.SpringApplication;

import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication

public class MySpringBootApplication {

public static void main(String[] args) {

SpringApplication.run(MySpringBootApplication.class);     

}

}

注意要采用右键添加类的方式添加这个类,否则会报错,package springbootDbDemo;这个是必须的,新建文件的方式没有这一句会报错。

5、编写Controller

package springbootDbDemo;

import org.springframework.stereotype.Controller;

import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.ResponseBody;

@Controller

public class StartController {

@RequestMapping("/hello")

@ResponseBody

public String init(){

return "hi,springboot!";

}

}

6、启动项目

点击debug按钮选择Debug As Java Application。

启动成功:

2020-04-13 14:51:37.771  INFO 6536 --- [           main] o.s.j.e.a.AnnotationMBeanExporter        : Registering beans for JMX exposure on startup

2020-04-13 14:51:38.019  INFO 6536 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 8080 (http) with context path ''

2020-04-13 14:51:38.026  INFO 6536 --- [           main] s.MySpringBootApplication                : Started MySpringBootApplication in 6.752 seconds (JVM running for 8.447)

在浏览器中输入:http://localhost:8080/hello 测试成功。

二、添加mysql支持

1、Pom.xml添加依赖库

引入jdbc支持

<dependency>

    <groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-starter-jdbc</artifactId>

</dependency>

引入MySQL连接依赖包

<dependency>

    <groupId>mysql</groupId>

<artifactId>mysql-connector-java</artifactId>

</dependency>

2、配置数据源信息

新建application.properties配置数据源信息

spring.datasource.url=jdbc:mysql://localhost:3306/test

spring.datasource.username=root

spring.datasource.password=123456

spring.datasource.driver-class-name=com.mysql.jdbc.Driver

在src/main/resources/application.yml 中添加效果是一样的

spring:

  datasource:

    driver-class-name: com.mysql.jdbc.Driver

    url: jdbc:mysql://localhost:3306/bank?useUnicode=true&characterEncoding=utf8

    username: root

password: ljf123456

3、创建数据库列表

CREATE TABLE `User` (

`NAME` VARCHAR(50) NULL DEFAULT NULL,

`AGE` INT(11) NULL DEFAULT NULL

)

4、添加测试代码

package springbootDbDemo;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.context.annotation.ComponentScan;

import org.springframework.jdbc.core.JdbcTemplate;

import org.springframework.stereotype.Controller;

import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.ResponseBody;

 

 

@ComponentScan

@Controller

public class StartController {

 

 @Autowired

 private JdbcTemplate jdbcTemplate;

 

 @RequestMapping("/insert")

     @ResponseBody

     public String insertAndSelectInfo() {

      String sql="insert into User(NAME,AGE) values(?,?)";

      User newUser = new User();

      newUser.setNAME("testname");

      newUser.setAGE(12);

 

   //int temp = jdbcTemplate.update(sql, newUser);  //异常,估计除了添加对象类还需要添加xml bean

      int temp = jdbcTemplate.update(sql, "test1",18); //ok  

if(temp > 0) {

return "insert ok";

 }

 return "insert error";

      

    }

 

 @RequestMapping("/hello")

     @ResponseBody

     public String init(){

         return "hi,springboot!";

     }

}

访问http://127.0.0.1:8080/insert请求后,插入数据成功。

 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值