首頁  >  文章  >  Java  >  SpringBoot怎麼使用JdbcTemplate操作資料庫

SpringBoot怎麼使用JdbcTemplate操作資料庫

王林
王林轉載
2023-05-16 11:07:051364瀏覽

JdbcTemplate 是 Spring 提供的一套 JDBC 模版框架,利用 AOP 技術來解決直接使用 JDBC 時大量重複程式碼的問題。雖然沒有 MyBatis 那麼靈活,但比直接使用 JDBC 要方便很多。

一、建立表格

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操作資料庫

#二、新增依賴、設定

1.先編輯 pom.xml 文件,加入相關依賴。

<!-- spring-jdbc -->
<dependency>
    <groupid>org.springframework.boot</groupid>
    <artifactid>spring-boot-starter-jdbc</artifactid>
</dependency>
 
<!-- 数据库驱动依赖 -->
<dependency>
    <groupid>mysql</groupid>
    <artifactid>mysql-connector-java</artifactid>
</dependency>
 
<!-- 数据库连接池 -->
<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;

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

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

    // 删除数据
    public int deleteDemoById(Integer id) {
        return jdbcTemplate.update("DELETE FROM t_demo WHERE id=?", id);
    }

    // 获取单条数据
    public Demo getDemoById(Integer id) {
        return jdbcTemplate.queryForObject("SELECT * FROM t_demo WHERE id=?",
                new BeanPropertyRowMapper(Demo.class), id);
    }

    // 获取多条数据
    public List<demo> getAllDemos() {
        return jdbcTemplate.query("SELECT * FROM t_demo",
                new BeanPropertyRowMapper(Demo.class));
    }

}</demo>

3、寫Controller程式碼

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

    @Autowired
    private DemoDao demoDao;

    @RequestMapping("")
    public void test(){
        // 新增数据
        int num = demoDao.addDemo(new Demo().setName("piao").setNum(20));
        System.out.println("插入一条数据:" + num);

        // 修改数据
        int num2 = demoDao.updateDemo(new Demo().setId(15).setName("piao").setNum(22));
        System.out.println("更新一条数据:" + num2);

        // 删除数据
        int num3 = demoDao.deleteDemoById(13);
        System.out.println("删除一条数据:" + num3);

        // 查询单条数据
        Demo demo = demoDao.getDemoById(15);
        System.out.println("查询1条数据:" + demo.toString());

        // 查询多条数据
        List<demo> demos = demoDao.getAllDemos();
        System.out.println("查询多条数据:" + demos);
    }

}</demo>

四、驗證結果

SpringBoot怎麼使用JdbcTemplate操作資料庫

以上是SpringBoot怎麼使用JdbcTemplate操作資料庫的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文轉載於:yisu.com。如有侵權,請聯絡admin@php.cn刪除