极简spring boot MySQL测试
默认:
-
spring boot环境已经搭好,可以跑最基本的hello world
-
有MySQL环境有部分测试数据表,并且有MySQL语法基础
配置 application.yml
如下配置,根据自己的数据库信息与个人需求配置
server:
tomcat:
uri-encoding: UTF-8
threads:
max: 200
min-spare: 30
connection-timeout: 5000ms
port: 8080
spring:
datasource:
url: jdbc:mysql://localhost:3306/login(你的路径)
username: root(你的用户名)
password: 你的密码
driver-class-name: com.mysql.cj.jdbc.Driver
配置pom.xml依赖
<!-- ....其他信息-->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>3.1.1</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<java.version>17</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.26</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<dependency>
<groupId>javax.persistence</groupId>
<artifactId>javax.persistence-api</artifactId>
<version>2.2</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.webjars.bower</groupId>
<artifactId>RWD-Table-Patterns</artifactId>
<version>5.2.5</version>
</dependency>
<dependency>
<groupId>org.webjars.bower</groupId>
<artifactId>RWD-Table-Patterns</artifactId>
<version>5.2.5</version>
</dependency>
<dependency>
<groupId>org.webjars.bower</groupId>
<artifactId>RWD-Table-Patterns</artifactId>
<version>5.2.5</version>
</dependency>
<dependency>
<groupId>org.webjars.bower</groupId>
<artifactId>RWD-Table-Patterns</artifactId>
<version>5.2.5</version>
</dependency>
<dependency>
<groupId>org.webjars.npm</groupId>
<artifactId>github-com-lorenzofox3-Smart-Table</artifactId>
<version>2.1.11</version>
</dependency>
<dependency>
<groupId>javax.persistence</groupId>
<artifactId>javax.persistence-api</artifactId>
<version>2.2</version>
</dependency>
<dependency>
<groupId>javax.persistence</groupId>
<artifactId>javax.persistence-api</artifactId>
<version>2.2</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
代码示例:
package app.helloworld3;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import java.util.List;
import java.util.Map;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import java.util.List;
import java.util.Map;
@SpringBootApplication
public class Helloworld3Application {
public static void main(String[] args) {
SpringApplication.run(Helloworld3Application.class, args);
}
@RestController
public class helloworld {
@GetMapping("/index")
public String index() {
return "Hello World";
}
}
@Controller
public class UserController {
@Autowired
private JdbcTemplate jdbcTemplate;
// private JdbcTemplate jdbcTemplate;
@RequestMapping(value = "/getUsers",method = RequestMethod.GET)
@ResponseBody
/*
* List 里的对象是Map对象,而Map对象是
* 由一个String类型的键和Object类型的值组成
* */
public List<Map<String,Object>> getUsers(){
String sql="select * from user";//SQL查询语句
List<Map<String,Object>> list=jdbcTemplate.queryForList(sql);
return list;
}
}
}
示例结果:
掌握了select,其他增加(insert,)删除(delete),改,就改一下字段就可以了
本文由 mdnice 多平台发布