Springboot from getting started to getting started (3 JDBC & MySQL)

wwz_ henu 2022-02-13 05:26:12 阅读数:838

springboot getting started getting started

One . add to dependency

stay pom.xml Add the following

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

Two . To configure dataSource

stay First article It is mentioned in that you can application.properties To configure , This approach is slightly complicated , Use application.yml To configure

server:
port: 80
spring:
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://127.0.0.1:3306/test?useUnicode=true&characterEncoding=utf8&useSSL=false&allowPublicKeyRetrieval=true&serverTimezone=GMT%2B8
username: root
password: root # Database name 、 Change the user name and password to your own 

3、 ... and . Create route

* Database tables are created in advance

 @Autowired
private JdbcTemplate jdbcTemplate;
public static void main(String[] args) {
SpringApplication.run(Demo1Application.class, args);
}
@GetMapping("/db")
public List<Map<String,Object>> db(){
String sql = "select * from user";
List<Map<String,Object>> list = jdbcTemplate.queryForList(sql);
return list;
}

Four . visit

copyright:author[wwz_ henu],Please bring the original link to reprint, thank you. https://en.javamana.com/2022/02/202202130526103496.html