欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Spring JdbcTemplate實(shí)現(xiàn)添加與查詢方法詳解

 更新時(shí)間:2022年11月11日 09:08:55   作者:學(xué)習(xí)使我快樂T  
JdbcTemplate是Spring框架自帶的對(duì)JDBC操作的封裝,目的是提供統(tǒng)一的模板方法使對(duì)數(shù)據(jù)庫(kù)的操作更加方便、友好,效率也不錯(cuò),這篇文章主要介紹了Spring?JdbcTemplate執(zhí)行數(shù)據(jù)庫(kù)操作,需要的朋友可以參考下

簡(jiǎn)介

Spring框架對(duì)JDBC進(jìn)行封裝,使用JdbcTemplate方便實(shí)現(xiàn)對(duì)數(shù)據(jù)庫(kù)操作

JdbcTemplate 目的是使JDBC更加易于使用,JdbcTemplate是Spring的一部分。JdbcTemplate 處理了資源的建立和釋放,它幫助我們避免一些常見的錯(cuò)誤,比如忘了總要關(guān)閉連接。他運(yùn)行核心的JDBC工作流,如Statement的建立和執(zhí)行,而我們只需要提供SQL語(yǔ)句和提取結(jié)果即可。

準(zhǔn)備工作

①創(chuàng)建一個(gè)Maven工程

②添加相關(guān)依賴

 <dependencies>
    <!-- 基于Maven依賴傳遞性,導(dǎo)入spring-context依賴即可導(dǎo)入當(dāng)前所需所有jar包 -->
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context</artifactId>
        <version>5.3.23</version>
    </dependency>
    <!-- Spring 持久化層支持jar包 -->
    <!-- Spring 在執(zhí)行持久化層操作、與持久化層技術(shù)進(jìn)行整合過程中,需要使用orm、jdbc、tx三個(gè)
    jar包 -->
    <!-- 導(dǎo)入 orm 包就可以通過 Maven 的依賴傳遞性把其他兩個(gè)也導(dǎo)入 -->
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-orm</artifactId>
        <version>5.3.23</version>
    </dependency>
    <!-- Spring 測(cè)試相關(guān) -->
    <dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-test</artifactId>
    <version>5.3.23</version>
    </dependency>
        <!-- junit測(cè)試 -->
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
            <scope>test</scope>
        </dependency>
        <!-- MySQL驅(qū)動(dòng) -->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>8.0.16</version>
        </dependency>
        <!-- 數(shù)據(jù)源 -->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid</artifactId>
            <version>1.0.31</version>
        </dependency>
    </dependencies>

③創(chuàng)建Spring配置文件Spring-jdbc

 <!--引入jdbc.properties-->
    <context:property-placeholder location="classpath:jdbc.properties"></context:property-placeholder>
    <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
        <property name="driverClassName" value="${jdbc.driver}"></property>
        <property name="url" value="${jdbc.url}"></property>
        <property name="username" value="${jdbc.username}"></property>
        <property name="password" value="${jdbc.password}"></property>
    </bean>
    <bean class="org.springframework.jdbc.core.JdbcTemplate">
        <property name="dataSource" ref="dataSource"></property>
    </bean>

④創(chuàng)建數(shù)據(jù)庫(kù)表

⑤創(chuàng)建測(cè)試類

//指定當(dāng)前測(cè)試類在Spring的測(cè)試環(huán)境中執(zhí)行,此時(shí)就可以通過注入的方式直接獲取IOC容器中的bean
@RunWith(SpringJUnit4ClassRunner.class)
//設(shè)置Spring測(cè)試環(huán)境的配置文件
@ContextConfiguration("classpath:spring-jdbc.xml")
public class JdbcTemplateTest {
    @Autowired
    private JdbcTemplate jdbcTemplate;
    @Test
    public void testInsert() {
        String sql = "insert into t_user values(null,?,?,?,?,?)";
        jdbcTemplate.update(sql,"root","123",23,"女","123@qq.com");
    }
}

然后進(jìn)行添加測(cè)試即可

JdbcTemplate實(shí)現(xiàn)查詢功能

①創(chuàng)建實(shí)體類

②測(cè)試類,查詢一條數(shù)據(jù)

    @Test
    public void testGetUserById() {
        String sql = "select * from t_user where id = ?";
        User user = jdbcTemplate.queryForObject(sql, new BeanPropertyRowMapper<>(User.class), 1);
        System.out.println(user);
    }

測(cè)試類,查詢多條數(shù)據(jù)

    @Test
    public void testGetAllUser() {
        String sql = "select * from t_user";
        List<User> list = jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(User.class));
        list.forEach(System.out::println);
    }

測(cè)試類,查詢單條數(shù)據(jù)

@Test
public void testGetCount() {
    String sql = "select count(*) from t_user";
    Integer count = jdbcTemplate.queryForObject(sql, Integer.class);
    System.out.println(count);
}

到此這篇關(guān)于Spring JdbcTemplate實(shí)現(xiàn)添加與查詢方法詳解的文章就介紹到這了,更多相關(guān)Spring JdbcTemplate內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評(píng)論