First create a new simple data table and demonstrate by operating this data table
DROP TABLE IF EXISTS `items`; CREATE TABLE `items` ( `id` int(11) NOT NULL AUTO_INCREMENT, `title` varchar(255) DEFAULT NULL, `name` varchar(10) DEFAULT NULL, `detail` varchar(255) DEFAULT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB AUTO_INCREMENT=7 DEFAULT CHARSET=utf8;
Introduce maven dependency and connection class of JdbcTemplate
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-jdbc</artifactId> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <scope>runtime</scope> </dependency>
In the application.properties file, configure the driver class, database address, database account, password information of mysql. Create application.properties in the src/main/resource folder.
spring.datasource.url=jdbc:mysql://127.0.0.1:3306/spring?useSSL=false spring.datasource.username=root spring.datasource.password=123456 spring.datasource.driver-class-name=com.mysql.jdbc.Driver spring.datasource.max-idle=10 spring.datasource.max-wait=10000 spring.datasource.min-idle=5 spring.datasource.initial-size=5 server.port=8080 server.session.timeout=10 server.tomcat.uri-encoding=UTF-8
Create a new entity class, the properties correspond to the SQL field
package org.amuxia.start; public class Items { private Integer id; private String title; private String name; private String detail; public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getTitle() { return title; } public void setTitle(String title) { this.title = title; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getDetail() { return detail; } public void setDetail(String detail) { this.detail = detail; } public Items() { super(); // TODO Auto-generated constructor stub } public Items(Integer id, String title, String name, String detail) { super(); this.id = id; this.title = title; this.name = name; this.detail = detail; } @Override public String toString() { return "Items [id=" + id + ",, name=" + name + ", detail=" + detail + "]"; } }New operations
/** * New data* @param items * @return */ @RequestMapping("/add") public @ResponseBody String addItems(Items items) { String sql = "insert into items (id,title,name,detail) value (?,?,?,?)"; Object args[] = {items.getId(),items.getTitle(),items.getName(),items.getDetail()}; int temp = jdbcTemplate.update(sql, args); if(temp > 0) { return "Article added successfully"; } return "A new error occurred"; }Let's do a test. Enter http://localhost:8080/items/add in the postman test tool
We can see that the new addition has been successful, it is indeed very convenient and there is no cumbersome configuration information.
The remaining deletes, update operations and new code remain unchanged, but only changes in SQL, and no demonstration is given here.
All query operations
/** * @return * Query all information*/ @RequestMapping("/list") public List<Map<String, Object>> itemsList() { String sql = "select * from items"; List<Map<String, Object>> list = jdbcTemplate.queryForList(sql); return list; }Let's do a test. Enter http://localhost:8080/items/list in the postman test tool
We have seen that, including the newly added data, have been found.
In order to learn the JdbcTemplate operation of springboot, all the addition, deletion, modification and search codes are written in the ItemsController class, which is also convenient for demonstration. The code is posted here, and you can run it if you need it.
package org.amuxia.start; import java.util.List; import java.util.Map; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.ComponentScan; import org.springframework.jdbc.core.JdbcTemplate; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseBody; import org.springframework.web.bind.annotation.RestController; @ComponentScan @RestController @RequestMapping("/items") public class ItemsController { @Autowired private JdbcTemplate jdbcTemplate; /** * @return * Query all information*/ @RequestMapping("/list") public List<Map<String, Object>> itemsList() { String sql = "select * from items"; List<Map<String, Object>> list = jdbcTemplate.queryForList(sql); return list; } /** * @param id * @return * Query a single message based on the ID*/ @RequestMapping("/detail/{id}") public Map<String, Object> detail(@PathVariable int id) { Map<String, Object> map = null; List<Map<String, Object>> list = itemsList(); map = list.get(id); return map; } /** * New data* @param items * @return */ @RequestMapping("/add") public @ResponseBody String addItems(Items items) { String sql = "insert into items (id,title,name,detail) value (?,?,?,?)"; Object args[] = {items.getId(),items.getTitle(),items.getName(),items.getDetail()}; int temp = jdbcTemplate.update(sql, args); if(temp > 0) { return "Article added successfully"; } return "Article added error"; } /** * @param items * @return * Delete data*/ @RequestMapping("/del") public @ResponseBody String delItems(Items items) { String sql = "delete from items where id = ?"; Object args[] = {items.getId()}; int temp = jdbcTemplate.update(sql, args); if(temp > 0) { return "Article delete successfully"; } return "Article deleted error"; } /** * @param items * @return * Update operation*/ @RequestMapping("/upd") public @ResponseBody String updItems(Items items) { String sql = "update items set title = ?,detail = ? where id = ?"; Object args[] = {items.getTitle(),items.getDetail(),items.getId()}; int temp = jdbcTemplate.update(sql, args); if(temp > 0) { return "Article modified successfully"; } return "Error in modification"; } }Here is an explanation
@ComponentScan:
@ComponentScan tells Spring which annotation class will be automatically scanned by spring and loaded into the bean container. If you have a class identified with the @Controller annotation, then if @ComponentScan is not added to automatically scan the controller, the controller will not be scanned by spring, and will not be loaded into the spring container, and the controller will not work.
Startup class code
package org.amuxia.start; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.EnableAutoConfiguration; import org.springframework.web.bind.annotation.RestController; @RestController @EnableAutoConfiguration public class App { public static void main( String[] args ) { System.out.println( "start..." ); SpringApplication.run(ItemsController.class, args); } }Summarize
The above is what the editor introduced to you. Springboot uses JdbcTemplate to complete the database addition, deletion, modification and search function. I hope it will be helpful to everyone. If you have any questions, please leave me a message and the editor will reply to everyone in time. Thank you very much for your support to Wulin.com website!