Learn spring boot recently, and keep it in mind.
The final project structure is
1 Create a project
For the rest, on the Dependencies page, select the web, as shown in the figure
2 Create controller s and access them
controller code
import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @RestController public class UserController { @RequestMapping("/hello") public String hello(){ return "hello"; } }
Start and access http://localhost:8080/hello
3 Configuration pom file
<dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.46</version> </dependency> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>1.3.2</version> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <version>1.16.10</version> </dependency>
4 Configure application.properties
spring.datasource.url=jdbc:mysql://127.0.0.1:3306/test?characterEncoding=UTF-8&useSSL=false spring.datasource.username=root spring.datasource.password=root spring.datasource.driver-class-name=com.mysql.jdbc.Driver
5. Establishing User Objects
import lombok.Data; import java.io.Serializable; @Data public class User implements Serializable { private Long id; private String name; }
6 mapper
import com.example.demo.entity.User; import org.springframework.stereotype.Repository; import java.util.List; @Repository public interface UserMapper { List<User> findAll(); }
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <mapper namespace="com.example.demo.mapper.UserMapper"> <select id="findAll" resultType="com.example.demo.entity.User"> select id, name from master </select> </mapper>
7 service
import com.example.demo.entity.User; import com.example.demo.mapper.UserMapper; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import java.util.List; @Service public class UserService { @Autowired private UserMapper userMapper; public List<User> findAll(){ return userMapper.findAll(); } }
8 controller transformed to
import com.example.demo.service.UserService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @RestController public class UserController { @Autowired private UserService userService; @RequestMapping("/hello") public String hello(){ return userService.findAll().toString(); } }
When it starts, it will report an error.
Description: Field userMapper in com.example.demo.service.UserService required a bean of type 'com.example.demo.mapper.UserMapper' that could not be found. The injection point has the following annotations: - @org.springframework.beans.factory.annotation.Autowired(required=true) Action: Consider defining a bean of type 'com.example.demo.mapper.UserMapper' in your configuration.
Looking at the error, we can see that there is no mapper bean, so we can add package scanning to the boot class.
import org.mybatis.spring.annotation.MapperScan; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication @MapperScan("com.example.demo.mapper") public class DemoApplication { public static void main(String[] args) { SpringApplication.run(DemoApplication.class, args); } }
However, even though it can be started at this time, there are still errors when accessing
org.apache.ibatis.binding.BindingException: Invalid bound statement (not found): com.example.demo.mapper.UserMapper.findAll
After several checks, it was found that no xml file was generated in the target, so the pom build added the following code
<build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> <resources> <resource> <directory>src/main/java</directory> <includes> <include>**/*.xml</include> </includes> </resource> </resources> </build>
resources is added.
At this point, the build is complete and accessible.