SSMP integration case (3) Create a data layer and run database addition, deletion, query and modification operations in the test class

The above SSMP integration case (2) Spring Boot integrates Lombok to simplify the development of entity classes. We have developed the entity class and
we can make the data layer.
At present, the most used data layer technology is naturally MyBatis, but in fact, MyBatis-Plus is widely used in China. SMEs still use it a lot

This time we mainly use MyBatis-Plus and Druid to do this.
We have imported the coordinates of these two tools before
insert image description here
, and then find the project configuration file application.yml
and add the following database configuration code

spring:
  datasource:
    druid:
      driver-class-name: com.mysql.jdbc.Driver
      url: jdbc:mysql://localhost:3306/test
      username: root
      password: root
mybatis-plus:
  global-config:
    db-config:
      id-type: auto

insert image description here

Here, don’t copy directly. First of all, the previous one is nothing but the URL. I just want the local port 3306. If you haven’t set it up, it is generally the same. Then specify the type of the test database below. We set up MySql and then the username and password below because I
have
always It has not been touched, so it is the default root.
The configuration below is telling him that our id uses the feature of data self-increment.

Then make the data layer interface.
Create a dao package in the startup similar directory.
insert image description here
Next, create an interface called bookDao.
The reference code is as follows

package com.example.bookconfig.dao;

import com.example.bookconfig.domain.book;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;

@Mapper
public interface bookDao {
    
    
    @Select("select * from book where id = #{id}")
    book getById(Integer id);
}

We want to use @Mapper to declare MyBatis
and then write a function to connect to SQL with Select. The function receives a parameter id. The meaning of numeric type sql is generally to find a corresponding piece of data through id.

But now the book in our database obviously has no data, so let’s add one to him or open the MySql management tool,
enter the query input and run sql

INSERT INTO `book`(`type`,`name`,`description`)
VALUES(1,"小猫猫","超级可爱");

insert image description here
This is an add statement, because id is a primary key auto-increasing field, we don’t care about it, so type 1 name kitten cat description is super cute.
After running successfully, we look at the book table again, we
can see that this data is added,
insert image description here
and then we come to the test kind
insert image description here

Write the code as follows

package com.example.bookconfig;

import com.example.bookconfig.dao.bookDao;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

@SpringBootTest
class BookConfigApplicationTests {
    
    

    @Autowired
    private bookDao bookDao;

    @Test
    void contextLoads() {
    
    
        System.out.println(bookDao.getById(1));
    }
}

Here we call the getById method in the bookDao interface we just wrote to query the data whose id is equal to 1.
We right-click to run the test class.
insert image description here
The running results are as follows.
insert image description here
It also successfully helped me find this data.

Of course, we are just here to see if we can run. Mp won’t develop it like this. It’s too troublesome for me to write sql like this. It has packaged many things for you. We directly modify the code of the bookDao interface as
follows

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.example.bookconfig.domain.book;
import org.apache.ibatis.annotations.Mapper;

@Mapper
public interface bookDao extends BaseMapper<book> {
    
    

}

We use the functions in it by inheriting the BaseMapper interface and then generically operate the book entity class. If you don’t specify the entity class, you must tell him which table and which class you want to operate, right? He will use the class you gave him to find the same name
. table

Here we hold down the Ctrl mouse and click in to have a look.
insert image description here
This interface comes with so many operations.
insert image description here
We can directly change getById to selectById in the test class.
insert image description here
The running results are as follows, and
insert image description here
it is obviously OK according to the id query. Then let’s test the other few The method
is to add first.
We write the following code in the test class

package com.example.bookconfig;

import com.example.bookconfig.dao.bookDao;
import com.example.bookconfig.domain.book;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

@SpringBootTest
class BookConfigApplicationTests {
    
    

    @Autowired
    private bookDao bookDao;

    @Test
    void contextLoads() {
    
    
        book book = new book();
        book.setType(0);
        book.setName("母猪的产后护理");
        book.setDescription("深度研究母猪产后护理的高级哲学以及母猪的历史价值");
        bookDao.insert(book);
    }
}

To put it simply, we define a book object and give its several values ​​​​to the content through the set method.
In other words, the id does not need to be controlled because it has self-incrementing attributes
, and then the content of the object is inserted into the database through the insert method provided by BaseMapper. In the middle, we run the code to view the database

This piece of data came in successfully
insert image description here
and then we wrote the following code in the test class

book book = new book();
book.setId(1);
book.setType(0);
book.setName("java从入门到放弃");
book.setDescription("讲述当代打工人从入学被坑到弃坑投奔其他行业的悲惨经历");
bookDao.updateById(book);

The way BaseMapper changes data is called updateById,
because it needs to make changes based on the id, so we still need to use set to assign the id to tell him which item to change.
Then we remember the content of the data whose id is 1 in the database.
insert image description here
We run the code and then go to the database to view the book. The table data is as follows. It is obvious that the modification has been successful.
insert image description here
Then we write the code as follows

bookDao.deleteById(2);

That’s right, the method of deleting according to the id is called deleteById.
We set the id to 2, which is the data we just added,
and delete it. Run the code and then go to the database to refresh the book table.
Obviously, the second piece of data will be deleted,
insert image description here
but this is not convenient. Let’s look at the query For the effect of all the data, we re-execute the added code to add the data back
insert image description here
, so it will be better

Then we write the code in the test class as follows

System.out.println(bookDao.selectList(null));

selectList is a method used to query all, but this needs to write conditions, but we want to check all, then directly pass null to him. The
running results are as follows,
insert image description here
you can see that he has queried all the data

Guess you like

Origin blog.csdn.net/weixin_45966674/article/details/131310690