Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How does SpringBoot use JdbcTemplate to operate the database

2025-03-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly introduces SpringBoot how to use JdbcTemplate operation database related knowledge, detailed and easy to understand, simple and fast operation, has a certain reference value, I believe that everyone will have a harvest after reading this SpringBoot how to use JdbcTemplate operation database article, let's take a look at it.

JdbcTemplate is a JDBC template framework provided by Spring, which uses AOP technology to solve the problem of a large number of duplicate codes when using JDBC directly. Although not as flexible as MyBatis, it is much more convenient than using JDBC directly.

I. Creating tables

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 table';

Second, add dependencies, configuration

First edit the pom.xml file and add dependencies.

org.springframework.boot spring-boot-starter-jdbc mysql mysql-connector-java com.alibaba druid 1.1.9

2. Write configuration

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

III. Writing code

1. Write entity class

@Data@Accessors(chain = true)public class Demo { private Integer id; private String name; private Integer num; private Date createTime;}

2. Write Dao code

@Repositorypublic class DemoDao { @Autowired private JdbcTemplate jdbcTemplate; //New data public int addDemo(Demo demo) { return jdbcTemplate.update("INSERT INTO t_demo(name, num) VALUE (?, ?) ", demo.getName(), demo.getNum()); } //modify data public int updateDemo(Demo demo) { return jdbcTemplate.update("UPDATE t_demo SET name=?, num=? WHERE id=? ", demo.getName(), demo.getNum(), demo.getId()); } //Delete data public int deleteDemoById(Integer id) { return jdbcTemplate.update("DELETE FROM t_demo WHERE id=? ", id); } //Get single data public Demo getDemoById(Integer id) { return jdbcTemplate.queryForObject("SELECT * FROM t_demo WHERE id=? ", new BeanPropertyRowMapper(Demo.class), id); } //Get multiple data public List getAllDemos() { return jdbcTemplate.query("SELECT * FROM t_demo", new BeanPropertyRowMapper(Demo.class)); }}

3. Write Controller Code

@RestController@RequestMapping("/demo")public class DemoController { @Autowired private DemoDao demoDao; @RequestMapping("") public void test(){ //New data int num = demoDao.addDemo(new Demo().setName("piao").setNum(20)); System.out.println("Insert a data: " + num); //modify data int num2 = demoDao.updateDemo(new Demo().setId(15).setName("piao").setNum(22)); System.out.println("Update a piece of data: " + num2); //Delete data int num3 = demoDao.deleteDemoById(13); System.out.println("Delete a piece of data: " + num3); //Query single data Demo demo = demoDao.getDemoById(15); System.out.println("Query 1 data: " + demo.toString()); //Query multiple data List demos = demoDao.getAllDemos(); System.out.println("Query multiple data: " + demos); }}

IV. Verification Results

About "SpringBoot how to use JdbcTemplate operation database" the content of this article is introduced here, thank you for reading! I believe everyone has a certain understanding of "SpringBoot how to use JdbcTemplate to operate database" knowledge, if you still want to learn more knowledge, welcome to pay attention to the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report