[Spring] Simple operation template class-JdbcTemplate

Basic knowledge of JdbcTemplate

1) First time seeing JdbcTemplate

JdbcTemplate is an encapsulation of cumbersome native JdbcAPI objects.

The Spring framework provides us with a lot of operation template classes. For example: JdbcTemplate and HibernateTemplate for operating relational data, RedisTemplate for operating nosql database, JmsTemplate for operating message queue.

2) Simple development steps of JdbcTemplate

First configure the DataSource data source object, and then inject it into the JdbcTemplate template object, and then use the template object to perform many operations on the database.

3) JdbcTemplate needs to import Maven coordinates

<!-- 下面的Maven坐标可以参考一下 >_< -->

<dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>8.0.16</version>
</dependency>
<dependency>
            <groupId>c3p0</groupId>
            <artifactId>c3p0</artifactId>
            <version>0.9.1.2</version>
</dependency>
<dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid</artifactId>
            <version>1.1.10</version>
</dependency>
<dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
</dependency>
<dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-test</artifactId>
            <version>5.0.5.RELEASE</version>
</dependency>
<dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>5.0.5.RELEASE</version>
</dependency>
<dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-jdbc</artifactId>
            <version>5.0.5.RELEASE</version>
</dependency>
<dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-tx</artifactId>
            <version>5.0.5.RELEASE</version>
</dependency>

 
 

JdbcTemplate development (not using Spring)

① Use 4 parameters to configure the DataSource data source object
② Use the configured DataSource data source object to configure the JdbcTemplate template object
③ It can be used

public class JdbcTemplateTest {
    
    

    @Test
    public void test() throws PropertyVetoException {
    
    

        // 创建DataSource数据源对象
        ComboPooledDataSource dataSource = new ComboPooledDataSource();
        dataSource.setDriverClass("com.mysql.cj.jdbc.Driver");
        dataSource.setJdbcUrl("jdbc:mysql://localhost:3306/loliDB");
        dataSource.setUser("root");
        dataSource.setPassword("123456");

        // 创建JdbcTemplate模板对象
        JdbcTemplate template = new JdbcTemplate();
        template.setDataSource(dataSource);

        // 使用
        int row = template.update("insert into account values(?, ?)", "Hana", 10);
        System.out.println(row);

    }
}

 
 

JdbcTemplate development (using Spring)

We found that whether it is configuring the DataSource or configuring the Template, the code uses the setXxx method-immediately think of dependency injection

jdbc.properties ---------------------------------------------------------------------------

jdbc.driver=com.mysql.cj.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/loliDB
jdbc.username=root
jdbc.password=123456
applicationContext.xml --------------------------------------------------------------------

<!-- 从外部加载properties资源文件 -->
<context:property-placeholder location="classpath:jdbc.properties" />

<!-- DataSource数据源对象 -->
<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>

<!-- JdbcTemplate模板对象 -->
<bean id="template" class="org.springframework.jdbc.core.JdbcTemplate" >
    <property name="dataSource" ref="dataSource" />
</bean>
test.JdbctemplateTest ---------------------------------------------------------------------

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

    @Autowired
    private JdbcTemplate template;

    @Test
    public void test() {
    
    
        int row = template.update("insert into account values(?, ?)", "Hana", 10);
        System.out.println(row);
    }
    
}

 
 

Use of JdbcTemplate

1) update-use it for additions, deletions and changes!

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

    @Autowired
    private JdbcTemplate template;

    @Test
    public void test1() {
    
    
        template.update("insert into lolihouse values(?, ?)", "Hana", 8);
    }

    @Test
    public void test2() {
    
    
        template.update("delete from lolihouse where name = ?", "Hana");
    }

    @Test
    public void test3() {
    
    
        template.update("update lolihouse set age = ? where name = ?", 11, "Alice");
    }

}

2) query-all kinds of queries!

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

    @Autowired
    private JdbcTemplate template;
    
    @Test
    // 查询结果为出对象列表
    public void test4() {
    
    
        List<Loli> loliList = template.query("select * from lolihouse", new BeanPropertyRowMapper<Loli>(Loli.class));
        System.out.println(loliList);
    }

    @Test
    // 查询结果为一个对象
    public void test5() {
    
    
        Loli loli = template.queryForObject("select * from lolihouse where name = ?", new BeanPropertyRowMapper<Loli>(Loli.class), "Alice");
        System.out.println(loli);
    }

    @Test
    // 查询结果为统计个数
    public void test6() {
    
    
        Integer count = template.queryForObject("select count(*) from lolihouse", Integer.class);
        System.out.println(count);
    }

}

 
 
 
 

 
 
 
 

 
 
 
 

More >_<

Guess you like

Origin blog.csdn.net/m0_46202073/article/details/114044375