First, the framework of the project preparation
1.1 New maven empty project, and introduced pom-dependent
1 <parent> 2 <groupId>org.springframework.boot</groupId> 3 <artifactId>spring-boot-starter-parent</artifactId> 4 <version>2.1.12.RELEASE</version> 5 </parent> 6 7 <properties> 8 <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> 9 <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> 10 <java.version>1.8</java.version> 11 </properties> 12 13 <dependencies> 14 <!-- web启动器 --> 15 <dependency> 16 <groupId>org.springframework.boot</groupId> 17 <artifactId>spring-boot-starter-web</artifactId> 18 </dependency> 19 <!--jdbc连接及其启动器 --> 20 <dependency> 21 <groupId>org.springframework.boot</groupId> 22 <artifactId>spring-boot-starter-data-jdbc</artifactId> 23 </dependency> 24 <dependency> 25 <groupId>mysql</groupId> 26 <artifactId>mysql-connector-java</artifactId> 27 <version>5.1.48</version> 28 </dependency> 29 <!--tk.mybatis及mybatis启动器 --> 30 <dependency> 31 <groupId>org.mybatis.spring.boot</groupId> 32 <artifactId>mybatis-spring-boot-starter</artifactId> 33 <version>1.3.2</version> 34 </dependency> 35 <dependency> 36 <groupId>tk.mybatis</groupId> 37 <artifactId>mapper-spring-boot-starter</artifactId> 38 <version>2.1.5</version> 39 </dependency> 40 <!--thymeleaf启动器 --> 41 <dependency> 42 <groupId>org.springframework.boot</groupId> 43 <artifactId>spring-boot-starter-thymeleaf</artifactId> 44 </dependency> 45 </dependencies> 46 47 <build> 48 <plugins> 49 <plugin> 50 <groupId>org.springframework.boot</groupId> 51 <artifactId>spring-boot-maven-plugin</artifactId> 52 </plugin> 53 </plugins> 54 </build>
1.2 Preparation mysql Datasheet
1 CREATE TABLE `user` ( 2 `id` int(11) NOT NULL AUTO_INCREMENT, 3 `username` varchar(255) NOT NULL, 4 `password` varchar(255) NOT NULL, 5 `birthday` date DEFAULT NULL, 6 `address` varchar(255) DEFAULT NULL, 7 PRIMARY KEY (`id`) 8 ) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8mb4; 9 INSERT INTO `user` VALUES ('1', '张三', '12345', '2020-01-16', '许昌市'); 10 INSERT INTO `user` VALUES ('2', ' John Doe ' , ' 54321 ' , ' 2020-01-01 ' , ' City, Henan Province ' );
Second, implement the presentation of data
2.1 New entry and start test
New MyApplication.java java file folder, start the program as the entrance.
1 @SpringBootApplication 2 public class MyApplication { 3 public static void main(String[] args) { 4 SpringApplication.run(MyApplication.class, args); 5 } 6 }
New application.yml files in the resources folder, the port configuration and database connections
1 server: 2 port: 7001 3 spring: 4 datasource: 5 driver-class-name: com.mysql.jdbc.Driver 6 url: jdbc:mysql://localhost:3306/mydb 7 username: root 8 password: fanbao0713
New controller -> UserController.java file, and start the test: http: // localhost: 7001 / user / hello
1 @RestController 2 @RequestMapping("user") 3 public class UserController { 4 @GetMapping("hello") 5 public String hello(){ 6 return "Hello World!"; 7 } 8 }
2.2 configuration database connection
New pojo -> User.java file:
1 @Table(name = "user") 2 public class User { 3 @Id 4 @GeneratedValue(strategy = GenerationType.IDENTITY) 5 private Integer id; 6 private String username; 7 private String password; 8 private Date birthday; 9 private String address; 10 11 public Integer getId() { 12 return id; 13 } 14 15 public void setId(Integer id) { 16 this.id = id; 17 } 18 19 public String getUsername() { 20 return username; 21 } 22 23 public void setUsername(String username) { 24 this.username = username; 25 } 26 27 public String getPassword() { 28 return password; 29 } 30 31 public void setPassword(String password) { 32 this.password = password; 33 } 34 35 public Date getBirthday() { 36 return birthday; 37 } 38 39 public void setBirthday(Date birthday) { 40 this.birthday = birthday; 41 } 42 43 public String getAddress() { 44 return address; 45 } 46 47 public void setAddress(String address) { 48 this.address = address; 49 } 50 }
In application.yml file, add mybatis association
1 mybatis: 2 type-aliases-package: com.springbootdemo.pojo
In mapper -> UserMapper.java file:
1 @Mapper 2 public interface UserMapper extends tk.mybatis.mapper.common.Mapper<User> { 3 }
In the service -> UserService.java file, not the spring due tk.mybati official plug-in, "userMapper" error will be ignored:
1 @Service 2 public class UserService { 3 @Autowired 4 private UserMapper userMapper; 5 6 public User selectById(Long id){ 7 return this.userMapper.selectByPrimaryKey(id); 8 } 9 10 public List<User> selectAll(){ 11 return userMapper.selectAll(); 12 } 13 }
In the controller -> UserController.java file, display interface and tested: http: // localhost: 7001 / user / findAll
1 @RestController 2 @RequestMapping("user") 3 public class UserController { 4 @Autowired 5 private UserService userService; 6 7 @GetMapping("{id}") 8 public User selectById(@PathVariable("id") Long id){ 9 return this.userService.selectById(id); 10 } 11 12 @GetMapping("findAll") 13 public Map<String, Object> findAll(){ 14 Map<String, Object> map = new HashMap<>(); 15 map.put("用户列表", userService.selectAll()); 16 return map; 17 } 18 }
Third, access to resources and static interceptor
3.1 access static resources
In the new static resources folder, and adding the directory, static in all types of resources can be accessed directly: http: // localhost: 7001 / js / common.js
3.2 blocker settings
In interceptors -> UserInterceptor, first we define an interceptor:
1 @Component 2 public class UserInterceptor implements HandlerInterceptor { 3 @Override 4 public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { 5 System.out.println("preHandle/前置拦截器 method is running!"); 6 return true; 7 } 8 9 @Override 10 public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception { 11 System.out.println("postHandle/运行拦截器 method is running!"); 12 } 13 14 @Override 15 public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception { 16 System.out.println("postHandle/后置拦截器 method is running!"); 17 } 18 }
New config -> MvcConfig.java, the custom configuration class, registered interceptors:
1 @Configuration 2 public class MvcConfig implements WebMvcConfigurer { 3 @Autowired 4 private HandlerInterceptor handlerInterceptor; 5 6 @Override 7 public void addInterceptors(InterceptorRegistry registry) { 8 registry.addInterceptor(handlerInterceptor).addPathPatterns("/**"); 9 } 10 }