JDBCTemplate

概述:

spring對很多技術(shù)復(fù)雜繁瑣的代碼進(jìn)行封裝,JDBCTemplate就是其中一種
如:操作關(guān)系型數(shù)據(jù)的JdbcTemplate和HibernateTemplate,操作nosql數(shù)據(jù)庫的RedisTemplate,操作消息隊(duì)列的JmsTemplate等等。

步驟:

  1. 導(dǎo)入spring-jdbc和spring-tx【】坐標(biāo)
<!--JDBCTemplte-->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-jdbc</artifactId>
            <version>5.2.5.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-tx</artifactId>
            <version>5.2.5.RELEASE</version>
  1. 創(chuàng)建數(shù)據(jù)庫表和實(shí)體
  2. 創(chuàng)建JdbcTemplate對象
  @Test
    public void test1() throws SQLException {
        //創(chuàng)建數(shù)據(jù)源
        DruidDataSource dataSource=new DruidDataSource();
        dataSource.setDriverClassName("com.mysql.jdbc.Driver");
        dataSource.setUrl("JDBC:mysql://localhost:3306/springtest");
        dataSource.setUsername("root");
        dataSource.setPassword("123456");
        
        //創(chuàng)建JDBCTemplate對象
        JdbcTemplate template=new JdbcTemplate(dataSource);
        String sql="insert into account(name,money) values(?,?)";
        int count = template.update(sql, "zhangsan", 1200);
        System.out.println(count);
    }
  1. 執(zhí)行數(shù)據(jù)庫操作

spring產(chǎn)生模板對象代碼實(shí)現(xiàn)(抽取jdbc.properties)

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans.xsd
       http://www.springframework.org/schema/context
       http://www.springframework.org/schema/context/spring-context.xsd
">
    <!--尋找jdbc.properties配置文件-->
    <context:property-placeholder location="classpath:Jdbc.properties" />
    <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
        <property name="driverClassName" value="${jdbc.driver}" />
        <property name="url" value="${jdbc.url}" />
        <property name="username" value="${jdbc.username}"/>
        <property name="password" value="${jdbc.password}"/>
    </bean>
    <bean id="template" class="org.springframework.jdbc.core.JdbcTemplate">
        <property name="dataSource" ref="dataSource" />
    </bean>
</beans>

Jdbc基本使用:

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext.xml")
public class JdbcTemplate_CRUD {
    @Autowired
    private JdbcTemplate template;

    @Test
    public void testUpdate(){
        int row = template.update("update account set money=? where name=?", 2000, "王五");
    }

    @Test
    public void testDelete(){
        int row = template.update("delete from account where name=?", "??");
    }

    /**
     * 查詢多個對象
     */
    @Test
    public void testQuery(){
        List<Account> AccountList = template.query("select * from account", new BeanPropertyRowMapper<Account>(Account.class));
        System.out.println(AccountList);
    }

    /**
     * 查詢多個多項(xiàng)
     */
    @Test
    public void testQueryOne(){
        Account account = template.queryForObject("select * from account where name=?", new BeanPropertyRowMapper<Account>(Account.class), "王五");
        System.out.println(account);
    }

    /**
     * 聚合函數(shù)查詢
     */
    @Test
    public void testQueryCount(){
        long count = template.queryForObject("select count(*) from account",long.class);
                System.out.println(count);
    }
}
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容