Implementing CURD of database in Spring Boot

1. Connect to the database

Connect to the database in the order shown in the diagram

 

Connect to the database

If the database is a database on the server, you need to perform the following operations:

 

 An interface appears indicating that the connection is successful.

 2. Import dependencies and other preparations

2.1. Import dependencies

<!--        MYSQL-->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <scope>runtime</scope>
        </dependency>

        <!--        web-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

If the dependency in the pom.xml file becomes popular, we need to refresh Maven manually.

2.2. Configure resources 

Spring Boot generally supports two resource configuration files. They are application.properties and application.yaml respectively

I personally prefer to use yaml for configuration.

Write relevant configurations in application.yaml 

 

 I made an error when running the program here. The reason is that the time zone is not configured in the URL, because sometimes it may be necessary to configure the time zone.

If the time zone error is reported, add the time zone serverTimezone=UTC to the url

3. Code writing

You don’t have to worry about where you perform jdbc operations. The main thing is to learn the methods.

 Introduce JdbcTemplate

    @Autowired     //自动注入
    JdbcTemplate jdbcTemplate;

 About JdbcTemplate

 JDBC can already meet the most basic needs of most users, but when using JDBC, you must manage database resources yourself, such as: obtaining PreparedStatement, setting SQL statement parameters, closing connections, etc.

JdbcTemplate is Spring's encapsulation of JDBC, aiming to make JDBC easier to use. JdbcTemplate is part of Spring. JdbcTemplate handles the creation and release of resources. It helps us avoid common mistakes, such as forgetting to always close the connection. He runs the core JDBC workflow, such as Statement creation and execution, and we only need to provide SQL statements and extract results.
Spring source code address: https://github.com/spring-projects/spring-framework
The methods of executing SQL statements in JdbcTemplate are roughly divided into three categories:

execute: can execute all SQL statements, generally used to execute DDL statements.
update: used to execute DML statements such as INSERT, UPDATE, and DELETE.
queryXxx: used for DQL data query statements.
 

 Inquire

 @GetMapping("/userList")  //设置Mapping
    public List<Map<String,Object>> userList(){
        String sql = "select * from student";  //sql语句
//通过jdbcTemplate.queryForList(sql)执行sql语句,并将查询出来的结构放在定义的List中
        List<Map<String, Object>> list_maps =  jdbcTemplate.queryForList(sql);
        return  list_maps; //通过List将查询出来的结果返回到前端
    }

Add to

@GetMapping("/addUser")
    public String addUser(){
        String sql = "insert into student(id,name,age,user) values(5,'七七',23,'七')";
        jdbcTemplate.update(sql);
        return "add-ok";
    }

delete

Here, the id that the user brings when entering the URL is used to determine which data in the table to delete. 

 @GetMapping("/deleteUser/{id}")
    public String delUser(@PathVariable("id") int id){
        String sql = "delete from student where id = ?" ;
        jdbcTemplate.update(sql,id);
        return"delete-->ok";
    }

Revise

  @GetMapping("/updateUser/{id}")
    public String update(@PathVariable("id") int id){
        String sql = "update student set name=?,age=? where id =" + id;
        //封装
        Object[] objects = new Object[2];
        objects[0]="马户子";
        objects[1]="45";
        jdbcTemplate.update(sql,objects);
        return "update-->ok";
    }

Guess you like

Origin blog.csdn.net/m0_52991090/article/details/121458057