Create a project

To create a new project, select SpringIntializr and use the official Spring link. (Click this link to create projects online)

gradle

Spring Boot DevTools
Spring web
Mybatis Framework
MySQL Driver

finish

Database connection test

Create the entity

public class UserEntity {
    private long id;
    private String name;
    private String code;

    public long getId(a) {
        return id;
    }

    public void setId(long id) {
        this.id = id;
    }

    public String getName(a) {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getCode(a) {
        return code;
    }

    public void setCode(String code) {
        this.code = code; }}Copy the code

Create dao

@Repository
public interface UserDao {
    @Select("select * from t_user")
    @Results({
            @Result(property = "name",column = "name"),
            @Result(property = "code",column="code")})List<UserEntity> getAll(a);
}
Copy the code

Create a controller

@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserDao userDao;

    @RequestMapping("/getAll")
    public List<UserEntity> getAll(a) {
        returnuserDao.getAll(); }}Copy the code

Adding @mapperscan (“com.example.demo9.dao”) to the Demo9Application file to scan daOs prevents each DAO from adding mapper annotations

@MapperScan("com.example.demo9.dao")
@SpringBootApplication
public class Demo9Application {

    public static void main(String[] args) { SpringApplication.run(Demo9Application.class, args); }}Copy the code

Configure the application. The properties

#datasourceSpring. The datasource. The driver - class - name = com. Mysql. JDBC. Driver spring. The datasource. Url = JDBC: mysql: / / 127.0.0.1:3306 /test? useSSL=false
spring.datasource.username=root
spring.datasource.password=testtest
Copy the code

Now that the project is configured, the directory structure is as follows:

http://localhost:8080/user/getAll