溫馨提示×

溫馨提示×

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

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

SpringBoot怎么使用JdbcTemplate操作數(shù)據(jù)庫

發(fā)布時間:2022-04-07 14:13:12 來源:億速云 閱讀:165 作者:iii 欄目:編程語言

這篇文章主要介紹了SpringBoot怎么使用JdbcTemplate操作數(shù)據(jù)庫的相關(guān)知識,內(nèi)容詳細(xì)易懂,操作簡單快捷,具有一定借鑒價值,相信大家閱讀完這篇SpringBoot怎么使用JdbcTemplate操作數(shù)據(jù)庫文章都會有所收獲,下面我們一起來看看吧。

JdbcTemplate 是 Spring 提供的一套 JDBC 模版框架,利用 AOP 技術(shù)來解決直接使用 JDBC 時大量重復(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ū)動依賴 -->
<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、編寫實體類

@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);
    }

}

四、驗證結(jié)果

SpringBoot怎么使用JdbcTemplate操作數(shù)據(jù)庫

關(guān)于“SpringBoot怎么使用JdbcTemplate操作數(shù)據(jù)庫”這篇文章的內(nèi)容就介紹到這里,感謝各位的閱讀!相信大家對“SpringBoot怎么使用JdbcTemplate操作數(shù)據(jù)庫”知識都有一定的了解,大家如果還想學(xué)習(xí)更多知識,歡迎關(guān)注億速云行業(yè)資訊頻道。

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

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

AI