Small knowledge, big challenge! This paper is participating in theEssentials for programmers”Creative activities.

Before we start the code, it is important to note that the Setup environment of Spring Boot is working properly before we move on. If there is a setup problem, please refer to this article.

Prepare the database environment – Create tables

CREATE TABLE `t_user` ( `id` int(11) NOT NULL AUTO_INCREMENT COMMENT 'id', 'name' varchar(50) DEFAULT NULL COMMENT 'age ',' age 'int(2) DEFAULT NULL COMMENT' age ', PRIMARY KEY (`id`) ) ENGINE=InnoDB AUTO_INCREMENT=12 DEFAULT CHARSET=utf8;Copy the code

Project directory structure

Edit the configuration files application.properties and POM.xml

Edit the application.properties file

# # data source configuration spring. The datasource. Url = JDBC: mysql: / / localhost: 3306 / spring_boot_test? useUnicode=true&characterEncoding=utf8 spring.datasource.driver-class-name=com.mysql.jdbc.Driver Spring. The datasource. The username = root spring. The datasource. The password = root # # # configure Mybatis configuration for the com. Kris. The entry point to entity class path. mybatis.typeAliasesPackage=com.kris.entryCopy the code

Edit the pom.xml file

<? The XML version = "1.0" encoding = "utf-8"? > < project XMLNS = "http://maven.apache.org/POM/4.0.0" XMLNS: xsi = "http://www.w3.org/2001/XMLSchema-instance" Xsi: schemaLocation = "http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" > < modelVersion > 4.0.0 < / modelVersion > < the parent > < groupId > org. Springframework. Boot < / groupId > The < artifactId > spring - the boot - starter - parent < / artifactId > < version > 2.1.3. RELEASE < / version > < relativePath / > <! -- lookup parent from repository --> </parent> <groupId>com.kris</groupId> <artifactId>demo2</artifactId> <version>0.0.1-SNAPSHOT</version> <name> Demo2 </name> <description>Demo project for Spring Boot</description> < the properties > < Java version > 1.8 < / Java. Version > <! -- This version of Mybatis should not be too low, otherwise there will be a problem. <mybatis-spring-boot>1.3.2</mybatis-spring-boot> <mysql-connector>5.1.39</mysql-connector> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId>  </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-jpa</artifactId> </dependency> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>${mybatis-spring-boot}</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>${mysql-connector}</version> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId>  </plugin> </plugins> </build> </project>Copy the code

Writing entity classes

package com.kris.entry; public class User { private Integer id; private String name; private Integer age; . getter and setter }Copy the code

Writing DAO interfaces

package com.kris.dao;

import com.kris.entry.User;
import org.apache.ibatis.annotations.*;
@Mapper
public interface UserDao {

    @Insert("insert into t_user(id,name,age) values (#{id},#{name},#{age})")
    boolean add(User user);

    @Delete("delete from t_user where id = #{id} ")
    boolean delete(Integer id);

    @Update("update t_user set name = #{name},age = #{age} where id = #{id} ")
    boolean update(User user);

    @Select("select * from t_user where id = #{id}")
    User select(Integer id);
}
Copy the code

How @mapper works: To be honest, it all works, but I’ve tested a bunch of them and it works fine. The reason follows.

@insert @delete @update @select Add, Delete, modify, and check the corresponding database.

If there are multiple arguments, @param (“XXX”) is bound to the parameter in the SQL statement. Such as:

@Update("update t_user set name = #{name},age = #{age} where id = #{id} ")
boolean update(@Param("name")String name,@Param("age")Integer age,@Param("id")Integer id);
Copy the code

Write Service interface and Service implementation

This module is no different from previous SSM projects, and for the current Demo, it is not necessary to write this layer.

package com.kris.service;

import com.kris.entry.User;

public interface UserService {
    boolean add(User user);
    boolean delete(Integer id);
    boolean update(User user);
    User select(Integer id);
}
Copy the code
package com.kris.service.impl; @Service public class UserServiceImpl implements UserService{ @Autowired UserDao userDao; @Override public boolean add(User user) { return userDao.add(user); } @Override public boolean delete(Integer id) { return userDao.delete(id); } @Override public boolean update(User user) { return userDao.update(user); } @Override public User select(Integer id) { return userDao.select(id); }}Copy the code

Writing the Controller layer

package com.kris.controller; @RestController @RequestMapping("/user") public class UserController { @Autowired private UserService userService; @RequestMapping(value = "/add",method = RequestMethod.POST) public boolean addUser(User user){ return userService.add(user); } @RequestMapping(value = "/delete",method = RequestMethod.DELETE) public boolean deleteUser(Integer id){ return userService.delete(id); } @RequestMapping(value = "/update",method = RequestMethod.PUT) public boolean updateUser(User user){ return userService.update(user); } @RequestMapping(value = "/select",method = RequestMethod.GET) public User selectUser(Integer id){ return userService.select(id); }}Copy the code

The @restController annotation is equal to @Controller + @responseBody and the result is returned in JSON format, and everything else is just a bit of history.

Run the project

package com.kris; @SpringBootApplication @MapperScan("com.kris.dao") public class Demo2Application { public static void main(String[] args) { SpringApplication.run(Demo2Application.class, args); }}Copy the code

SpringBootApplication: Enables component scanning and automatic configuration.

We wanted to implement the Dao layer. We used the Mapper dynamic proxy development mechanism, but the externality was to add @Mapper to the Dao interface. However, we need to add annotations for each Dao file, which is too troublesome. Now for convenience, we can use @mapperscan to scan directly. In addition, the @mapperscan annotation can scan multiple files at the same time, separated by commas. We could write it like this

@MapperScan("com.kris.dao","com.yu.mapper")
Copy the code

test

Test the Postman I’m using here, but you can also use Restlet. Uh, uh, I’ll take the test myself. I won’t take screenshots. If you have to, just use your browser, but pay attention to the form of the request.

Finally, this article has more code, there is no problem to follow this, maybe it looks very simple, but it is still recommended to practice, do not have high ambitions and low hand.