
原先在spring中,配置数据源,将数据源导入到jdbctample即可。
在springboot中,只要导入数据源,jdbctample自动填充,直接使用jdbctample即可
spring:
datasource:
url: jdbc:mysql://localhost:3306/shixun6.7?useSSL=false
driver-class-name: com.mysql.jdbc.Driver
username: root
password: mysql
type: com.zaxxer.hikari.HikariDataSource
package com.atguigui.conntroller;
import com.atguigui.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
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;
import org.springframework.web.bind.annotation.RestController;
import java.sql.ResultSet;
import java.util.List;
//RestController相当于 @Controller +@ResponseBody
@RestController
public class springbootcontroller {
@Autowired
private JdbcTemplate jdbcTemplate;
@RequestMapping("/hello")
public String handele01()
{
List users= jdbcTemplate.query("select * from user",new BeanPropertyRowMapper(User.class));
for(User user:users)
{
System.out.println(user);
}
return "success";
}
}
spring jdbctample方式访问数据库_仰望星空的快乐的博客-CSDN博客