Spring - 09JdbcTemplate

Spring - 09JdbcTemplate

(1)JdbcTemplate基本使用

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

例如:操作关系数据库的JdbcTemplate和HibernateTemplate,操作Redis的RedisTemplate,操作消息队列的JmsTemplate等等。

(1.1)导入spring-jdbc和spring-tx坐标

导入spring-context、spring-test、junit、mysql-connector-java、c3p0、druid

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-jdbc</artifactId>
            <version>5.0.10.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-tx</artifactId>
            <version>5.0.10.RELEASE</version>
        </dependency>

(1.2)创建数据库表和实体

public class Account {
    private String name;
    private double money;
    // 省略getter/setter/toString
}

(1.3)创建JdbcTemplate对象

    @Test
    public void test1(){
        DruidDataSource dataSource = new DruidDataSource();       // 创建数据源对象
        dataSource.setDriverClassName("com.mysql.jdbc.Driver");
        dataSource.setUrl("jdbc:mysql://localhost:3306/dev");
        dataSource.setUsername("root");
        dataSource.setPassword("Mysql2020");

        JdbcTemplate jdbcTemplate = new JdbcTemplate();         
        jdbcTemplate.setDataSource(dataSource);                   // 设置数据源对象,知道数据库在哪
        int row = jdbcTemplate.update("insert into account values(?,?)", "tom", 5000.00);
        System.out.println(row);
    }

(1.4)执行数据库操作

        JdbcTemplate jdbcTemplate = new JdbcTemplate();
        jdbcTemplate.setDataSource(dataSource);
        int row = jdbcTemplate.update("insert into account values(?,?)", "tom", 5000.00);
        System.out.println(row);

(1.5)Sprin*生JdbcTemplate对象

我们可以将JdbcTemplate的创建权交给Spring,将数据源DataSource的创建权也交给Spring,在Spring容器内部将数据源DataSource注入到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">   // jdbc模板对象
        <property name="dataSource" ref="dataSource"></property>
    </bean>

 

    @Test
    public void test2() throws PropertyAccessException {
        ApplicationContext app = new ClassPathXmlApplicationContext("applicationContext.xml");
        JdbcTemplate jdbcTemplate = app.getBean(JdbcTemplate.class);
        int row = jdbcTemplate.update("insert into account values(?,?)", "xiaoming", 4000);
        System.out.println(row);
    }

 

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext.xml")
public class JdbcTemplateTest {

    @Autowired
    private JdbcTemplate jdbcTemplate;

    @Test
    public void testQueryCount(){     
        Long count = jdbcTemplate.queryForObject("select count(*) from account", Long.class);
        System.out.println(count);
    }

    @Test
    public void testQueryOne(){
        Account account = jdbcTemplate.queryForObject("select * from account where name = ?", new BeanPropertyRowMapper<Account>(Account.class),"tom");
        System.out.println(account);
    }

    @Test
    public void testQueryAll(){
        List<Account> accountList = jdbcTemplate.query("select * from account", new BeanPropertyRowMapper<Account>(Account.class));
        System.out.println(accountList);
    }

    @Test
    public void testDelete(){
        int row = jdbcTemplate.update("delete from account where name = ?",  "aaa");
        System.out.println(row);
    }

    @Test
    public void testUpdate(){
        int row = jdbcTemplate.update("update account set money = ? where name = ?",  8000,"xiaoming");
        System.out.println(row);
    }
}

 

上一篇:JAVA复习Day10


下一篇:Spring JdbcTemplate