在springboot底层无论关系型还是非关系型数据库都是用spring-data进行交互
新建:
通过spring initialer勾选重要依赖jdbc api和mysql driver:
设置数据源配置:
通过源码可以看到我们一个通过spring.datasource.xxx来设置我们的yaml配置文件
同时我们可以观察到默认数据源是:
yaml文件:
整合jdbc,增删改查:
思考:在没有创建pojo实体类情况下,怎么才能获取数据库表信息并操作之?
答案:map
代码
package com.lian.controller; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.jdbc.core.JdbcTemplate; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import java.util.List; import java.util.Map; @RestController//@RestController=@Controller+@ResponseBody public class JDBCController { @Autowired JdbcTemplate jdbcTemplate; @RequestMapping("/userlist") public List<Map<String,Object>> list(){ String sql="select * from user"; List<Map<String,Object>> mapList=jdbcTemplate.queryForList(sql); return mapList; } @RequestMapping("/useradd") public String add(){ String sql="insert into user (id, name, pwd) VALUES (11,'消息','dddadd')"; jdbcTemplate.update(sql); return "add-ok"; } @RequestMapping("/userdel/{id}") public String del(@PathVariable("id") int id){ String sql="delete from user where id="+id; jdbcTemplate.update(sql); return "del-ok"; } @RequestMapping("/userupdate/{id}") public String update(@PathVariable("id") int id){ String sql="update user set name=?,pwd=? where id="+id; Object[] objects=new Object[2]; objects[0]="小明"; objects[1]="skdncc"; jdbcTemplate.update(sql,objects); return "update-ok"; } }View Code
标签:jdbc,springboot,org,springframework,整合,sql,import,id,String From: https://www.cnblogs.com/MyBlogs-joyiyii/p/17374257.html