这里使用mysql数据库,省略数据库创建过程
1、添加依赖
<!--jdbc-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<!--mysql-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>6.0.6</version>
</dependency>
2、添加数据库配置
#数据库
spring.datasource.url=jdbc:mysql://localhost:3306/test
spring.datasource.username=root
spring.datasource.password=root
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
3、编写代码
@Repository
public class UserDao { @Autowired
private JdbcTemplate jt; public List<Map<String, Object>> listUser(){
StringBuilder sql = new StringBuilder();
sql.append("SELECT * FROM t_sp_user");
return jt.queryForList(sql.toString());
}
} @Service
public class UserService { @Autowired
private UserDao userDao; public List<Map<String, Object>> listUser(){
return userDao.listUser();
}
} @RestController
@RequestMapping(value = "user")
public class UserController { @Autowired
private UserService userService; @GetMapping(value = "list")
public List<Map<String, Object>> listUser(){
return userService.listUser();
}
}