溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

SpringBoot如何使用JdbcTemplate操作數(shù)據(jù)庫

發(fā)布時(shí)間:2021-07-06 18:22:22 來源:億速云 閱讀:185 作者:chen 欄目:編程語言

本篇內(nèi)容介紹了“SpringBoot如何使用JdbcTemplate操作數(shù)據(jù)庫”的有關(guān)知識(shí),在實(shí)際案例的操作過程中,不少人都會(huì)遇到這樣的困境,接下來就讓小編帶領(lǐng)大家學(xué)習(xí)一下如何處理這些情況吧!希望大家仔細(xì)閱讀,能夠?qū)W有所成!

JdbcTemplate 是 Spring 提供的一套 JDBC 模版框架,利用 AOP 技術(shù)來解決直接使用 JDBC 時(shí)大量重復(fù)代碼的問題。雖然沒有 MyBatis 那么靈活,但是比直接使用 JDBC 要方便很多。

一、創(chuàng)建表

CREATE TABLE `t_demo` (
  `id` bigint(20) NOT NULL AUTO_INCREMENT,
  `name` varchar(120) NOT NULL,
  `num` int(11) NOT NULL,
  `create_time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8 COMMENT='demo表';

SpringBoot如何使用JdbcTemplate操作數(shù)據(jù)庫

二、添加依賴、配置

1、首先編輯 pom.xml 文件,添加相關(guān)依賴。

<!-- spring-jdbc -->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
 
<!-- 數(shù)據(jù)庫驅(qū)動(dòng)依賴 -->
<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
</dependency>
 
<!-- 數(shù)據(jù)庫連接池 -->
<dependency>
    <groupId>com.alibaba</groupId>
    <artifactId>druid</artifactId>
    <version>1.1.9</version>
</dependency>

2、編寫配置

spring.datasource.type = com.alibaba.druid.pool.DruidDataSource
spring.datasource.url = jdbc:mysql://localhost:3306/PiaoDB?useUnicode=swater&characterEncoding=UTF-8
spring.datasource.username = root
spring.datasource.password = root
spring.datasource.driver-class-name = com.mysql.jdbc.Driver

三、編寫代碼

1、編寫實(shí)體類

@Data
@Accessors(chain = true)
public class Demo {

    private Integer id;

    private String name;

    private Integer num;

    private Date createTime;

}

2、編寫Dao代碼

@Repository
public class DemoDao {

    @Autowired
    private JdbcTemplate jdbcTemplate;

    // 新增數(shù)據(jù)
    public int addDemo(Demo demo) {
        return jdbcTemplate.update("INSERT INTO t_demo(name, num) VALUE (?, ?)",
                demo.getName(), demo.getNum());
    }

    // 修改數(shù)據(jù)
    public int updateDemo(Demo demo) {
        return jdbcTemplate.update("UPDATE t_demo SET name=?, num=? WHERE id=?",
                demo.getName(), demo.getNum(), demo.getId());
    }

    // 刪除數(shù)據(jù)
    public int deleteDemoById(Integer id) {
        return jdbcTemplate.update("DELETE FROM t_demo WHERE id=?", id);
    }

    // 獲取單條數(shù)據(jù)
    public Demo getDemoById(Integer id) {
        return jdbcTemplate.queryForObject("SELECT * FROM t_demo WHERE id=?",
                new BeanPropertyRowMapper<>(Demo.class), id);
    }

    // 獲取多條數(shù)據(jù)
    public List<Demo> getAllDemos() {
        return jdbcTemplate.query("SELECT * FROM t_demo",
                new BeanPropertyRowMapper<>(Demo.class));
    }

}

3、編寫Controller代碼

@RestController
@RequestMapping("/demo")
public class DemoController {

    @Autowired
    private DemoDao demoDao;

    @RequestMapping("")
    public void test(){
        // 新增數(shù)據(jù)
        int num = demoDao.addDemo(new Demo().setName("piao").setNum(20));
        System.out.println("插入一條數(shù)據(jù):" + num);

        // 修改數(shù)據(jù)
        int num2 = demoDao.updateDemo(new Demo().setId(15).setName("piao").setNum(22));
        System.out.println("更新一條數(shù)據(jù):" + num2);

        // 刪除數(shù)據(jù)
        int num3 = demoDao.deleteDemoById(13);
        System.out.println("刪除一條數(shù)據(jù):" + num3);

        // 查詢單條數(shù)據(jù)
        Demo demo = demoDao.getDemoById(15);
        System.out.println("查詢1條數(shù)據(jù):" + demo.toString());

        // 查詢多條數(shù)據(jù)
        List<Demo> demos = demoDao.getAllDemos();
        System.out.println("查詢多條數(shù)據(jù):" + demos);
    }

}

四、驗(yàn)證結(jié)果

SpringBoot如何使用JdbcTemplate操作數(shù)據(jù)庫

“SpringBoot如何使用JdbcTemplate操作數(shù)據(jù)庫”的內(nèi)容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業(yè)相關(guān)的知識(shí)可以關(guān)注億速云網(wǎng)站,小編將為大家輸出更多高質(zhì)量的實(shí)用文章!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI