创建数据库 create database springbootdb; create table user( id int not null auto_increment, name varchar(100), email varchar(100), PRIMARY KEY ( id ) )ENGINE=InnoDB DEFAULT CHARSET=utf8; commit; insert user(name,email) values('aa','dd'); insert user(name,email) values('天生自然','天空'); commit;
<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>com.dd</groupId> <artifactId>dd</artifactId> <version>0.0.1-SNAPSHOT</version> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.0.0.RELEASE</version> <relativePath /> <!-- lookup parent from repository --> </parent> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-jdbc</artifactId> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <scope>runtime</scope> <version>8.0.13</version> </dependency> </dependencies> <build> <plugins> <plugin> <artifactId>maven-compiler-plugin</artifactId> <configuration> <source>1.8</source> <target>1.8</target> </configuration> </plugin> </plugins> </build> </project>
server.port =8089 spring.datasource.url=jdbc:mysql://localhost:3306/springbootdb?serverTimezone=UTC&autoReconnect=true spring.datasource.username=root spring.datasource.password=admin spring.jpa.hibernate.ddl-auto=create-drop ##spring.datasource.driver-class-name=com.mysql.jdbc.Driver spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
package com.lidihuo; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class SpringBootJdbcApplication { public static void main(String[] args) { SpringApplication.run(SpringBootJdbcApplication.class, args); } }
package com.lidihuo; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.jdbc.core.JdbcTemplate; import org.springframework.web.bind.annotation.RestController; @RestController public class SpringBootJdbcController { @Autowired JdbcTemplate jdbc; @RequestMapping("/insert") public String index(){ jdbc.execute("insert into user(name,email)values('天黑了','java@lidihuo.com')"); return"data inserted Successfully"; } }