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){

扫描二维码关注公众号,回复: 5766094 查看本文章

    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);

    }

  }

}

猜你喜欢

转载自www.cnblogs.com/fanqiexin/p/10651087.html