栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

Spring整合JDBC

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Spring整合JDBC

文章目录
  • JDBC整合
    • applicationContext.xml
    • 代码
  • JDBC模板类
    • 配置文件
    • 代码
    • Map
    • List
    • 使用BeanPropertyRowMapper自动进行映射

JDBC整合 applicationContext.xml

    
    
        
        
        
        
    
代码

package cn.tedu.test;

import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class Test01 {
    @Test
    public void test01(){
        //容器初始化
        ApplicationContext context=new
                ClassPathXmlApplicationContext("applicationContext.xml");
        //获取bean---获取数据源
        DataSource source = (DataSource) context.getBean("dataSource");
        Connection conn=null;
        PreparedStatement ps=null;
        ResultSet rs=null;
        try {
            conn=source.getConnection();
            ps = conn.prepareStatement("select * from user where id 
                System.out.println(rs.getString("name"));
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally {
            if(rs!=null)
                try {
                    rs.close();
                } catch (SQLException throwables) {
                    throwables.printStackTrace();
                }finally {
                    rs=null;
                }
            if(ps!=null)
                try {
                    ps.close();
                } catch (SQLException throwables) {
                    throwables.printStackTrace();
                }finally {
                    ps=null;
                }
            if(conn!=null)
                try {
                    conn.close();
                } catch (SQLException throwables) {
                    throwables.printStackTrace();
                }finally {
                    conn=null;
                }
        }
        }
    }


JDBC模板类 配置文件

 
    
        
        
        
        
    
    
    
        
        
    
代码

package cn.tedu.test;

import cn.tedu.domain.User;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;

import java.util.List;
import java.util.Map;

public class Test01 {

    private ApplicationContext context = null;
    private JdbcTemplate jdbcTemplate = null;

    @Before//比所有的层单元测试方法先执行
    public void before() {
        //容器初始化
        context = new ClassPathXmlApplicationContext("applicationContext.xml");
        //获取bean
        jdbcTemplate = (JdbcTemplate) context.getBean("jdbcTemplate");
    }

    @After//比所有的层单元测试方法后执行
    public void after() {
        //容器关闭
        ((ClassPathXmlApplicationContext) context).close();
    }

    
    @Test
    public void test01() {
        jdbcTemplate.update("insert into user values(null ,?,?)", "xxx", 99);
    }

    
    @Test
    public void test02() {
        jdbcTemplate.update("update user set age=? where name=?", 55, "xxx");
    }

    
    @Test
    public void test03() {
        jdbcTemplate.update("delete from user where id=?", 17);
    }

    
    @Test
    public void test04() {
        Map map = jdbcTemplate.queryForMap("select * from user where id=?", 3);
        System.out.println(map);
    }


    
    @Test
    public void test05() {
        List> list = jdbcTemplate.queryForList("select * from user where id=?", 3);
        System.out.println(list);
    }


    
    @Test
    public void test06() {
        User user = jdbcTemplate.queryForObject("select * from user where id=?",
                new BeanPropertyRowMapper(User.class), 3);
        System.out.println(user);
    }

    
    @Test
    public void test07() {
        List list = jdbcTemplate.query("select * from user where id(User.class), 3);
        System.out.println(list);
    }
}

Map

List

使用BeanPropertyRowMapper自动进行映射


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/850980.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号