JdbcTemplate 增删查改
准备工作:1,拷贝工具类JDBCUtils,
2,导入包commons-logging-1.2-javadoc.jar spring-beans-5.0.0.RELEASE-javadoc.jar spring-core-5.0.0.RELEASE-javadoc.jar spring-jdbc-5.0.0.RELEASE-javadoc.jar spring-tx-5.0.0.RELEASE-javadoc.jar
3,将properties文件放入src文件夹里
import org.junit.Test;
import java.util.List;
import java.util.Map;
public class cs{
public static void main(String[] args){
JdbcTemplate template = new JdbcTemplate(JDBCUtils.getDataSource());
//生成template对象(于数据库的链接已经接收到) 其依附于dataSource建立
String s="insert into tb values(5,'苏苓儿','二世')";
//创建sql语句
template.update(s);
//进行更新
System.out.println("The change has come into effect!");
//更新成功
}
JdbcTemplate tem = new JdbcTemplate(JDBCUtils.getDataSource());
public void test(){//查询所有的数据放入自定义的Map集合当中
String sql="select * from mn";
List<Map<String,Object>> maplist=template.queryForList(sql);
for(Map<String,Object> ml:maplist){
System.out.println("ml");
}
}
public void test1(){//将查询对象放入map类的list集合当中
String s="select * from mn";
List<emp> list=tem.query(s,new BeanPropertyRowMapper<emp>(emp.class));
for(emp e:list){//遍历集合
System.out.println(e);
}
}
}