Home> Java> javaTutorial> body text

How SpringBoot uses JdbcTemplate to operate the database

王林
Release: 2023-05-16 11:07:05
forward
1334 people have browsed it

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

1. Create table

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表';
Copy after login

How SpringBoot uses JdbcTemplate to operate the database

##2. Add dependencies and configuration

1. First edit the

pom.xmlfile and add relevant dependencies.

  org.springframework.boot spring-boot-starter-jdbc    mysql mysql-connector-java    com.alibaba druid 1.1.9 
Copy after login
2. Write configuration

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
Copy after login

3. Write code

1. Write entity class

@Data @Accessors(chain = true) public class Demo { private Integer id; private String name; private Integer num; private Date createTime; }
Copy after login
2. Write Dao Code

@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 getAllDemos() { return jdbcTemplate.query("SELECT * FROM t_demo", new BeanPropertyRowMapper(Demo.class)); } }
Copy after login
3. Write Controller code

@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 demos = demoDao.getAllDemos(); System.out.println("查询多条数据:" + demos); } }
Copy after login

4. Verification results

How SpringBoot uses JdbcTemplate to operate the database

The above is the detailed content of How SpringBoot uses JdbcTemplate to operate the database. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:yisu.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!