Spring JDBC Template基本使用

1、JDBC Template概述

        是spring框架中提供的一个对象,是对原始繁琐的jdbc API对象的简单封装。Spring框架为我们提供了很多操作模板类

2、JDBCTemplate开发步骤:

        导入Spring-jdbc和Spring-tx坐标

        创建数据库和实体

        创建JDBCTemplate对象

        执行数据库操作

常用操作更新和查询

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:ApplicationContext.xml")
public class JdbcTemplatateCRUDTest {
    @Autowired
    public JdbcTemplate jdbcTemplate;
    //查询多少个
    @Test
    public void testQueryCount(){
        Long query = jdbcTemplate.queryForObject("select count(*) from account",long.class);
        System.out.println(query);
    }
    //查询单个
    @Test
    public void testQueryAone(){
        Account query = jdbcTemplate.queryForObject("select * from account where name=?", new BeanPropertyRowMapper<Account>(Account.class), "zhangsan");
        System.out.println(query);
    }
    //查询全部
    @Test
    public void testQueryAll(){
        List<Account> query = jdbcTemplate.query("select * from account", new BeanPropertyRowMapper<Account>(Account.class));
        System.out.println(query);
    }
    //更新
    @Test
    public void testUpdate(){
        int row = jdbcTemplate.update("update account set money=? where name=?", 10000, "zhangsan");
        System.out.println(row);
    }
    //删除
    @Test
    public void testDelete(){
        int row = jdbcTemplate.update("delete from account where name=?","tom");
        System.out.println(row);
    }

 JdbcTemplate的使用

    <context:property-placeholder location="classpath:jdbc.properties"/>

    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
        <property name="driverClass" value="${jdbc.driver}"/>
        <property name="jdbcUrl" value="${jdbc.url}"/>
        <property name="user" value="${jdbc.username}"/>
        <property name="password" value="${jdbc.password}"/>
    </bean>
    <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
        <property name="dataSource" ref="dataSource"/>
    </bean>

    @Test
    public void test2() throws Exception{
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
        JdbcTemplate jdbcTemplate = applicationContext.getBean(JdbcTemplate.class);
        int row = jdbcTemplate.update("insert into account3 values (?,?);", "李四", 5000);
        System.out.println(row);
    }
    @Test
    public void test1() throws Exception {
        ComboPooledDataSource dataSource = new ComboPooledDataSource();
        dataSource.setDriverClass("com.mysql.cj.jdbc.Driver");;
        dataSource.setJdbcUrl("jdbc:mysql://localhost:3306/test?serverTimezone=GMT%2B8");
        dataSource.setUser("root");
        dataSource.setPassword("123456");

        JdbcTemplate jdbcTemplate=new JdbcTemplate();
        jdbcTemplate.setDataSource(dataSource);

        int a=jdbcTemplate.update("insert into account values (?,?);","张三",5000);
        System.out.println(a);
    }

知识要点:

        导入Spring-jdbc和Spring-tx坐标

        创建数据库表和实体

        创建jdbcTemplate对象

                JdbcTemplate jdbcTemplate=new JdbcTemplate();
                jdbcTemplate.setDataSource(dataSource);

        执行数据库操作

                更新操作:

                        jdbcTemolate.update(sql,parms)

                查询操作:

                        jdbcTemplate.query(sql,Mapper,params)

                        jdbcTemplate.queryForObject(sql,Mapper,params)