How does SpringBoot use JdbcTemplate to operate the database
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.