数据库连接(二) JdbcTemplate

6 篇文章 0 订阅

使用spring框架中的JdbcTemplate 来连接处理数据库, JdbcTemplate接收来自 druid 的 DataSource

环境配置 spring的包

libs/commons-logging-1.2.jar
libs/druid-1.1.20.jar
libs/hamcrest-core-1.3.jar   //junit的依赖
libs/junit-4.12.jar
libs/mysql-connector-java-8.0.21.jar
libs/spring-beans-5.2.5.RELEASE.jar
libs/spring-context-5.2.5.RELEASE.jar
libs/spring-core-5.2.5.RELEASE.jar
libs/spring-expression-5.2.5.RELEASE.jar
libs/spring-jdbc-5.2.5.RELEASE.jar    //jdbcTemple所在包
libs/spring-tx-5.2.5.RELEASE.jar

.queryForMap   

/*
     *  .queryForMap
     * 只能查询结果为1 条,否则报错 org.springframework.dao.IncorrectResultSizeDataAccessException: Incorrect result size: expected 1, actual 2
     */
    @Test
    public void Test() throws Exception {
        Properties properties = new Properties();
        properties.load(ClassLoader.getSystemResourceAsStream("resources/db_server.properties"));
        DataSource dataSource = DruidDataSourceFactory.createDataSource(properties);
        JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);
        String sql="select id,NameSpace from NameSpace where id between ? and ?";
        Map<String, Object> stringObjectMap = jdbcTemplate.queryForMap(sql,1,1);
        System.out.println(stringObjectMap);
    }

.queryForList

 /*
     *  .queryForList
     *  可以查出多个结果 list包含map
     */
    @Test
    public void Test2() throws Exception {
        Properties properties = new Properties();
        properties.load(ClassLoader.getSystemResourceAsStream("resources/db_server.properties"));
        DataSource dataSource = DruidDataSourceFactory.createDataSource(properties);
        JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);
        String sql="select id,NameSpace from NameSpace where id between ? and ?";
        List<Map<String, Object>> maps = jdbcTemplate.queryForList(sql, 2, 7);
        System.out.println(maps);
    }

.queryForObject  聚合查询

/*
     *  .queryForObject
     *  查询出聚合操作
     */
    @Test
    public void Test3() throws Exception {
        Properties properties = new Properties();
        properties.load(ClassLoader.getSystemResourceAsStream("resources/db_server.properties"));
        DataSource dataSource = DruidDataSourceFactory.createDataSource(properties);
        JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);
        String sql="select count(1) from NameSpace";
        Integer integer = jdbcTemplate.queryForObject(sql, Integer.class);
        System.out.println(integer);
    }

.query 查询结果封装成对象

/*
     *  .query
     *  查询出的结果 封装成对象 需要重写new RowMapper<>() 自定义RowMapper<>
     */
    @Test
    public void Test4() throws Exception {
        Properties properties = new Properties();
        properties.load(ClassLoader.getSystemResourceAsStream("resources/db_server.properties"));
        DataSource dataSource = DruidDataSourceFactory.createDataSource(properties);
        JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);
        String sql="select id,NameSpace from NameSpace where id between ? and ?";
        //jdbcTemplate.queryForObject()
        List<NameSpace> query = jdbcTemplate.query(sql,new Integer[]{2,5} , new RowMapper<NameSpace>() {
            @Override
            public NameSpace mapRow(ResultSet resultSet, int i) throws SQLException {
                Integer id = resultSet.getInt("id");
                String nameSpace1 = resultSet.getString("NameSpace");
                NameSpace nameSpace = new NameSpace(id,nameSpace1);
                return nameSpace;
            }
        });
        System.out.println(query);
    }

.query 查询结果封装成对象 BeanPropertyRowMapper<>

/*
     *  .query
     *  查询出的结果 封装成对象 需要重写new RowMapper<>(),
     *  使用new BeanPropertyRowMapper<>(NameSpace.class) 代替 RowMapper
     *  其中分装的对象类 必须要有 误参构造方法 否则报错
     *  No default constructor found; nested exception is java.lang.NoSuchMethodException:
     */
    @Test
    public void Test5() throws Exception {
        Properties properties = new Properties();
        properties.load(ClassLoader.getSystemResourceAsStream("resources/db_server.properties"));
        DataSource dataSource = DruidDataSourceFactory.createDataSource(properties);
        JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);
        String sql="select id,NameSpace from NameSpace where id between ? and ?";
        List<NameSpace> query=jdbcTemplate.query(sql,new Integer[]{2,5},new BeanPropertyRowMapper<>(NameSpace.class));
        System.out.println(query);
    }

 

 

 

 

 

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值