package xyz.xiaonanbyq.datasource.JDBCTemplate;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import xyz.xiaonanbyq.datasource.utils.JDBCDatasouce;
import java.util.List;
import java.util.Map;
public class demo01 {
public static void main(String[] args) {
//1.导包
//2.创建对象
JdbcTemplate jdbcTemplate = new JdbcTemplate(JDBCDatasouce.getDataSource());
//3.调用方法
String sql = "select * FROM stu";
// jdbcTemplate.update(sql,"秋哥","by2q");
// .query BeanPropertyRowMapper<stu>(stu.class));
List<stu> query = jdbcTemplate.query(sql, new BeanPropertyRowMapper<stu>(stu.class));
for (stu stu : query) {
System.out.println(stu);
}
System.out.println("*-*****************************************");
// queryForList(sql); 返回 LIST 元素是每个记录封装的map
List<Map<String, Object>> maps = jdbcTemplate.queryForList(sql);
for (int i = 0; i < maps.size(); i++) {
System.out.println(maps.get(i));
}
}
}
package xyz.xiaonanbyq.datasource.JDBCTemplate;
import java.util.Date;
public class stu {
private Integer stu_id;
private String name;
private String xingbie ;
private Date submission_date;
public stu(Integer stu_id, String name, String xingbie, Date submission_date) {
this.stu_id = stu_id;
this.name = name;
this.xingbie = xingbie;
this.submission_date = submission_date;
}
public stu() {
}
@Override
public String toString() {
return "stu{" +
"stu_id=" + stu_id +
", name='" + name + '\'' +
", xingbie='" + xingbie + '\'' +
", submission_date=" + submission_date +
'}';
}
public Integer getStu_id() {
return stu_id;
}
public void setStu_id(Integer stu_id) {
this.stu_id = stu_id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getXingbie() {
return xingbie;
}
public void setXingbie(String xingbie) {
this.xingbie = xingbie;
}
public Date getSubmission_date() {
return submission_date;
}
public void setSubmission_date(Date submission_date) {
this.submission_date = submission_date;
}
}