JdbcTemplate template = new JdbcTemplate(JDBCUtils.getDataSource());
用JdbcTemplate完成crud操作
- update():执行DML语句,增删改语句
- queryForMap():查询结果将结果集封装为map集合
-- 注意 : 这个方法查询的结果集长度只能是1
- queryForList():查询结果将结果集封装为list集合
-- 注意: 将每一条记录封装为一个Map集合,再将map集合装载到List集合中。
- query()查询结果,将结果封装为javaBean对象
- queryForObject(): 查询结果,将结果封装为对象
public class JavaDemo01 {
public static void main(String[] args) {
JdbcTemplate template = new JdbcTemplate(JDBCUtils.getDataSource());
String sql = "update account set username = 'Abbe' where id = ?";
int count = template.update(sql ,1);
System.out.println(count);
}
}
package com.itheima;
import org.springframework.jdbc.core.JdbcTemplate;
import util.JDBCUtils;
public class JavaDemo01 {
public static void main(String[] args) {
JdbcTemplate template = new JdbcTemplate(JDBCUtils.getDataSource());
String sql = "insert into account(id,username,password)values(?,?,?)";
int count = template.update(sql,2,"Jerry","123");
System.out.println(count);
}
}
public class JavaDemo01 {
public static void main(String[] args) {
JdbcTemplate template = new JdbcTemplate(JDBCUtils.getDataSource());
String sql = "delete from account where id = ?";
int count = template.update(sql,1);
System.out.println(count);
}
}
public class JavaDemo01 {
public static void main(String[] args) {
JdbcTemplate template = new JdbcTemplate(JDBCUtils.getDataSource());
String sql = "select * from account where id = ?";
Map<String, Object> map = template.queryForMap(sql, 2);
for (String key : map.keySet()) {
System.out.println(key + "--" +map.get(key));
}
}
public class JavaDemo01 {
public static void main(String[] args) {
JdbcTemplate template = new JdbcTemplate(JDBCUtils.getDataSource());
String sql = "select * from account where id = ?";
List<Map<String, Object>> list = template.queryForList(sql, 2);
for (Map<String, Object> map : list) {
for (Map.Entry<String, Object> en : map.entrySet()) {
System.out.println(en.getKey() +" -" + en.getValue());
}
}
}
}
public class JavaDemo01 {
public static void main(String[] args) {
JdbcTemplate template = new JdbcTemplate(JDBCUtils.getDataSource());
String sql = "select * from account where id = ?";
Account account = template.queryForObject(sql, new BeanPropertyRowMapper<Account>(Account.class), 2);
System.out.println(account);
}
}
public class JavaDemo01 {
public static void main(String[] args) {
JdbcTemplate template = new JdbcTemplate(JDBCUtils.getDataSource());
String sql = "select * from account ";
List<Account> list = template.query(sql, new BeanPropertyRowMapper<Account>(Account.class));
for (Account account : list) {
System.out.println(account);
}
}
}
public class JavaDemo01 {
public static void main(String[] args) {
JdbcTemplate template = new JdbcTemplate(JDBCUtils.getDataSource());
String sql = "select count(*) from account ";
Long total = template.queryForObject(sql, Long.class);
System.out.println(total);
}
}
util 包下 JDBUtils.java src druid.properties
package com.itheima.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.web.bind.annotation.GetMapping;
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
public class JDBCController {
@Autowired
JdbcTemplate template;
@RequestMapping("/findAll")
public List<Map<String,Object>> findAll(){
String sql = "select * from user";
List<Map<String, Object>> list_maps = template.queryForList(sql);
return list_maps;
}
@GetMapping("/addUser")
public String addUser(){
String sql = "insert into user(username,birthday,sex,address)values('Anne','1987/11/12','男','济南')";
template.update(sql);
return "ok!";
}
@GetMapping("/updateUser/{id}")
public String updateUser(@PathVariable("id")Integer id){
String sql = "update user set username = ? , address = ? where id =" +id;
Object[] obj = new Object[2];
obj[0] = "Jeac";
obj[1] = "济南";
template.update(sql,obj);
return "update_ok";
}
@GetMapping("/deleteUser/{id}")
public String deleteUser(@PathVariable("id")Integer id){
String sql = "delete from user where id = "+id;
template.update(sql);
return "delete_ok";
}
}