Notes on learning Spring framework: setting up spring boot+mybatis plus+mysql project environment

The previous one Spring framework learning notes (5) -- creation and use of Spring Boot , I found that how many details were not mentioned. Now I have learned the framework of mybatis plus. I plan to reorganize it and make the details clear

1. Create spring boot through IDEA

2. Project related configuration

You only need to change the first one and the second one, and the other options below will change automatically

3. Check the required Library

After this step is checked, spring boot will automatically add the dependency of the corresponding third-party open source library in pom.xml file

If you are not familiar with this, it is recommended that you do not check mybatis and mysql, so that the created spring boot can run directly without error,

After that, you can directly visit localhost:8080 in the browser to see the white page prompt, which indicates that the spring boot has been created successfully. This is explained in detail in the previous blog post, not much more here

4. Add mybatis plus dependency

Here, the official prompt uses mybatis plus, so do not use the dependency of mybatis to prevent conflicts

So, we use the following dependency to replace the original one of mybatis spring boot starter

<dependency>
    <groupId>com.baomidou</groupId>
    <artifactId>mybatis-plus-boot-starter</artifactId>
    <version>3.3.1.tmp</version>
</dependency>

We also need to remove the runtime line of MySQL connector Java in dependency, otherwise the driver file will not be found when the following configuration database drives

5. Configure the application.yml file


You can see the application.properties file in the project, which indicates that the default configuration file used in the spring boot project is in the property format, but there is another format support, namely yml format.

Compared with properties, yml supports nesting and has better readability, so I chose the format of yml

server:
  port: #Port number, the default is 8080
  servlet:
    context-path: #Project context, must start with '/'
spring:
  datasource:
    username: #Database user name
    password: #Database password
    url: jdbc:mysql://localhost:3306/mofang?useUnicode=true&characterEncoding=UTF-8
    driver-class-name: com.mysql.cj.jdbc.Driver #You will be prompted if you type mysql directly

#Show SQL log output, modify according to your package name
logging:
  level:
    com:
      example:
        mapper : debug

PS: note that spaces are required after colons in yml format

If oracle is used, check oracle drive before

6. Establish entity class

Create the corresponding entity class according to the table in the database

@Data
@ToString
public class User {
    private String userName;
    private String userPass;
}

PS: there is a table named user in my database. The column names are user name, user pass, and mybatis will automatically convert the fields

7. Create mapper interface file

@Component
public interface UserMapper extends BaseMapper<User> {
    
}

Note that annotations are added to let this class be managed by the spring container

The interface here inherits the BaseMapper interface, which is provided by mybatis plus and encapsulates some common CRUD operations

In this way, we don't need to write xml, so we can quickly implement some basic CRUD operations

8. Add MapperScan annotation

mapperScan annotation scans the mapper interface file in the specified package name and automatically loads it through spring

@SpringBootApplication
@MapperScan("com.wan.shunbang.mapper")
public class ShunbangApplication {

    public static void main(String[] args) {
        SpringApplication.run(ShunbangApplication.class, args);
    }

}

9. test

@SpringBootTest
public class UserTest {
    @Autowired
    private UserMapper userMapper;

    @Test
    void testInsert() {
        List<User> users = userMapper.selectList(null);
        for (User user : users) {
            System.out.println(user.toString());
        }
    }
}

I put the test file in the test directory

Tags: Java Spring Mybatis MySQL Database

Posted on Mon, 03 Feb 2020 04:58:43 -0500 by deived